Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/awt/FullScreen/NoResizeEventOnDMChangeTest/NoResizeEventOnDMChangeTest.java
66645 views
1
/*
2
* Copyright (c) 2007, 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
* @test
25
* @key headful
26
* @bug 6646411
27
* @summary Tests that full screen window and its children receive resize
28
event when display mode changes
29
* @run main/othervm NoResizeEventOnDMChangeTest
30
* @run main/othervm -Dsun.java2d.d3d=false NoResizeEventOnDMChangeTest
31
*/
32
33
import java.awt.Canvas;
34
import java.awt.Color;
35
import java.awt.Component;
36
import java.awt.DisplayMode;
37
import java.awt.EventQueue;
38
import java.awt.Frame;
39
import java.awt.Graphics;
40
import java.awt.GraphicsDevice;
41
import java.awt.GraphicsEnvironment;
42
import java.awt.Window;
43
import java.awt.event.ComponentAdapter;
44
import java.awt.event.ComponentEvent;
45
import java.awt.event.WindowAdapter;
46
import java.awt.event.WindowEvent;
47
48
public class NoResizeEventOnDMChangeTest {
49
public static void main(String[] args) {
50
final GraphicsDevice gd = GraphicsEnvironment.
51
getLocalGraphicsEnvironment().getDefaultScreenDevice();
52
53
if (!gd.isFullScreenSupported()) {
54
System.out.println("Full screen not supported, test passed");
55
return;
56
}
57
58
DisplayMode dm = gd.getDisplayMode();
59
final DisplayMode dms[] = new DisplayMode[2];
60
for (DisplayMode dm1 : gd.getDisplayModes()) {
61
if (dm1.getWidth() != dm.getWidth() ||
62
dm1.getHeight() != dm.getHeight())
63
{
64
dms[0] = dm1;
65
break;
66
}
67
}
68
if (dms[0] == null) {
69
System.out.println("Test Passed: all DMs have same dimensions");
70
return;
71
}
72
dms[1] = dm;
73
74
Frame f = new Frame() {
75
@Override
76
public void paint(Graphics g) {
77
g.setColor(Color.red);
78
g.fillRect(0, 0, getWidth(), getHeight());
79
g.setColor(Color.green);
80
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
81
}
82
};
83
f.setUndecorated(true);
84
testFSWindow(gd, dms, f);
85
86
Window w = new Window(f) {
87
@Override
88
public void paint(Graphics g) {
89
g.setColor(Color.magenta);
90
g.fillRect(0, 0, getWidth(), getHeight());
91
g.setColor(Color.cyan);
92
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
93
}
94
};
95
testFSWindow(gd, dms, w);
96
System.out.println("Test Passed.");
97
}
98
99
private static void testFSWindow(final GraphicsDevice gd,
100
final DisplayMode dms[],
101
final Window fsWin)
102
{
103
System.out.println("Testing FS window: "+fsWin);
104
Component c = new Canvas() {
105
@Override
106
public void paint(Graphics g) {
107
g.setColor(Color.blue);
108
g.fillRect(0, 0, getWidth(), getHeight());
109
g.setColor(Color.magenta);
110
g.drawRect(0, 0, getWidth()-1, getHeight()-1);
111
g.setColor(Color.red);
112
g.drawString("FS Window : " + fsWin, 50, 50);
113
DisplayMode dm =
114
getGraphicsConfiguration().getDevice().getDisplayMode();
115
g.drawString("Display Mode: " +
116
dm.getWidth() + "x" + dm.getHeight(), 50, 75);
117
}
118
};
119
fsWin.add("Center", c);
120
fsWin.addWindowListener(new WindowAdapter() {
121
@Override
122
public void windowClosing(WindowEvent e) {
123
fsWin.dispose();
124
if (fsWin.getOwner() != null) {
125
fsWin.getOwner().dispose();
126
}
127
}
128
});
129
130
try {
131
EventQueue.invokeAndWait(new Runnable() {
132
public void run() {
133
gd.setFullScreenWindow(fsWin);
134
}
135
});
136
} catch (Exception ex) {}
137
138
sleep(1000);
139
140
final ResizeEventChecker r1 = new ResizeEventChecker();
141
final ResizeEventChecker r2 = new ResizeEventChecker();
142
143
if (gd.isDisplayChangeSupported()) {
144
fsWin.addComponentListener(r1);
145
c.addComponentListener(r2);
146
for (final DisplayMode dm1 : dms) {
147
try {
148
EventQueue.invokeAndWait(new Runnable() {
149
public void run() {
150
System.err.printf("----------- Setting DM %dx%d:\n",
151
dm1.getWidth(), dm1.getHeight());
152
try {
153
Frame f = fsWin instanceof Frame ? (Frame) fsWin : (Frame) fsWin.getOwner();
154
DisplayMode oldMode = f.getGraphicsConfiguration().getDevice().getDisplayMode();
155
gd.setDisplayMode(dm1);
156
sleep(2000);
157
// Check if setting new display mode actually results in frame being
158
// placed onto display with different resolution.
159
DisplayMode newMode = f.getGraphicsConfiguration().getDevice().getDisplayMode();
160
if (oldMode.getWidth() != newMode.getWidth()
161
|| oldMode.getHeight() != newMode.getHeight()) {
162
r1.incDmChanges();
163
r2.incDmChanges();
164
} else {
165
System.out.println("Skipping this iteration. Details:");
166
System.out.println("Requested device = " + gd);
167
System.out.println("Actual device = " + f.getGraphicsConfiguration().getDevice());
168
}
169
} catch (IllegalArgumentException iae) {}
170
}
171
});
172
} catch (Exception ex) {}
173
for (int i = 0; i < 3; i++) {
174
fsWin.repaint();
175
sleep(1000);
176
}
177
}
178
fsWin.removeComponentListener(r1);
179
c.removeComponentListener(r2);
180
}
181
182
try {
183
EventQueue.invokeAndWait(new Runnable() {
184
public void run() {
185
gd.setFullScreenWindow(null);
186
fsWin.dispose();
187
if (fsWin.getOwner() != null) {
188
fsWin.getOwner().dispose();
189
}
190
}
191
});
192
} catch (Exception ex) {}
193
194
System.out.printf("FS Window: resizes=%d, dm changes=%d\n",
195
r1.getResizes(), r1.getDmChanges());
196
System.out.printf("Component: resizes=%d, dm changes=%d\n",
197
r2.getResizes(), r2.getDmChanges());
198
if (r1.getResizes() < r1.getDmChanges()) {
199
throw new RuntimeException("FS Window didn't receive all resizes!");
200
}
201
if (r2.getResizes() < r2.getDmChanges()) {
202
throw new RuntimeException("Component didn't receive all resizes!");
203
}
204
}
205
206
static void sleep(long ms) {
207
long targetTime = System.currentTimeMillis() + ms;
208
do {
209
try {
210
Thread.sleep(targetTime - System.currentTimeMillis());
211
} catch (InterruptedException ex) {}
212
} while (System.currentTimeMillis() < targetTime);
213
}
214
215
static class ResizeEventChecker extends ComponentAdapter {
216
int dmChanges;
217
int resizes;
218
219
@Override
220
public synchronized void componentResized(ComponentEvent e) {
221
System.out.println("Received resize event for "+e.getSource());
222
resizes++;
223
}
224
public synchronized int getResizes() {
225
return resizes;
226
}
227
public synchronized void incDmChanges() {
228
dmChanges++;
229
}
230
public synchronized int getDmChanges() {
231
return dmChanges;
232
}
233
}
234
}
235
236