Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/awt/FullScreen/AltTabCrashTest/AltTabCrashTest.java
41154 views
1
/*
2
* Copyright (c) 2005, 2021, 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 6275887 6429971 6459792 8198613
28
@summary Test that we don't crash when alt+tabbing in and out of
29
fullscreen app
30
@author [email protected]: area=FullScreen
31
@run main/othervm/timeout=100 AltTabCrashTest -auto -changedm
32
@run main/othervm/timeout=100 -Dsun.java2d.d3d=True AltTabCrashTest -auto -changedm
33
@run main/othervm/timeout=100 -Dsun.java2d.d3d=True AltTabCrashTest -auto -usebs -changedm
34
*/
35
36
import java.awt.AWTException;
37
import java.awt.Color;
38
import java.awt.DisplayMode;
39
import java.awt.Frame;
40
import java.awt.Graphics;
41
import java.awt.Graphics2D;
42
import java.awt.GraphicsDevice;
43
import java.awt.GraphicsEnvironment;
44
import java.awt.Image;
45
import java.awt.RenderingHints;
46
import java.awt.Robot;
47
import java.awt.event.KeyAdapter;
48
import java.awt.event.KeyEvent;
49
import java.awt.event.MouseAdapter;
50
import java.awt.event.MouseEvent;
51
import java.awt.image.BufferStrategy;
52
import java.awt.image.BufferedImage;
53
import java.awt.image.VolatileImage;
54
import java.util.Random;
55
import java.util.Vector;
56
57
/**
58
* Note that the alt+tabbing in and out part will most likely only work
59
* on Windows, and only if there are no interventions.
60
*/
61
62
public class AltTabCrashTest extends Frame {
63
public static int width;
64
public static int height;
65
public static volatile boolean autoMode;
66
public static boolean useBS;
67
public static final int NUM_OF_BALLS = 70;
68
// number of times to alt+tab in and out of the app
69
public static int altTabs = 5;
70
private final Vector<Ball> balls = new Vector<>();
71
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
72
.getDefaultScreenDevice();
73
VolatileImage vimg = null;
74
BufferStrategy bufferStrategy = null;
75
volatile boolean timeToQuit = false;
76
77
enum SpriteType {
78
OVALS, VIMAGES, BIMAGES, AAOVALS, TEXT
79
}
80
81
private static boolean changeDM = false;
82
private static SpriteType spriteType;
83
static Random rnd = new Random();
84
85
public AltTabCrashTest( ) {
86
addKeyListener(new KeyAdapter() {
87
public void keyPressed(KeyEvent e) {
88
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
89
timeToQuit = true;
90
}
91
}
92
});
93
setIgnoreRepaint(true);
94
addMouseListener(new MouseHandler());
95
for (int i = 0; i < NUM_OF_BALLS; i++) {
96
int x = 50 + rnd.nextInt(550), y = 50 + rnd.nextInt(400);
97
98
balls.addElement(createRandomBall(y, x));
99
}
100
setUndecorated(true);
101
gd.setFullScreenWindow(this);
102
GraphicsDevice gd = getGraphicsConfiguration().getDevice();
103
if (gd.isDisplayChangeSupported() && changeDM) {
104
DisplayMode dm = findDisplayMode();
105
if (dm != null) {
106
try {
107
gd.setDisplayMode(dm);
108
} catch (IllegalArgumentException iae) {
109
System.err.println("Error setting display mode");
110
}
111
}
112
}
113
if (useBS) {
114
createBufferStrategy(2);
115
bufferStrategy = getBufferStrategy();
116
} else {
117
Graphics2D g = (Graphics2D) getGraphics();
118
render(g);
119
g.dispose();
120
}
121
Thread ballThread = new BallThread();
122
ballThread.start();
123
if (autoMode) {
124
Thread tabberThread = new AltTabberThread();
125
tabberThread.start();
126
try {
127
ballThread.join();
128
tabberThread.join();
129
} catch (InterruptedException e) {
130
throw new RuntimeException(e);
131
}
132
dispose();
133
}
134
}
135
136
private Ball createRandomBall(final int y, final int x) {
137
Ball b;
138
SpriteType type;
139
140
if (spriteType == null) {
141
int index = rnd.nextInt(SpriteType.values().length);
142
type = SpriteType.values()[index];
143
} else {
144
type = spriteType;
145
}
146
switch (type) {
147
case VIMAGES: b = new VISpriteBall(x, y); break;
148
case AAOVALS: b = new AAOvalBall(x, y); break;
149
case BIMAGES: b = new BISpriteBall(x, y); break;
150
case TEXT: b = new TextBall(x,y, "Text Sprite!"); break;
151
default: b = new Ball(x, y); break;
152
}
153
return b;
154
}
155
156
private class MouseHandler extends MouseAdapter {
157
public void mousePressed(MouseEvent e) {
158
synchronized (balls) {
159
balls.addElement(createRandomBall(e.getX(), e.getY()));
160
}
161
}
162
}
163
164
private class AltTabberThread extends Thread {
165
Robot robot;
166
167
void pressAltTab() {
168
robot.keyPress(KeyEvent.VK_ALT);
169
robot.keyPress(KeyEvent.VK_TAB);
170
robot.keyRelease(KeyEvent.VK_TAB);
171
robot.keyRelease(KeyEvent.VK_ALT);
172
}
173
void pressShiftAltTab() {
174
robot.keyPress(KeyEvent.VK_SHIFT);
175
pressAltTab();
176
robot.keyRelease(KeyEvent.VK_SHIFT);
177
}
178
public void run() {
179
try {
180
robot = new Robot();
181
robot.setAutoDelay(200);
182
} catch (AWTException e) {
183
throw new RuntimeException("Can't create robot");
184
}
185
boolean out = true;
186
while (altTabs-- > 0 && !timeToQuit) {
187
System.err.println("Alt+tabber Iteration: "+altTabs);
188
try { Thread.sleep(2500); } catch (InterruptedException ex) {}
189
190
if (out) {
191
System.err.println("Issuing alt+tab");
192
pressAltTab();
193
} else {
194
System.err.println("Issuing shift ");
195
pressShiftAltTab();
196
}
197
out = !out;
198
}
199
System.err.println("Alt+tabber finished.");
200
timeToQuit = true;
201
}
202
}
203
204
private class BallThread extends Thread {
205
public void run() {
206
while (!timeToQuit) {
207
if (useBS) {
208
renderToBS();
209
bufferStrategy.show();
210
} else {
211
Graphics g = AltTabCrashTest.this.getGraphics();
212
render(g);
213
g.dispose();
214
}
215
}
216
gd.setFullScreenWindow(null);
217
AltTabCrashTest.this.dispose();
218
}
219
}
220
221
static class Ball {
222
223
int x, y; // current location
224
int dx, dy; // motion delta
225
int diameter = 40;
226
Color color = Color.red;
227
228
public Ball() {
229
}
230
231
public Ball(int x, int y) {
232
this.x = x;
233
this.y = y;
234
dx = x % 20 + 1;
235
dy = y % 20 + 1;
236
color = new Color(rnd.nextInt(0x00ffffff));
237
}
238
239
public void move() {
240
if (x < 10 || x >= AltTabCrashTest.width - 20)
241
dx = -dx;
242
if (y < 10 || y > AltTabCrashTest.height - 20)
243
dy = -dy;
244
x += dx;
245
y += dy;
246
}
247
248
public void paint(Graphics g, Color c) {
249
if (c == null) {
250
g.setColor(color);
251
} else {
252
g.setColor(c);
253
}
254
g.fillOval(x, y, diameter, diameter);
255
}
256
257
}
258
259
static class TextBall extends Ball {
260
String text;
261
public TextBall(int x, int y, String text) {
262
super(x, y);
263
this.text = text;
264
}
265
266
public void paint(Graphics g, Color c) {
267
if (c == null) {
268
g.setColor(color);
269
} else {
270
g.setColor(c);
271
}
272
g.drawString(text, x, y);
273
}
274
}
275
276
static class AAOvalBall extends Ball {
277
public AAOvalBall(int x, int y) {
278
super(x, y);
279
}
280
public void paint(Graphics g, Color c) {
281
if (c == null) {
282
Graphics2D g2d = (Graphics2D)g.create();
283
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
284
RenderingHints.VALUE_ANTIALIAS_ON);
285
g2d.setColor(color);
286
g2d.fillOval(x, y, diameter, diameter);
287
} else {
288
g.setColor(c);
289
g.fillOval(x-2, y-2, diameter+4, diameter+4);
290
}
291
}
292
}
293
294
static abstract class SpriteBall extends Ball {
295
Image image;
296
public SpriteBall(int x, int y) {
297
super(x, y);
298
image = createSprite();
299
Graphics g = image.getGraphics();
300
g.setColor(color);
301
g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
302
}
303
public void paint(Graphics g, Color c) {
304
if (c != null) {
305
g.setColor(c);
306
g.fillRect(x, y, image.getWidth(null), image.getHeight(null));
307
} else do {
308
validateSprite();
309
g.drawImage(image, x, y, null);
310
} while (renderingIncomplete());
311
}
312
public abstract Image createSprite();
313
public void validateSprite() {}
314
public boolean renderingIncomplete() { return false; }
315
}
316
class VISpriteBall extends SpriteBall {
317
318
public VISpriteBall(int x, int y) {
319
super(x, y);
320
}
321
public boolean renderingIncomplete() {
322
return ((VolatileImage)image).contentsLost();
323
}
324
325
public Image createSprite() {
326
return gd.getDefaultConfiguration().
327
createCompatibleVolatileImage(20, 20);
328
}
329
public void validateSprite() {
330
int result =
331
((VolatileImage)image).validate(getGraphicsConfiguration());
332
if (result == VolatileImage.IMAGE_INCOMPATIBLE) {
333
image = createSprite();
334
result = VolatileImage.IMAGE_RESTORED;
335
}
336
if (result == VolatileImage.IMAGE_RESTORED) {
337
Graphics g = image.getGraphics();
338
g.setColor(color);
339
g.fillRect(0, 0, image.getWidth(null), image.getHeight(null));
340
}
341
}
342
}
343
class BISpriteBall extends SpriteBall {
344
public BISpriteBall(int x, int y) {
345
super(x, y);
346
}
347
public Image createSprite() {
348
return new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB);
349
}
350
}
351
352
353
public void renderOffscreen() {
354
Graphics2D g2d = (Graphics2D) vimg.getGraphics();
355
synchronized (balls) {
356
for (Ball b : balls) {
357
b.paint(g2d, getBackground());
358
b.move();
359
b.paint(g2d, null);
360
}
361
}
362
g2d.dispose();
363
}
364
365
public void renderToBS() {
366
width = getWidth();
367
height = getHeight();
368
369
do {
370
Graphics2D g2d = (Graphics2D)bufferStrategy.getDrawGraphics();
371
372
g2d.clearRect(0, 0, width, height);
373
synchronized (balls) {
374
for (Ball b : balls) {
375
b.move();
376
b.paint(g2d, null);
377
}
378
}
379
g2d.dispose();
380
} while (bufferStrategy.contentsLost() ||
381
bufferStrategy.contentsRestored());
382
}
383
384
public void render(Graphics g) {
385
do {
386
height = getBounds().height;
387
width = getBounds().width;
388
if (vimg == null) {
389
vimg = createVolatileImage(width, height);
390
renderOffscreen();
391
}
392
int returnCode = vimg.validate(getGraphicsConfiguration());
393
if (returnCode == VolatileImage.IMAGE_RESTORED) {
394
renderOffscreen();
395
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
396
vimg = getGraphicsConfiguration().
397
createCompatibleVolatileImage(width, height);
398
renderOffscreen();
399
} else if (returnCode == VolatileImage.IMAGE_OK) {
400
renderOffscreen();
401
}
402
g.drawImage(vimg, 0, 0, this);
403
} while (vimg.contentsLost());
404
}
405
406
public static void main(String args[]) {
407
for (String arg : args) {
408
if (arg.equalsIgnoreCase("-auto")) {
409
autoMode = true;
410
System.err.println("Running in automatic mode using Robot");
411
} else if (arg.equalsIgnoreCase("-usebs")) {
412
useBS = true;
413
System.err.println("Using BufferStrategy instead of VI");
414
} else if (arg.equalsIgnoreCase("-changedm")) {
415
changeDM= true;
416
System.err.println("The test will change display mode");
417
} else if (arg.equalsIgnoreCase("-vi")) {
418
spriteType = SpriteType.VIMAGES;
419
} else if (arg.equalsIgnoreCase("-bi")) {
420
spriteType = SpriteType.BIMAGES;
421
} else if (arg.equalsIgnoreCase("-ov")) {
422
spriteType = SpriteType.OVALS;
423
} else if (arg.equalsIgnoreCase("-aaov")) {
424
spriteType = SpriteType.AAOVALS;
425
} else if (arg.equalsIgnoreCase("-tx")) {
426
spriteType = SpriteType.TEXT;
427
} else {
428
System.err.println("Usage: AltTabCrashTest [-usebs][-auto]" +
429
"[-changedm][-vi|-bi|-ov|-aaov|-tx]");
430
System.err.println(" -usebs: use BufferStrategy instead of VI");
431
System.err.println(" -auto: automatically alt+tab in and out" +
432
" of the application ");
433
System.err.println(" -changedm: change display mode");
434
System.err.println(" -(vi|bi|ov|tx|aaov) : use only VI, BI, " +
435
"text or [AA] [draw]Oval sprites");
436
System.exit(0);
437
}
438
}
439
if (spriteType != null) {
440
System.err.println("The test will only use "+spriteType+" sprites.");
441
}
442
new AltTabCrashTest();
443
}
444
445
private DisplayMode findDisplayMode() {
446
GraphicsDevice gd = getGraphicsConfiguration().getDevice();
447
DisplayMode dms[] = gd.getDisplayModes();
448
DisplayMode currentDM = gd.getDisplayMode();
449
for (DisplayMode dm : dms) {
450
if (dm.getBitDepth() > 8 &&
451
dm.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
452
dm.getBitDepth() != currentDM.getBitDepth() &&
453
dm.getWidth() == currentDM.getWidth() &&
454
dm.getHeight() == currentDM.getHeight())
455
{
456
// found a mode which has the same dimensions but different
457
// depth
458
return dm;
459
}
460
if (dm.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI &&
461
(dm.getWidth() != currentDM.getWidth() ||
462
dm.getHeight() != currentDM.getHeight()))
463
{
464
// found a mode which has the same depth but different
465
// dimensions
466
return dm;
467
}
468
}
469
470
return null;
471
}
472
}
473
474