Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6694823/bug6694823.java
38918 views
/*1* Copyright (c) 2008, 2012, 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* @bug 669482326* @summary Checks that popup menu cannot be partially hidden27* by the task bar in applets.28* @author Mikhail Lapshin29* @run main bug669482330*/3132import javax.swing.*;33import java.awt.*;34import java.security.Permission;3536public class bug6694823 {37private static JFrame frame;38private static JPopupMenu popup;39private static Toolkit toolkit;40private static Insets screenInsets;41private static Robot robot;4243public static void main(String[] args) throws Exception {44robot = new Robot();45toolkit = Toolkit.getDefaultToolkit();46SwingUtilities.invokeAndWait(new Runnable() {47public void run() {48createGui();49}50});5152robot.waitForIdle();5354// Get screen insets55screenInsets = toolkit.getScreenInsets(frame.getGraphicsConfiguration());56if (screenInsets.bottom == 0) {57// This test is only for configurations with taskbar on the bottom58return;59}6061System.setSecurityManager(new SecurityManager(){6263@Override64public void checkPermission(Permission perm) {65if (perm.getName().equals("setWindowAlwaysOnTop") ) {66throw new SecurityException();67}68}6970});7172// Show popup as if from an applet73// The popup shouldn't overlap the task bar. It should be shifted up.74checkPopup();7576}7778private static void createGui() {79frame = new JFrame();80frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);81frame.setUndecorated(true);8283popup = new JPopupMenu("Menu");84for (int i = 0; i < 7; i++) {85popup.add(new JMenuItem("MenuItem"));86}87JPanel panel = new JPanel();88panel.setComponentPopupMenu(popup);89frame.add(panel);9091frame.setSize(200, 200);92}9394private static void checkPopup() throws Exception {95SwingUtilities.invokeAndWait(new Runnable() {96public void run() {97// Place frame just above the task bar98Dimension screenSize = toolkit.getScreenSize();99frame.setLocation(screenSize.width / 2,100screenSize.height - frame.getHeight() - screenInsets.bottom);101frame.setVisible(true);102}103});104105// Ensure frame is visible106robot.waitForIdle();107108final Point point = new Point();109SwingUtilities.invokeAndWait(new Runnable() {110public void run() {111// Place popup over the task bar112point.x = 0;113point.y = frame.getHeight() - popup.getPreferredSize().height + screenInsets.bottom;114popup.show(frame, point.x, point.y);115}116});117118// Ensure popup is visible119robot.waitForIdle();120121SwingUtilities.invokeAndWait(new Runnable() {122123public void run() {124Point frameLoc = frame.getLocationOnScreen();125if (popup.getLocationOnScreen().equals(new Point(frameLoc.x, frameLoc.y + point.y))) {126throw new RuntimeException("Popup is not shifted");127}128popup.setVisible(false);129frame.dispose();130}131});132}133}134135136