Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Modal/MultipleDialogs/MultipleDialogs3Test.java
38828 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*/2223/*24* @test25* @bug 805435826* @summary Check correctness of modal blocking behavior for a chain of Dialogs27* having different modality types with a Frame as a document root.28*29* @library ../helpers ../../../../lib/testlibrary/30* @build ExtendedRobot31* @build Flag32* @build TestDialog33* @build TestFrame34* @run main/timeout=500 MultipleDialogs3Test35*/363738import java.awt.*;39import static jdk.testlibrary.Asserts.*;40import java.util.ArrayList;41import java.util.List;42import java.util.Collections;43import java.util.Iterator;444546public class MultipleDialogs3Test {4748private volatile CustomFrame frame;49private List<CustomDialog> dialogList;50private static int delay = 500;5152private int dialogCount = -1;5354public void createGUI() {5556final int n = 8;57dialogList = new ArrayList<>();5859frame = new CustomFrame();60frame.setLocation(50, 50);61frame.setVisible(true);6263int x = 250;64int y = 50;65for (int i = 0; i < n; ++i) {6667CustomDialog dlg;68if (i == 0) {69dlg = new CustomDialog(frame);70} else {71dlg = new CustomDialog(dialogList.get(i - 1));72}73dlg.setLocation(x, y);74x += 200;75if (x > 600) {76x = 50;77y += 200;78}7980Dialog.ModalityType type;8182if (i % 4 == 0) {83type = Dialog.ModalityType.MODELESS;84} else if (i % 4 == 1) {85type = Dialog.ModalityType.DOCUMENT_MODAL;86} else if (i % 4 == 2) {87type = Dialog.ModalityType.APPLICATION_MODAL;88} else {89type = Dialog.ModalityType.TOOLKIT_MODAL;90}9192dlg.setModalityType(type);93dialogList.add(dlg);94}95}9697public void doTest() throws Exception {9899try {100EventQueue.invokeAndWait(this::createGUI);101102ExtendedRobot robot = new ExtendedRobot();103robot.waitForIdle(delay);104105frame.clickOpenButton(robot);106robot.waitForIdle(delay);107108List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);109final int n = dialogs.size();110111synchronized(dialogs) {112for (int i = 0; i < n; ++i) {113dialogs.get(i).activated.waitForFlagTriggered();114assertTrue(dialogs.get(i).activated.flag(), i + ": Dialog did not " +115"trigger windowActivated event when it became visible.");116117dialogs.get(i).closeGained.waitForFlagTriggered();118assertTrue(dialogs.get(i).closeGained.flag(), i + ": Close button " +119"did not gain focus when Dialog became visible.");120121assertTrue(dialogs.get(i).closeButton.hasFocus(), i +122": Close button gained focus but then lost it.");123124dialogs.get(i).checkUnblockedDialog(robot,125i + ": The dialog shouldn't be blocked.");126127if (i == 0) {128assertTrue(dialogs.get(0).getModalityType() ==129Dialog.ModalityType.MODELESS, "0: invalid modality type.");130131frame.checkUnblockedFrame(robot, i + ": A modeless dialog was " +132"shown, but the parent frame became blocked.");133134} else {135if (i % 4 == 0) { // modeless dialog136assertTrue(dialogs.get(i).getModalityType() ==137Dialog.ModalityType.MODELESS, i +138": incorrect dialog modality type.");139140dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +141"dialog was shown, but the parent dialog became blocked.");142143if (i > 0) {144for (int j = 0; j < i - 1; ++j) {145146dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +147": Showing a modeless dialog as a child of a " +148"modal dialog unblocked some dialog in its hierarchy.");149}150}151} else {152153for (int j = 0; j < i; ++j) {154155dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +156": A modal dialog was shown, but some dialog " +157"in its hierarchy became unblocked.");158}159}160161frame.checkBlockedFrame(robot, i + ": A modal was dialog shown, " +162"but the document root frame became unblocked.");163}164165if (i != n - 1) {166dialogs.get(i).clickOpenButton(robot);167robot.waitForIdle(delay);168}169}170171for (int i = n - 1; i >= 0; --i) {172173resetAll();174dialogs.get(i).clickCloseButton(robot);175robot.waitForIdle(delay);176177if (i > 0) {178179dialogs.get(i - 1).activated.waitForFlagTriggered();180assertTrue(dialogs.get(i - 1).activated.flag(), i + ": Dialog " +181"was not activated when a child dialog was closed.");182183if (i == 1) {184185frame.checkUnblockedFrame(robot, "1: Frame having " +186"a child modeless dialog was blocked.");187188dialogs.get(0).checkUnblockedDialog(robot,189"0: A modeless dialog at the bottom " +190"of the hierarchy was blocked.");191192} else if ((i - 1) % 4 == 0) { // dialog[i - 1] is modeless193194for (int j = 0; j < i - 2; ++j) {195196dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +197": A dialog in the hierarchy was not blocked. " +198"A dialog blocking a modeless dialog was closed.");199}200201dialogs.get(i - 2).checkUnblockedDialog(robot, i + ": A modal " +202"dialog having a child modeless dialog was blocked.");203204dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +205"dialog at the bottom of the hierarchy was blocked.");206207frame.checkBlockedFrame(robot, i +208": Frame having a child modal dialog was not blocked.");209210} else {211for (int j = 0; j <= i - 2; ++j) {212dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +213": A dialog in the hierarchy was not blocked. " +214"A child dialog was closed.");215}216217dialogs.get(i - 1).checkUnblockedDialog(robot, (i - 1) +218": A dialog was not unblocked when the modal dialog was closed.");219220frame.checkBlockedFrame(robot, i + ": Frame having " +221"a child modal dialog was not blocked. " +222"Another child dialog was closed.");223}224} else {225frame.activated.waitForFlagTriggered();226assertTrue(frame.activated.flag(), i + ": Frame was not " +227"activated when a child dialog was closed.");228}229}230} // synchronized231232} finally {233EventQueue.invokeAndWait(this::closeAll);234}235}236237private void resetAll() {238frame.resetStatus();239Iterator<CustomDialog> it = dialogList.iterator();240while (it.hasNext()) { it.next().resetStatus(); }241}242243public void closeAll() {244if (frame != null) { frame.dispose(); }245if (dialogList != null) {246Iterator<CustomDialog> it = dialogList.iterator();247while (it.hasNext()) { it.next().dispose(); }248}249}250251class CustomFrame extends TestFrame {252253@Override254public void doOpenAction() {255if ((dialogList != null) && (dialogList.size() > dialogCount)) {256dialogCount++;257CustomDialog d = dialogList.get(dialogCount);258if (d != null) { d.setVisible(true); }259}260}261}262263class CustomDialog extends TestDialog {264265public CustomDialog(Frame frame) { super(frame); }266public CustomDialog(Dialog dialog) { super(dialog); }267268@Override269public void doCloseAction() { this.dispose(); }270271@Override272public void doOpenAction() {273if ((dialogList != null) && (dialogList.size() > dialogCount)) {274dialogCount++;275CustomDialog d = dialogList.get(dialogCount);276if (d != null) { d.setVisible(true); }277}278}279}280281282public static void main(String[] args) throws Exception {283(new MultipleDialogs3Test()).doTest();284}285}286287288