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 correctness of modal blocking behavior for a chain of Dialogs
  28  *          having different modality types with a Frame as a document root.
  29  *
  30  * @library ../helpers ../../../../lib/testlibrary/
  31  * @build ExtendedRobot
  32  * @build Flag
  33  * @build TestDialog
  34  * @build TestFrame
  35  * @run main/timeout=500 MultipleDialogs3Test
  36  */
  37 
  38 
  39 import java.awt.*;
  40 import static jdk.testlibrary.Asserts.*;
  41 import java.util.ArrayList;
  42 import java.util.List;
  43 import java.util.Collections;
  44 import java.util.Iterator;
  45 
  46 
  47 public class MultipleDialogs3Test {
  48 
  49     private volatile CustomFrame  frame;
  50     private List<CustomDialog> dialogList;
  51     private static int delay = 500;
  52 
  53     private int dialogCount = -1;
  54 
  55     public void createGUI() {
  56 
  57         final int n = 8;
  58         dialogList = new ArrayList<>();
  59 
  60         frame = new CustomFrame();
  61         frame.setLocation(50, 50);
  62         frame.setVisible(true);
  63 
  64         int x = 250;
  65         int y = 50;
  66         for (int i = 0; i < n; ++i) {
  67 
  68             CustomDialog dlg;
  69             if (i == 0) {
  70                 dlg = new CustomDialog(frame);
  71             } else {
  72                 dlg = new CustomDialog(dialogList.get(i - 1));
  73             }
  74             dlg.setLocation(x, y);
  75             x += 200;
  76             if (x > 600) {
  77                 x = 50;
  78                 y += 200;
  79             }
  80 
  81             Dialog.ModalityType type;
  82 
  83             if (i % 4 == 0) {
  84                 type = Dialog.ModalityType.MODELESS;
  85             } else if (i % 4 == 1) {
  86                 type = Dialog.ModalityType.DOCUMENT_MODAL;
  87             } else if (i % 4 == 2) {
  88                 type = Dialog.ModalityType.APPLICATION_MODAL;
  89             } else {
  90                 type = Dialog.ModalityType.TOOLKIT_MODAL;
  91             }
  92 
  93             dlg.setModalityType(type);
  94             dialogList.add(dlg);
  95         }
  96     }
  97 
  98     public void doTest() throws Exception {
  99 
 100         try {
 101             EventQueue.invokeAndWait(this::createGUI);
 102 
 103             ExtendedRobot robot = new ExtendedRobot();
 104             robot.waitForIdle(delay);
 105 
 106             frame.clickOpenButton(robot);
 107             robot.waitForIdle(delay);
 108 
 109             List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);
 110             final int n = dialogs.size();
 111 
 112             synchronized(dialogs) {
 113                 for (int i = 0; i < n; ++i) {
 114                     dialogs.get(i).activated.waitForFlagTriggered();
 115                     assertTrue(dialogs.get(i).activated.flag(), i + ": Dialog did not " +
 116                         "trigger windowActivated event when it became visible.");
 117 
 118                     dialogs.get(i).closeGained.waitForFlagTriggered();
 119                     assertTrue(dialogs.get(i).closeGained.flag(), i + ": Close button " +
 120                         "did not gain focus when Dialog became visible.");
 121 
 122                     assertTrue(dialogs.get(i).closeButton.hasFocus(), i + 
 123                         ": Close button gained focus but then lost it.");
 124 
 125                     dialogs.get(i).checkUnblockedDialog(robot,
 126                         i + ": The dialog shouldn't be blocked.");
 127 
 128                     if (i == 0) {
 129                         assertTrue(dialogs.get(0).getModalityType() == 
 130                             Dialog.ModalityType.MODELESS, "0: invalid modality type.");
 131 
 132                         frame.checkUnblockedFrame(robot, i + ": A modeless dialog was " +
 133                             "shown, but the parent frame became blocked.");
 134 
 135                     } else {
 136                         if (i % 4 == 0) { // modeless dialog
 137                             assertTrue(dialogs.get(i).getModalityType() == 
 138                                 Dialog.ModalityType.MODELESS, i + 
 139                                 ": incorrect dialog modality type.");
 140 
 141                             dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +
 142                                 "dialog was shown, but the parent dialog became blocked.");
 143 
 144                             if (i > 0) {
 145                                 for (int j = 0; j < i - 1; ++j) {
 146 
 147                                     dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +
 148                                         ": Showing a modeless dialog as a child of a " +
 149                                         "modal dialog unblocked some dialog in its hierarchy.");
 150                                 }
 151                             }
 152                         } else {
 153 
 154                             for (int j = 0; j < i; ++j) {
 155 
 156                                 dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +
 157                                     ": A modal dialog was shown, but some dialog " +
 158                                     "in its hierarchy became unblocked.");
 159                             }
 160                         }
 161 
 162                         frame.checkBlockedFrame(robot, i + ": A modal was dialog shown, " +
 163                             "but the document root frame became unblocked.");
 164                     }
 165 
 166                     if (i != n - 1) {
 167                         dialogs.get(i).clickOpenButton(robot);
 168                         robot.waitForIdle(delay);
 169                     }
 170                 }
 171 
 172                 for (int i = n - 1; i >= 0; --i) {
 173 
 174                     resetAll();
 175                     dialogs.get(i).clickCloseButton(robot);
 176                     robot.waitForIdle(delay);
 177 
 178                     if (i > 0) {
 179 
 180                         dialogs.get(i - 1).activated.waitForFlagTriggered();
 181                         assertTrue(dialogs.get(i - 1).activated.flag(), i + ": Dialog " +
 182                             "was not activated when a child dialog was closed.");
 183 
 184                         if (i == 1) {
 185 
 186                             frame.checkUnblockedFrame(robot, "1: Frame having " +
 187                                 "a child modeless dialog was blocked.");
 188 
 189                             dialogs.get(0).checkUnblockedDialog(robot,
 190                                 "0: A modeless dialog at the bottom " +
 191                                 "of the hierarchy was blocked.");
 192 
 193                         } else if ((i - 1) % 4 == 0) { // dialog[i - 1] is modeless
 194 
 195                             for (int j = 0; j < i - 2; ++j) {
 196 
 197                                 dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +
 198                                     ": A dialog in the hierarchy was not blocked. " +
 199                                     "A dialog blocking a modeless dialog was closed.");
 200                             }
 201 
 202                             dialogs.get(i - 2).checkUnblockedDialog(robot, i + ": A modal " +
 203                                 "dialog having a child modeless dialog was blocked.");
 204 
 205                             dialogs.get(i - 1).checkUnblockedDialog(robot, i + ": A modeless " +
 206                                 "dialog at the bottom of the hierarchy was blocked.");
 207 
 208                             frame.checkBlockedFrame(robot, i +
 209                                 ": Frame having a child modal dialog was not blocked.");
 210 
 211                         } else {
 212                             for (int j = 0; j <= i - 2; ++j) {
 213                                 dialogs.get(j).checkBlockedDialog(robot, i + ", " + j +
 214                                     ": A dialog in the hierarchy was not blocked. " +
 215                                     "A child dialog was closed.");
 216                             }
 217 
 218                             dialogs.get(i - 1).checkUnblockedDialog(robot, (i - 1) +
 219                                 ": A dialog was not unblocked when the modal dialog was closed.");
 220 
 221                             frame.checkBlockedFrame(robot, i + ": Frame having " +
 222                                 "a child modal dialog was not blocked. " +
 223                                 "Another child dialog was closed.");
 224                         }
 225                     } else {
 226                         frame.activated.waitForFlagTriggered();
 227                         assertTrue(frame.activated.flag(), i + ": Frame was not " +
 228                             "activated when a child dialog was closed.");
 229                     }
 230                 }
 231             } // synchronized
 232 
 233         } finally {
 234             EventQueue.invokeAndWait(this::closeAll);
 235         }
 236     }
 237 
 238     private void resetAll() {
 239         frame.resetStatus();
 240         Iterator<CustomDialog> it = dialogList.iterator();
 241         while (it.hasNext()) { it.next().resetStatus(); }
 242     }
 243 
 244     public void closeAll() {
 245         if (frame != null) { frame.dispose(); }
 246         if (dialogList != null) {
 247             Iterator<CustomDialog> it = dialogList.iterator();
 248             while (it.hasNext()) { it.next().dispose(); }
 249         }
 250     }
 251 
 252     class CustomFrame extends TestFrame {
 253 
 254         @Override
 255         public void doOpenAction() {
 256             if ((dialogList != null) && (dialogList.size() > dialogCount)) {
 257                 dialogCount++;
 258                 CustomDialog d = dialogList.get(dialogCount);
 259                 if (d != null) { d.setVisible(true); }
 260             }
 261         }
 262     }
 263 
 264     class CustomDialog extends TestDialog {
 265 
 266         public CustomDialog(Frame frame)   { super(frame); }
 267         public CustomDialog(Dialog dialog) { super(dialog); }
 268 
 269         @Override
 270         public void doCloseAction() { this.dispose(); }
 271 
 272         @Override
 273         public void doOpenAction() {
 274             if ((dialogList != null) && (dialogList.size() > dialogCount)) {
 275                 dialogCount++;
 276                 CustomDialog d = dialogList.get(dialogCount);
 277                 if (d != null) { d.setVisible(true); }
 278             }
 279         }
 280     }
 281 
 282 
 283     public static void main(String[] args) throws Exception {
 284         (new MultipleDialogs3Test()).doTest();
 285     }
 286 }