Skip to content

Commit

Permalink
Tests: add a white background to Mermaid renderings
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosame committed Jul 28, 2024
1 parent de3f128 commit 8150f44
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void test(String file) throws TranscoderException, IOException {
}

void test(String file, int expectedErrorCount) throws TranscoderException, IOException {
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, null, true, expectedErrorCount);
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, null, null, true, expectedErrorCount);
}

/**
Expand All @@ -74,7 +74,7 @@ void test(String file, int expectedErrorCount) throws TranscoderException, IOExc
*/
void testPrint(String file, int expectedErrorCount)
throws TranscoderException, IOException {
test(file, PRINT_MEDIUM, false, null, true, expectedErrorCount);
test(file, PRINT_MEDIUM, false, null, null, true, expectedErrorCount);
}

/**
Expand All @@ -87,7 +87,7 @@ void testPrint(String file, int expectedErrorCount)
*/
void testDark(String file, int expectedErrorCount)
throws TranscoderException, IOException {
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, true, null, true, expectedErrorCount);
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, true, null, null, true, expectedErrorCount);
}

/**
Expand All @@ -112,7 +112,23 @@ void testNV(String file)
*/
void testNV(String file, int expectedErrorCount)
throws TranscoderException, IOException {
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, null, false, expectedErrorCount);
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, null, null, false, expectedErrorCount);
}

/**
* A non-validating test.
*
* @param file the SVG file to test.
* @param expectedErrorCount the expected error count.
* @param backgroundColor the background color, or {@code null} if
* transparent.
* @throws TranscoderException
* @throws IOException
*/
void testNV(String file, int expectedErrorCount, Color backgroundColor) throws TranscoderException,
IOException {
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, backgroundColor, null, false,
expectedErrorCount);
}

float getBelowThresholdAllowed() {
Expand Down Expand Up @@ -204,15 +220,16 @@ void testUserSheet(String file, boolean validating, int expectedErrorCount)
* @throws TranscoderException
* @throws IOException
*/
void test(String file, String medium, boolean darkMode, String selector,
void test(String file, String medium, boolean darkMode, Color backgroundColor, String selector,
boolean validating, int expectedErrorCount) throws TranscoderException, IOException {
BypassRenderingTest runner = new BypassRenderingTest(medium, expectedErrorCount);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);
}

void configureAndRun(BypassRenderingTest runner, String file, boolean darkMode,
void configureAndRun(BypassRenderingTest runner, String file, boolean darkMode, Color backgroundColor,
String selector, boolean validating) throws TranscoderException, IOException {
runner.setDarkMode(darkMode);
runner.setBackgroundColor(backgroundColor);
runner.setSelector(selector);
runner.setValidating(validating);
runner.setFile(file);
Expand All @@ -232,34 +249,36 @@ void configureAndRun(BypassRenderingTest runner, String file, boolean darkMode,
* @param medium the target medium ({@code screen}, {@code print},
* etc).
* @param darkMode if true, dark mode is enabled in CSS.
* @param backgroundColor the background color, or {@code null} if
* transparent.
* @param selector the selector to find the SVG element.
* @param validating if true, the SVG is validated.
* @param expectedErrorCount the expected number of errors.
* @throws TranscoderException
* @throws IOException
*/
void testAllInputSources(String file, String medium, boolean darkMode,
void testAllInputSources(String file, String medium, boolean darkMode, Color backgroundColor,
String selector, boolean validating, int expectedErrorCount) throws TranscoderException, IOException {
BypassRenderingTest runner = new BypassRenderingTest(medium, expectedErrorCount);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);

Document doc = runner.getRenderDocument();
runner = new DocumentInputHelperRenderingTest(medium, expectedErrorCount);
runner.setRenderDocument(doc);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);

runner = new TIDocumentInputHelperRenderingTest(medium, expectedErrorCount);
runner.setRenderDocument(doc);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);

runner = new TIInputStreamHelperRenderingTest(medium, expectedErrorCount);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);

runner = new TIReaderInputHelperRenderingTest(medium, expectedErrorCount);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);

runner = new TIURIInputHelperRenderingTest(medium, expectedErrorCount);
configureAndRun(runner, file, darkMode, selector, validating);
configureAndRun(runner, file, darkMode, backgroundColor, selector, validating);
}

private class BypassRenderingTest extends RenderingTest {
Expand All @@ -271,6 +290,8 @@ private class BypassRenderingTest extends RenderingTest {
*/
private boolean darkMode = false;

private Color backgroundColor;

/**
* Selector to locate SVG element
*/
Expand All @@ -294,6 +315,10 @@ private class BypassRenderingTest extends RenderingTest {
setMedia(medium);
}

public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}

/**
* Enables or disables dark mode.
*
Expand Down Expand Up @@ -348,6 +373,9 @@ protected void encode(URL srcURL, FileOutputStream fos)
// Opaque background for dark mode
transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR,
new Color(0, 0, 0, 255));
} else if (backgroundColor != null) {
transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR,
backgroundColor);
}

TranscoderOutput dst = new TranscoderOutput(fos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package io.sf.carte.echosvg.test.svg;

import java.awt.Color;
import java.io.IOException;

import org.junit.jupiter.api.BeforeAll;
Expand All @@ -36,155 +37,179 @@
*/
public class MermaidRenderingTest extends AbstractBypassRenderingCheck {

/**
* A standard Mermaid test.
*
* @param file the SVG file to test.
* @throws TranscoderException
* @throws IOException
*/
void testMermaid(String file) throws TranscoderException, IOException {
testMermaid(file, 0);
}

/**
* A standard Mermaid test, with an expected error count.
*
* @param file the SVG file to test.
* @param expectedErrorCount the expected error count.
* @throws TranscoderException
* @throws IOException
*/
void testMermaid(String file, int expectedErrorCount) throws TranscoderException, IOException {
test(file, SVGRenderingAccuracyTest.DEFAULT_MEDIUM, false, Color.white, null, false,
expectedErrorCount);
}

@BeforeAll
public static void setUpBeforeClass() throws Exception {
TestFonts.loadTestFonts();
}

@Test
public void testMermaid() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid.svg");
testMermaid("samples/tests/spec2/foreign/mermaid.svg");
}

@Test
public void testWebsiteDiagram93() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-93.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-93.svg");
}

