1 /*
2  * Copyright (c) 1999, 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 /*
25 * @test
26 * @summary To check proper WINDOW_EVENTS are triggered when JFrame gains or losses the focus
27 * @author Jitender(jitender.singh@eng.sun.com) area=AWT
28 * @author yan
29 * @library ../../../../lib/testlibrary
30 * @build ExtendedRobot
31 * @run main ActiveSwingWindowTest
32 */
33 
34 import java.awt.*;
35 import java.awt.event.*;
36 import javax.swing.JFrame;
37 import javax.swing.JTextField;
38 import javax.swing.JButton;
39 
40 
41 public class ActiveSwingWindowTest {
42 
43     private JFrame frame, frame2;
44     private JButton button, button2;
45     private JTextField textField, textField2;
46     private int eventType, eventType1;
47     private ExtendedRobot robot;
48     private Object lock1 = new Object();
49     private Object lock2 = new Object();
50     private Object lock3 = new Object();
51     private boolean passed = true;
52     private int delay = 150;
53 
main(String[] args)54     public static void main(String[] args) {
55         ActiveSwingWindowTest test = new ActiveSwingWindowTest();
56         test.doTest();
57     }
58 
ActiveSwingWindowTest()59     public ActiveSwingWindowTest() {
60         try{
61             EventQueue.invokeAndWait( () -> {
62                     initializeGUI();
63             });
64         } catch (Exception e) {
65             e.printStackTrace();
66             throw new RuntimeException("Interrupted or unexpected Exception occured");
67         }
68     }
69 
initializeGUI()70     private void initializeGUI() {
71         frame = new JFrame();
72         frame.setLayout(new FlowLayout());
73 
74         frame.setLocation(5, 20);
75         frame.setSize(200, 200);
76         frame.setUndecorated(true);
77         frame.addWindowFocusListener(new WindowFocusListener() {
78             public void windowGainedFocus(WindowEvent event) {
79                 System.out.println("Frame Focus gained");
80                 synchronized (lock3) {
81                     try {
82                         lock3.notifyAll();
83                     } catch (Exception ex) {
84                         ex.printStackTrace();
85                     }
86                 }
87             }
88 
89             public void windowLostFocus(WindowEvent event) {
90                     System.out.println("Frame Focus lost");
91             }
92         });
93         frame.addWindowListener(new WindowAdapter() {
94             public void windowActivated(WindowEvent e) {
95                 eventType = WindowEvent.WINDOW_ACTIVATED;
96                 System.out.println("Undecorated Frame is activated\n");
97                 synchronized (lock1) {
98                     try {
99                         lock1.notifyAll();
100                     } catch (Exception ex) {
101                         ex.printStackTrace();
102                     }
103                 }
104             }
105 
106             public void windowDeactivated(WindowEvent e) {
107                 eventType = WindowEvent.WINDOW_DEACTIVATED;
108                 System.out.println("Undecorated Frame got Deactivated\n");
109                 synchronized (lock2) {
110                     try {
111                             lock2.notifyAll();
112                     } catch (Exception ex) {
113                         ex.printStackTrace();
114                     }
115                 }
116             }
117         });
118         textField = new JTextField("TextField");
119         button = new JButton("Click me");
120         button.addActionListener(new ActionListener() {
121             public void actionPerformed(ActionEvent e) {
122                 textField.setText("Focus gained");
123             }
124         });
125 
126         frame.setBackground(Color.green);
127         frame.add(button);
128         frame.add(textField);
129         frame.setVisible(true);
130 
131         frame2 = new JFrame();
132         frame2.setLayout(new FlowLayout());
133         frame2.setLocation(5, 250);
134         frame2.setSize(200, 200);
135         frame2.setBackground(Color.green);
136         button2 = new JButton("Click me");
137         textField2 = new JTextField("TextField");
138         button2.addActionListener(new ActionListener() {
139             public void actionPerformed(ActionEvent e) {
140                 textField2.setText("Got the focus");
141             }
142         });
143 
144         frame2.add(button2, BorderLayout.SOUTH);
145         frame2.add(textField2, BorderLayout.NORTH);
146         frame2.setVisible(true);
147 
148         frame.toFront();
149     }
150 
doTest()151     public void doTest() {
152         try {
153             robot = new ExtendedRobot();
154         } catch (Exception e) {
155             e.printStackTrace();
156             throw new RuntimeException("Cannot create robot");
157         }
158 
159         robot.waitForIdle(5*delay);
160         robot.mouseMove(button.getLocationOnScreen().x + button.getSize().width / 2,
161                         button.getLocationOnScreen().y + button.getSize().height / 2);
162         robot.waitForIdle(delay);
163         robot.mousePress(InputEvent.BUTTON1_MASK);
164         robot.waitForIdle(delay);
165         robot.mouseRelease(InputEvent.BUTTON1_MASK);
166 
167         if (eventType != WindowEvent.WINDOW_ACTIVATED) {
168             synchronized (lock1) {
169                 try {
170                     lock1.wait(delay * 10);
171                 } catch (Exception e) {
172                     e.printStackTrace();
173                 }
174             }
175         }
176         if (eventType != WindowEvent.WINDOW_ACTIVATED) {
177             passed = false;
178             System.err.println("WINDOW_ACTIVATED event did not occur when the " +
179                     "undecorated frame is activated!");
180         }
181 
182         eventType1 = -1;
183         eventType = -1;
184 
185         robot.mouseMove(button2.getLocationOnScreen().x + button2.getSize().width / 2,
186                         button2.getLocationOnScreen().y + button2.getSize().height / 2);
187         robot.waitForIdle(delay);
188         robot.mousePress(InputEvent.BUTTON1_MASK);
189         robot.waitForIdle(delay);
190         robot.mouseRelease(InputEvent.BUTTON1_MASK);
191 
192         if (eventType != WindowEvent.WINDOW_DEACTIVATED) {
193             synchronized (lock2) {
194                 try {
195                     lock2.wait(delay * 10);
196                 } catch (Exception e) {
197                 }
198             }
199         }
200         if (eventType != WindowEvent.WINDOW_DEACTIVATED) {
201             passed = false;
202             System.err.println("FAIL: WINDOW_DEACTIVATED event did not occur for the " +
203                     "undecorated frame when another frame gains focus!");
204         }
205         if (frame.hasFocus()) {
206             passed = false;
207             System.err.println("FAIL: The undecorated frame has focus even when " +
208                         "another frame is clicked!");
209         }
210 
211         if (!passed) {
212             //captureScreenAndSave();
213             System.err.println("Test failed!");
214             throw new RuntimeException("Test failed.");
215         } else {
216             System.out.println("Test passed");
217         }
218     }
219 }
220