Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JPopupMenu/6691503/bug6691503.java
38854 views
/*1* Copyright (c) 2008, 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 669150326* @summary Checks that there is no opportunity for a malicious applet27* to show a popup menu which has whole screen size.28* a heaviweight popup menu is shown from an applet.29* @author Mikhail Lapshin30* @run main bug669150331*/3233import javax.swing.*;34import java.awt.*;3536public class bug6691503 {37private JPopupMenu popupMenu;38private JFrame frame;39private boolean isAlwaysOnTop1 = false;40private boolean isAlwaysOnTop2 = true;4142public static void main(String[] args) {43bug6691503 test = new bug6691503();44test.setupUI();45test.testApplication();46test.testApplet();47test.checkResult();48test.stopEDT();49}5051private void setupUI() {52SwingUtilities.invokeLater(new Runnable() {53public void run() {54frame = new JFrame();55frame.setVisible(true);56popupMenu = new JPopupMenu();57JMenuItem click = new JMenuItem("Click");58popupMenu.add(click);59}60});61}6263private void testApplication() {64SwingUtilities.invokeLater(new Runnable() {65public void run() {66popupMenu.show(frame, 0, 0);67Window popupWindow = (Window)68(popupMenu.getParent().getParent().getParent().getParent());69isAlwaysOnTop1 = popupWindow.isAlwaysOnTop();70System.out.println(71"Application: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop1);72popupMenu.setVisible(false);73}74});75}7677private void testApplet() {78SwingUtilities.invokeLater(new Runnable() {79public void run() {80System.setSecurityManager(new SecurityManager());81popupMenu.show(frame, 0, 0);82Window popupWindow = (Window)83(popupMenu.getParent().getParent().getParent().getParent());84isAlwaysOnTop2 = popupWindow.isAlwaysOnTop();85System.out.println(86"Applet: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop2);87popupMenu.setVisible(false);88}89});90}9192private void checkResult() {93try {94Robot robot = new Robot();95robot.waitForIdle();96}catch(Exception ex) {97ex.printStackTrace();98throw new RuntimeException("Unexpected failure");99}100if (!isAlwaysOnTop1 || isAlwaysOnTop2) {101throw new RuntimeException("Malicious applet can show always-on-top " +102"popup menu which has whole screen size");103}104System.out.println("Test passed");105}106107private void stopEDT() {108SwingUtilities.invokeLater(new Runnable() {109public void run() {110frame.dispose();111}112});113}114}115116117