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