Path: blob/master/test/jdk/java/awt/Modal/ModalExclusionTests/ExcludeDialogTest.java
41153 views
/*1* Copyright (c) 2007, 2018, 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*/2223import java.awt.*;24import java.awt.event.*;25import java.awt.print.*;2627import static jdk.test.lib.Asserts.*;282930public class ExcludeDialogTest implements AWTEventListener {3132class ParentDialog extends TestDialog {3334public ParentDialog() {35super((Frame) null);36}3738@Override39public void doOpenAction() {40if (window != null) {41window.setVisible(true);42}43}44}4546class CustomDialog extends TestDialog {4748public CustomDialog(Dialog dialog) {49super(dialog);50}5152@Override53public void doOpenAction() {5455switch (childDialogType) {56case PRINT_SETUP:57PrinterJob.getPrinterJob().printDialog();58break;59case PAGE_SETUP:60PrinterJob.getPrinterJob().pageDialog(new PageFormat());61break;62case FILE_DIALOG:63fileDialog = new FileDialog((Dialog) null);64fileDialog.setLocation(20, 200);65fileDialog.setVisible(true);66break;67}68}69}7071class CustomWindow extends TestWindow {7273public CustomWindow(Dialog parent) {74super(parent);75}7677@Override78public void doOpenAction() {79if (dialog != null) {80dialog.setVisible(true);81}82}83}8485private TestWindow window;86private TestDialog dialog;87private ParentDialog parent;88private FileDialog fileDialog;8990private static final int delay = 1000;9192private final ExtendedRobot robot;9394private boolean windowAppeared = false;95private final Object windowLock;9697private final Dialog.ModalExclusionType exclusionType;9899public enum DialogToShow {PAGE_SETUP, PRINT_SETUP, FILE_DIALOG};100private DialogToShow childDialogType;101private String type;102103@Override104public void eventDispatched(AWTEvent event) {105if (event.getID() == WindowEvent.WINDOW_OPENED) {106windowAppeared = true;107synchronized (windowLock) {108windowLock.notifyAll();109}110}111}112113ExcludeDialogTest(Dialog.ModalExclusionType exclType,114DialogToShow dialogType) throws Exception {115exclusionType = exclType;116childDialogType = dialogType;117118type = "File";119if (dialogType == DialogToShow.PAGE_SETUP) {120type = "Page setup";121} else if (dialogType == DialogToShow.PRINT_SETUP) {122type = "Print setup";123}124125robot = new ExtendedRobot();126windowLock = new Object();127EventQueue.invokeAndWait( this::createGUI );128}129130private void createGUI() {131parent = new ParentDialog();132parent.setLocation(20, 20);133parent.setModalExclusionType(exclusionType);134dialog = new CustomDialog(parent);135dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);136dialog.setLocation(220, 20);137window = new CustomWindow(parent);138window.setLocation(420, 20);139Toolkit.getDefaultToolkit().addAWTEventListener(140ExcludeDialogTest.this, AWTEvent.WINDOW_EVENT_MASK);141parent.setVisible(true);142}143144private void closeAll() {145if (dialog != null) { dialog.dispose(); }146if (parent != null) { parent.dispose(); }147if (window != null) { window.dispose(); }148if (fileDialog != null) {149fileDialog.dispose();150} else {151robot.type(KeyEvent.VK_ESCAPE);152}153}154155public void doTest() throws Exception {156157robot.waitForIdle(delay);158159parent.clickOpenButton(robot);160robot.waitForIdle(delay);161162window.clickOpenButton(robot);163robot.waitForIdle(delay);164165dialog.clickOpenButton(robot);166robot.waitForIdle(delay);167168if (! windowAppeared) {169synchronized (windowLock) {170try {171windowLock.wait(10 * delay);172} catch (InterruptedException e) {}173}174}175176assertTrue(windowAppeared, type + " dialog didn't appear");177178parent.toFront();179robot.waitForIdle(delay);180181String excl = "";182if (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) {183excl = "Application";184} else if (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE) {185excl = "Toolkit";186}187188parent.checkUnblockedDialog(robot, excl + " modal " + type +189" dialog should not block this modal excluded Dialog");190dialog.checkUnblockedDialog(robot, excl + " modal " + type +191" dialog should not block this modal excluded app. modal Dialog");192window.checkUnblockedWindow(robot, excl + " modal " + type +193" dialog should not block this modal excluded Window");194195robot.waitForIdle();196closeAll();197}198}199200201