1 /*
   2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8054358
  27  * @summary Check whether a set of dialogs created with an application excluded Frame
  28  *          parent has a proper modal blocking behavior. Also show a document modal
  29  *          dialog and check if it blocks the document properly.
  30  *
  31  * @library ../helpers ../../../../lib/testlibrary/
  32  * @build ExtendedRobot
  33  * @build Flag
  34  * @build TestDialog
  35  * @build TestFrame
  36  * @run main/timeout=500 MultipleDialogs2Test
  37  */
  38 
  39 import java.awt.*;
  40 
  41 public class MultipleDialogs2Test {
  42 
  43     private CustomFrame frame;
  44     private CustomDialog dialogs[];
  45     private static final int delay = 500;
  46 
  47     private int dialogCount = -1;
  48 
  49     private void createGUI() {
  50 
  51         final int n = 8;
  52         dialogs = new CustomDialog[n];
  53 
  54         frame = new CustomFrame();
  55         frame.setLocation(50, 50);
  56         frame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  57         frame.setVisible(true);
  58 
  59         int x = 250, y = 50;
  60         for (int i = 0; i < n - 1; ++i) {
  61 
  62             dialogs[i] = new CustomDialog(frame);
  63             dialogs[i].setLocation(x, y);
  64             x += 200;
  65             if (x > 600) {
  66                 x = 50;
  67                 y += 200;
  68             }
  69 
  70             Dialog.ModalityType type;
  71             if (i % 3 == 0) {
  72                 type = Dialog.ModalityType.MODELESS;
  73             } else if (i % 3 == 1) {
  74                 type = Dialog.ModalityType.APPLICATION_MODAL;
  75             } else {
  76                 type = Dialog.ModalityType.TOOLKIT_MODAL;
  77             }
  78             dialogs[i].setModalityType(type);
  79 
  80         }
  81         dialogs[n - 1] = new CustomDialog(frame);
  82         dialogs[n - 1].setLocation(x, y);
  83         dialogs[n - 1].setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  84     }
  85 
  86     public void doTest() throws Exception {
  87 
  88         try {
  89             EventQueue.invokeAndWait(this::createGUI);
  90 
  91             ExtendedRobot robot = new ExtendedRobot();
  92             robot.waitForIdle(delay);
  93 
  94             final int n = dialogs.length;
  95             for (int i = 0; i < n - 1; ++i) {
  96 
  97                 frame.clickOpenButton(robot);
  98                 robot.waitForIdle(delay);
  99 
 100                 dialogs[i].checkUnblockedDialog(
 101                     robot, i + ": This is " + getType(i) + ".");
 102 
 103                 if (isToolkitModal(i)) {
 104 
 105                     for (int j = 0; j < i; ++j) {
 106                         dialogs[j].checkBlockedDialog(robot, j + ": This dialog " +
 107                             "should be blocked by a toolkit modal dialog.");
 108                     }
 109 
 110                     frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +
 111                         "should block this application modality excluded frame.");
 112 
 113                     break;
 114 
 115                 } else {
 116                     for (int j = 0; j < i; ++j) {
 117                         dialogs[j].checkUnblockedDialog(robot,
 118                             j + ": An application modality excluded frame " +
 119                             "is the parent of this dialog.");
 120                     }
 121 
 122                     frame.checkUnblockedFrame(robot, i + ": The frame is " +
 123                         "application modality excluded, but it is blocked.");
 124                 }
 125             }
 126 
 127             int tkIndex = dialogCount; // continue testing
 128             final int tk0 = tkIndex;
 129 
 130             for (int i = tk0 + 1; i < n - 1; ++i) {
 131 
 132                 dialogs[tkIndex].clickOpenButton(robot);
 133                 robot.waitForIdle(delay);
 134 
 135                 frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +
 136                     "should block this application modality excluded frame.");
 137 
 138                 if (isToolkitModal(i)) {
 139                     dialogs[i].checkUnblockedDialog(robot, i + ": This is " +
 140                         "a toolkit modal dialog with blocked frame parent. " +
 141                         "Another toolkit modal dialog is visible.");
 142 
 143                     for (int j = 0; j < i; ++j) {
 144                         dialogs[j].checkBlockedDialog(robot, j + ": " +
 145                             "A toolkit modal dialog should block this child " +
 146                             "dialog of application modality excluded frame.");
 147                     }
 148                     tkIndex = i;
 149                 } else {
 150                     dialogs[i].checkBlockedDialog(
 151                         robot, i + ": This is " + getType(i) + " with blocked " +
 152                         "Frame parent. Also, a toolkit modal dialog is visible.");
 153 
 154                     for (int j = 0; j < i; ++j) {
 155                         if (j != tkIndex) {
 156                             dialogs[j].checkBlockedDialog(robot, j +
 157                                 ": A toolkit modal dialog should block this " +
 158                                 "child dialog of an application modality excluded frame.");
 159                         }
 160                     }
 161                 }
 162             }
 163 
 164             // show a document modal dialog; the toolkit modal dialog should block it
 165             dialogs[tkIndex].clickOpenButton(robot);
 166             robot.waitForIdle(delay);
 167 
 168             frame.checkBlockedFrame(robot, "A toolkit modal dialog is visible.");
 169 
 170             for (int i = 0; i < n; ++i) {
 171                 if (i == tkIndex) {
 172                     dialogs[tkIndex].checkUnblockedDialog(robot,
 173                         tkIndex + ": This is a toolkit modal dialog. " +
 174                         "A document modal dialog is visible.");
 175                 } else {
 176                     dialogs[i].checkBlockedDialog(robot,
 177                         i + ": A toolkit modal dialog should block this dialog.");
 178                 }
 179             }
 180 
 181             dialogs[tk0 + 3].clickCloseButton(robot); // close 2nd toolkit dialog
 182             robot.waitForIdle(delay);
 183 
 184             for (int i = 0; i < n; ++i) {
 185 
 186                 if (i == tk0 + 3) { continue; }
 187                 if (i == tk0) {
 188                     dialogs[i].checkUnblockedDialog(robot,
 189                         i + ": This is a toolkit modal dialog. A blocking " +
 190                         "toolkit modal dialog was opened and then closed.");
 191                 } else {
 192                     dialogs[i].checkBlockedDialog(robot,
 193                         i + ": This dialog should be blocked by a toolkit modal dialog. " +
 194                         "Another blocking toolkit modal dialog was closed.");
 195                 }
 196             }
 197 
 198             dialogs[tk0].clickCloseButton(robot);
 199             robot.waitForIdle(delay);
 200 
 201             frame.checkBlockedFrame(
 202                     robot, "A document modal dialog should block this Frame.");
 203 
 204             for (int i = 0; i < n - 1; ++i) {
 205                 if (!isToolkitModal(i)) {
 206                     dialogs[i].checkUnblockedDialog(robot, i + ": The parent " +
 207                         "of the dialog is an app modality excluded Frame.");
 208                 }
 209             }
 210 
 211             dialogs[n - 1].clickCloseButton(robot);
 212             robot.waitForIdle(delay);
 213 
 214             frame.checkUnblockedFrame(robot, "A document modal dialog " +
 215                 "blocking this Frame was closed.");
 216 
 217             for (int i = 0; i < n - 1; ++i) {
 218                 if (!isToolkitModal(i)) {
 219                     dialogs[i].checkUnblockedDialog(robot, i + ": A document modal " +
 220                         "dialog blocking the parent frame was closed.");
 221                 }
 222             }
 223 
 224         } finally {
 225             EventQueue.invokeAndWait(this::closeAll);
 226         }
 227     }
 228 
 229     private boolean isToolkitModal(int i) {
 230         return (i % 3 == 2);
 231     }
 232 
 233     private String getType(int i) {
 234 
 235         switch (dialogs[i].getModalityType()) {
 236             case APPLICATION_MODAL:
 237                 return "an application modal dialog";
 238             case DOCUMENT_MODAL:
 239                 return "a document modal dialog";
 240             case TOOLKIT_MODAL:
 241                 return "a toolkit modal dialog";
 242         }
 243         return "a modeless dialog";
 244     }
 245 
 246     public void closeAll() {
 247 
 248         if (frame != null) { frame.dispose(); }
 249         if (dialogs != null) {
 250             for (CustomDialog dialog : dialogs) { dialog.dispose(); }
 251         }
 252     }
 253 
 254     class CustomFrame extends TestFrame {
 255 
 256         @Override
 257         public void doOpenAction() {
 258             if ((dialogs != null) && (dialogs.length > dialogCount)) {
 259                 dialogCount++;
 260                 if (dialogs[dialogCount] != null) {
 261                     dialogs[dialogCount].setVisible(true);
 262                 }
 263             }
 264         }
 265     }
 266 
 267     class CustomDialog extends TestDialog {
 268 
 269         public CustomDialog(Frame frame) { super(frame); }
 270 
 271         @Override
 272         public void doCloseAction() { this.dispose(); }
 273 
 274         @Override
 275         public void doOpenAction() {
 276             if ((dialogs != null) && (dialogs.length > dialogCount)) {
 277                 dialogCount++;
 278                 if (dialogs[dialogCount] != null) {
 279                     dialogs[dialogCount].setVisible(true);
 280                 }
 281             }
 282         }
 283     }
 284 
 285     public static void main(String[] args) throws Exception {
 286         (new MultipleDialogs2Test()).doTest();
 287     }
 288 }