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