0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * Util class used for testing RFE 6187066.
0N/A * @author anton.tarasov
0N/A */
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport test.java.awt.regtesthelpers.Util;
0N/Aimport java.util.concurrent.atomic.AtomicBoolean;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/A
0N/Apublic class TestHelper {
0N/A private static volatile boolean focusChanged;
0N/A private static volatile boolean trackFocusChange;
0N/A private static boolean focusChangeTrackerSet;
0N/A
0N/A /*
0N/A * @param action the action to perform
0N/A * @return if {@code action} caused focus change
0N/A */
0N/A public static boolean trackFocusChangeFor(Runnable action, Robot robot) {
0N/A if (!focusChangeTrackerSet) {
0N/A setFocusChangeTracker();
0N/A }
0N/A
0N/A focusChanged = false;
0N/A trackFocusChange = true;
0N/A
0N/A action.run();
0N/A
0N/A Util.waitForIdle(robot);
0N/A
0N/A trackFocusChange = false;
0N/A
0N/A return focusChanged;
0N/A }
0N/A
0N/A public static void invokeLaterAndWait(Runnable action, Robot robot) {
0N/A EventQueue.invokeLater(action);
0N/A try {
0N/A EventQueue.invokeAndWait(new Runnable() { // waiting for action
0N/A public void run() {}
0N/A });
0N/A } catch (InterruptedException ie) {
0N/A } catch (InvocationTargetException ite) {}
0N/A
0N/A Util.waitForIdle(robot); // waiting for events
0N/A }
0N/A
0N/A private static void setFocusChangeTracker() {
0N/A Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
0N/A public void eventDispatched(AWTEvent e) {
0N/A int id = e.getID();
0N/A if (trackFocusChange &&
0N/A (id == FocusEvent.FOCUS_GAINED || id == FocusEvent.FOCUS_LOST ||
0N/A id == WindowEvent.WINDOW_GAINED_FOCUS || id == WindowEvent.WINDOW_LOST_FOCUS ||
0N/A id == WindowEvent.WINDOW_ACTIVATED || id == WindowEvent.WINDOW_DEACTIVATED))
0N/A {
0N/A System.out.println(e.toString());
0N/A focusChanged = true;
0N/A }
0N/A }
0N/A }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
0N/A
0N/A focusChangeTrackerSet = true;
0N/A }
0N/A}