bug6694823.java revision 17113:d17577d4839b
1/*
2 * Copyright (c) 2008, 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/*
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
34import javax.swing.*;
35import java.awt.*;
36import java.security.Permission;
37
38public class bug6694823 {
39    private static JFrame frame;
40    private static JPopupMenu popup;
41    private static Toolkit toolkit;
42    private static Insets screenInsets;
43    private static Robot robot;
44
45    public static void main(String[] args) throws Exception {
46        robot = new Robot();
47        toolkit = Toolkit.getDefaultToolkit();
48        SwingUtilities.invokeAndWait(new Runnable() {
49            public void run() {
50                createGui();
51            }
52        });
53
54        robot.waitForIdle();
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            @Override
66            public void checkPermission(Permission perm) {
67                if (perm.getName().equals("setWindowAlwaysOnTop") ) {
68                    throw new SecurityException();
69                }
70            }
71
72        });
73
74        // Show popup as if from an applet
75        // The popup shouldn't overlap the task bar. It should be shifted up.
76        checkPopup();
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 checkPopup() 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        robot.waitForIdle();
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        robot.waitForIdle();
122
123        SwingUtilities.invokeAndWait(new Runnable() {
124
125            public void run() {
126                Point frameLoc = frame.getLocationOnScreen();
127                if (popup.getLocationOnScreen().equals(new Point(frameLoc.x, frameLoc.y + point.y))) {
128                    throw new RuntimeException("Popup is not shifted");
129                }
130                popup.setVisible(false);
131                frame.dispose();
132            }
133        });
134    }
135}
136