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