Path: blob/master/test/jdk/java/awt/Modal/MultipleDialogs/MultipleDialogs3Test.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 correctness of modal blocking behavior for a chain of Dialogs28* having different modality types with a Frame as a document root.29*30* @library ../helpers /lib/client/31* @library /test/lib32* @build ExtendedRobot33* @build Flag34* @build TestDialog35* @build TestFrame36* @run main/timeout=500 MultipleDialogs3Test37*/383940import java.awt.*;41import static jdk.test.lib.Asserts.*;42import java.util.ArrayList;43import java.util.List;44import java.util.Collections;45import java.util.Iterator;464748public class MultipleDialogs3Test {4950private volatile CustomFrame frame;51private List<CustomDialog> dialogList;52private static int delay = 500;5354private int dialogCount = -1;5556public void createGUI() {5758final int n = 8;59dialogList = new ArrayList<>();6061frame = new CustomFrame();62frame.setLocation(50, 50);63frame.setVisible(true);6465int x = 250;66int y = 50;67for (int i = 0; i < n; ++i) {6869CustomDialog dlg;70if (i == 0) {71dlg = new CustomDialog(frame);72} else {73dlg = new CustomDialog(dialogList.get(i - 1));74}75dlg.setLocation(x, y);76x += 200;77if (x > 600) {78x = 50;79y += 200;80}8182Dialog.ModalityType type;8384if (i % 4 == 0) {85type = Dialog.ModalityType.MODELESS;86} else if (i % 4 == 1) {87type = Dialog.ModalityType.DOCUMENT_MODAL;88} else if (i % 4 == 2) {89type = Dialog.ModalityType.APPLICATION_MODAL;90} else {91type = Dialog.ModalityType.TOOLKIT_MODAL;92}9394dlg.setModalityType(type);95dialogList.add(dlg);96}97}9899public void doTest() throws Exception {100101try {102EventQueue.invokeAndWait(this::createGUI);103104ExtendedRobot robot = new ExtendedRobot();105robot.waitForIdle(delay);106107frame.clickOpenButton(robot);108robot.waitForIdle(delay);109110List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);111final int n = dialogs.size();112113synchronized(dialogs) {114for (int i = 0; i < n; ++i) {115dialogs.get(i).activated.waitForFlagTriggered();116assertTrue(dialogs.get(i).activated.flag(), i + ": Dialog did not " +117"trigger windowActivated event when it became visible.");118119dialogs.get(i).closeGained.waitForFlagTriggered();120assertTrue(dialogs.get(i).closeGained.flag(), i + ": Close button " +121"did not gain focus when Dialog became visible.");122123assertTrue(dialogs.get(i).closeButton.hasFocus(), i +124": Close button gained focus but then lost it.");125126dialogs.get(i).checkUnblockedDialog(robot,127i + ": The dialog shouldn't be blocked.");128129if (i == 0) {130assertTrue(dialogs.get(0).getModalityType() ==131Dialog.ModalityType.MODELESS, "0: invalid modality type.");132133frame.checkUnblockedFrame(robot, i + ": A modeless dialog was " +134"shown, but the parent frame became blocked.");135136} else {137if (i % 4 == 0) { // modeless dialog138assertTrue(dialogs.get(i).getModalityType() ==139Dialog.ModalityType.MODELESS, i +140": incorrect dialog modality type.");141142dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +143"dialog was shown, but the parent dialog became blocked.");144145if (i > 0) {146for (int j = 0; j < i - 1; ++j) {147148dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +149": Showing a modeless dialog as a child of a " +150"modal dialog unblocked some dialog in its hierarchy.");151}152}153} else {154155for (int j = 0; j < i; ++j) {156157dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +158": A modal dialog was shown, but some dialog " +159"in its hierarchy became unblocked.");160}161}162163frame.checkBlockedFrame(robot, i + ": A modal was dialog shown, " +164"but the document root frame became unblocked.");165}166167if (i != n - 1) {168dialogs.get(i).clickOpenButton(robot);169robot.waitForIdle(delay);170}171}172173for (int i = n - 1; i >= 0; --i) {174175resetAll();176dialogs.get(i).clickCloseButton(robot);177robot.waitForIdle(delay);178179if (i > 0) {180181dialogs.get(i - 1).activated.waitForFlagTriggered();182assertTrue(dialogs.get(i - 1).activated.flag(), i + ": Dialog " +183"was not activated when a child dialog was closed.");184185if (i == 1) {186187frame.checkUnblockedFrame(robot, "1: Frame having " +188"a child modeless dialog was blocked.");189190dialogs.get(0).checkUnblockedDialog(robot,191"0: A modeless dialog at the bottom " +192"of the hierarchy was blocked.");193194} else if ((i - 1) % 4 == 0) { // dialog[i - 1] is modeless195196for (int j = 0; j < i - 2; ++j) {197198dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +199": A dialog in the hierarchy was not blocked. " +200"A dialog blocking a modeless dialog was closed.");201}202203dialogs.get(i - 2).checkUnblockedDialog(robot, i + ": A modal " +204"dialog having a child modeless dialog was blocked.");205206dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +207"dialog at the bottom of the hierarchy was blocked.");208209frame.checkBlockedFrame(robot, i +210": Frame having a child modal dialog was not blocked.");211212} else {213for (int j = 0; j <= i - 2; ++j) {214dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +215": A dialog in the hierarchy was not blocked. " +216"A child dialog was closed.");217}218219dialogs.get(i - 1).checkUnblockedDialog(robot, (i - 1) +220": A dialog was not unblocked when the modal dialog was closed.");221222frame.checkBlockedFrame(robot, i + ": Frame having " +223"a child modal dialog was not blocked. " +224"Another child dialog was closed.");225}226} else {227frame.activated.waitForFlagTriggered();228assertTrue(frame.activated.flag(), i + ": Frame was not " +229"activated when a child dialog was closed.");230}231}232} // synchronized233234} finally {235EventQueue.invokeAndWait(this::closeAll);236}237}238239private void resetAll() {240frame.resetStatus();241Iterator<CustomDialog> it = dialogList.iterator();242while (it.hasNext()) { it.next().resetStatus(); }243}244245public void closeAll() {246if (frame != null) { frame.dispose(); }247if (dialogList != null) {248Iterator<CustomDialog> it = dialogList.iterator();249while (it.hasNext()) { it.next().dispose(); }250}251}252253class CustomFrame extends TestFrame {254255@Override256public void doOpenAction() {257if ((dialogList != null) && (dialogList.size() > dialogCount)) {258dialogCount++;259CustomDialog d = dialogList.get(dialogCount);260if (d != null) { d.setVisible(true); }261}262}263}264265class CustomDialog extends TestDialog {266267public CustomDialog(Frame frame) { super(frame); }268public CustomDialog(Dialog dialog) { super(dialog); }269270@Override271public void doCloseAction() { this.dispose(); }272273@Override274public void doOpenAction() {275if ((dialogList != null) && (dialogList.size() > dialogCount)) {276dialogCount++;277CustomDialog d = dialogList.get(dialogCount);278if (d != null) { d.setVisible(true); }279}280}281}282283284public static void main(String[] args) throws Exception {285(new MultipleDialogs3Test()).doTest();286}287}288289290