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.KeyEvent;
  26 
  27 /*
  28  * @test
  29  * @summary Change the focus between components and check whether the
  30  *          FocusEvents has been triggered properly and check whether the
  31  *          information about the Opposite Component can be obtained.
  32  * @author Aruna Samji
  33  * @library ../../../lib/testlibrary
  34  * @build ExtendedRobot
  35  * @run main TaskOppositeComponent
  36  */
  37 
  38 public class TaskOppositeComponent extends Task<GUIMerlinFocus> {
  39 
  40     public static void main (String[] args) throws Exception {
  41         new TaskOppositeComponent(GUIMerlinFocus.class, new ExtendedRobot()).runTask();
  42     }
  43 
  44     TaskOppositeComponent(Class guiClass, ExtendedRobot robot) throws Exception {
  45          super(guiClass, robot);
  46     }
  47 
  48     public void task() throws Exception {
  49         EventQueue.invokeAndWait(() ->
  50             gui.frame4.setVisible(true)
  51         );
  52         robot.waitForIdle(1000);
  53 
  54         // Transfer the focus from Button to List & check whether the
  55         // getOppositeComponent is returning the component which lost
  56         // the focus.
  57         Point button2Origin = gui.button2.getLocationOnScreen();
  58         Point button2Center = gui.button2.getLocationOnScreen();
  59         button2Center.translate(gui.button2.getWidth()/2, gui.button2.getHeight()/2);
  60         robot.glide(button2Origin, button2Center);
  61         robot.waitForIdle(1000);
  62         robot.click();
  63 
  64         robot.type(KeyEvent.VK_TAB);
  65 
  66         if (gui.focusLostComp != gui.button2)
  67             throw new RuntimeException("didn't get the information about " +
  68                     "opposite Component which lose the focus.\n");
  69 
  70         // Transfer the focus from List to LwButton & check whether the
  71         // getOppositeComponent is returning the component which gain
  72         // the focus.
  73         robot.type(KeyEvent.VK_TAB);
  74 
  75         if (gui.focusGainedComp!=gui.list2)
  76             throw new RuntimeException("didn't get the information about " +
  77                     "opposite Component which gain the focus");
  78     }
  79 }