Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/Focus/AutoRequestFocusTest/TestHelper.java
41152 views
1
/*
2
* Copyright (c) 2007, 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
/*
25
* Util class used for testing RFE 6187066.
26
* @author anton.tarasov
27
*/
28
29
import java.awt.*;
30
import java.awt.event.*;
31
import test.java.awt.regtesthelpers.Util;
32
import java.util.concurrent.atomic.AtomicBoolean;
33
import java.lang.reflect.InvocationTargetException;
34
35
public class TestHelper {
36
private static volatile boolean focusChanged;
37
private static volatile boolean trackFocusChange;
38
private static boolean focusChangeTrackerSet;
39
40
/*
41
* @param action the action to perform
42
* @return if {@code action} caused focus change
43
*/
44
public static boolean trackFocusChangeFor(Runnable action, Robot robot) {
45
if (!focusChangeTrackerSet) {
46
setFocusChangeTracker();
47
}
48
49
focusChanged = false;
50
trackFocusChange = true;
51
52
action.run();
53
54
Util.waitForIdle(robot);
55
56
trackFocusChange = false;
57
58
return focusChanged;
59
}
60
61
public static void invokeLaterAndWait(Runnable action, Robot robot) {
62
EventQueue.invokeLater(action);
63
try {
64
EventQueue.invokeAndWait(new Runnable() { // waiting for action
65
public void run() {}
66
});
67
} catch (InterruptedException ie) {
68
} catch (InvocationTargetException ite) {}
69
70
Util.waitForIdle(robot); // waiting for events
71
}
72
73
private static void setFocusChangeTracker() {
74
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
75
public void eventDispatched(AWTEvent e) {
76
int id = e.getID();
77
if (trackFocusChange &&
78
(id == FocusEvent.FOCUS_GAINED || id == FocusEvent.FOCUS_LOST ||
79
id == WindowEvent.WINDOW_GAINED_FOCUS || id == WindowEvent.WINDOW_LOST_FOCUS ||
80
id == WindowEvent.WINDOW_ACTIVATED || id == WindowEvent.WINDOW_DEACTIVATED))
81
{
82
System.out.println(e.toString());
83
focusChanged = true;
84
}
85
}
86
}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
87
88
focusChangeTrackerSet = true;
89
}
90
}
91
92