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 import java.awt.*;
25 
26 public class FileDialogModalityTest {
27 
28     private volatile TestDialog  dialog;
29     private volatile ParentFrame parent;
30     private volatile TestWindow  window;
31     private volatile FileDialog  fileDialog;
32 
33     private static final int delay = 500;
34     private final ExtendedRobot robot;
35 
36     private final Dialog.ModalityType modalityType;
37 
main(String[] args)38     public static void main(String[] args) throws Exception {
39         (new FileDialogModalityTest(Dialog.ModalityType.DOCUMENT_MODAL)).doTest();
40         (new FileDialogModalityTest(Dialog.ModalityType.TOOLKIT_MODAL)).doTest();
41         (new FileDialogModalityTest(Dialog.ModalityType.MODELESS)).doTest();
42     }
43 
FileDialogModalityTest(Dialog.ModalityType t)44     public FileDialogModalityTest(Dialog.ModalityType t) throws Exception {
45         modalityType = t;
46         robot = new ExtendedRobot();
47     }
48 
createGUI()49     private void createGUI() {
50 
51         parent = new ParentFrame();
52         dialog = new CustomDialog((Frame) null);
53         window = new CustomWindow(parent);
54 
55         int x = Toolkit.getDefaultToolkit().getScreenSize().width -
56             parent.getWidth() - 50;
57         int y = 50;
58 
59         parent.setLocation(x, y);
60         y += (parent.getHeight() + 50);
61         window.setLocation(x, y);
62         y += (window.getHeight() + 50);
63         dialog.setLocation(x, y);
64 
65         parent.setVisible(true);
66     }
67 
startTest()68     private void startTest() throws Exception {
69 
70         EventQueue.invokeLater(this::createGUI);
71 
72         robot.waitForIdle(delay);
73         parent.clickOpenButton(robot);
74         robot.waitForIdle(delay);
75         window.clickOpenButton(robot);
76         robot.waitForIdle(delay);
77         dialog.clickOpenButton(robot);
78         robot.waitForIdle(delay);
79     }
80 
checkUnblockedWindows()81     private void checkUnblockedWindows() throws Exception {
82 
83         String msg = " should not be blocked.";
84         parent.checkUnblockedFrame (robot, "This frame" + msg);
85         robot.waitForIdle(delay);
86         window.checkUnblockedWindow(robot, "This window" + msg);
87         robot.waitForIdle(delay);
88         dialog.checkUnblockedDialog(robot, "This dialog" + msg);
89         robot.waitForIdle(delay);
90     }
91 
checkBlockedWindows()92     private void checkBlockedWindows() throws Exception {
93 
94         String msg = " should be blocked by the FileDialog.";
95         parent.checkBlockedFrame (robot, "This Frame" + msg);
96         robot.waitForIdle(delay);
97         window.checkBlockedWindow(robot, "This Window" + msg);
98         robot.waitForIdle(delay);
99         dialog.checkBlockedDialog(robot, "This Dialog" + msg);
100         robot.waitForIdle(delay);
101     }
102 
docModalTest()103     private void docModalTest() throws Exception {
104 
105         String msg = "Document modal FileDialog should ";
106         parent.checkUnblockedFrame (robot, msg + "not block this Frame.");
107         robot.waitForIdle(delay);
108         window.checkUnblockedWindow(robot, msg + "not block this Window.");
109         robot.waitForIdle(delay);
110         dialog.checkBlockedDialog(robot, msg + "block its parent Dialog.");
111         robot.waitForIdle(delay);
112     }
113 
doTest()114     public void doTest() throws Exception {
115 
116         try {
117             startTest();
118 
119             switch (modalityType) {
120                 case APPLICATION_MODAL:
121                 case TOOLKIT_MODAL:
122                     checkBlockedWindows();
123                     break;
124                 case DOCUMENT_MODAL:
125                     docModalTest();
126                     break;
127                 case MODELESS:
128                     checkUnblockedWindows();
129                     break;
130             }
131 
132             EventQueue.invokeAndWait(() -> { fileDialog.dispose(); });
133             robot.waitForIdle(delay);
134 
135             if (modalityType != Dialog.ModalityType.MODELESS) {
136                 checkUnblockedWindows();
137             }
138         } finally {
139             EventQueue.invokeLater(this::closeAll);
140         }
141     }
142 
closeAll()143     private void closeAll() {
144         if (parent != null) { parent.dispose(); }
145         if (dialog != null) { dialog.dispose(); }
146         if (window != null) { window.dispose(); }
147         if (fileDialog != null) { fileDialog.dispose(); }
148     }
149 
150     class ParentFrame extends TestFrame {
151 
152         @Override
doOpenAction()153         public void doOpenAction() {
154             if (window != null) { window.setVisible(true); }
155         }
156     }
157 
158     class CustomDialog extends TestDialog {
159 
CustomDialog(Frame f)160         public CustomDialog(Frame f) { super(f); }
161 
162         @Override
doOpenAction()163         public void doOpenAction() {
164             fileDialog = new FileDialog(this);
165             fileDialog.setModalityType(modalityType);
166             fileDialog.setLocation(50, 50);
167             fileDialog.setVisible(true);
168         }
169     }
170 
171     class CustomWindow extends TestWindow {
172 
CustomWindow(TestFrame f)173         public CustomWindow(TestFrame f) { super(f); }
174 
175         @Override
doOpenAction()176         public void doOpenAction() {
177             if (dialog != null) { dialog.setVisible(true); }
178         }
179     }
180 }
181