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
25import java.awt.*;
26
27
28public class FocusTransferDialogsTest {
29
30    class CustomDialog1 extends TestDialog {
31
32        public CustomDialog1(Frame f) {
33            super(f);
34        }
35
36        @Override
37        public void doOpenAction() {
38            if (dialog2 != null) {
39                dialog2.setVisible(true);
40            }
41        }
42
43        @Override
44        public void doCloseAction() {
45            this.dispose();
46        }
47    }
48
49    class CustomDialog2 extends TestDialog {
50
51        public CustomDialog2(Dialog d) {
52            super(d);
53        }
54
55        @Override
56        public void doOpenAction() {
57            if (dialog3 != null) {
58                dialog3.setVisible(true);
59            }
60        }
61
62        @Override
63        public void doCloseAction() {
64            this.dispose();
65        }
66    }
67
68    class CustomDialog3 extends TestDialog {
69
70        public CustomDialog3(Frame f) {
71            super(f);
72        }
73
74        public CustomDialog3(Dialog d) {
75            super(d);
76        }
77
78        @Override
79        public void doCloseAction() {
80            this.dispose();
81        }
82    }
83
84
85    private TestDialog dialog1, dialog2, dialog3;
86    private Frame parentFrame;
87
88    private static final int delay = 1000;
89    private final ExtendedRobot robot;
90    private Dialog.ModalityType modalityType;
91
92    FocusTransferDialogsTest(Dialog.ModalityType modType) throws Exception {
93
94        modalityType = modType;
95        robot = new ExtendedRobot();
96        EventQueue.invokeLater(this::createGUI);
97    }
98
99    private void createGUI() {
100
101        dialog1 = new CustomDialog1((Frame) null);
102        dialog1.setTitle("Dialog1");
103        dialog1.setLocation(50, 50);
104
105        if (modalityType != null) {
106            dialog1.setModalityType(modalityType);
107        } else {
108            modalityType = Dialog.ModalityType.MODELESS;
109        }
110
111        dialog2 = new CustomDialog2(dialog1);
112        dialog2.setTitle("Dialog2");
113        dialog2.setLocation(250, 50);
114
115        parentFrame = new Frame();
116        dialog3 = new CustomDialog3(parentFrame);
117        dialog3.setTitle("Dialog3");
118        dialog3.setLocation(450, 50);
119
120        dialog1.setVisible(true);
121    }
122
123    private void closeAll() {
124        if (dialog1 != null) { dialog1.dispose(); }
125        if (dialog2 != null) { dialog2.dispose(); }
126        if (dialog3 != null) { dialog3.dispose(); }
127        if (parentFrame != null) { parentFrame.dispose(); }
128    }
129
130    public void doTest() throws Exception {
131
132        robot.waitForIdle(delay);
133
134        try {
135
136            dialog1.checkCloseButtonFocusGained(true);
137
138            dialog1.clickOpenButton(robot);
139            robot.waitForIdle(delay);
140
141            dialog2.checkCloseButtonFocusGained(true);
142            dialog1.checkOpenButtonFocusLost(true);
143
144            dialog1.openGained.reset();
145            dialog2.clickOpenButton(robot);
146            robot.waitForIdle(delay);
147
148            switch (modalityType) {
149                case APPLICATION_MODAL:
150
151                    dialog3.checkCloseButtonFocusGained(false, 10);
152                    dialog2.checkOpenButtonFocusLost(true);
153
154                    dialog1.checkCloseButtonFocusGained(true);
155                    dialog3.closeGained.reset();
156
157                    dialog1.clickCloseButton(robot);
158                    robot.waitForIdle(delay);
159
160                    dialog3.checkCloseButtonFocusGained(true);
161
162                    break;
163
164                case DOCUMENT_MODAL:
165                case MODELESS:
166
167                    dialog3.checkCloseButtonFocusGained(true);
168                    dialog2.checkOpenButtonFocusLost(true);
169
170                    dialog1.openGained.reset();
171
172                    dialog2.clickCloseButton(robot);
173                    robot.waitForIdle(delay);
174
175                    dialog1.checkOpenButtonFocusGained(true);
176
177                    break;
178            }
179
180        } catch (Exception e) {
181
182            // make screenshot before exit
183            Rectangle rect = new Rectangle(0, 0, 650, 250);
184            java.awt.image.BufferedImage img = robot.createScreenCapture(rect);
185            javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));
186
187            throw e;
188        }
189
190        robot.waitForIdle(delay);
191        EventQueue.invokeAndWait(this::closeAll);
192    }
193}
194