1 /*
   2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @key headful
  26  * @bug 6580930 7184956
  27  * @summary Swing Popups should overlap taskbar
  28  * @author Alexander Potochkin
  29  * @library ../../../../lib/testlibrary
  30  * @build ExtendedRobot
  31  * @run main bug6580930
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 import java.awt.event.InputEvent;
  37 import java.awt.event.KeyEvent;
  38 
  39 public class bug6580930 {
  40     private static ExtendedRobot robot;
  41     private static JFrame frame;
  42     private static JPopupMenu popup;
  43     private static Toolkit toolkit;
  44     private static volatile boolean skipTest = false;
  45 
  46     private static void createGui() {
  47         frame = new JFrame();
  48         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49         frame.setUndecorated(true);
  50 
  51         popup = new JPopupMenu("Menu");
  52         for (int i = 0; i < 7; i++) {
  53             popup.add(new JMenuItem("MenuItem"));
  54         }
  55         JPanel panel = new JPanel();
  56         panel.setComponentPopupMenu(popup);
  57         frame.add(panel);
  58 
  59         frame.setSize(200, 200);
  60     }
  61 
  62 
  63     public static void main(String[] args) throws Exception {
  64         SwingUtilities.invokeAndWait(new Runnable() {
  65             public void run() {
  66                 JPopupMenu.setDefaultLightWeightPopupEnabled(true);
  67                 bug6580930.createGui();
  68             }
  69         });
  70 
  71         toolkit = Toolkit.getDefaultToolkit();
  72         robot = new ExtendedRobot();
  73         robot.setAutoDelay(10);
  74         robot.waitForIdle();
  75 
  76         SwingUtilities.invokeAndWait(new Runnable() {
  77             public void run() {
  78                 Insets insets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());
  79                 if (insets.bottom == 0) {
  80                     System.out.println("This test is only for configurations with taskbar on the bottom");
  81 
  82                     skipTest = true;
  83                 }
  84 
  85                 Dimension screenSize = toolkit.getScreenSize();
  86                 frame.setLocation(screenSize.width/2, screenSize.height - frame.getHeight() - insets.bottom + 10);
  87                 frame.setVisible(true);
  88             }
  89         });
  90 
  91         robot.waitForIdle();
  92 
  93         if(skipTest) {
  94             return;
  95         }
  96         Point loc = frame.getLocationOnScreen();
  97 
  98         robot.mouseMove(loc.x, loc.y);
  99         showPopup();
 100         robot.waitForIdle();
 101         if (isHeavyWeightMenuVisible()) {
 102             throw new RuntimeException("HeavyWeightPopup is unexpectedly visible");
 103         }
 104 
 105         robot.keyPress(KeyEvent.VK_ESCAPE);
 106         robot.keyRelease(KeyEvent.VK_ESCAPE);
 107 
 108         int x = loc.x;
 109         int y = loc.y + (frame.getHeight() - popup.getPreferredSize().height) + 1;
 110         robot.mouseMove(x, y);
 111 
 112         showPopup();
 113 
 114         if (!popup.getLocationOnScreen().equals(new Point(x, y))) {
 115             throw new RuntimeException("Popup is unexpectedly shifted");
 116         }
 117 
 118         if (!isHeavyWeightMenuVisible()) {
 119             throw new RuntimeException("HeavyWeightPopup is unexpectedly hidden");
 120         }
 121     }
 122 
 123     private static void showPopup() {
 124         robot.mousePress(InputEvent.BUTTON1_MASK);
 125         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126         robot.waitForIdle();
 127         if (!popup.isShowing()) {
 128             robot.mousePress(InputEvent.BUTTON2_MASK);
 129             robot.mouseRelease(InputEvent.BUTTON2_MASK);
 130             robot.waitForIdle();
 131             if (!popup.isShowing()) {
 132                 robot.mousePress(InputEvent.BUTTON3_MASK);
 133                 robot.mouseRelease(InputEvent.BUTTON3_MASK);
 134                 robot.waitForIdle();
 135             }
 136         }
 137     }
 138 
 139     private static boolean isHeavyWeightMenuVisible() {
 140         Window[] windows = Window.getWindows();
 141         for (Window window : windows) {
 142             if (window.getClass().getSimpleName().equals("HeavyWeightWindow")
 143                     && window.isVisible()) {
 144                     return true;
 145             }
 146         }
 147         return false;
 148     }
 149 }