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