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