Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/EventQueue/6980209/bug6980209.java
41154 views
1
/*
2
* Copyright (c) 2015, 2017, 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
* @test
26
* @key headful
27
* @bug 6980209
28
* @summary Make tracking SecondaryLoop.enter/exit methods easier
29
* @author Semyon Sadetsky
30
*/
31
32
import javax.swing.*;
33
import java.awt.*;
34
import java.awt.event.*;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.awt.event.KeyEvent;
38
import java.awt.event.KeyListener;
39
import java.util.logging.Logger;
40
41
public class bug6980209 implements ActionListener {
42
private final static Logger log =
43
Logger.getLogger("java.awt.event.WaitDispatchSupport");
44
public static final int ATTEMPTS = 100;
45
public static final int EVENTS = 5;
46
47
private static boolean runInEDT;
48
private static JFrame frame;
49
private static int disorderCounter = 0;
50
private static Boolean enterReturn;
51
private static Boolean exitReturn;
52
private static int dispatchedEvents;
53
private static JButton button;
54
private static Point point;
55
56
public static void main(String[] args) throws Exception {
57
System.out.println(
58
"PLEASE DO NOT TOUCH KEYBOARD AND MOUSE DURING THE TEST RUN!");
59
// log.setLevel(java.util.logging.Level.FINE);
60
// log.setLevel(java.util.logging.Level.FINEST);
61
try {
62
SwingUtilities.invokeAndWait(new Runnable() {
63
public void run() {
64
frame = new JFrame();
65
frame.setUndecorated(true);
66
setup(frame);
67
}
68
});
69
final Robot robot = new Robot();
70
robot.delay(100);
71
robot.waitForIdle();
72
robot.setAutoDelay(10);
73
robot.setAutoWaitForIdle(true);
74
SwingUtilities.invokeAndWait(new Runnable() {
75
@Override
76
public void run() {
77
point = button.getLocationOnScreen();
78
}
79
});
80
robot.mouseMove( point.x + 5, point.y + 5 );
81
robot.mousePress(InputEvent.BUTTON1_MASK);
82
robot.mouseRelease(InputEvent.BUTTON1_MASK);
83
robot.delay(100);
84
robot.waitForIdle();
85
86
testExitBeforeEnter();
87
System.out.println("Run random test in EDT");
88
runInEDT = true;
89
testRandomly();
90
System.out.println("Run random test in another thread");
91
runInEDT = false;
92
testRandomly();
93
System.out.println("ok");
94
95
} finally {
96
SwingUtilities.invokeAndWait(new Runnable() {
97
@Override
98
public void run() {
99
frame.dispose();
100
}
101
});
102
}
103
}
104
105
private static void testExitBeforeEnter() throws Exception {
106
final SecondaryLoop loop =
107
Toolkit.getDefaultToolkit().getSystemEventQueue()
108
.createSecondaryLoop();
109
loop.exit();
110
Robot robot = new Robot();
111
robot.mouseWheel(1);
112
robot.waitForIdle();
113
SwingUtilities.invokeAndWait(new Runnable() {
114
@Override
115
public void run() {
116
if(loop.enter()) {
117
throw new RuntimeException("Wrong enter() return value");
118
}
119
}
120
});
121
}
122
123
private static void testRandomly() throws AWTException {
124
disorderCounter = 0;
125
final Robot robot = new Robot();
126
robot.setAutoDelay(1);
127
for (int i = 0; i < ATTEMPTS; i++) {
128
enterReturn = null;
129
exitReturn = null;
130
dispatchedEvents = 0;
131
synchronized (bug6980209.class) {
132
try {
133
for (int j = 0; j < EVENTS; j++) {
134
robot.keyPress(KeyEvent.VK_1);
135
robot.keyRelease(KeyEvent.VK_1);
136
}
137
138
// trigger the button action that starts secondary loop
139
robot.keyPress(KeyEvent.VK_SPACE);
140
robot.keyRelease(KeyEvent.VK_SPACE);
141
142
for (int j = 0; j < EVENTS; j++) {
143
robot.keyPress(KeyEvent.VK_1);
144
robot.keyRelease(KeyEvent.VK_1);
145
}
146
long time = System.nanoTime();
147
// wait for enter() returns
148
bug6980209.class.wait(1000);
149
if (enterReturn == null) {
150
System.out.println("wait time=" +
151
((System.nanoTime() - time) / 1E9) +
152
" seconds");
153
throw new RuntimeException(
154
"It seems the secondary loop will never end");
155
}
156
if (!enterReturn) disorderCounter++;
157
158
robot.waitForIdle();
159
if (dispatchedEvents <
160
2 * EVENTS) { //check that all events are dispatched
161
throw new RuntimeException(
162
"KeyEvent.VK_1 has been lost!");
163
}
164
165
} catch (InterruptedException e) {
166
throw new RuntimeException("Interrupted!");
167
}
168
}
169
}
170
if (disorderCounter == 0) {
171
System.out.println(
172
"Zero disordered enter/exit caught. It is recommended to run scenario again");
173
} else {
174
System.out.println(
175
"Disordered calls is " + disorderCounter + " from " +
176
ATTEMPTS);
177
}
178
}
179
180
private static void setup(final JFrame frame) {
181
button = new JButton("Button");
182
frame.getContentPane().add(button);
183
button.addActionListener(new bug6980209());
184
frame.pack();
185
frame.setVisible(true);
186
button.setFocusable(true);
187
button.requestFocus();
188
button.addKeyListener(new KeyListener() {
189
@Override
190
public void keyTyped(KeyEvent e) {
191
}
192
193
@Override
194
public void keyPressed(KeyEvent e) {
195
if (e.getKeyChar() == '1') dispatchedEvents++;
196
}
197
198
@Override
199
public void keyReleased(KeyEvent e) {
200
if (e.getKeyChar() == '1') dispatchedEvents++;
201
}
202
});
203
}
204
205
206
@Override
207
public void actionPerformed(ActionEvent e) {
208
if (runInEDT) {
209
runSecondaryLoop();
210
return;
211
}
212
new Thread("Secondary loop run thread") {
213
@Override
214
public void run() {
215
runSecondaryLoop();
216
}
217
}.start();
218
}
219
220
private static void runSecondaryLoop() {
221
log.fine("\n---TEST START---");
222
223
final SecondaryLoop loop =
224
Toolkit.getDefaultToolkit().getSystemEventQueue()
225
.createSecondaryLoop();
226
227
final Object LOCK = new Object(); //lock to start simultaneously
228
Thread exitThread = new Thread("Exit thread") {
229
@Override
230
public void run() {
231
synchronized (LOCK) {
232
LOCK.notify();
233
}
234
Thread.yield();
235
exitReturn = loop.exit();
236
log.fine("exit() returns " + exitReturn);
237
}
238
};
239
240
synchronized (LOCK) {
241
try {
242
exitThread.start();
243
LOCK.wait();
244
} catch (InterruptedException e1) {
245
throw new RuntimeException("What?");
246
}
247
}
248
249
enterReturn = loop.enter();
250
log.fine("enter() returns " + enterReturn);
251
252
try {
253
exitThread.join();
254
} catch (InterruptedException e) {
255
throw new RuntimeException("What?");
256
}
257
synchronized (bug6980209.class) {
258
bug6980209.class.notifyAll();
259
}
260
log.fine("\n---TEST END---");
261
}
262
}
263
264