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