1 /*
   2  * Copyright (c) 2004, 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 import java.awt.*;
  25 import java.awt.event.*;
  26 
  27 /*
  28  * @test
  29  * @summary Active and check whether the respective WindowEvents are triggered
  30  *          properly and traverse the focus through focusable & non focusable
  31  *          components in a window and check whether the FocusEvents are
  32  *          triggered properly.
  33  * @author Aruna Samji
  34  * @library ../../../lib/testlibrary
  35  * @build ExtendedRobot
  36  * @run main TaskWindowFocusEvents
  37  */
  38 
  39 public class TaskWindowFocusEvents extends Task<GUIMerlinFocus> {
  40 
  41     public static void main (String[] args) throws Exception {
  42         new TaskWindowFocusEvents(new GUIMerlinFocus(), new ExtendedRobot()).runTask();
  43     }
  44 
  45     TaskWindowFocusEvents(GUIMerlinFocus gui, ExtendedRobot robot) {
  46          super(gui, robot);
  47     }
  48 
  49     public void task() throws Exception {
  50         gui.setBooleans();
  51         gui.frame1.setVisible(true);
  52         gui.frame2.setVisible(true);
  53         robot.waitForIdle(1000);
  54 
  55         gui.framesSetLocationAndSize();
  56         robot.waitForIdle(1000);
  57 
  58         // WindowEvent.WINDOW_DEACTIVATED must be dispatched to
  59         // the Frame
  60 
  61         // WindowEvent.WINDOW_ACTIVATED must be dispatched to
  62         // Frame when this Frame becomes the active window
  63 
  64         // An Active Window must be either of the following
  65         // Frame which is the Focused Window
  66         // First Frame that is the owner of the focused Window
  67 
  68         Point movePoint = gui.frame2.getLocationOnScreen();
  69         movePoint.translate(gui.frame2.getWidth()/2, gui.frame2.getHeight()/2);
  70         robot.mouseMove(movePoint);
  71         robot.waitForIdle(1000);
  72         robot.click();
  73 
  74         gui.setBooleans();
  75 
  76         movePoint = gui.frame1.getLocationOnScreen();
  77         movePoint.translate(gui.frame1.getWidth()/2, gui.frame1.getHeight()/2);
  78         robot.mouseMove(movePoint);
  79         robot.waitForIdle(1000);
  80         robot.click();
  81 
  82         if (!gui.frame1Activated)
  83             throw new RuntimeException("Window activated event is not fired for " +
  84                     gui.frame1);
  85 
  86         if (!gui.frame2Deactivated)
  87             throw new RuntimeException("Window deactivated event is not fired for " +
  88                     gui.frame2);
  89 
  90         if (!gui.frame1.isActive())
  91             throw new RuntimeException("Not an active window " + gui.frame1);
  92 
  93         // WindowEvent.WINDOW_DEACTIVATED must be dispatched to
  94         // the Dialog
  95 
  96         // WindowEvent.WINDOW_ACTIVATED must be dispatched to
  97         // Dialog when this Dialog becomes the active window
  98 
  99         // An Active Window must be either of the following
 100         // Dialog which is the Focused Window
 101         // First Dialog that is the owner of the focused Window
 102 
 103         gui.dialog1.setVisible(true);
 104         gui.dialog2.setVisible(true);
 105         robot.waitForIdle(1000);
 106 
 107         gui.dialogsSetLocationAndSize();
 108         robot.waitForIdle(1000);
 109 
 110         movePoint = gui.dialog2.getLocationOnScreen();
 111         movePoint.translate(gui.dialog2.getWidth()/2, gui.dialog2.getHeight()/2);
 112         robot.mouseMove(movePoint);
 113         robot.waitForIdle(1000);
 114         robot.click();
 115 
 116         gui.setBooleans();
 117 
 118         movePoint = gui.dialog1.getLocationOnScreen();
 119         movePoint.translate(gui.dialog1.getWidth()/2, gui.dialog1.getHeight()/2);
 120         robot.mouseMove(movePoint);
 121         robot.waitForIdle(1000);
 122         robot.click();
 123 
 124         if (!gui.dialog1Activated)
 125             throw new RuntimeException("Window activated event is not fired for " +
 126                     gui.dialog1);
 127 
 128         if (!gui.dialog2Deactivated)
 129             throw new RuntimeException("Window deactivated event is not fired for " +
 130                     gui.dialog2);
 131 
 132         if (!gui.dialog1.isActive())
 133             throw new RuntimeException("Not an active window " + gui.dialog1);
 134 
 135         gui.setBooleans();
 136         gui.frame1.dispose();
 137         gui.frame2.dispose();
 138         gui.dialog1.dispose();
 139         gui.dialog2.dispose();
 140 
 141         gui.frame3.setVisible(true);
 142         robot.waitForIdle(1000);
 143 
 144         // Only focus traversable component can receive
 145         // FocusEvent.FOCUS_GAINED event. When client code or user
 146         // try to focus a non focus traversable component,
 147         // this component can not receive FocusEvent.FOCUS_GAINED event
 148 
 149         // Bring mouse pointer on to button
 150         Point buttonCenter = gui.button1.getLocationOnScreen();
 151         buttonCenter.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2);
 152         robot.mouseMove(buttonCenter);
 153         robot.waitForIdle(1000);
 154         robot.click();
 155 
 156         if(gui.textfieldFocusLost )
 157             throw new RuntimeException("Focus lost event has triggered for text " +
 158                     "field when clicked on disabled component.\n");
 159 
 160         if(gui.button1FocusGained)
 161             throw new RuntimeException("Disabled component gained focus " +
 162                     gui.button1);
 163 
 164         // FocusEvent.FOCUS_GAINED must be dispatched to a
 165         // focus traversable component when this component
 166         // becomes the Focus Owner
 167 
 168         // FocusEvent.FOCUS_LOST must be dispatched to a component
 169         // when this component is no longer a Focus Owner
 170 
 171         gui.setBooleans();
 172 
 173         Point textfieldCenter = gui.textfield.getLocationOnScreen();
 174         textfieldCenter.translate(gui.textfield.getWidth() / 2, gui.textfield.getHeight() / 2);
 175         robot.mouseMove(textfieldCenter);
 176         robot.waitForIdle();
 177         robot.click();
 178         robot.type(KeyEvent.VK_TAB);
 179 
 180         if( !gui.textfieldFocusLost )
 181             throw new RuntimeException("When tab is pressed, Focus lost event " +
 182                     "didn't fire for " + gui.textfield);
 183 
 184         if(!gui.textareaFocusGained)
 185             throw new RuntimeException("When tab is pressed, Focus gained event " +
 186                     "didn't fire for " + gui.textarea);
 187 
 188         gui.frame3.dispose();
 189     }
 190 }