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