Path: blob/master/test/jdk/javax/swing/JPopupMenu/6694823/bug6694823.java
41153 views
/*1* Copyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 669482327* @summary Checks that popup menu cannot be partially hidden28* by the task bar in applets.29* @author Mikhail Lapshin30* @run main/othervm -Djava.security.manager=allow bug669482331*/3233import javax.swing.*;34import java.awt.*;35import java.security.Permission;3637public class bug6694823 {38private static JFrame frame;39private static JPopupMenu popup;40private static Toolkit toolkit;41private static Insets screenInsets;42private static Robot robot;4344public static void main(String[] args) throws Exception {45robot = new Robot();46toolkit = Toolkit.getDefaultToolkit();47SwingUtilities.invokeAndWait(new Runnable() {48public void run() {49createGui();50}51});5253robot.waitForIdle();5455// Get screen insets56screenInsets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());57if (screenInsets.bottom == 0) {58// This test is only for configurations with taskbar on the bottom59return;60}6162System.setSecurityManager(new SecurityManager(){6364@Override65public void checkPermission(Permission perm) {66if (perm.getName().equals("setWindowAlwaysOnTop") ) {67throw new SecurityException();68}69}7071});7273// Show popup as if from an applet74// The popup shouldn't overlap the task bar. It should be shifted up.75checkPopup();7677}7879private static void createGui() {80frame = new JFrame();81frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);82frame.setUndecorated(true);8384popup = new JPopupMenu("Menu");85for (int i = 0; i < 7; i++) {86popup.add(new JMenuItem("MenuItem"));87}88JPanel panel = new JPanel();89panel.setComponentPopupMenu(popup);90frame.add(panel);9192frame.setSize(200, 200);93}9495private static void checkPopup() throws Exception {96SwingUtilities.invokeAndWait(new Runnable() {97public void run() {98// Place frame just above the task bar99Dimension screenSize = toolkit.getScreenSize();100frame.setLocation(screenSize.width / 2,101screenSize.height - frame.getHeight() - screenInsets.bottom);102frame.setVisible(true);103}104});105106// Ensure frame is visible107robot.waitForIdle();108109final Point point = new Point();110SwingUtilities.invokeAndWait(new Runnable() {111public void run() {112// Place popup over the task bar113point.x = 0;114point.y = frame.getHeight() - popup.getPreferredSize().height + screenInsets.bottom;115popup.show(frame, point.x, point.y);116}117});118119// Ensure popup is visible120robot.waitForIdle();121122SwingUtilities.invokeAndWait(new Runnable() {123124public void run() {125Point frameLoc = frame.getLocationOnScreen();126if (popup.getLocationOnScreen().equals(new Point(frameLoc.x, frameLoc.y + point.y))) {127throw new RuntimeException("Popup is not shifted");128}129popup.setVisible(false);130frame.dispose();131}132});133}134}135136137