--- /dev/null 2014-07-31 16:13:19.122792434 +0400 +++ new/test/java/awt/reliability/TaskKeyboardFocusManagerEvent.java 2014-07-31 17:27:53.878615094 +0400 @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.awt.*; + +/* + * @test + * @summary Triggering Focus, Window & Key Events and checking they are + * handled properly by the KeyboardFocusManager and KeyEventDispatcher. + * @author Aruna Samji + * @library ../../../lib/testlibrary + * @build ExtendedRobot + * @run main TaskKeyboardFocusManagerEvent + */ + +public class TaskKeyboardFocusManagerEvent extends Task { + + public static void main (String[] args) throws Exception { + new TaskKeyboardFocusManagerEvent(GUIMerlinFocus.class, new ExtendedRobot()).runTask(); + } + + TaskKeyboardFocusManagerEvent(Class guiClass, ExtendedRobot robot) throws Exception { + super(guiClass, robot); + } + + public void task() throws Exception { + EventQueue.invokeAndWait(() -> { + gui.button.setEnabled(false); + gui.setVisible(true); + }); + robot.waitForIdle(1000); + + gui.setBooleans(); + EventQueue.invokeAndWait(() -> gui.button.setEnabled(true)); + robot.waitForIdle(1000); + + EventQueue.invokeAndWait(gui.button::requestFocusInWindow); + robot.waitForIdle(1000); + + //No two Components must receive the KeyEvents at the same time + if (gui.buttonFocusGained && gui.listFocusGained) + throw new RuntimeException("Two Components receive the KeyEvents " + + "at the same time\n"); + + if (gui.buttonFocusGained == false) + throw new RuntimeException("Button has not Gained the focus even " + + "after requesting\n"); + + //-------------------------------------------------------------------- + + //When any Focus/ Window/ Key Events are triggered, the dispatchEvent() + //of the installed KeyboardFocusManager must be invoked by + //AWTEventDispatcher + EventQueue.invokeAndWait(() -> + KeyboardFocusManager.setCurrentKeyboardFocusManager(gui.cdk) + ); + robot.waitForIdle(1000); + + if (!(KeyboardFocusManager.getCurrentKeyboardFocusManager() + instanceof GUIMerlinFocus.CustomDefaultKeyBoardFocusManager1)) + throw new RuntimeException("CurrentKeyboardFocusManager Is:" + + KeyboardFocusManager.getCurrentKeyboardFocusManager()); + + //Change the focus to the next component. + EventQueue.invokeAndWait(gui.cdk::focusNextComponent); + robot.waitForIdle(100); + + if (gui.listFocusGained == false) + throw new RuntimeException("List has not Gained the focus even " + + "after changing the focus\n"); + + //Check if the old component has lost the focus + if (gui.buttonFocusLost == false) + throw new RuntimeException("Button has not lost the focus\n"); + + //Check for the active window + if (gui.cdk.getActiveWindow() == null) + throw new RuntimeException("Active Window is not the Frame\n"); + + //Check the focus owner + if (gui.cdk.getFocusOwner() == null) + throw new RuntimeException("Focus Owner is not the List\n"); + + //Check for the key event + Point listOrigin = gui.list.getLocationOnScreen(); + Point listCenter = gui.list.getLocationOnScreen(); + listCenter.translate(gui.list.getWidth()/2, gui.list.getHeight()/2); + robot.mouseMove(listOrigin); + robot.glide(listOrigin, listCenter); + robot.waitForIdle(1000); + robot.click(); + robot.type('N'); + + if (gui.KeyPressed == false) + throw new RuntimeException("Key Event is not generated\n"); + + //-------------------------------------------------------------------- + //When KeyboardFocusManager.dispatchEvent() returns false, + //AWTEventDispatcher must dispatch the event by itself + + Point buttonOrigin = gui.button.getLocationOnScreen(); + Point buttonCenter = gui.button.getLocationOnScreen(); + buttonCenter.translate(gui.button.getWidth()/2, gui.button.getHeight()/2); + robot.glide(buttonOrigin, buttonCenter); + robot.click(); + robot.waitForIdle(1000); + + if (gui.buttonClicked == false) + throw new RuntimeException("AWTEventDispatcher does not dispatch the event by itself\n"); + + EventQueue.invokeAndWait(() -> + KeyboardFocusManager.setCurrentKeyboardFocusManager(null) + ); + } +}