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
24import java.awt.*;
25import java.awt.event.*;
26
27import static jdk.testlibrary.Asserts.*;
28
29
30
31public class TestFrame extends Frame implements ActionListener,
32    FocusListener, WindowFocusListener, WindowListener {
33
34    public Button closeButton, openButton, dummyButton;
35
36    public Flag closeClicked, openClicked, dummyClicked;
37    public Flag closeGained,  openGained,  dummyGained;
38    public Flag closeLost,    openLost,    dummyLost;
39    public Flag focusGained, focusLost;
40    public Flag activated;
41
42    public Panel topPanel;
43
44    public static int delay = 500;
45    public static int keyDelay = 100;
46
47    public TestFrame() {
48        super();
49        initializeGUI();
50    }
51
52    private void initFlags() {
53        closeClicked = new Flag();
54        openClicked  = new Flag();
55        dummyClicked = new Flag();
56        closeGained  = new Flag();
57        openGained   = new Flag();
58        dummyGained  = new Flag();
59        closeLost    = new Flag();
60        openLost     = new Flag();
61        dummyLost    = new Flag();
62        focusGained  = new Flag();
63        focusLost    = new Flag();
64        activated    = new Flag();
65    }
66
67    public void resetStatus() {
68        activated.reset();
69        focusGained.reset();
70        closeGained.reset();
71        openGained.reset();
72        closeClicked.reset();
73        openClicked.reset();
74    }
75
76    private void initializeGUI() {
77
78        initFlags();
79        this.setTitle("Frame");
80
81        this.addWindowFocusListener(this);
82        this.addWindowListener(this);
83
84        this.setLayout(new GridLayout(3, 1));
85
86        topPanel = new Panel();
87        topPanel.setFocusable(false);
88        this.add(topPanel);
89
90        Panel p = new Panel();
91        p.setLayout(new GridLayout(1, 3));
92
93        closeButton = new Button("Close");
94        closeButton.addActionListener(this);
95        closeButton.addFocusListener(this);
96
97        openButton = new Button("Open");
98        openButton.addActionListener(this);
99        openButton.addFocusListener(this);
100
101        dummyButton = new Button("Dummy");
102        dummyButton.addActionListener(this);
103        dummyButton.addFocusListener(this);
104
105        p.add(closeButton);
106        p.add(openButton);
107        p.add(dummyButton);
108
109        this.add(p);
110
111        Panel bottomPanel = new Panel();
112        bottomPanel.setFocusable(false);
113        this.add(bottomPanel);
114
115        setSize(150, 150);
116    }
117
118    public void doOpenAction()  {}
119    public void doCloseAction() {}
120    public void doDummyAction() {}
121
122    @Override
123    public void actionPerformed(ActionEvent event) {
124        if (closeButton.equals(event.getSource())) {
125            closeClicked.flagTriggered();
126            doCloseAction();
127        } else if (openButton.equals(event.getSource())) {
128            openClicked.flagTriggered();
129            doOpenAction();
130        } else if (dummyButton.equals(event.getSource())) {
131            dummyClicked.flagTriggered();
132            doDummyAction();
133        }
134    }
135
136    @Override
137    public void focusGained(FocusEvent event) {
138        if (closeButton.equals(event.getSource())) {
139            closeGained.flagTriggered();
140        } else if (openButton.equals(event.getSource())) {
141            openGained.flagTriggered();
142        } else if (dummyButton.equals(event.getSource())) {
143            dummyGained.flagTriggered();
144        }
145    }
146
147    @Override
148    public void focusLost(FocusEvent event) {
149        if (closeButton.equals(event.getSource())) {
150            closeLost.flagTriggered();
151        } else if (openButton.equals(event.getSource())) {
152            openLost.flagTriggered();
153        } else if (dummyButton.equals(event.getSource())) {
154            dummyLost.flagTriggered();
155        }
156    }
157
158    @Override
159    public void windowGainedFocus(WindowEvent event) {
160        focusGained.flagTriggered();
161    }
162
163    @Override
164    public void windowLostFocus(WindowEvent event) {
165        focusLost.flagTriggered();
166    }
167
168    @Override
169    public void windowActivated(WindowEvent e) {
170        activated.flagTriggered();
171    }
172
173    @Override
174    public void windowClosed(WindowEvent e) {}
175
176    @Override
177    public void windowClosing(WindowEvent e) {
178        assertTrue(false, "user closed window");
179    }
180
181    @Override
182    public void windowDeactivated(WindowEvent e) {}
183    @Override
184    public void windowDeiconified(WindowEvent e) {}
185    @Override
186    public void windowIconified(WindowEvent e) {}
187    @Override
188    public void windowOpened(WindowEvent e) {}
189
190    public void clickButton(Button b, ExtendedRobot robot) {
191        try {
192            Flag.waitTillShown(b);
193        } catch (InterruptedException e) {}
194
195        if ((closeButton.equals(b) || openButton.equals(b) ||
196            dummyButton.equals(b)) && robot != null) {
197            robot.mouseMove((int) b.getLocationOnScreen().x + b.getSize().width / 2,
198                            (int) b.getLocationOnScreen().y + b.getSize().height / 2);
199            robot.delay(delay);
200            robot.click();
201            robot.delay(delay);
202        }
203    }
204
205    public void clickOpenButton(ExtendedRobot robot) throws Exception {
206        clickOpenButton(robot, true, "");
207    }
208
209    public void clickOpenButton(ExtendedRobot robot,
210                                boolean       refState,
211                                String        message) throws Exception {
212        openClicked.reset();
213        clickButton(openButton, robot);
214        openClicked.waitForFlagTriggered();
215
216        String msg = "Clicking the frame Open button " + (refState ?
217            "did not trigger an action." :
218            "triggered an action when it should not.");
219        assertEQ(openClicked.flag(), refState, msg + " " + message);
220    }
221
222    public void clickCloseButton(ExtendedRobot robot) throws Exception {
223        clickCloseButton(robot, true, "");
224    }
225
226    public void clickCloseButton(ExtendedRobot robot,
227                                 boolean       refState,
228                                 String        message) throws Exception {
229        closeClicked.reset();
230        clickButton(closeButton, robot);
231        closeClicked.waitForFlagTriggered();
232
233        String msg = "Clicking the frame Close button " + (refState ?
234            "did not trigger an action." :
235            "triggered an action when it should not.");
236        assertEQ(closeClicked.flag(), refState, msg + " " + message);
237    }
238
239    public void clickDummyButton(ExtendedRobot robot) throws Exception {
240        clickDummyButton(robot, Flag.ATTEMPTS);
241    }
242
243    public void clickDummyButton(ExtendedRobot robot,
244                                 int           attempts) throws Exception {
245        clickDummyButton(robot, attempts, true, "");
246    }
247
248    public void clickDummyButton(ExtendedRobot robot,
249                                 int           attempts,
250                                 boolean       refState,
251                                 String        message) throws Exception {
252        dummyClicked.reset();
253        clickButton(dummyButton, robot);
254        dummyClicked.waitForFlagTriggered();
255
256        String msg = "Clicking the frame Dummy button " + (refState ?
257            "did not trigger an action." :
258            "triggered an action when it should not.");
259        assertEQ(dummyClicked.flag(), refState, msg + " " + message);
260    }
261
262    public void clickInside(ExtendedRobot robot) throws Exception {
263        try {
264            Flag.waitTillShown(topPanel);
265        } catch (InterruptedException e) {}
266
267        if (robot != null) {
268            robot.mouseMove((int) topPanel.getLocationOnScreen().x + topPanel.getSize().width / 2,
269                            (int) topPanel.getLocationOnScreen().y + topPanel.getSize().height / 2);
270            robot.delay(delay);
271            robot.click();
272            robot.delay(delay);
273        }
274    }
275
276    public void transferFocusToFrame(ExtendedRobot robot,
277                                     String message,
278                                     Button b) throws Exception {
279        focusGained.reset();
280        clickInside(robot);
281
282        focusGained.waitForFlagTriggered();
283        assertTrue(focusGained.flag(),
284            "Clicking inside the Frame did not make it focused. " + message);
285
286        if (b != null) {
287            assertTrue(b.hasFocus(), "Button " + b.getLabel() +
288                " did not gain focus when Frame brought to top");
289        }
290    }
291
292    public void transferFocusToBlockedFrame(ExtendedRobot robot,
293                                            String message,
294                                            Button b) throws Exception {
295        focusGained.reset();
296        clickInside(robot);
297
298        robot.waitForIdle(delay);
299
300        assertFalse(focusGained.flag(),
301            "Clicking inside a blocked Frame made it focused. " + message);
302
303        robot.waitForIdle(delay);
304        if (b != null) {
305            assertFalse(b.hasFocus(), "Button " + b.getLabel() +
306                " present in a blocked frame gained focus");
307        }
308    }
309
310    public void checkBlockedFrame(
311            ExtendedRobot robot, String message) throws Exception {
312
313        dummyGained.reset();
314        dummyClicked.reset();
315        focusGained.reset();
316
317        clickButton(dummyButton, robot);
318
319        robot.waitForIdle(delay);
320
321        assertFalse(dummyClicked.flag(),
322            "DummyButton on blocked Frame triggered action when clicked. " + message);
323
324        assertFalse(dummyGained.flag(),
325            "DummyButton on blocked Frame gained focus when clicked. " + message);
326
327        assertFalse(focusGained.flag(),
328            "A blocked Frame gained focus when component clicked. " + message);
329    }
330
331    public void checkUnblockedFrame(ExtendedRobot robot,
332                                    String message) throws Exception {
333        dummyGained.reset();
334        dummyClicked.reset();
335        clickButton(dummyButton, robot);
336
337        dummyGained.waitForFlagTriggered();
338        assertTrue(dummyGained.flag(),
339            "DummyButton on Frame did not gain focus on clicking. " + message);
340
341        dummyClicked.waitForFlagTriggered();
342        assertTrue(dummyClicked.flag(),
343            "DummyButton on Frame did not trigger action on clicking. " + message);
344
345        closeGained.reset();
346        robot.type(KeyEvent.VK_TAB);
347
348        closeGained.waitForFlagTriggered();
349        assertTrue(closeGained.flag(),
350            "FAIL: Tab navigation did not happen properly on Frame. First " +
351            "button did not gain focus on tab press. " + message);
352    }
353
354    public void checkCloseButtonFocusGained(boolean refState) {
355        checkCloseButtonFocusGained(refState, Flag.ATTEMPTS);
356    }
357
358    public void checkCloseButtonFocusGained(boolean refState, int attempts) {
359        try {
360            closeGained.waitForFlagTriggered(attempts);
361        } catch (InterruptedException e) {}
362
363        String msg = "frame Close button ";
364        msg += (refState ? "did not gain focus" :
365                "gained focus when it should not");
366
367        assertTrue(closeGained.flag() == refState, msg);
368    }
369
370    public void checkOpenButtonFocusGained(boolean refState) {
371        checkOpenButtonFocusGained(refState, Flag.ATTEMPTS);
372    }
373
374    public void checkOpenButtonFocusGained(boolean refState, int attempts) {
375        try {
376            openGained.waitForFlagTriggered(attempts);
377        } catch (InterruptedException e) {}
378
379        String msg = "frame Open button ";
380        msg += (refState ? "did not gain focus" :
381                "gained focus when it should not");
382
383        assertTrue(openGained.flag() == refState, msg);
384    }
385
386    public void checkOpenButtonFocusLost(boolean refState) {
387        checkOpenButtonFocusLost(refState, Flag.ATTEMPTS);
388    }
389
390    public void checkOpenButtonFocusLost(boolean refState, int attempts) {
391        try {
392            openLost.waitForFlagTriggered(attempts);
393        } catch (InterruptedException e) {}
394
395        String msg = "frame Open button ";
396        msg += (refState ? "did not lose focus" :
397                "lost focus when it should not");
398        assertTrue(openLost.flag()== refState, msg);
399    }
400}
401