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 import java.awt.*;
25 import java.awt.event.*;
26 import java.awt.print.*;
27 
28 import static jdk.test.lib.Asserts.*;
29 
30 
31 public class ExcludeDialogTest implements AWTEventListener {
32 
33     class ParentDialog extends TestDialog {
34 
ParentDialog()35         public ParentDialog() {
36             super((Frame) null);
37         }
38 
39         @Override
doOpenAction()40         public void doOpenAction() {
41             if (window != null) {
42                 window.setVisible(true);
43             }
44         }
45     }
46 
47     class CustomDialog extends TestDialog {
48 
CustomDialog(Dialog dialog)49         public CustomDialog(Dialog dialog) {
50             super(dialog);
51         }
52 
53         @Override
doOpenAction()54         public void doOpenAction() {
55 
56             switch (childDialogType) {
57                 case PRINT_SETUP:
58                     PrinterJob.getPrinterJob().printDialog();
59                     break;
60                 case PAGE_SETUP:
61                     PrinterJob.getPrinterJob().pageDialog(new PageFormat());
62                     break;
63                 case FILE_DIALOG:
64                     fileDialog = new FileDialog((Dialog) null);
65                     fileDialog.setLocation(20, 200);
66                     fileDialog.setVisible(true);
67                     break;
68             }
69         }
70     }
71 
72     class CustomWindow extends TestWindow {
73 
CustomWindow(Dialog parent)74         public CustomWindow(Dialog parent) {
75             super(parent);
76         }
77 
78         @Override
doOpenAction()79         public void doOpenAction() {
80             if (dialog != null) {
81                 dialog.setVisible(true);
82             }
83         }
84     }
85 
86     private TestWindow window;
87     private TestDialog dialog;
88     private ParentDialog parent;
89     private FileDialog fileDialog;
90 
91     private static final int delay = 1000;
92 
93     private final ExtendedRobot robot;
94 
95     private boolean windowAppeared = false;
96     private final Object windowLock;
97 
98     private final Dialog.ModalExclusionType exclusionType;
99 
100     public enum DialogToShow {PAGE_SETUP, PRINT_SETUP, FILE_DIALOG};
101     private DialogToShow childDialogType;
102     private String type;
103 
104     @Override
eventDispatched(AWTEvent event)105     public void eventDispatched(AWTEvent event) {
106         if (event.getID() == WindowEvent.WINDOW_OPENED) {
107             windowAppeared = true;
108             synchronized (windowLock) {
109                 windowLock.notifyAll();
110             }
111         }
112     }
113 
ExcludeDialogTest(Dialog.ModalExclusionType exclType, DialogToShow dialogType)114     ExcludeDialogTest(Dialog.ModalExclusionType exclType,
115                       DialogToShow dialogType) throws Exception {
116         exclusionType = exclType;
117         childDialogType = dialogType;
118 
119         type = "File";
120         if (dialogType == DialogToShow.PAGE_SETUP) {
121             type = "Page setup";
122         } else if (dialogType == DialogToShow.PRINT_SETUP) {
123             type = "Print setup";
124         }
125 
126         robot = new ExtendedRobot();
127         windowLock = new Object();
128         EventQueue.invokeAndWait( this::createGUI );
129     }
130 
createGUI()131     private void createGUI() {
132         parent = new ParentDialog();
133         parent.setLocation(20, 20);
134         parent.setModalExclusionType(exclusionType);
135         dialog = new CustomDialog(parent);
136         dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
137         dialog.setLocation(220, 20);
138         window = new CustomWindow(parent);
139         window.setLocation(420, 20);
140         Toolkit.getDefaultToolkit().addAWTEventListener(
141             ExcludeDialogTest.this, AWTEvent.WINDOW_EVENT_MASK);
142         parent.setVisible(true);
143     }
144 
closeAll()145     private void closeAll() {
146         if (dialog != null) { dialog.dispose(); }
147         if (parent != null) { parent.dispose(); }
148         if (window != null) { window.dispose(); }
149         if (fileDialog != null) {
150             fileDialog.dispose();
151         } else {
152             robot.type(KeyEvent.VK_ESCAPE);
153         }
154     }
155 
doTest()156     public void doTest() throws Exception {
157 
158         robot.waitForIdle(delay);
159 
160         parent.clickOpenButton(robot);
161         robot.waitForIdle(delay);
162 
163         window.clickOpenButton(robot);
164         robot.waitForIdle(delay);
165 
166         dialog.clickOpenButton(robot);
167         robot.waitForIdle(delay);
168 
169         if (! windowAppeared) {
170             synchronized (windowLock) {
171                 try {
172                     windowLock.wait(10 * delay);
173                 } catch (InterruptedException e) {}
174             }
175         }
176 
177         assertTrue(windowAppeared, type + " dialog didn't appear");
178 
179         parent.toFront();
180         robot.waitForIdle(delay);
181 
182         String excl = "";
183         if (exclusionType == Dialog.ModalExclusionType.APPLICATION_EXCLUDE) {
184             excl = "Application";
185         } else if (exclusionType == Dialog.ModalExclusionType.TOOLKIT_EXCLUDE) {
186             excl = "Toolkit";
187         }
188 
189         parent.checkUnblockedDialog(robot, excl + " modal " + type +
190                 " dialog should not block this modal excluded Dialog");
191         dialog.checkUnblockedDialog(robot, excl + " modal " + type +
192                 " dialog should not block this modal excluded app. modal Dialog");
193         window.checkUnblockedWindow(robot, excl + " modal " + type +
194                 " dialog should not block this modal excluded Window");
195 
196         robot.waitForIdle();
197         closeAll();
198     }
199 }
200