Path: blob/master/test/jdk/java/awt/Modal/FileDialog/FileDialogModalityTest.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.*;2425public class FileDialogModalityTest {2627private volatile TestDialog dialog;28private volatile ParentFrame parent;29private volatile TestWindow window;30private volatile FileDialog fileDialog;3132private static final int delay = 500;33private final ExtendedRobot robot;3435private final Dialog.ModalityType modalityType;3637public static void main(String[] args) throws Exception {38(new FileDialogModalityTest(Dialog.ModalityType.DOCUMENT_MODAL)).doTest();39(new FileDialogModalityTest(Dialog.ModalityType.TOOLKIT_MODAL)).doTest();40(new FileDialogModalityTest(Dialog.ModalityType.MODELESS)).doTest();41}4243public FileDialogModalityTest(Dialog.ModalityType t) throws Exception {44modalityType = t;45robot = new ExtendedRobot();46}4748private void createGUI() {4950parent = new ParentFrame();51dialog = new CustomDialog((Frame) null);52window = new CustomWindow(parent);5354int x = Toolkit.getDefaultToolkit().getScreenSize().width -55parent.getWidth() - 50;56int y = 50;5758parent.setLocation(x, y);59y += (parent.getHeight() + 50);60window.setLocation(x, y);61y += (window.getHeight() + 50);62dialog.setLocation(x, y);6364parent.setVisible(true);65}6667private void startTest() throws Exception {6869EventQueue.invokeLater(this::createGUI);7071robot.waitForIdle(delay);72parent.clickOpenButton(robot);73robot.waitForIdle(delay);74window.clickOpenButton(robot);75robot.waitForIdle(delay);76dialog.clickOpenButton(robot);77robot.waitForIdle(delay);78}7980private void checkUnblockedWindows() throws Exception {8182String msg = " should not be blocked.";83parent.checkUnblockedFrame (robot, "This frame" + msg);84robot.waitForIdle(delay);85window.checkUnblockedWindow(robot, "This window" + msg);86robot.waitForIdle(delay);87dialog.checkUnblockedDialog(robot, "This dialog" + msg);88robot.waitForIdle(delay);89}9091private void checkBlockedWindows() throws Exception {9293String msg = " should be blocked by the FileDialog.";94parent.checkBlockedFrame (robot, "This Frame" + msg);95robot.waitForIdle(delay);96window.checkBlockedWindow(robot, "This Window" + msg);97robot.waitForIdle(delay);98dialog.checkBlockedDialog(robot, "This Dialog" + msg);99robot.waitForIdle(delay);100}101102private void docModalTest() throws Exception {103104String msg = "Document modal FileDialog should ";105parent.checkUnblockedFrame (robot, msg + "not block this Frame.");106robot.waitForIdle(delay);107window.checkUnblockedWindow(robot, msg + "not block this Window.");108robot.waitForIdle(delay);109dialog.checkBlockedDialog(robot, msg + "block its parent Dialog.");110robot.waitForIdle(delay);111}112113public void doTest() throws Exception {114115try {116startTest();117118switch (modalityType) {119case APPLICATION_MODAL:120case TOOLKIT_MODAL:121checkBlockedWindows();122break;123case DOCUMENT_MODAL:124docModalTest();125break;126case MODELESS:127checkUnblockedWindows();128break;129}130131EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });132robot.waitForIdle(delay);133134if (modalityType != Dialog.ModalityType.MODELESS) {135checkUnblockedWindows();136}137} finally {138EventQueue.invokeLater(this::closeAll);139}140}141142private void closeAll() {143if (parent != null) { parent.dispose(); }144if (dialog != null) { dialog.dispose(); }145if (window != null) { window.dispose(); }146if (fileDialog != null) { fileDialog.dispose(); }147}148149class ParentFrame extends TestFrame {150151@Override152public void doOpenAction() {153if (window != null) { window.setVisible(true); }154}155}156157class CustomDialog extends TestDialog {158159public CustomDialog(Frame f) { super(f); }160161@Override162public void doOpenAction() {163fileDialog = new FileDialog(this);164fileDialog.setModalityType(modalityType);165fileDialog.setLocation(50, 50);166fileDialog.setVisible(true);167}168}169170class CustomWindow extends TestWindow {171172public CustomWindow(TestFrame f) { super(f); }173174@Override175public void doOpenAction() {176if (dialog != null) { dialog.setVisible(true); }177}178}179}180181182