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 a toolkit 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 MultipleDialogs1Test
  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 MultipleDialogs1Test {
  47 
  48     private volatile CustomFrame frame;
  49     private List<CustomDialog> dialogList;
  50 
  51     private static int delay = 500;
  52     private int dialogCount = -1;
  53 
  54     public 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.TOOLKIT_EXCLUDE);
  62         frame.setVisible(true);
  63 
  64         int x = 250, y = 50;
  65 
  66         CustomDialog dlg;
  67 
  68         for (int i = 0; i < n - 1; ++i) {
  69 
  70             dlg = new CustomDialog(frame);
  71             dlg.setLocation(x, y);
  72             x += 200;
  73 
  74             if (x > 600) {
  75                 x = 50;
  76                 y += 200;
  77             }
  78 
  79             Dialog.ModalityType type;
  80 
  81             if (i % 3 == 0) {
  82                 type = Dialog.ModalityType.MODELESS;
  83             } else if (i % 3 == 1) {
  84                 type = Dialog.ModalityType.APPLICATION_MODAL;
  85             } else {
  86                 type = Dialog.ModalityType.TOOLKIT_MODAL;
  87             }
  88 
  89             dlg.setModalityType(type);
  90             dialogList.add(dlg);
  91         }
  92 
  93         dlg = new CustomDialog(frame);
  94         dlg.setLocation(x, y);
  95         dlg.setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
  96         dialogList.add(dlg);
  97     }
  98 
  99     public void doTest() throws Exception {
 100 
 101         try {
 102             EventQueue.invokeAndWait(this::createGUI);
 103 
 104             ExtendedRobot robot = new ExtendedRobot();
 105             robot.waitForIdle(delay);
 106 
 107             List<CustomDialog> dialogs = Collections.synchronizedList(dialogList);
 108 
 109             final int n = dialogs.size();
 110 
 111             synchronized(dialogs) {
 112                 for (int i = 0; i < n - 1; ++i) {
 113 
 114                     frame.clickOpenButton(robot);
 115                     robot.waitForIdle(delay);
 116 
 117                     dialogs.get(i).checkUnblockedDialog(
 118                         robot, i + ": This is " + getType(i) + ".");
 119 
 120                     frame.checkUnblockedFrame(robot, i + ": Other dialogs are visible.");
 121 
 122                     if (i > 0) {
 123                         for (int j = 0; j < i; j++) {
 124                             dialogs.get(j).checkUnblockedDialog(robot, j + ": A toolkit modality " +
 125                                 "excluded frame is a parent of this dialog.");
 126                         }
 127                     }
 128                 } // i
 129 
 130                 frame.clickOpenButton(robot);
 131                 robot.waitForIdle(delay);
 132 
 133                 dialogs.get(n - 1).checkUnblockedDialog(
 134                     robot, (n - 1) + ": This is " + getType(n - 1) + ".");
 135 
 136                 frame.checkBlockedFrame(robot, 
 137                     "A document modal dialog with Frame parent is visible.");
 138 
 139                 for (int i = 0; i < n - 1; ++i) {
 140                     dialogs.get(i).checkUnblockedDialog(robot,
 141                         i + ": A document modal dialog should not block " +
 142                         "this dialog. The parent is modality excluded.");
 143                 }
 144 
 145                 dialogs.get(n - 1).clickCloseButton(robot);
 146                 robot.waitForIdle(delay);
 147 
 148                 for (int i = 0; i < n - 1; ++i) {
 149                     dialogs.get(i).checkUnblockedDialog(robot, i + ": A document modal " +
 150                         "dialog which blocked this dialog was closed.");
 151                 }
 152                 frame.checkUnblockedFrame(robot,
 153                     "A blocking document modal dialog was closed.");
 154                 robot.waitForIdle(delay);
 155             } // synchronized
 156 
 157         } finally {
 158             EventQueue.invokeAndWait(this::closeAll);
 159         }
 160     }
 161 
 162     private String getType(int i) {
 163 
 164         switch (dialogList.get(i).getModalityType()) {
 165             case APPLICATION_MODAL:
 166                 return "an application modal dialog";
 167             case DOCUMENT_MODAL:
 168                 return "a document modal dialog";
 169             case TOOLKIT_MODAL:
 170                 return "a toolkit modal dialog";
 171         }
 172         return "a modeless dialog";
 173     }
 174 
 175     private void closeAll() {
 176 
 177         if (frame   != null) { frame.dispose(); }
 178         if (dialogList != null) {
 179             Iterator<CustomDialog> it = dialogList.iterator();
 180             while (it.hasNext()) { it.next().dispose(); }
 181         }
 182     }
 183 
 184     class CustomFrame extends TestFrame {
 185 
 186         @Override
 187         public void doOpenAction() {
 188             if ((dialogList != null) && (dialogList.size() > dialogCount)) {
 189                 dialogCount++;
 190                 CustomDialog d = dialogList.get(dialogCount);
 191                 if (d != null) { d.setVisible(true); }
 192             }
 193         }
 194     }
 195 
 196     class CustomDialog extends TestDialog {
 197 
 198         public CustomDialog(Frame  f) { super(f); }
 199         public CustomDialog(Dialog d) { super(d); }
 200 
 201         @Override
 202         public void doCloseAction() { this.dispose(); }
 203     }
 204 
 205 
 206     public static void main(String[] args) throws Exception {
 207         (new MultipleDialogs1Test()).doTest();
 208     }
 209 }