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