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(GUIMerlinFocus.class, new ExtendedRobot()).runTask();
  42     }
  43 
  44     TaskRequestFocusOwner(Class guiClass, ExtendedRobot robot) throws Exception {
  45          super(guiClass, robot);
  46     }
  47 
  48     public void task() throws Exception {
  49         EventQueue.invokeAndWait(() ->
  50             gui.testframe3.setVisible(true)
  51         );
  52         robot.waitForIdle(1000);
  53 
  54         Point button14Center = gui.button14.getLocationOnScreen();
  55         button14Center.translate(gui.button14.getWidth()/2, gui.button14.getHeight()/2);
  56         robot.glide(gui.button14.getLocationOnScreen(), button14Center);
  57         robot.click();
  58         robot.waitForIdle(1000);
  59 
  60         //Make the checkbox Disabled using the setVisible(false) and check
  61         //whether the checkbox is getting the Focus
  62         EventQueue.invokeAndWait(() -> {
  63             gui.checkbox14.setEnabled(false);
  64             gui.checkbox14.setVisible(false);
  65         });
  66         robot.waitForIdle(1000);
  67 
  68         EventQueue.invokeAndWait(gui.checkbox14::requestFocusInWindow);
  69         robot.waitForIdle(1000);
  70         if(gui.checkbox14.isFocusOwner())
  71             throw new RuntimeException("The checkbox has got the Focus when made " +
  72                     "setEnable(false) and setVisible(false).");
  73 
  74         EventQueue.invokeAndWait(() -> {
  75             gui.checkbox14.setEnabled(true);
  76             gui.checkbox14.setVisible(true);
  77         });
  78         robot.waitForIdle(1000);
  79         EventQueue.invokeAndWait(gui.checkbox14::requestFocusInWindow);
  80         robot.waitForIdle(1000);
  81         if(!gui.checkbox14.isFocusOwner())
  82             throw new RuntimeException("The Checkbox did get the focus when made " +
  83                     "setEnable(true) and setVisible(true).");
  84 
  85         //Checking whether the TextArea has gained the Focus after making
  86         //setFocusable as false
  87         EventQueue.invokeAndWait(() -> {
  88             gui.textarea14.setFocusable(false);
  89             gui.textarea14.requestFocusInWindow();
  90         });
  91         robot.waitForIdle(1000);
  92         if(gui.textarea14.isFocusOwner())
  93             throw new RuntimeException("The Focus is transferred to Non-Focusable " +
  94                     "TextArea.");
  95 
  96         EventQueue.invokeAndWait(() ->
  97             gui.textarea14.setFocusable(true)
  98         );
  99         robot.waitForIdle(1000);
 100 
 101         EventQueue.invokeAndWait(() ->
 102             gui.textarea14.requestFocusInWindow()
 103         );
 104         robot.waitForIdle(1000);
 105         if(!gui.textarea14.isFocusOwner())
 106             throw new RuntimeException("The Focus is not transferred to Focusable " +
 107                     "TextArea");
 108     }
 109 }