Path: blob/master/test/jdk/java/awt/Modal/FileDialog/FileDialogFWDTest.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*/2223import java.awt.*;24252627// FWD: Frame, Window, Dialog2829public class FileDialogFWDTest {3031private volatile FileDialog fileDialog;32private volatile CustomDialog dialog;33private volatile TestFrame frame;34private volatile TestWindow window;3536private static final int delay = 500;37private final ExtendedRobot robot;3839private volatile Dialog hiddenDialog;40private volatile Frame hiddenFrame;4142public enum DialogOwner {43HIDDEN_DIALOG, NULL_DIALOG, HIDDEN_FRAME, NULL_FRAME, FRAME};4445private DialogOwner owner;46boolean setModal;4748Dialog.ModalityType modalityType;4950private FileDialogFWDTest(Dialog.ModalityType modType,51boolean modal,52DialogOwner o) throws Exception {53modalityType = modType;54setModal = modal;55owner = o;5657robot = new ExtendedRobot();58EventQueue.invokeLater(this::createGUI);59}6061public FileDialogFWDTest(Dialog.ModalityType modalityType,62DialogOwner o) throws Exception {63this(modalityType, false, o);64}6566public FileDialogFWDTest(DialogOwner o) throws Exception {67this(null, true, o);68}6970private void createGUI() {7172frame = new CustomFrame();7374switch (owner) {75case HIDDEN_DIALOG:76hiddenDialog = new Dialog((Frame) null);77dialog = new CustomDialog(hiddenDialog);78break;79case NULL_DIALOG:80dialog = new CustomDialog((Dialog) null);81break;82case HIDDEN_FRAME:83hiddenFrame = new Frame();84dialog = new CustomDialog(hiddenFrame);85break;86case NULL_FRAME:87dialog = new CustomDialog((Frame) null);88break;89case FRAME:90dialog = new CustomDialog(frame);91break;92}9394if (setModal) {95dialog.setModal(true);96modalityType = dialog.getModalityType();97} else if (modalityType != null) {98dialog.setModalityType(modalityType);99}100101window = new CustomWindow(frame);102103int x = Toolkit.getDefaultToolkit().getScreenSize().width -104frame.getWidth() - 50;105int y = 50;106frame.setLocation(x, y);107y += (frame.getHeight() + 50);108window.setLocation(x, y);109y += (window.getHeight() + 50);110dialog.setLocation(x, y);111112frame.setVisible(true);113}114115private void openAll() throws Exception {116robot.waitForIdle(delay);117frame.clickOpenButton(robot);118robot.waitForIdle(delay);119window.clickOpenButton(robot);120robot.waitForIdle(delay);121dialog.clickOpenButton(robot);122robot.waitForIdle(delay);123}124125private void checkBlockedWindows() throws Exception {126127String msg = "FileDialog should block this ";128frame.checkBlockedFrame(robot, msg + "Frame.");129robot.waitForIdle(delay);130window.checkBlockedWindow(robot, msg + "Window.");131robot.waitForIdle(delay);132dialog.checkBlockedDialog(robot, msg + "Dialog.");133robot.waitForIdle(delay);134}135136private void checkUnblockedWindows() throws Exception {137138String msg = "Blocking dialogs were closed.";139frame.checkUnblockedFrame(robot, msg + "Frame.");140robot.waitForIdle(delay);141window.checkUnblockedWindow(robot, msg + "Window.");142robot.waitForIdle(delay);143}144145private void modalTest(String type) throws Exception {146147checkBlockedWindows();148149EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });150robot.waitForIdle(delay);151152String msg = "FileDialog was closed, " +153"but the " + type + " modal dialog should block this ";154155frame.checkBlockedFrame(robot, msg + "Frame.");156robot.waitForIdle(delay);157158window.checkBlockedWindow(robot, msg + "Window.");159robot.waitForIdle(delay);160161dialog.checkUnblockedDialog(robot, "FileDialog was closed.");162robot.waitForIdle(delay);163164dialog.clickCloseButton(robot);165robot.waitForIdle(delay);166167checkUnblockedWindows();168}169170private void docModalTest() throws Exception {171172if (owner == DialogOwner.FRAME) {173174checkBlockedWindows();175176EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });177robot.waitForIdle(delay);178179String msg = "FileDialog was closed.";180181dialog.checkUnblockedDialog(robot, msg);182robot.waitForIdle(delay);183184msg += " But the blocking document modal dialog is still open.";185186frame.checkBlockedFrame(robot, msg);187robot.waitForIdle(delay);188189window.checkBlockedWindow(robot, msg);190robot.waitForIdle(delay);191192dialog.clickCloseButton(robot);193robot.waitForIdle(delay);194195checkUnblockedWindows();196197} else {198nonModalTest();199}200}201202private void nonModalTest() throws Exception {203204checkBlockedWindows();205206EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });207robot.waitForIdle(delay);208209dialog.checkUnblockedDialog(robot, "FileDialog was closed.");210robot.waitForIdle(delay);211212checkUnblockedWindows();213}214215public void doTest() throws Exception {216217try {218openAll();219220if (modalityType == null) {221nonModalTest();222return;223}224225switch (modalityType) {226case APPLICATION_MODAL:227modalTest("application");228break;229case DOCUMENT_MODAL:230docModalTest();231break;232case TOOLKIT_MODAL:233modalTest("toolkit");234break;235case MODELESS:236nonModalTest();237break;238}239240} finally {241EventQueue.invokeAndWait(this::closeAll);242}243}244245private void closeAll() {246if (dialog != null) { dialog.dispose(); }247if (frame != null) { frame.dispose(); }248if (window != null) { window.dispose(); }249if (fileDialog != null) { fileDialog.dispose(); }250if (hiddenDialog != null) { hiddenDialog.dispose(); }251if (hiddenFrame != null) { hiddenFrame.dispose(); }252}253254255class CustomDialog extends TestDialog {256257public CustomDialog(Dialog d) { super(d); }258public CustomDialog(Frame f) { super(f); }259260@Override261public void doOpenAction() {262fileDialog = new FileDialog((Frame) null);263fileDialog.setLocation(50, 50);264fileDialog.setVisible(true);265}266267@Override268public void doCloseAction() {269this.dispose();270}271}272273class CustomFrame extends TestFrame {274275@Override276public void doOpenAction() {277if (window != null) { window.setVisible(true); }278}279}280281class CustomWindow extends TestWindow {282283public CustomWindow(Frame f) { super(f); }284285@Override286public void doOpenAction() {287if (dialog != null) { dialog.setVisible(true); }288}289}290}291292293