-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompareScreenshotTest.java
More file actions
367 lines (311 loc) · 13 KB
/
CompareScreenshotTest.java
File metadata and controls
367 lines (311 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package org.lwjgl.opengl.awt;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Window;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import com.github.romankh3.image.comparison.ImageComparison;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import static org.lwjgl.opengl.GL.createCapabilities;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glClearColor;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glViewport;
public class CompareScreenshotTest {
static {
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (ClassNotFoundException | IllegalAccessException | UnsupportedLookAndFeelException | InstantiationException e) {
throw new RuntimeException(e);
}
}
private final Map<TestInfo, Integer> screenShotIndexMap = new HashMap<>();
private JFrame frame;
@BeforeEach
void setup(TestInfo testInfo) {
frame = new JFrame(testInfo.getDisplayName());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
@AfterEach
void tearDown() {
if (frame != null) {
frame.dispose();
frame = null;
}
}
@Test
void canvasInContentPane(TestInfo testInfo) throws AWTException, IOException {
frame.setLayout(new BorderLayout());
GLData data = new GLData();
data.samples = 0;
data.swapInterval = 0;
AWTGLCanvas canvas = new DemoCanvas(data);
frame.add(canvas, BorderLayout.CENTER);
canvas.setPreferredSize(new Dimension(600, 600));
frame.add(new JPanel() {{
setBackground(Color.BLUE);
}}, BorderLayout.NORTH);
frame.add(new JPanel() {{
setBackground(Color.RED);
}}, BorderLayout.SOUTH);
frame.add(new JPanel() {{
setBackground(Color.GREEN);
}}, BorderLayout.EAST);
frame.add(new JPanel() {{
setBackground(Color.YELLOW);
}}, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
compareWithScreenshot(testInfo, frame, canvas);
}
@Test
void canvasInSplitPane(TestInfo testInfo) throws AWTException, IOException {
frame.setLayout(new BorderLayout());
GLData data = new GLData();
data.samples = 0;
data.swapInterval = 0;
JSplitPane vert1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane vert2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane horiz = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
horiz.setLeftComponent(vert1);
horiz.setRightComponent(vert2);
DemoCanvas canvas1 = new DemoCanvas(data);
canvas1.setPreferredSize(new Dimension(200, 200));
vert1.setTopComponent(canvas1);
DemoCanvas canvas2 = new DemoCanvas(data);
canvas2.setPreferredSize(new Dimension(200, 200));
vert1.setBottomComponent(canvas2);
DemoCanvas canvas3 = new DemoCanvas(data);
canvas3.setPreferredSize(new Dimension(200, 200));
vert2.setTopComponent(canvas3);
DemoCanvas canvas4 = new DemoCanvas(data);
canvas4.setPreferredSize(new Dimension(200, 200));
vert2.setBottomComponent(canvas4);
frame.add(horiz, BorderLayout.CENTER);
frame.add(new JPanel() {{
setBackground(Color.BLUE);
}}, BorderLayout.NORTH);
frame.add(new JPanel() {{
setBackground(Color.RED);
}}, BorderLayout.SOUTH);
frame.add(new JPanel() {{
setBackground(Color.GREEN);
}}, BorderLayout.EAST);
frame.add(new JPanel() {{
setBackground(Color.YELLOW);
}}, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
compareWithScreenshot(testInfo, frame, canvas1, canvas2, canvas3, canvas4);
}
@Test
void reAddCanvas(TestInfo testInfo) throws AWTException, IOException, InvocationTargetException, InterruptedException {
frame.setLayout(new BorderLayout());
GLData data = new GLData();
data.samples = 0;
data.swapInterval = 0;
AWTGLCanvas canvas = new DemoCanvas(data);
frame.add(canvas, BorderLayout.CENTER);
canvas.setPreferredSize(new Dimension(600, 600));
frame.add(new JPanel() {{
setBackground(Color.BLUE);
}}, BorderLayout.NORTH);
frame.add(new JPanel() {{
setBackground(Color.RED);
}}, BorderLayout.SOUTH);
frame.add(new JPanel() {{
setBackground(Color.GREEN);
}}, BorderLayout.EAST);
frame.add(new JPanel() {{
setBackground(Color.YELLOW);
}}, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
// make sure the underlying OpenGL Context is created
SwingUtilities.invokeAndWait(canvas::render);
// remove and re-add
frame.remove(canvas);
frame.add(canvas, BorderLayout.CENTER);
frame.pack();
compareWithScreenshot(testInfo, frame, canvas);
}
@Test
void hideAndShowCanvas(TestInfo testInfo) throws AWTException, IOException, InvocationTargetException, InterruptedException {
JFrame frame = new JFrame(testInfo.getDisplayName());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
GLData data = new GLData();
data.samples = 0;
data.swapInterval = 0;
AWTGLCanvas canvas = new DemoCanvas(data);
frame.add(canvas, BorderLayout.CENTER);
canvas.setPreferredSize(new Dimension(600, 600));
frame.add(new JPanel() {{
setBackground(Color.BLUE);
}}, BorderLayout.NORTH);
frame.add(new JPanel() {{
setBackground(Color.RED);
}}, BorderLayout.SOUTH);
frame.add(new JPanel() {{
setBackground(Color.GREEN);
}}, BorderLayout.EAST);
frame.add(new JPanel() {{
setBackground(Color.YELLOW);
}}, BorderLayout.WEST);
frame.pack();
frame.setVisible(true);
frame.transferFocus();
// make sure the underlying OpenGL Context is created
SwingUtilities.invokeAndWait(canvas::render);
frame.pack();
compareWithScreenshot(testInfo, frame, canvas);
// hide
canvas.setVisible(false);
compareWithScreenshot(testInfo, frame);
// show
canvas.setVisible(true);
compareWithScreenshot(testInfo, frame, canvas);
}
private void compareWithScreenshot(TestInfo testInfo, Window window, AWTGLCanvas... canvases) throws AWTException, IOException {
AtomicInteger renderCount = new AtomicInteger(0);
AtomicReference<Exception> renderException = new AtomicReference<>();
Runnable renderLoop = new Runnable() {
public void run() {
for (AWTGLCanvas canvas : canvases) {
if (!canvas.isValid())
return;
if (renderException.get() != null) {
return;
}
renderCount.incrementAndGet();
try {
if (renderCount.get() < 10) {
canvas.render();
}
} catch (Exception e) {
renderException.set(e);
}
}
SwingUtilities.invokeLater(this);
}
};
SwingUtilities.invokeLater(renderLoop);
long startTime = System.currentTimeMillis();
// Wait until we definitely have been rendered, or a timeout of 20 seconds occurred
while (canvases.length > 0 && renderException.get() == null && renderCount.get() < 10 && System.currentTimeMillis() - startTime < 20_000) {
Thread.yield();
}
Robot rbt = new Robot();
// Calculate inner window area
Rectangle frameBounds = window.getBounds();
Insets insets = window.getInsets();
frameBounds.y += insets.top;
frameBounds.height -= (insets.top + insets.bottom);
frameBounds.x += insets.left;
frameBounds.width -= (insets.left + insets.right);
window.toFront();
BufferedImage background = rbt.createScreenCapture(frameBounds);
// Some Operating Systems have a window open/close animation, so we wait until the screenshot is stable
while (true) {
// Pause at least 500 milliseconds between captures
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Thread interrupted during screenshot delay", e);
}
BufferedImage s = rbt.createScreenCapture(frameBounds);
if (new ImageComparison(background, s).compareImages().getDifferencePercent() == 0) {
break;
} else {
background = s;
}
}
String screenShotSuffix = "";
int screenShotIndex = screenShotIndexMap.compute(testInfo, (info, index) -> index == null ? 1 : index + 1);
if (screenShotIndex > 1) {
screenShotSuffix = "_" + screenShotIndex;
}
File outputDir = new File("build/test-screenshots");
outputDir.mkdirs();
ImageIO.write(background, "png", new File(
outputDir,
System.getProperty("os.name") + "_" +
testInfo.getTestClass().map(Class::getSimpleName).orElse("unknown") + "_" +
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + screenShotSuffix + ".png"));
if (renderException.get() != null) {
renderException.get().printStackTrace();
throw new RuntimeException(renderException.get());
}
BufferedImage expectedImage = ImageIO.read(getClass().getResource("/" + testInfo.getTestClass().map(Class::getSimpleName).orElse("unknown") + "_" +
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + screenShotSuffix + ".png"));
File resultDestination = new File(
outputDir,
System.getProperty("os.name") + "_" +
testInfo.getTestClass().map(Class::getSimpleName).orElse("unknown") + "_" +
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + screenShotSuffix + "_diff.png");
//Create ImageComparison object for it.
ImageComparison imageComparison = new ImageComparison(expectedImage, background, resultDestination);
//Mac OS has round corners in the bottom, so we need to ignore a few pixels
imageComparison.setAllowingPercentOfDifferentPixels(0.02d);
Assertions.assertTrue(imageComparison.compareImages().getDifferencePercent() < 0.1f);
}
private static class DemoCanvas extends AWTGLCanvas {
public DemoCanvas(GLData data) {
super(data);
}
public void initGL() {
System.out.println("OpenGL version: " + effective.majorVersion + "." + effective.minorVersion + " (Profile: " + effective.profile + ")");
createCapabilities();
glClearColor(0.3f, 0.4f, 0.5f, 1);
}
public void paintGL() {
int w = getFramebufferWidth();
int h = getFramebufferHeight();
float aspect = (float) w / h;
double now = 100;
float width = (float) Math.abs(Math.sin(now * 0.3));
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, w, h);
glBegin(GL_QUADS);
glColor3f(0.4f, 0.6f, 0.8f);
glVertex2f(-0.75f * width / aspect, 0.0f);
glVertex2f(0, -0.75f);
glVertex2f(+0.75f * width / aspect, 0);
glVertex2f(0, +0.75f);
glEnd();
swapBuffers();
}
}
}