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