Skip to content

Commit

Permalink
For #208 - Put automatic visual regression testing into junit tests s…
Browse files Browse the repository at this point in the history
…o they run automatically at ci.

Hopefully this works on travis.
  • Loading branch information
danfickle committed May 14, 2018
1 parent 586ef67 commit b9641b2
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.imageio.ImageIO;

Expand All @@ -23,6 +26,9 @@

import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import com.openhtmltopdf.testcases.TestcaseRunner;
import com.openhtmltopdf.util.JDKXRLogger;
import com.openhtmltopdf.util.XRLog;
import com.openhtmltopdf.util.XRLogger;


/**
Expand Down Expand Up @@ -66,6 +72,8 @@ private byte[] runRenderer(String resourcePath, String html, BuilderConfig confi
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withHtmlContent(html, VisualTester.class.getResource(this.resourcePath).toString());
builder.toStream(actual);
builder.useFastMode();
builder.testMode(true);
config.configure(builder);
try {
builder.run();
Expand All @@ -78,15 +86,40 @@ private byte[] runRenderer(String resourcePath, String html, BuilderConfig confi
return actual.toByteArray();
}

public void runTest(String resource) throws IOException {
runTest(resource, new BuilderConfig() {
public boolean runTest(String resource) throws IOException {
return runTest(resource, new BuilderConfig() {
@Override
public void configure(PdfRendererBuilder builder) {
}
});
}

private StringBuilder logToStringBuilder() {
final XRLogger delegate = new JDKXRLogger();
final StringBuilder sb = new StringBuilder();
XRLog.setLoggerImpl(new XRLogger() {
@Override
public void setLevel(String logger, Level level) {
}

@Override
public void log(String where, Level level, String msg, Throwable th) {
StringWriter sw = new StringWriter();
th.printStackTrace(new PrintWriter(sw, true));
sb.append(where + ": " + level + ":\n" + msg + sw.toString() + "\n");
delegate.log(where, level, msg, th);
}

@Override
public void log(String where, Level level, String msg) {
sb.append(where + ": " + level + ": " + msg + "\n");
delegate.log(where, level, msg);
}
});
return sb;
}

public void runTest(String resource, BuilderConfig additionalBuilderConfiguration) throws IOException {
public boolean runTest(String resource, BuilderConfig additionalBuilderConfiguration) throws IOException {
String absResPath = this.resourcePath + resource + ".html";

File override = new File(this.overridePath, resource + ".pdf");
Expand All @@ -98,22 +131,24 @@ public void runTest(String resource, BuilderConfig additionalBuilderConfiguratio
.toByteArray(TestcaseRunner.class.getResourceAsStream(absResPath));
String html = new String(htmlBytes, Charsets.UTF_8);

StringBuilder sb = logToStringBuilder();
byte[] actualPdfBytes = runRenderer(resourcePath, html, additionalBuilderConfiguration);

if (actualPdfBytes == null) {
File output = new File(this.outputPath.getCanonicalPath() + resource + ".failure");
FileUtils.writeByteArrayToFile(output, "FAILED".getBytes(Charsets.UTF_8));
return;
System.err.println("When running test (" + resource + "), rendering failed, writing log to failure file.");
File output = new File(this.outputPath, resource + ".failure.txt");
FileUtils.writeByteArrayToFile(output, sb.toString().getBytes(Charsets.UTF_8));
return false;
}

PDDocument docActual = PDDocument.load(actualPdfBytes);

if (!testFile.exists()) {
System.err.println("When running test (" + resource + "), nothing to compare against as file (" + testFile.getCanonicalPath() + ") does not exist.");
System.out.println("Writing generated PDF to file instead in output directory.");
System.err.println("Writing generated PDF to file instead in output directory.");
File output = new File(this.outputPath, resource + ".pdf");
FileUtils.writeByteArrayToFile(output, actualPdfBytes);
return;
return false;
}

PDDocument docExpected = PDDocument.load(testFile);
Expand All @@ -136,17 +171,26 @@ public void runTest(String resource, BuilderConfig additionalBuilderConfiguratio
BufferedImage diff = compareImages(imgActual, imgExpected);

if (diff != null) {
System.err.println("When running test (" + resource + "), differences were found. Please check diff image in output directory.");
File output = new File(this.outputPath, resource + "---" + i + ".png");
System.err.println("When running test (" + resource + "), differences were found. Please check diff images in output directory.");
File output = new File(this.outputPath, resource + "---" + i + "---diff.png");
ImageIO.write(diff, "png", output);

output = new File(this.outputPath, resource + "---" + i + "---actual.png");
ImageIO.write(imgActual, "png", output);

output = new File(this.outputPath, resource + "---" + i + "---expected.png");
ImageIO.write(imgExpected, "png", output);
problems = true;
}
}

if (problems) {
File outPdf = new File(this.outputPath, resource + ".pdf");
FileUtils.writeByteArrayToFile(outPdf, actualPdfBytes);
return false;
}

return true;
}

public BufferedImage compareImages(BufferedImage img1, BufferedImage img2) {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,12 @@
background-color: green;
z-index: 1;
}

#explain {
top: 180px;
width: 100%;
height: auto;
}
</style>
</head>
<body>
<div id="one"/>
<div id="two"/>
<div id="three"/>
<div id="explain">You should see a red square on top of a blue square on top of a green square.</div>
<!-- You should see a red square on top of a blue square on top of a green square. -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.openhtmltopdf.visualregressiontests;

import java.io.File;
import static org.junit.Assert.assertTrue;
import java.io.IOException;

import org.junit.Before;
import org.junit.Test;

import com.openhtmltopdf.visualtest.VisualTester;

public class VisualRegressionTest {
private VisualTester vt;

@Before
public void configureTester() {
File overrideDirectory = new File("target/test/visual-tests/user-override/");
File outputDirectory = new File("target/test/visual-tests/test-output/");

overrideDirectory.mkdirs();
outputDirectory.mkdirs();

vt = new VisualTester("/visualtest/html/", /* Resource path. */
new File("src/main/resources/visualtest/expected/"), /* Expected directory */
overrideDirectory,
outputDirectory
);
}

@Test
public void testZIndexWithAbsolutePosition() throws IOException {
assertTrue(vt.runTest("z-index-absolute"));
}



}

0 comments on commit b9641b2

Please sign in to comment.