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