Skip to content

Commit

Permalink
allow multiple screenshots per test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tisoft authored and httpdigest committed Jan 21, 2021
1 parent 129cdb7 commit ad7ac2a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
if: ${{ always() }}
with:
name: images
path: '**/*.png'
path: 'target/*.png'
test-windows:
runs-on: windows-latest
steps:
Expand All @@ -60,7 +60,7 @@ jobs:
if: ${{ always() }}
with:
name: images
path: '**/*.png'
path: 'target/*.png'
test-macos:
runs-on: macos-latest
steps:
Expand All @@ -76,4 +76,4 @@ jobs:
if: ${{ always() }}
with:
name: images
path: '**/*.png'
path: 'target/*.png'
60 changes: 41 additions & 19 deletions test/org/lwjgl/opengl/awt/CompareScreenshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@
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;

Expand All @@ -50,10 +54,26 @@ public class CompareScreenshotTest {
}
}

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 {
JFrame frame = new JFrame(testInfo.getDisplayName());
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new BorderLayout());
GLData data = new GLData();
data.samples = 0;
Expand Down Expand Up @@ -84,33 +104,31 @@ void canvasInContentPane(TestInfo testInfo) throws AWTException, IOException {

@Test
void canvasInSplitPane(TestInfo testInfo) throws AWTException, IOException {
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;

JSplitPane vert1=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane vert2=new JSplitPane(JSplitPane.VERTICAL_SPLIT);
JSplitPane horiz=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
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));
canvas1.setPreferredSize(new Dimension(200, 200));
vert1.setTopComponent(canvas1);

DemoCanvas canvas2 = new DemoCanvas(data);
canvas2.setPreferredSize(new Dimension(200,200));
canvas2.setPreferredSize(new Dimension(200, 200));
vert1.setBottomComponent(canvas2);

DemoCanvas canvas3 = new DemoCanvas(data);
canvas3.setPreferredSize(new Dimension(200,200));
canvas3.setPreferredSize(new Dimension(200, 200));
vert2.setTopComponent(canvas3);

DemoCanvas canvas4 = new DemoCanvas(data);
canvas4.setPreferredSize(new Dimension(200,200));
canvas4.setPreferredSize(new Dimension(200, 200));
vert2.setBottomComponent(canvas4);

frame.add(horiz, BorderLayout.CENTER);
Expand All @@ -137,8 +155,6 @@ void canvasInSplitPane(TestInfo testInfo) throws AWTException, IOException {

@Test
void reAddCanvas(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;
Expand Down Expand Up @@ -182,7 +198,7 @@ private void compareWithScreenshot(TestInfo testInfo, Window window, AWTGLCanvas

Runnable renderLoop = new Runnable() {
public void run() {
for (AWTGLCanvas canvas: canvases) {
for (AWTGLCanvas canvas : canvases) {
if (!canvas.isValid())
return;
if (renderException.get() != null) {
Expand Down Expand Up @@ -228,31 +244,37 @@ public void run() {
}
}

String screenShotSuffix = "";
int screenShotIndex = screenShotIndexMap.compute(testInfo, (info, index) -> index == null ? 1 : index + 1);
if (screenShotIndex > 1) {
screenShotSuffix = "_" + screenShotIndex;
}

ImageIO.write(background, "png", new File(
new File("target"),
System.getProperty("os.name") + "_" +
testInfo.getTestClass().map(Class::getSimpleName).orElse("unknown") + "_" +
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + ".png"));
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") + ".png"));
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + screenShotSuffix + ".png"));

File resultDestination = new File(
new File("target"),
System.getProperty("os.name") + "_" +
testInfo.getTestClass().map(Class::getSimpleName).orElse("unknown") + "_" +
testInfo.getTestMethod().map(Method::getName).orElse("unknown") + "_diff.png");
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);

window.dispose();
}

private static class DemoCanvas extends AWTGLCanvas {
Expand Down

0 comments on commit ad7ac2a

Please sign in to comment.