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