Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/Popup/TaskbarPositionTest.java
38838 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;25import javax.swing.*;26import javax.swing.event.*;2728/**29* @test @bug 4245587 4474813 4425878 4767478 801559930* @author Mark Davidson31* @summary Tests the location of the heavy weight popup portion of JComboBox,32* JMenu and JPopupMenu.33* @library ../regtesthelpers34* @build Util35* @run main TaskbarPositionTest36*/37public class TaskbarPositionTest extends JFrame implements ActionListener {3839private boolean done;40private Throwable error;41private static TaskbarPositionTest test;42private static JPopupMenu popupMenu;43private static JPanel panel;44private static JComboBox<String> combo1;45private static JComboBox<String> combo2;46private static JMenuBar menubar;47private static JMenu menu1;48private static JMenu menu2;49private static Rectangle fullScreenBounds;50// The usable desktop space: screen size - screen insets.51private static Rectangle screenBounds;52private static String[] numData = {53"One", "Two", "Three", "Four", "Five", "Six", "Seven"54};55private static String[] dayData = {56"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"57};58private static char[] mnDayData = {59'M', 'T', 'W', 'R', 'F', 'S', 'U'60};6162public TaskbarPositionTest() {63super("Use CTRL-down to show a JPopupMenu");64setContentPane(panel = createContentPane());65setJMenuBar(createMenuBar("1 - First Menu", true));66setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);6768// CTRL-down will show the popup.69panel.getInputMap().put(KeyStroke.getKeyStroke(70KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP");71panel.getActionMap().put("OPEN_POPUP", new PopupHandler());7273pack();7475Toolkit toolkit = Toolkit.getDefaultToolkit();76fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize());77screenBounds = new Rectangle(new Point(), toolkit.getScreenSize());7879// Place the frame near the bottom. This is a pretty wild guess.80this.setLocation(0, (int) screenBounds.getHeight() - 2 * this.getHeight());8182// Reduce the screen bounds by the insets.83GraphicsConfiguration gc = this.getGraphicsConfiguration();84if (gc != null) {85Insets screenInsets = toolkit.getScreenInsets(gc);86screenBounds = gc.getBounds();87screenBounds.width -= (screenInsets.left + screenInsets.right);88screenBounds.height -= (screenInsets.top + screenInsets.bottom);89screenBounds.x += screenInsets.left;90screenBounds.y += screenInsets.top;91}9293setVisible(true);94}9596public static class ComboPopupCheckListener implements PopupMenuListener {9798public void popupMenuCanceled(PopupMenuEvent ev) {99}100101public void popupMenuWillBecomeVisible(PopupMenuEvent ev) {102}103104public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) {105Point cpos = combo1.getLocation();106SwingUtilities.convertPointToScreen(cpos, panel);107108JPopupMenu pm = (JPopupMenu) combo1.getUI().getAccessibleChild(combo1, 0);109110if (pm != null) {111Point p = pm.getLocation();112SwingUtilities.convertPointToScreen(p, pm);113if (p.y < cpos.y) {114throw new RuntimeException("ComboBox popup is wrongly aligned");115} // check that popup was opened down116}117}118}119120private class PopupHandler extends AbstractAction {121122public void actionPerformed(ActionEvent e) {123if (!popupMenu.isVisible()) {124popupMenu.show((Component) e.getSource(), 40, 40);125}126isPopupOnScreen(popupMenu, fullScreenBounds);127}128}129130class PopupListener extends MouseAdapter {131132private JPopupMenu popup;133134public PopupListener(JPopupMenu popup) {135this.popup = popup;136}137138public void mousePressed(MouseEvent e) {139maybeShowPopup(e);140}141142public void mouseReleased(MouseEvent e) {143maybeShowPopup(e);144}145146private void maybeShowPopup(MouseEvent e) {147if (e.isPopupTrigger()) {148popup.show(e.getComponent(), e.getX(), e.getY());149isPopupOnScreen(popup, fullScreenBounds);150}151}152}153154/**155* Tests if the popup is on the screen.156*/157public static void isPopupOnScreen(JPopupMenu popup, Rectangle checkBounds) {158Dimension dim = popup.getSize();159Point pt = new Point();160SwingUtilities.convertPointToScreen(pt, popup);161Rectangle bounds = new Rectangle(pt, dim);162163if (!SwingUtilities.isRectangleContainingRectangle(checkBounds, bounds)) {164throw new RuntimeException("We do not match! " + checkBounds + " / " + bounds);165}166167}168169private JPanel createContentPane() {170JPanel panel = new JPanel();171172combo1 = new JComboBox<>(numData);173panel.add(combo1);174combo2 = new JComboBox<>(dayData);175combo2.setEditable(true);176panel.add(combo2);177panel.setSize(300, 200);178179popupMenu = new JPopupMenu();180JMenuItem item;181for (int i = 0; i < dayData.length; i++) {182item = popupMenu.add(new JMenuItem(dayData[i], mnDayData[i]));183item.addActionListener(this);184}185panel.addMouseListener(new PopupListener(popupMenu));186187JTextField field = new JTextField("CTRL+down for Popup");188// CTRL-down will show the popup.189field.getInputMap().put(KeyStroke.getKeyStroke(190KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP");191field.getActionMap().put("OPEN_POPUP", new PopupHandler());192193panel.add(field);194195return panel;196}197198/**199* @param str name of Menu200* @param bFlag set mnemonics on menu items201*/202private JMenuBar createMenuBar(String str, boolean bFlag) {203menubar = new JMenuBar();204205menu1 = new JMenu(str);206menu1.setMnemonic(str.charAt(0));207menu1.addActionListener(this);208209menubar.add(menu1);210for (int i = 0; i < 8; i++) {211JMenuItem menuitem = new JMenuItem("1 JMenuItem" + i);212menuitem.addActionListener(this);213if (bFlag) {214menuitem.setMnemonic('0' + i);215}216menu1.add(menuitem);217}218219// second menu220menu2 = new JMenu("2 - Second Menu");221menu2.addActionListener(this);222menu2.setMnemonic('2');223224menubar.add(menu2);225for (int i = 0; i < 5; i++) {226JMenuItem menuitem = new JMenuItem("2 JMenuItem" + i);227menuitem.addActionListener(this);228229if (bFlag) {230menuitem.setMnemonic('0' + i);231}232menu2.add(menuitem);233}234JMenu submenu = new JMenu("Sub Menu");235submenu.setMnemonic('S');236submenu.addActionListener(this);237for (int i = 0; i < 5; i++) {238JMenuItem menuitem = new JMenuItem("S JMenuItem" + i);239menuitem.addActionListener(this);240if (bFlag) {241menuitem.setMnemonic('0' + i);242}243submenu.add(menuitem);244}245menu2.add(new JSeparator());246menu2.add(submenu);247248return menubar;249}250251public void actionPerformed(ActionEvent evt) {252Object obj = evt.getSource();253if (obj instanceof JMenuItem) {254// put the focus on the noneditable combo.255combo1.requestFocus();256}257}258259public static void main(String[] args) throws Throwable {260261262SwingUtilities.invokeAndWait(new Runnable() {263public void run() {264test = new TaskbarPositionTest();265}266});267268// Use Robot to automate the test269Robot robot;270robot = new Robot();271robot.setAutoDelay(125);272273// 1 - menu274Util.hitMnemonics(robot, KeyEvent.VK_1);275276robot.waitForIdle();277isPopupOnScreen(menu1.getPopupMenu(), screenBounds);278279// 2 menu with sub menu280robot.keyPress(KeyEvent.VK_RIGHT);281robot.keyRelease(KeyEvent.VK_RIGHT);282Util.hitMnemonics(robot, KeyEvent.VK_S);283284robot.waitForIdle();285isPopupOnScreen(menu2.getPopupMenu(), screenBounds);286287robot.keyPress(KeyEvent.VK_ENTER);288robot.keyRelease(KeyEvent.VK_ENTER);289290// Focus should go to non editable combo box291robot.waitForIdle();292Thread.sleep(500);293294robot.keyPress(KeyEvent.VK_DOWN);295296// How do we check combo boxes?297298// Editable combo box299robot.keyPress(KeyEvent.VK_TAB);300robot.keyRelease(KeyEvent.VK_TAB);301robot.keyPress(KeyEvent.VK_DOWN);302robot.keyRelease(KeyEvent.VK_DOWN);303304// combo1.getUI();305306// Popup from Text field307robot.keyPress(KeyEvent.VK_TAB);308robot.keyRelease(KeyEvent.VK_TAB);309robot.keyPress(KeyEvent.VK_CONTROL);310robot.keyPress(KeyEvent.VK_DOWN);311robot.keyRelease(KeyEvent.VK_DOWN);312robot.keyRelease(KeyEvent.VK_CONTROL);313314// Popup from a mouse click.315Point pt = new Point(2, 2);316SwingUtilities.convertPointToScreen(pt, panel);317robot.mouseMove((int) pt.getX(), (int) pt.getY());318robot.mousePress(InputEvent.BUTTON3_MASK);319robot.mouseRelease(InputEvent.BUTTON3_MASK);320321robot.waitForIdle();322SwingUtilities.invokeAndWait(new Runnable() {323public void run() {324test.setLocation(-30, 100);325combo1.addPopupMenuListener(new ComboPopupCheckListener());326combo1.requestFocus();327}328});329330robot.keyPress(KeyEvent.VK_DOWN);331robot.keyRelease(KeyEvent.VK_DOWN);332robot.keyPress(KeyEvent.VK_ESCAPE);333robot.keyRelease(KeyEvent.VK_ESCAPE);334335robot.waitForIdle();336Thread.sleep(500);337}338}339340341