Path: blob/master/test/jdk/java/awt/Modal/MultipleDialogs/MultipleDialogs2Test.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*/2223/*24* @test25* @key headful26* @bug 805435827* @summary Check whether a set of dialogs created with an application excluded Frame28* parent has a proper modal blocking behavior. Also show a document modal29* dialog and check if it blocks the document properly.30*31* @library ../helpers /lib/client/32* @library /test/lib33* @build ExtendedRobot34* @build Flag35* @build TestDialog36* @build TestFrame37* @run main/timeout=500 MultipleDialogs2Test38*/394041import java.awt.*;42import java.util.ArrayList;43import java.util.List;44import java.util.Collections;45import java.util.Iterator;4647public class MultipleDialogs2Test {4849private volatile CustomFrame frame;50private List<CustomDialog> dialogList;51private static final int delay = 500;5253private int dialogCount = -1;5455private void createGUI() {5657final int n = 8;58dialogList = new ArrayList<>();5960frame = new CustomFrame();61frame.setLocation(50, 50);62frame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);63frame.setVisible(true);6465CustomDialog dlg;6667int x = 250, y = 50;68for (int i = 0; i < n - 1; ++i) {6970dlg = new CustomDialog(frame);71dlg.setLocation(x, y);72x += 200;73if (x > 600) {74x = 50;75y += 200;76}7778Dialog.ModalityType type;79if (i % 3 == 0) {80type = Dialog.ModalityType.MODELESS;81} else if (i % 3 == 1) {82type = Dialog.ModalityType.APPLICATION_MODAL;83} else {84type = Dialog.ModalityType.TOOLKIT_MODAL;85}86dlg.setModalityType(type);87dialogList.add(dlg);88}8990dlg = new CustomDialog(frame);91dlg.setLocation(x, y);92dlg.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);93dialogList.add(dlg);94}9596public void doTest() throws Exception {9798try {99EventQueue.invokeAndWait(this::createGUI);100101ExtendedRobot robot = new ExtendedRobot();102robot.waitForIdle(delay);103104List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);105final int n = dialogs.size();106107synchronized(dialogs) {108for (int i = 0; i < n - 1; ++i) {109110frame.clickOpenButton(robot);111robot.waitForIdle(delay);112113dialogs.get(i).checkUnblockedDialog(114robot, i + ": This is " + getType(i) + ".");115116if (isToolkitModal(i)) {117118for (int j = 0; j < i; ++j) {119dialogs.get(j).checkBlockedDialog(robot, j + ": This dialog " +120"should be blocked by a toolkit modal dialog.");121}122123frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +124"should block this application modality excluded frame.");125126break;127128} else {129for (int j = 0; j < i; ++j) {130dialogs.get(j).checkUnblockedDialog(robot,131j + ": An application modality excluded frame " +132"is the parent of this dialog.");133}134135frame.checkUnblockedFrame(robot, i + ": The frame is " +136"application modality excluded, but it is blocked.");137}138}139140int tkIndex = dialogCount; // continue testing141final int tk0 = tkIndex;142143for (int i = tk0 + 1; i < n - 1; ++i) {144145dialogs.get(tkIndex).clickOpenButton(robot);146robot.waitForIdle(delay);147148frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +149"should block this application modality excluded frame.");150151if (isToolkitModal(i)) {152dialogs.get(i).checkUnblockedDialog(robot, i + ": This is " +153"a toolkit modal dialog with blocked frame parent. " +154"Another toolkit modal dialog is visible.");155156for (int j = 0; j < i; ++j) {157dialogs.get(j).checkBlockedDialog(robot, j + ": " +158"A toolkit modal dialog should block this child " +159"dialog of application modality excluded frame.");160}161tkIndex = i;162} else {163dialogs.get(i).checkBlockedDialog(164robot, i + ": This is " + getType(i) + " with blocked " +165"Frame parent. Also, a toolkit modal dialog is visible.");166167for (int j = 0; j < i; ++j) {168if (j != tkIndex) {169dialogs.get(j).checkBlockedDialog(robot, j +170": A toolkit modal dialog should block this " +171"child dialog of an application modality excluded frame.");172}173}174}175}176177// show a document modal dialog; the toolkit modal dialog should block it178dialogs.get(tkIndex).clickOpenButton(robot);179robot.waitForIdle(delay);180181frame.checkBlockedFrame(robot, "A toolkit modal dialog is visible.");182183for (int i = 0; i < n; ++i) {184if (i == tkIndex) {185dialogs.get(tkIndex).checkUnblockedDialog(robot,186tkIndex + ": This is a toolkit modal dialog. " +187"A document modal dialog is visible.");188} else {189dialogs.get(i).checkBlockedDialog(robot,190i + ": A toolkit modal dialog should block this dialog.");191}192}193194dialogs.get(tk0 + 3).clickCloseButton(robot); // close 2nd toolkit dialog195robot.waitForIdle(delay);196197for (int i = 0; i < n; ++i) {198199if (i == tk0 + 3) { continue; }200if (i == tk0) {201dialogs.get(i).checkUnblockedDialog(robot,202i + ": This is a toolkit modal dialog. A blocking " +203"toolkit modal dialog was opened and then closed.");204} else {205dialogs.get(i).checkBlockedDialog(robot,206i + ": This dialog should be blocked by a toolkit modal dialog. " +207"Another blocking toolkit modal dialog was closed.");208}209}210211dialogs.get(tk0).clickCloseButton(robot);212robot.waitForIdle(delay);213214frame.checkBlockedFrame(215robot, "A document modal dialog should block this Frame.");216217for (int i = 0; i < n - 1; ++i) {218if (!isToolkitModal(i)) {219dialogs.get(i).checkUnblockedDialog(robot, i + ": The parent " +220"of the dialog is an app modality excluded Frame.");221}222}223224dialogs.get(n - 1).clickCloseButton(robot);225robot.waitForIdle(delay);226227frame.checkUnblockedFrame(robot, "A document modal dialog " +228"blocking this Frame was closed.");229230for (int i = 0; i < n - 1; ++i) {231if (!isToolkitModal(i)) {232dialogs.get(i).checkUnblockedDialog(robot, i + ": A document modal " +233"dialog blocking the parent frame was closed.");234}235}236} // synchronized237238} finally {239EventQueue.invokeAndWait(this::closeAll);240}241}242243private boolean isToolkitModal(int i) { return (i % 3 == 2); }244245private String getType(int i) {246247switch (dialogList.get(i).getModalityType()) {248case APPLICATION_MODAL:249return "an application modal dialog";250case DOCUMENT_MODAL:251return "a document modal dialog";252case TOOLKIT_MODAL:253return "a toolkit modal dialog";254}255return "a modeless dialog";256}257258public void closeAll() {259260if (frame != null) { frame.dispose(); }261if (dialogList != null) {262Iterator<CustomDialog> it = dialogList.iterator();263while (it.hasNext()) { it.next().dispose(); }264}265}266267class CustomFrame extends TestFrame {268269@Override270public void doOpenAction() {271if ((dialogList != null) && (dialogList.size() > dialogCount)) {272dialogCount++;273CustomDialog d = dialogList.get(dialogCount);274if (d != null) { d.setVisible(true); }275}276}277}278279class CustomDialog extends TestDialog {280281public CustomDialog(Frame frame) { super(frame); }282283@Override284public void doCloseAction() { this.dispose(); }285286@Override287public void doOpenAction() {288if ((dialogList != null) && (dialogList.size() > dialogCount)) {289dialogCount++;290if (dialogList.get(dialogCount) != null) {291dialogList.get(dialogCount).setVisible(true);292}293}294}295}296297public static void main(String[] args) throws Exception {298(new MultipleDialogs2Test()).doTest();299}300}301302303