Path: blob/master/test/jdk/java/awt/Modal/FileDialog/FileDialogDWDTest.java
41152 views
/*1* Copyright (c) 2007, 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*/222324import java.awt.*;252627// DWD: Dialog, Window, Dialog2829public class FileDialogDWDTest {3031private volatile FileDialog fileDialog;32private volatile ParentDialog parent;33private volatile CustomDialog dialog;34private volatile TestWindow window;3536private static final int delay = 500;37private final ExtendedRobot robot;3839boolean setModal;4041Dialog.ModalityType modalityType;4243private FileDialogDWDTest(Dialog.ModalityType modType,44boolean modal) throws Exception {45modalityType = modType;46setModal = modal;4748robot = new ExtendedRobot();49EventQueue.invokeLater(this::createGUI);50}5152public FileDialogDWDTest(Dialog.ModalityType modalityType) throws Exception {53this(modalityType, false);54}5556public FileDialogDWDTest() throws Exception {57this(null, true);58}5960private void createGUI() {6162parent = new ParentDialog();63dialog = new CustomDialog(parent);6465if (setModal) {66dialog.setModal(true);67modalityType = dialog.getModalityType();68} else if (modalityType != null) {69dialog.setModalityType(modalityType);70}7172window = new CustomWindow(parent);7374int x = Toolkit.getDefaultToolkit().getScreenSize().width -75parent.getWidth() - 50;76int y = 50;77parent.setLocation(x, y);78y += (parent.getHeight() + 50);79window.setLocation(x, y);80y += (window.getHeight() + 50);81dialog.setLocation(x, y);8283parent.setVisible(true);84}8586private void openAll() throws Exception {87robot.waitForIdle(delay);88parent.clickOpenButton(robot);89robot.waitForIdle(delay);90window.clickOpenButton(robot);91robot.waitForIdle(delay);92dialog.clickOpenButton(robot);93robot.waitForIdle(delay);94}9596private void checkBlockedWindows() throws Exception {9798String msg = "FileDialog should block this ";99parent.checkBlockedDialog(robot, msg + "Dialog.");100robot.waitForIdle(delay);101window.checkBlockedWindow(robot, msg + "Window.");102robot.waitForIdle(delay);103dialog.checkBlockedDialog(robot, msg + "Dialog.");104robot.waitForIdle(delay);105}106107private void checkUnblockedWindows() throws Exception {108109String msg = "Blocking dialogs were closed.";110parent.checkUnblockedDialog(robot, msg + "Frame.");111robot.waitForIdle(delay);112window.checkUnblockedWindow(robot, msg + "Window.");113robot.waitForIdle(delay);114}115116117private void modalTest(String type) throws Exception {118119checkBlockedWindows();120121EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });122robot.waitForIdle(delay);123124String msg = "FileDialog was closed, " +125"but the " + type + " modal dialog should block this ";126127parent.checkBlockedDialog(robot, msg + "Dialog.");128robot.waitForIdle(delay);129130window.checkBlockedWindow(robot, msg + "Window.");131robot.waitForIdle(delay);132133dialog.checkUnblockedDialog(robot, "FileDialog was closed.");134robot.waitForIdle(delay);135136dialog.clickCloseButton(robot);137robot.waitForIdle(delay);138139checkUnblockedWindows();140}141142private void nonModalTest() throws Exception {143144checkBlockedWindows();145146EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });147robot.waitForIdle(delay);148149dialog.checkUnblockedDialog(robot, "FileDialog was closed.");150robot.waitForIdle(delay);151152checkUnblockedWindows();153}154155public void doTest() throws Exception {156157try {158159openAll();160161if (modalityType == null) {162nonModalTest();163return;164}165166switch (modalityType) {167case APPLICATION_MODAL:168modalTest("application");169break;170case DOCUMENT_MODAL:171modalTest("document");172break;173case TOOLKIT_MODAL:174modalTest("toolkit");175break;176case MODELESS:177nonModalTest();178break;179}180181} finally {182EventQueue.invokeAndWait(this::closeAll);183}184}185186private void closeAll() {187if (parent != null) { parent.dispose(); }188if (dialog != null) { dialog.dispose(); }189if (window != null) { window.dispose(); }190if (fileDialog != null) { fileDialog.dispose(); }191192}193194class ParentDialog extends TestDialog {195196public ParentDialog() { super((Frame) null); }197198@Override199public void doOpenAction() {200if (window != null) { window.setVisible(true); }201}202203}204205206class CustomDialog extends TestDialog {207208public CustomDialog(Dialog d) { super(d); }209210@Override211public void doOpenAction() {212fileDialog = new FileDialog((Frame) null);213fileDialog.setLocation(50, 50);214fileDialog.setVisible(true);215}216217@Override218public void doCloseAction() {219this.dispose();220}221}222223class CustomWindow extends TestWindow {224225public CustomWindow(Dialog d) { super(d); }226227@Override228public void doOpenAction() {229if (dialog != null) { dialog.setVisible(true); }230}231}232}233234235