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