Path: blob/master/test/jdk/javax/swing/JPopupMenu/6580930/bug6580930.java
41153 views
/*1* Copyright (c) 2007, 2019, 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*/22/*23* @test24* @key headful25* @bug 6580930 718495626* @summary Swing Popups should overlap taskbar27* @author Alexander Potochkin28* @library /lib/client29* @build ExtendedRobot30* @run main bug658093031*/3233import javax.swing.*;34import java.awt.*;35import java.awt.event.InputEvent;36import java.awt.event.KeyEvent;3738public class bug6580930 {39private static ExtendedRobot robot;40private static JFrame frame;41private static JPopupMenu popup;42private static Toolkit toolkit;43private static volatile boolean skipTest = false;44private static Point loc;45private static int y;4647private static void createGui() {48frame = new JFrame();49frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);50frame.setUndecorated(true);5152popup = new JPopupMenu("Menu");53for (int i = 0; i < 7; i++) {54popup.add(new JMenuItem("MenuItem"));55}56JPanel panel = new JPanel();57panel.setComponentPopupMenu(popup);58frame.add(panel);5960frame.setSize(200, 200);61}626364public static void main(String[] args) throws Exception {65try {66SwingUtilities.invokeAndWait(new Runnable() {67public void run() {68JPopupMenu.setDefaultLightWeightPopupEnabled(true);69bug6580930.createGui();70}71});7273toolkit = Toolkit.getDefaultToolkit();74robot = new ExtendedRobot();75robot.setAutoDelay(10);76robot.waitForIdle();7778SwingUtilities.invokeAndWait(new Runnable() {79public void run() {80Insets insets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());81if (insets.bottom == 0) {82System.out.println("This test is only for configurations with taskbar on the bottom");8384skipTest = true;85}8687Dimension screenSize = toolkit.getScreenSize();88frame.setLocation(screenSize.width/2, screenSize.height - frame.getHeight() - insets.bottom + 10);89frame.setVisible(true);90}91});9293robot.waitForIdle();9495if(skipTest) {96return;97}9899SwingUtilities.invokeAndWait(() -> loc = frame.getLocationOnScreen());100robot.waitForIdle();101102robot.mouseMove(loc.x, loc.y);103showPopup();104robot.waitForIdle();105if (!System.getProperty("os.name").startsWith("Mac")106&& isHeavyWeightMenuVisible()) {107throw new RuntimeException("HeavyWeightPopup is unexpectedly visible");108}109110robot.keyPress(KeyEvent.VK_ESCAPE);111robot.keyRelease(KeyEvent.VK_ESCAPE);112113int x = loc.x;114SwingUtilities.invokeAndWait( () -> y = loc.y + (frame.getHeight() -115popup.getPreferredSize().height) + 1);116robot.waitForIdle();117robot.mouseMove(x, y);118119showPopup();120SwingUtilities.invokeAndWait(() -> loc = popup.getLocationOnScreen());121robot.waitForIdle();122123if (!loc.equals(new Point(x, y))) {124throw new RuntimeException("Popup is unexpectedly shifted");125}126127if (!isHeavyWeightMenuVisible()) {128throw new RuntimeException("HeavyWeightPopup is unexpectedly hidden");129}130} finally {131if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());132}133}134135private static void showPopup() {136robot.mousePress(InputEvent.BUTTON1_MASK);137robot.mouseRelease(InputEvent.BUTTON1_MASK);138robot.waitForIdle();139if (!popup.isShowing()) {140robot.mousePress(InputEvent.BUTTON2_MASK);141robot.mouseRelease(InputEvent.BUTTON2_MASK);142robot.waitForIdle();143if (!popup.isShowing()) {144robot.mousePress(InputEvent.BUTTON3_MASK);145robot.mouseRelease(InputEvent.BUTTON3_MASK);146robot.waitForIdle();147}148}149}150151private static boolean isHeavyWeightMenuVisible() {152Window[] windows = Window.getWindows();153for (Window window : windows) {154if (window.getClass().getSimpleName().equals("HeavyWeightWindow")155&& window.isVisible()) {156return true;157}158}159return false;160}161}162163164