Path: blob/master/test/jdk/javax/swing/JPopupMenu/6691503/bug6691503.java
41155 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* @key headful26* @bug 669150327* @summary Checks that there is no opportunity for a malicious applet28* to show a popup menu which has whole screen size.29* a heaviweight popup menu is shown from an applet.30* @author Mikhail Lapshin31* @run main/othervm -Djava.security.manager=allow bug669150332*/3334import javax.swing.*;35import java.awt.*;3637public class bug6691503 {38private JPopupMenu popupMenu;39private JFrame frame;40private boolean isAlwaysOnTop1 = false;41private boolean isAlwaysOnTop2 = true;4243public static void main(String[] args) {44bug6691503 test = new bug6691503();45test.setupUI();46test.testApplication();47test.testApplet();48test.checkResult();49test.stopEDT();50}5152private void setupUI() {53SwingUtilities.invokeLater(new Runnable() {54public void run() {55frame = new JFrame();56frame.setVisible(true);57popupMenu = new JPopupMenu();58JMenuItem click = new JMenuItem("Click");59popupMenu.add(click);60}61});62}6364private void testApplication() {65SwingUtilities.invokeLater(new Runnable() {66public void run() {67popupMenu.show(frame, 0, 0);68Window popupWindow = (Window)69(popupMenu.getParent().getParent().getParent().getParent());70isAlwaysOnTop1 = popupWindow.isAlwaysOnTop();71System.out.println(72"Application: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop1);73popupMenu.setVisible(false);74}75});76}7778private void testApplet() {79SwingUtilities.invokeLater(new Runnable() {80public void run() {81System.setSecurityManager(new SecurityManager());82popupMenu.show(frame, 0, 0);83Window popupWindow = (Window)84(popupMenu.getParent().getParent().getParent().getParent());85isAlwaysOnTop2 = popupWindow.isAlwaysOnTop();86System.out.println(87"Applet: popupWindow.isAlwaysOnTop() = " + isAlwaysOnTop2);88popupMenu.setVisible(false);89}90});91}9293private void checkResult() {94try {95Robot robot = new Robot();96robot.waitForIdle();97}catch(Exception ex) {98ex.printStackTrace();99throw new RuntimeException("Unexpected failure");100}101if (!isAlwaysOnTop1 || isAlwaysOnTop2) {102throw new RuntimeException("Malicious applet can show always-on-top " +103"popup menu which has whole screen size");104}105System.out.println("Test passed");106}107108private void stopEDT() {109SwingUtilities.invokeLater(new Runnable() {110public void run() {111frame.dispose();112}113});114}115}116117118