@Disabled
@Test
public void testBlock() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-block.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-block.svg");
}

@Test
public void testC4Context() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-c4-context.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-c4-context.svg");
}

@Test
public void testC4Component() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-c4-component.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-c4-component.svg");
}

@Test
public void testC4Container() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-c4-container.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-c4-container.svg");
}

@Test
public void testC4Deployment() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-c4-deployment.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-c4-deployment.svg");
}

@Test
public void testC4Dynamic() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-c4-dynamic.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-c4-dynamic.svg");
}

@Test
public void testClass() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-class.svg", 1);
testMermaid("samples/tests/spec2/foreign/mermaid-class.svg", 1);
}

@Test
public void testEntityRelationship() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-erd.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-erd.svg");
}

@Test
public void testEntityRelationship2() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-erd2.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-erd2.svg");
}

@Test
public void testFlowChart() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-flowchart.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-flowchart.svg");
}

@Test
public void testFlowChartCyrillic() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-flowchart-cyrillic.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-flowchart-cyrillic.svg");
}

@Test
public void testGantt() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-gantt.svg", 6);
testMermaid("samples/tests/spec2/foreign/mermaid-gantt.svg", 6);
}

@Test
public void testGitGraph() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-git-graph.svg", 1);
testMermaid("samples/tests/spec2/foreign/mermaid-git-graph.svg", 1);
}

@Test
public void testJourney() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-journey.svg", 9);
testMermaid("samples/tests/spec2/foreign/mermaid-journey.svg", 9);
}

@Test
public void testMindmap() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-mindmap.svg", 1);
testMermaid("samples/tests/spec2/foreign/mermaid-mindmap.svg", 1);
}

@Test
public void testPie() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-pie.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-pie.svg");
}

@Test
public void testQuadrant() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-quadrant.svg", 6);
testMermaid("samples/tests/spec2/foreign/mermaid-quadrant.svg", 6);
}

@Test
public void testRequirement() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-requirement.svg", 1);
testMermaid("samples/tests/spec2/foreign/mermaid-requirement.svg", 1);
}

@Test
public void testSankey() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sankey.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-sankey.svg");
}

@Test
public void testSequence() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sequence.svg", 9);
testMermaid("samples/tests/spec2/foreign/mermaid-sequence.svg", 9);
}

@Test
public void testSequenceBackground() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sequence-background.svg", 5);
testMermaid("samples/tests/spec2/foreign/mermaid-sequence-background.svg", 5);
}

@Test
public void testSequenceCritical() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sequence-critical.svg", 5);
testMermaid("samples/tests/spec2/foreign/mermaid-sequence-critical.svg", 5);
}

@Test
public void testSequenceGrouping() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sequence-grouping.svg", 11);
testMermaid("samples/tests/spec2/foreign/mermaid-sequence-grouping.svg", 11);
}

@Test
public void testSequenceParallel() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-sequence-parallel.svg", 7);
testMermaid("samples/tests/spec2/foreign/mermaid-sequence-parallel.svg", 7);
}

@Test
public void testState() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-state.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-state.svg");
}

@Test
public void testTimeline() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-timeline.svg", 2);
testMermaid("samples/tests/spec2/foreign/mermaid-timeline.svg", 2);
}

@Test
public void testXY() throws TranscoderException, IOException {
testNV("samples/tests/spec2/foreign/mermaid-xy.svg");
testMermaid("samples/tests/spec2/foreign/mermaid-xy.svg");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ public void testLinkingViewBox() throws TranscoderException, IOException {

@Test
public void testMissingRef_All() throws TranscoderException, IOException {
testAllInputSources("samples/tests/spec/linking/missingRef.svg", null, false, null, false,
16);
testAllInputSources("samples/tests/spec/linking/missingRef.svg", null, false, null, null,
false, 16);
}

@Test
Expand Down Expand Up @@ -1332,7 +1332,8 @@ public void testBug19363() throws TranscoderException, IOException {
*/
@Test
public void testCSS3_All() throws TranscoderException, IOException {
testAllInputSources("samples/tests/spec2/styling/css3.html", null, false, null, false, 4);
testAllInputSources("samples/tests/spec2/styling/css3.html", null, false, null, null,
false, 4);
}

@Test
Expand All @@ -1353,7 +1354,7 @@ public void testCSS3Dark() throws TranscoderException, IOException {
@Test
public void testCSS3_Selector() throws TranscoderException, IOException {
test("samples/tests/spec2/styling/css3.html", SVGRenderingAccuracyTest.DEFAULT_MEDIUM,
false, "#theSVG", true, 4);
false, null, "#theSVG", true, 4);
}

}
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-93.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-class.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-erd2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-flowchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-gantt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-journey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-mindmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-pie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-quadrant.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-sankey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-sequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-state.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-timeline.png
Binary file modified test-references/samples/tests/spec2/foreign/mermaid-xy.png
Binary file modified test-references/samples/tests/spec2/foreign/mermaid.png

0 comments on commit 8150f44

Please sign in to comment.