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 
  26 /*
  27  * @test
  28  * @summary Try to request focus to a component in a top level window which
  29  *          is not focused and check whether the component is becoming the
  30  *          focus owner.Check for the FOCUS_GAINED event and the keyevents by
  31   *          making the component focusable and non focusable.
  32  * @author Aruna Samji
  33  * @library ../../../lib/testlibrary
  34  * @build ExtendedRobot
  35  * @run main TaskRequestFocusOwner
  36  */
  37 
  38 public class TaskRequestFocusOwner extends Task<GUIMerlinFocus> {
  39 
  40     public static void main (String[] args) throws Exception {
  41         new TaskRequestFocusOwner(new GUIMerlinFocus(), new ExtendedRobot()).runTask();
  42     }
  43 
  44     TaskRequestFocusOwner(GUIMerlinFocus gui, ExtendedRobot robot) {
  45          super(gui, robot);
  46     }
  47 
  48     public void task() throws Exception {
  49         gui.testframe3.setVisible(true);
  50         robot.waitForIdle(1000);
  51 
  52         Point button14Center = gui.button14.getLocationOnScreen();
  53         button14Center.translate(gui.button14.getWidth()/2, gui.button14.getHeight()/2);
  54         robot.glide(gui.button14.getLocationOnScreen(), button14Center);
  55         robot.click();
  56         robot.waitForIdle(1000);
  57 
  58         //Make the checkbox Disabled using the setVisible(false) and check
  59         //whether the checkbox is getting the Focus
  60         gui.checkbox14.setEnabled(false);
  61         gui.checkbox14.setVisible(false);
  62         robot.waitForIdle(1000);
  63 
  64         gui.checkbox14.requestFocusInWindow();
  65         robot.waitForIdle(1000);
  66         if(gui.checkbox14.isFocusOwner())
  67             throw new RuntimeException("The checkbox has got the Focus when made " +
  68                     "setEnable(false) and setVisible(false).");
  69 
  70         gui.checkbox14.setEnabled(true);
  71         gui.checkbox14.setVisible(true);
  72         robot.waitForIdle(1000);
  73         gui.checkbox14.requestFocusInWindow();
  74         robot.waitForIdle(1000);
  75         if(!gui.checkbox14.isFocusOwner())
  76             throw new RuntimeException("The Checkbox did get the focus when made " +
  77                     "setEnable(true) and setVisible(true).");
  78 
  79         //Checking whether the TextArea has gained the Focus after making
  80         //setFocusable as false
  81         gui.textarea14.setFocusable(false);
  82         gui.textarea14.requestFocusInWindow();
  83         robot.waitForIdle(1000);
  84         if(gui.textarea14.isFocusOwner())
  85             throw new RuntimeException("The Focus is transferred to Non-Focusable " +
  86                     "TextArea.");
  87 
  88         gui.textarea14.setFocusable(true);
  89         robot.waitForIdle(1000);
  90         gui.textarea14.requestFocusInWindow();
  91         robot.waitForIdle(1000);
  92         if(!gui.textarea14.isFocusOwner())
  93             throw new RuntimeException("The Focus is not transferred to Focusable " +
  94                     "TextArea");
  95     }
  96 }