Path: blob/master/test/jdk/java/awt/Focus/AutoRequestFocusTest/TestHelper.java
41152 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Util class used for testing RFE 6187066.25* @author anton.tarasov26*/2728import java.awt.*;29import java.awt.event.*;30import test.java.awt.regtesthelpers.Util;31import java.util.concurrent.atomic.AtomicBoolean;32import java.lang.reflect.InvocationTargetException;3334public class TestHelper {35private static volatile boolean focusChanged;36private static volatile boolean trackFocusChange;37private static boolean focusChangeTrackerSet;3839/*40* @param action the action to perform41* @return if {@code action} caused focus change42*/43public static boolean trackFocusChangeFor(Runnable action, Robot robot) {44if (!focusChangeTrackerSet) {45setFocusChangeTracker();46}4748focusChanged = false;49trackFocusChange = true;5051action.run();5253Util.waitForIdle(robot);5455trackFocusChange = false;5657return focusChanged;58}5960public static void invokeLaterAndWait(Runnable action, Robot robot) {61EventQueue.invokeLater(action);62try {63EventQueue.invokeAndWait(new Runnable() { // waiting for action64public void run() {}65});66} catch (InterruptedException ie) {67} catch (InvocationTargetException ite) {}6869Util.waitForIdle(robot); // waiting for events70}7172private static void setFocusChangeTracker() {73Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {74public void eventDispatched(AWTEvent e) {75int id = e.getID();76if (trackFocusChange &&77(id == FocusEvent.FOCUS_GAINED || id == FocusEvent.FOCUS_LOST ||78id == WindowEvent.WINDOW_GAINED_FOCUS || id == WindowEvent.WINDOW_LOST_FOCUS ||79id == WindowEvent.WINDOW_ACTIVATED || id == WindowEvent.WINDOW_DEACTIVATED))80{81System.out.println(e.toString());82focusChanged = true;83}84}85}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);8687focusChangeTrackerSet = true;88}89}909192