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  * @key headful
  27  * @bug 8054358
  28  * @summary Check whether a set of dialogs created with an application excluded Frame
  29  *          parent has a proper modal blocking behavior. Also show a document modal
  30  *          dialog and check if it blocks the document properly.
  31  *
  32  * @library ../helpers /lib/testlibrary/
  33  * @library /test/lib
  34  * @build ExtendedRobot
  35  * @build Flag
  36  * @build TestDialog
  37  * @build TestFrame
  38  * @run main/timeout=500 MultipleDialogs2Test
  39  */
  40 
  41 
  42 import java.awt.*;
  43 import java.util.ArrayList;
  44 import java.util.List;
  45 import java.util.Collections;
  46 import java.util.Iterator;
  47 
  48 public class MultipleDialogs2Test {
  49 
  50     private volatile CustomFrame frame;
  51     private List<CustomDialog> dialogList;
  52     private static final int delay = 500;
  53 
  54     private int dialogCount = -1;
  55 
  56     private void createGUI() {
  57 
  58         final int n = 8;
  59         dialogList = new ArrayList<>();
  60 
  61         frame = new CustomFrame();
  62         frame.setLocation(50, 50);
  63         frame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  64         frame.setVisible(true);
  65 
  66         CustomDialog dlg;
  67 
  68         int x = 250, y = 50;
  69         for (int i = 0; i < n - 1; ++i) {
  70 
  71             dlg = new CustomDialog(frame);
  72             dlg.setLocation(x, y);
  73             x += 200;
  74             if (x > 600) {
  75                 x = 50;
  76                 y += 200;
  77             }
  78 
  79             Dialog.ModalityType type;
  80             if (i % 3 == 0) {
  81                 type = Dialog.ModalityType.MODELESS;
  82             } else if (i % 3 == 1) {
  83                 type = Dialog.ModalityType.APPLICATION_MODAL;
  84             } else {
  85                 type = Dialog.ModalityType.TOOLKIT_MODAL;
  86             }
  87             dlg.setModalityType(type);
  88             dialogList.add(dlg);
  89         }
  90 
  91         dlg = new CustomDialog(frame);
  92         dlg.setLocation(x, y);
  93         dlg.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  94         dialogList.add(dlg);
  95     }
  96 
  97     public void doTest() throws Exception {
  98 
  99         try {
 100             EventQueue.invokeAndWait(this::createGUI);
 101 
 102             ExtendedRobot robot = new ExtendedRobot();
 103             robot.waitForIdle(delay);
 104 
 105             List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);
 106             final int n = dialogs.size();
 107 
 108             synchronized(dialogs) {
 109                 for (int i = 0; i < n - 1; ++i) {
 110 
 111                     frame.clickOpenButton(robot);
 112                     robot.waitForIdle(delay);
 113 
 114                     dialogs.get(i).checkUnblockedDialog(
 115                         robot, i + ": This is " + getType(i) + ".");
 116 
 117                     if (isToolkitModal(i)) {
 118 
 119                         for (int j = 0; j < i; ++j) {
 120                             dialogs.get(j).checkBlockedDialog(robot, j + ": This dialog " +
 121                                 "should be blocked by a toolkit modal dialog.");
 122                         }
 123 
 124                         frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +
 125                             "should block this application modality excluded frame.");
 126 
 127                         break;
 128 
 129                     } else {
 130                         for (int j = 0; j < i; ++j) {
 131                             dialogs.get(j).checkUnblockedDialog(robot,
 132                                 j + ": An application modality excluded frame " +
 133                                 "is the parent of this dialog.");
 134                         }
 135 
 136                         frame.checkUnblockedFrame(robot, i + ": The frame is " +
 137                             "application modality excluded, but it is blocked.");
 138                     }
 139                 }
 140 
 141                 int tkIndex = dialogCount; // continue testing
 142                 final int tk0 = tkIndex;
 143 
 144                 for (int i = tk0 + 1; i < n - 1; ++i) {
 145 
 146                     dialogs.get(tkIndex).clickOpenButton(robot);
 147                     robot.waitForIdle(delay);
 148 
 149                     frame.checkBlockedFrame(robot, i + ": A toolkit modal dialog " +
 150                         "should block this application modality excluded frame.");
 151 
 152                     if (isToolkitModal(i)) {
 153                         dialogs.get(i).checkUnblockedDialog(robot, i + ": This is " +
 154                             "a toolkit modal dialog with blocked frame parent. " +
 155                             "Another toolkit modal dialog is visible.");
 156 
 157                         for (int j = 0; j < i; ++j) {
 158                             dialogs.get(j).checkBlockedDialog(robot, j + ": " +
 159                                 "A toolkit modal dialog should block this child " +
 160                                 "dialog of application modality excluded frame.");
 161                         }
 162                         tkIndex = i;
 163                     } else {
 164                         dialogs.get(i).checkBlockedDialog(
 165                             robot, i + ": This is " + getType(i) + " with blocked " +
 166                             "Frame parent. Also, a toolkit modal dialog is visible.");
 167 
 168                         for (int j = 0; j < i; ++j) {
 169                             if (j != tkIndex) {
 170                                 dialogs.get(j).checkBlockedDialog(robot, j +
 171                                     ": A toolkit modal dialog should block this " +
 172                                     "child dialog of an application modality excluded frame.");
 173                             }
 174                         }
 175                     }
 176                 }
 177 
 178                 // show a document modal dialog; the toolkit modal dialog should block it
 179                 dialogs.get(tkIndex).clickOpenButton(robot);
 180                 robot.waitForIdle(delay);
 181 
 182                 frame.checkBlockedFrame(robot, "A toolkit modal dialog is visible.");
 183 
 184                 for (int i = 0; i < n; ++i) {
 185                     if (i == tkIndex) {
 186                         dialogs.get(tkIndex).checkUnblockedDialog(robot,
 187                             tkIndex + ": This is a toolkit modal dialog. " +
 188                             "A document modal dialog is visible.");
 189                     } else {
 190                         dialogs.get(i).checkBlockedDialog(robot,
 191                             i + ": A toolkit modal dialog should block this dialog.");
 192                     }
 193                 }
 194 
 195                 dialogs.get(tk0 + 3).clickCloseButton(robot); // close 2nd toolkit dialog
 196                 robot.waitForIdle(delay);
 197 
 198                 for (int i = 0; i < n; ++i) {
 199 
 200                     if (i == tk0 + 3) { continue; }
 201                     if (i == tk0) {
 202                         dialogs.get(i).checkUnblockedDialog(robot,
 203                             i + ": This is a toolkit modal dialog. A blocking " +
 204                             "toolkit modal dialog was opened and then closed.");
 205                     } else {
 206                         dialogs.get(i).checkBlockedDialog(robot,
 207                             i + ": This dialog should be blocked by a toolkit modal dialog. " +
 208                             "Another blocking toolkit modal dialog was closed.");
 209                     }
 210                 }
 211 
 212                 dialogs.get(tk0).clickCloseButton(robot);
 213                 robot.waitForIdle(delay);
 214 
 215                 frame.checkBlockedFrame(
 216                         robot, "A document modal dialog should block this Frame.");
 217 
 218                 for (int i = 0; i < n - 1; ++i) {
 219                     if (!isToolkitModal(i)) {
 220                         dialogs.get(i).checkUnblockedDialog(robot, i + ": The parent " +
 221                             "of the dialog is an app modality excluded Frame.");
 222                     }
 223                 }
 224 
 225                 dialogs.get(n - 1).clickCloseButton(robot);
 226                 robot.waitForIdle(delay);
 227 
 228                 frame.checkUnblockedFrame(robot, "A document modal dialog " +
 229                     "blocking this Frame was closed.");
 230 
 231                 for (int i = 0; i < n - 1; ++i) {
 232                     if (!isToolkitModal(i)) {
 233                         dialogs.get(i).checkUnblockedDialog(robot, i + ": A document modal " +
 234                             "dialog blocking the parent frame was closed.");
 235                     }
 236                 }
 237             } // synchronized
 238 
 239         } finally {
 240             EventQueue.invokeAndWait(this::closeAll);
 241         }
 242     }
 243 
 244     private boolean isToolkitModal(int i) { return (i % 3 == 2); }
 245 
 246     private String getType(int i) {
 247 
 248         switch (dialogList.get(i).getModalityType()) {
 249             case APPLICATION_MODAL:
 250                 return "an application modal dialog";
 251             case DOCUMENT_MODAL:
 252                 return "a document modal dialog";
 253             case TOOLKIT_MODAL:
 254                 return "a toolkit modal dialog";
 255         }
 256         return "a modeless dialog";
 257     }
 258 
 259     public void closeAll() {
 260 
 261         if (frame != null) { frame.dispose(); }
 262         if (dialogList != null) {
 263             Iterator<CustomDialog> it = dialogList.iterator();
 264             while (it.hasNext()) { it.next().dispose(); }
 265         }
 266     }
 267 
 268     class CustomFrame extends TestFrame {
 269 
 270         @Override
 271         public void doOpenAction() {
 272             if ((dialogList != null) && (dialogList.size() > dialogCount)) {
 273                 dialogCount++;
 274                 CustomDialog d = dialogList.get(dialogCount);
 275                 if (d != null) { d.setVisible(true); }
 276             }
 277         }
 278     }
 279 
 280     class CustomDialog extends TestDialog {
 281 
 282         public CustomDialog(Frame frame) { super(frame); }
 283 
 284         @Override
 285         public void doCloseAction() { this.dispose(); }
 286 
 287         @Override
 288         public void doOpenAction() {
 289             if ((dialogList != null) && (dialogList.size() > dialogCount)) {
 290                 dialogCount++;
 291                 if (dialogList.get(dialogCount) != null) {
 292                     dialogList.get(dialogCount).setVisible(true);
 293                 }
 294             }
 295         }
 296     }
 297 
 298     public static void main(String[] args) throws Exception {
 299         (new MultipleDialogs2Test()).doTest();
 300     }
 301 }