1 /*
   2  * Copyright (c) 2008, 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 /*
  25  * @test
  26  * @bug 6694823
  27  * @summary Checks that popup menu cannot be partially hidden
  28  * by the task bar in applets.
  29  * @author Mikhail Lapshin
  30  * @run main bug6694823
  31  */
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import sun.awt.SunToolkit;
  36 
  37 public class bug6694823 {
  38     private static JFrame frame;
  39     private static JPopupMenu popup;
  40     private static SunToolkit toolkit;
  41     private static Insets screenInsets;
  42 
  43     public static void main(String[] args) throws Exception {
  44         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  45         SwingUtilities.invokeAndWait(new Runnable() {
  46             public void run() {
  47                 createGui();
  48             }
  49         });
  50 
  51         // Get screen insets
  52         screenInsets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());
  53         if (screenInsets.bottom == 0) {
  54             // This test is only for configurations with taskbar on the bottom
  55             return;
  56         }
  57 
  58         // Show popup as if from a standalone application
  59         // The popup should be able to overlap the task bar
  60         showPopup(false);
  61 
  62         // Emulate applet security restrictions
  63         toolkit.realSync();
  64         System.setSecurityManager(new SecurityManager());
  65 
  66         // Show popup as if from an applet
  67         // The popup shouldn't overlap the task bar. It should be shifted up.
  68         showPopup(true);
  69 
  70         toolkit.realSync();
  71         System.out.println("Test passed!");
  72 
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             public void run() {
  75                 frame.dispose();
  76             }
  77         });
  78     }
  79 
  80     private static void createGui() {
  81         frame = new JFrame();
  82         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  83         frame.setUndecorated(true);
  84 
  85         popup = new JPopupMenu("Menu");
  86         for (int i = 0; i < 7; i++) {
  87             popup.add(new JMenuItem("MenuItem"));
  88         }
  89         JPanel panel = new JPanel();
  90         panel.setComponentPopupMenu(popup);
  91         frame.add(panel);
  92 
  93         frame.setSize(200, 200);
  94     }
  95 
  96     private static void showPopup(final boolean shouldBeShifted) throws Exception {
  97         SwingUtilities.invokeAndWait(new Runnable() {
  98             public void run() {
  99                 // Place frame just above the task bar
 100                 Dimension screenSize = toolkit.getScreenSize();
 101                 frame.setLocation(screenSize.width / 2,
 102                         screenSize.height - frame.getHeight() - screenInsets.bottom);
 103                 frame.setVisible(true);
 104             }
 105         });
 106 
 107         // Ensure frame is visible
 108         toolkit.realSync();
 109 
 110         final Point point = new Point();
 111         SwingUtilities.invokeAndWait(new Runnable() {
 112             public void run() {
 113                 // Place popup over the task bar
 114                 point.x = 0;
 115                 point.y = frame.getHeight() - popup.getPreferredSize().height + screenInsets.bottom;
 116                 popup.show(frame, point.x, point.y);
 117             }
 118         });
 119 
 120         // Ensure popup is visible
 121         toolkit.realSync();
 122 
 123         SwingUtilities.invokeAndWait(new Runnable() {
 124             public void run() {
 125                 Point frameLoc = frame.getLocationOnScreen();
 126                 if (shouldBeShifted) {
 127                     if (popup.getLocationOnScreen()
 128                             .equals(new Point(frameLoc.x, frameLoc.y + point.y))) {
 129                         throw new RuntimeException("Popup is not shifted");
 130                     }
 131                 } else {
 132                     if (!popup.getLocationOnScreen()
 133                             .equals(new Point(frameLoc.x, frameLoc.y + point.y))) {
 134                         throw new RuntimeException("Popup is unexpectedly shifted");
 135                     }
 136                 }
 137                 popup.setVisible(false);
 138             }
 139         });
 140     }
 141 }