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 Triggering Focus, Window & Key Events and checking they are
  29  *          handled properly by the KeyboardFocusManager and KeyEventDispatcher.
  30  * @author Aruna Samji
  31  * @library ../../../lib/testlibrary
  32  * @build ExtendedRobot
  33  * @run main TaskKeyboardFocusManagerEvent
  34  */
  35 
  36 public class TaskKeyboardFocusManagerEvent extends Task<GUIMerlinFocus> {
  37 
  38     public static void main (String[] args) throws Exception {
  39         new TaskKeyboardFocusManagerEvent(GUIMerlinFocus.class, new ExtendedRobot()).runTask();
  40     }
  41 
  42     TaskKeyboardFocusManagerEvent(Class guiClass, ExtendedRobot robot) throws Exception {
  43          super(guiClass, robot);
  44     }
  45 
  46     public void task() throws Exception {
  47         EventQueue.invokeAndWait(() -> {
  48             gui.button.setEnabled(false);
  49             gui.setVisible(true);
  50         });
  51         robot.waitForIdle(1000);
  52 
  53         gui.setBooleans();
  54         EventQueue.invokeAndWait(() -> gui.button.setEnabled(true));
  55         robot.waitForIdle(1000);
  56 
  57         EventQueue.invokeAndWait(gui.button::requestFocusInWindow);
  58         robot.waitForIdle(1000);
  59 
  60         //No two Components must receive the KeyEvents at the same time
  61         if (gui.buttonFocusGained && gui.listFocusGained)
  62             throw new RuntimeException("Two Components receive the KeyEvents " +
  63                     "at the same time\n");
  64 
  65         if (gui.buttonFocusGained == false)
  66             throw new RuntimeException("Button has not Gained the focus even " +
  67                     "after requesting\n");
  68 
  69         //--------------------------------------------------------------------
  70 
  71         //When any Focus/ Window/ Key Events are triggered, the dispatchEvent()
  72         //of the installed KeyboardFocusManager must be invoked by
  73         //AWTEventDispatcher
  74         EventQueue.invokeAndWait(() ->
  75             KeyboardFocusManager.setCurrentKeyboardFocusManager(gui.cdk)
  76         );
  77         robot.waitForIdle(1000);
  78 
  79         if (!(KeyboardFocusManager.getCurrentKeyboardFocusManager()
  80            instanceof GUIMerlinFocus.CustomDefaultKeyBoardFocusManager1))
  81             throw new RuntimeException("CurrentKeyboardFocusManager Is:" +
  82                     KeyboardFocusManager.getCurrentKeyboardFocusManager());
  83 
  84         //Change the focus to the next component.
  85         EventQueue.invokeAndWait(gui.cdk::focusNextComponent);
  86         robot.waitForIdle(100);
  87 
  88         if (gui.listFocusGained == false)
  89             throw new RuntimeException("List has not Gained the focus even " +
  90                     "after changing the focus\n");
  91 
  92         //Check if the old component has lost the focus
  93         if (gui.buttonFocusLost == false)
  94             throw new RuntimeException("Button has not lost the focus\n");
  95 
  96         //Check for the active window
  97         if (gui.cdk.getActiveWindow() == null)
  98             throw new RuntimeException("Active Window is not the Frame\n");
  99 
 100         //Check the focus owner
 101         if (gui.cdk.getFocusOwner() == null)
 102             throw new RuntimeException("Focus Owner is not the List\n");
 103 
 104         //Check for the key event
 105         Point listOrigin = gui.list.getLocationOnScreen();
 106         Point listCenter = gui.list.getLocationOnScreen();
 107         listCenter.translate(gui.list.getWidth()/2, gui.list.getHeight()/2);
 108         robot.mouseMove(listOrigin);
 109         robot.glide(listOrigin, listCenter);
 110         robot.waitForIdle(1000);
 111         robot.click();
 112         robot.type('N');
 113 
 114         if (gui.KeyPressed == false)
 115             throw new RuntimeException("Key Event is not generated\n");
 116 
 117         //--------------------------------------------------------------------
 118         //When KeyboardFocusManager.dispatchEvent() returns false,
 119         //AWTEventDispatcher must dispatch the event by itself
 120 
 121         Point buttonOrigin = gui.button.getLocationOnScreen();
 122         Point buttonCenter = gui.button.getLocationOnScreen();
 123         buttonCenter.translate(gui.button.getWidth()/2, gui.button.getHeight()/2);
 124         robot.glide(buttonOrigin, buttonCenter);
 125         robot.click();
 126         robot.waitForIdle(1000);
 127 
 128         if (gui.buttonClicked == false)
 129             throw new RuntimeException("AWTEventDispatcher does not dispatch the event by itself\n");
 130 
 131         EventQueue.invokeAndWait(() ->
 132             KeyboardFocusManager.setCurrentKeyboardFocusManager(null)
 133         );
 134     }
 135 }