bug6694823.java revision 11018:66682f651425
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 * @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
33import javax.swing.*;
34import java.awt.*;
35import java.security.Permission;
36
37public class bug6694823 {
38    private static JFrame frame;
39    private static JPopupMenu popup;
40    private static Toolkit toolkit;
41    private static Insets screenInsets;
42    private static Robot robot;
43
44    public static void main(String[] args) throws Exception {
45        robot = new Robot();
46        toolkit = Toolkit.getDefaultToolkit();
47        SwingUtilities.invokeAndWait(new Runnable() {
48            public void run() {
49                createGui();
50            }
51        });
52
53        robot.waitForIdle();
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            @Override
65            public void checkPermission(Permission perm) {
66                if (perm.getName().equals("setWindowAlwaysOnTop") ) {
67                    throw new SecurityException();
68                }
69            }
70
71        });
72
73        // Show popup as if from an applet
74        // The popup shouldn't overlap the task bar. It should be shifted up.
75        checkPopup();
76
77    }
78
79    private static void createGui() {
80        frame = new JFrame();
81        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
82        frame.setUndecorated(true);
83
84        popup = new JPopupMenu("Menu");
85        for (int i = 0; i < 7; i++) {
86            popup.add(new JMenuItem("MenuItem"));
87        }
88        JPanel panel = new JPanel();
89        panel.setComponentPopupMenu(popup);
90        frame.add(panel);
91
92        frame.setSize(200, 200);
93    }
94
95    private static void checkPopup() throws Exception {
96        SwingUtilities.invokeAndWait(new Runnable() {
97            public void run() {
98                // Place frame just above the task bar
99                Dimension screenSize = toolkit.getScreenSize();
100                frame.setLocation(screenSize.width / 2,
101                        screenSize.height - frame.getHeight() - screenInsets.bottom);
102                frame.setVisible(true);
103            }
104        });
105
106        // Ensure frame is visible
107        robot.waitForIdle();
108
109        final Point point = new Point();
110        SwingUtilities.invokeAndWait(new Runnable() {
111            public void run() {
112                // Place popup over the task bar
113                point.x = 0;
114                point.y = frame.getHeight() - popup.getPreferredSize().height + screenInsets.bottom;
115                popup.show(frame, point.x, point.y);
116            }
117        });
118
119        // Ensure popup is visible
120        robot.waitForIdle();
121
122        SwingUtilities.invokeAndWait(new Runnable() {
123
124            public void run() {
125                Point frameLoc = frame.getLocationOnScreen();
126                if (popup.getLocationOnScreen().equals(new Point(frameLoc.x, frameLoc.y + point.y))) {
127                    throw new RuntimeException("Popup is not shifted");
128                }
129                popup.setVisible(false);
130                frame.dispose();
131            }
132        });
133    }
134}
135