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     private boolean focusGained = false;
  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                 focusGained = true;
  69                 System.out.println("Frame Focus gained");
  70                 synchronized (lock4) {
  71                     try {
  72                         lock4.notifyAll();
  73                     } catch (Exception ex) {
  74                         ex.printStackTrace();
  75                     }
  76                 }
  77             }
  78 
  79             public void windowLostFocus(WindowEvent event) {
  80                 System.out.println("Frame Focus lost");
  81             }
  82         });
  83         frame.addWindowListener(new WindowAdapter() {
  84             public void windowActivated(WindowEvent e) {
  85                 eventType = WindowEvent.WINDOW_ACTIVATED;
  86                 System.out.println("Undecorated Frame is activated");
  87                 synchronized (lock1) {
  88                     try {
  89                         lock1.notifyAll();
  90                     } catch (Exception ex) {
  91                         ex.printStackTrace();
  92                     }
  93                 }
  94             }
  95 
  96             public void windowDeactivated(WindowEvent e) {
  97                 eventType1 = WindowEvent.WINDOW_DEACTIVATED;
  98                 System.out.println("Undecorated Frame got Deactivated");
  99                 synchronized (lock2) {
 100                     try {
 101                         lock2.notifyAll();
 102                     } catch (Exception ex) {
 103                         ex.printStackTrace();
 104                     }
 105                 }
 106             }
 107 
 108             public void windowClosed(WindowEvent e) {
 109                 eventType2 = WindowEvent.WINDOW_CLOSED;
 110                 System.out.println("Undecorated Frame got closed");
 111                 synchronized (lock3) {
 112                     try {
 113                         lock3.notifyAll();
 114                     } catch (Exception ex) {
 115                         ex.printStackTrace();
 116                     }
 117                 }
 118             }
 119         });
 120         dummyButton = swingFrame? new JButton("Click me") : new Button("Click me");
 121 
 122         frame.setBackground(Color.green);
 123         frame.add((button = createButton(swingFrame, "Close me")));
 124         frame.add(dummyButton);
 125         frame.setVisible(true);
 126         frame.toFront();
 127     }
 128     private Component createButton(boolean swingControl, String txt) {
 129         if(swingControl) {
 130             JButton jbtn = new JButton(txt);
 131             jbtn.addActionListener(new ActionListener() {
 132                 public void actionPerformed(ActionEvent e) {
 133                     frame.dispose();
 134                 }
 135             });
 136             return jbtn;
 137         }else {
 138             Button btn = new Button(txt);
 139             btn.addActionListener(new ActionListener() {
 140                 public void actionPerformed(ActionEvent e) {
 141                     frame.dispose();
 142                 }
 143             });
 144             return btn;
 145         }
 146     }
 147 
 148     public void doTest(boolean swingControl) {
 149         try {
 150             Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 151                 public void run() {
 152                     initializeGUI(swingControl);
 153                 }
 154             });
 155         } catch (Exception e) {
 156             e.printStackTrace();
 157             throw new RuntimeException("Interrupted or unexpected Exception occured");
 158         }
 159         try {
 160             robot = new ExtendedRobot();
 161             robot.waitForIdle(1000);
 162         } catch (Exception e) {
 163             e.printStackTrace();
 164             throw new RuntimeException("Cannot create robot");
 165         }
 166 
 167         robot.mouseMove(dummyButton.getLocationOnScreen().x + dummyButton.getSize().width / 2,
 168                         dummyButton.getLocationOnScreen().y + dummyButton.getSize().height / 2);
 169         robot.waitForIdle(delay);
 170         robot.mousePress(InputEvent.BUTTON1_MASK);
 171         robot.waitForIdle(delay);
 172         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 173         robot.waitForIdle(delay);
 174 
 175         eventType1 = -1;
 176         eventType = -1;
 177         eventType2 = -1;
 178 
 179         robot.mouseMove(button.getLocationOnScreen().x + button.getSize().width / 2,
 180                         button.getLocationOnScreen().y + button.getSize().height / 2);
 181         robot.waitForIdle(delay);
 182         robot.mousePress(InputEvent.BUTTON1_MASK);
 183         robot.waitForIdle(delay);
 184         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 185         robot.waitForIdle(delay * 10);
 186 
 187         if (eventType2 != WindowEvent.WINDOW_CLOSED) {
 188             synchronized (lock3) {
 189                 try {
 190                     lock3.wait(delay * 10);
 191                 } catch (Exception e) {
 192                     e.printStackTrace();
 193                 }
 194             }
 195         }
 196         if (eventType2 != WindowEvent.WINDOW_CLOSED) {
 197             passed = false;
 198             System.err.println("WINDOW_CLOSED event did not occur when the " +
 199                     "undecorated frame is closed!");
 200         }
 201         if (eventType == WindowEvent.WINDOW_ACTIVATED) {
 202             passed = false;
 203             System.err.println("WINDOW_ACTIVATED event occured when the " +
 204                     "undecorated frame is closed!");
 205         }
 206 
 207         if (!passed) {
 208             System.err.println("Test failed!");
 209             throw new RuntimeException("Test failed");
 210         } else {
 211             System.out.println("Test passed");
 212         }
 213     }
 214 }