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