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