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