1 /*
   2  * Copyright (c) 2008, 2012, 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 import java.security.Permission;
  37 import sun.security.util.SecurityConstants;
  38 
  39 public class bug6694823 {
  40     private static JFrame frame;
  41     private static JPopupMenu popup;
  42     private static SunToolkit toolkit;
  43     private static Insets screenInsets;
  44 
  45     public static void main(String[] args) throws Exception {
  46         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  47         SwingUtilities.invokeAndWait(new Runnable() {
  48             public void run() {
  49                 createGui();
  50             }
  51         });
  52 
  53         toolkit.realSync();
  54 
  55         // Get screen insets
  56         screenInsets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());
  57         if (screenInsets.bottom == 0) {
  58             // This test is only for configurations with taskbar on the bottom
  59             return;
  60         }
  61 
  62         System.setSecurityManager(new SecurityManager(){
  63 
  64             private String allowsAlwaysOnTopPermission = SecurityConstants.AWT.SET_WINDOW_ALWAYS_ON_TOP_PERMISSION.getName();
  65 
  66             @Override
  67             public void checkPermission(Permission perm) {
  68                 if (allowsAlwaysOnTopPermission.equals(perm.getName())) {
  69                     throw new SecurityException();
  70                 }
  71             }
  72 
  73         });
  74 
  75         // Show popup as if from an applet
  76         // The popup shouldn't overlap the task bar. It should be shifted up.
  77         checkPopup();
  78 
  79     }
  80 
  81     private static void createGui() {
  82         frame = new JFrame();
  83         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  84         frame.setUndecorated(true);
  85 
  86         popup = new JPopupMenu("Menu");
  87         for (int i = 0; i < 7; i++) {
  88             popup.add(new JMenuItem("MenuItem"));
  89         }
  90         JPanel panel = new JPanel();
  91         panel.setComponentPopupMenu(popup);
  92         frame.add(panel);
  93 
  94         frame.setSize(200, 200);
  95     }
  96 
  97     private static void checkPopup() throws Exception {
  98         SwingUtilities.invokeAndWait(new Runnable() {
  99             public void run() {
 100                 // Place frame just above the task bar
 101                 Dimension screenSize = toolkit.getScreenSize();
 102                 frame.setLocation(screenSize.width / 2,
 103                         screenSize.height - frame.getHeight() - screenInsets.bottom);
 104                 frame.setVisible(true);
 105             }
 106         });
 107 
 108         // Ensure frame is visible
 109         toolkit.realSync();
 110 
 111         final Point point = new Point();
 112         SwingUtilities.invokeAndWait(new Runnable() {
 113             public void run() {
 114                 // Place popup over the task bar
 115                 point.x = 0;
 116                 point.y = frame.getHeight() - popup.getPreferredSize().height + screenInsets.bottom;
 117                 popup.show(frame, point.x, point.y);
 118             }
 119         });
 120 
 121         // Ensure popup is visible
 122         toolkit.realSync();
 123 
 124         SwingUtilities.invokeAndWait(new Runnable() {
 125 
 126             public void run() {
 127                 Point frameLoc = frame.getLocationOnScreen();
 128                 if (popup.getLocationOnScreen().equals(new Point(frameLoc.x, frameLoc.y + point.y))) {
 129                     throw new RuntimeException("Popup is not shifted");
 130                 }
 131                 popup.setVisible(false);
 132                 frame.dispose();
 133             }
 134         });
 135     }
 136 }