skia2/experimental/pathkit/tests/pathops.spec.js

234 lines
9.8 KiB
JavaScript
Raw Normal View History

[PathKit] Adding test infrastructure to support Gold output To get the gold images out of the browser tests, this adds testReporter.js and pathkit_aggregator.go. testReporter bundles up the output as a base64 encoded PNG and sends it over the local network to pathkit_aggregator. pathkit_aggregator will keep a list of test results reported in this way and write the PNGs to /OUT of the container (which is the swarming output directory). Finally, after all the tests are run, the helper script "test_pathkit.sh" makes a POST request that creates the JSON file that gold expects (following the schema https://github.com/google/skia-buildbot/blob/master/golden/docs/INGESTION.md) pathkit_aggregator takes many command line arguments which control the keys that Gold needs in order to ingest and handle the data. Of note, this creates a new set (i.e. source_type) of gold images called "pathkit", which will distinguish it from "gm", "image", etc. There will be at least 2 sub-sets of "pathkit" images, "canvas" and "svg", (representing the 2 output types of PathKit). This CL doesn't quite handle SVG yet, as it needs a way to convert SVG to PNG in the browser and will be addressed in a follow up CL. A "standard" gm is sized at 600x600. This was arbitrarily picked. Note that the functions in testReporter.js return Promises based on the fetch requests to post the data. This eliminates the race condition between the /report_gold_data and /dump_json since running the karma tests won't return until all reports are done. Other changes of note: - Adds go to karma-chrome-tests container. - renames recipe_modules/build/wasm.py -> pathkit.py to be consistent with the name of test_pathkit.py and make for easier grepping. - Increases the JS test timeout to 10s (up from 5) to hopefully avoid the flakes seen in the Debug Test. Bug: skia:8216 Change-Id: Ic2cad54f3d19cc16601cf2e9a87798db1e6887a2 Reviewed-on: https://skia-review.googlesource.com/147042 Reviewed-by: Stephan Altmueller <stephana@google.com>
2018-08-15 17:45:28 +00:00
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
var dumpErrors = false;
var container;
function getViewBox(path) {
let bounds = path.getBounds();
return `${(bounds.fLeft-2)*.95} ${(bounds.fTop-2)*.95} ${(bounds.fRight+2)*1.05} ${(bounds.fBottom+2)*1.05}`;
}
function addSVG(testName, expectedPath, actualPath, message) {
if (!dumpErrors) {
return;
}
if (!container) {
let styleEl = document.createElement('style');
document.head.appendChild(styleEl);
let sheet = styleEl.sheet;
sheet.insertRule(`svg {
border: 1px solid #DDD;
max-width: 45%;
vertical-align: top;
}`, 0);
container = document.createElement('div');
document.body.appendChild(container);
}
let thisTest = document.createElement('div');
thisTest.innerHTML = `
<h2>Failed test ${testName}</h2>
<div>${message}</div>
<svg class='expected' viewBox='${getViewBox(expectedPath)}'>
<path stroke=black d="${expectedPath.toSVGString()}"></path>
</svg>
<svg class='actual' viewBox='${getViewBox(actualPath)}'>
<path stroke=black d="${actualPath.toSVGString()}"></path>
</svg>
`;
container.appendChild(thisTest);
}
const TOLERANCE = 0.0001;
function diffPaths(expected, actual) {
// Look through commands and see if they are within tolerance.
let eCmds = expected.toCmds(), aCmds = actual.toCmds();
if (eCmds.length !== aCmds.length) {
//console.log(`Expected: ${JSON.stringify(eCmds)} and Actual: ${JSON.stringify(aCmds)}`);
return `Different amount of verbs. Expected had ${eCmds.length}, Actual had ${aCmds.length}`;
}
for(let idx = 0; idx < eCmds.length; idx++){
let eCmd = eCmds[idx], aCmd = aCmds[idx];
if (eCmd.length !== aCmd.length) {
// Should never happen, means WASM code is returning bad ops.
return `Command index ${idx} differs in num arguments. Expected had ${eCmd.length}, Actual had ${aCmd.length}`;
}
let eVerb = eCmd[0], aVerb = aCmd[0];
if (eVerb !== aVerb) {
return `Command index ${idx} differs. Expected had ${eVerb}, Actual had ${aVerb}`;
}
for (let arg = 1; arg < eCmd.length; arg++) {
if (Math.abs(eCmd[arg] - aCmd[arg]) > TOLERANCE) {
return `Command index ${idx} has different argument for verb ${eVerb} at position ${arg}. Expected had ${eCmd[arg]}, Actual had ${aCmd[arg]}`
}
}
}
return null;
}
describe('PathKit\'s PathOps Behavior', function() {
// Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
var PathKit = null;
var PATHOP_MAP = {};
var FILLTYPE_MAP = {};
const LoadPathKit = new Promise(function(resolve, reject) {
if (PathKit) {
resolve();
} else {
PathKitInit({
locateFile: (file) => '/pathkit/'+file,
}).then((_PathKit) => {
PathKit = _PathKit;
PATHOP_MAP = {
'kIntersect_SkPathOp':PathKit.PathOp.INTERSECT,
'kDifference_SkPathOp':PathKit.PathOp.DIFFERENCE,
'kUnion_SkPathOp': PathKit.PathOp.UNION,
'kXOR_SkPathOp': PathKit.PathOp.XOR,
'kXOR_PathOp': PathKit.PathOp.XOR,
'kReverseDifference_SkPathOp': PathKit.PathOp.REVERSE_DIFFERENCE,
};
FILLTYPE_MAP = {
'kWinding_FillType':PathKit.FillType.WINDING,
'kEvenOdd_FillType':PathKit.FillType.EVENODD,
'kInverseWinding_FillType': PathKit.FillType.INVERSE_WINDING,
'kInverseEvenOdd_FillType': PathKit.FillType.INVERSE_EVENODD,
};
resolve();
});
}
});
function getFillType(str) {
let e = FILLTYPE_MAP[str];
expect(e).toBeTruthy(`Could not find FillType Enum for ${str}`);
return e;
}
function getPathOp(str) {
let e = PATHOP_MAP[str];
expect(e).toBeTruthy(`Could not find PathOp Enum for ${str}`);
return e;
}
function fromCmds(cmds) {
let [ptr, len] = PathKit.loadCmdsTypedArray(cmds);
return PathKit.FromCmds(ptr, len);
}
it('combines two paths with .op() and matches what we see from C++', function(done) {
LoadPathKit.then(() => {
// Test JSON created with:
// ./out/Clang/pathops_unittest -J ./pathkit/experimental/tests/PathOpsOp.json -m PathOpsOp$
fetch('/base/tests/PathOpsOp.json').then((r) => {
r.json().then((json)=>{
expect(json).toBeTruthy();
let testNames = Object.keys(json);
expect(testNames.length).toBe(351); // Remove if test data changes a lot.
testNames.sort();
for (testName of testNames) {
let test = json[testName];
let path1 = fromCmds(test.p1);
expect(path1).not.toBeNull(`path1 error when loading cmds '${test.p1}'`);
path1.setFillType(getFillType(test.fillType1));
let path2 = fromCmds(test.p2);
expect(path2).not.toBeNull(`path2 error when loading cmds '${test.p2}'`);
path2.setFillType(getFillType(test.fillType2));
let combined = path1.op(path2, getPathOp(test.op));
if (test.expectSuccess === 'no') {
expect(combined).toBeNull(`Test ${testName} should have not created output, but did`);
} else {
expect(combined).not.toBeNull();
let expected = fromCmds(test.out);
// Do a tolerant match.
let diff = diffPaths(expected, combined);
if (test.expectMatch === 'yes'){
// Check fill type
expect(combined.getFillType().value).toEqual(getFillType(test.fillTypeOut).value);
// diff should be null if the paths are identical (modulo rounding)
if (diff) {
expect(`[${testName}] ${diff}`).toBe('');
addSVG('[PathOps] ' + testName, expected, combined, diff);
}
} else if (test.expectMatch === 'flaky') {
// Don't worry about it, at least it didn't crash.
} else {
if (!diff) {
expect(`[${testName}] was expected to have paths that differed`).not.toBe('');
}
}
expected.delete();
}
combined.delete();
path1.delete();
path2.delete();
}
done();
});
});
});
});
it('simplifies a path with .simplify() and matches what we see from C++', function(done) {
LoadPathKit.then(() => {
// Test JSON created with:
// ./out/Clang/pathops_unittest -J ./pathkit/experimental/tests/PathOpsSimplify.json -m PathOpsSimplify$
fetch('/base/tests/PathOpsSimplify.json').then((r) => {
r.json().then((json)=>{
expect(json).toBeTruthy();
let testNames = Object.keys(json);
expect(testNames.length).toBe(457); // Remove if test data changes a lot.
testNames.sort();
for (testName of testNames) {
let test = json[testName];
let path = fromCmds(test.path);
expect(path).not.toBeNull(`path1 error when loading cmds '${test.path}'`);
path.setFillType(getFillType(test.fillType));
let simplified = path.simplify();
if (test.expectSuccess === 'no') {
expect(simplified).toBeNull(`Test ${testName} should have not created output, but did`);
} else {
expect(simplified).not.toBeNull();
let expected = fromCmds(test.out);
// Do a tolerant match.
let diff = diffPaths(expected, simplified);
if (test.expectMatch === 'yes'){
// Check fill type
expect(simplified.getFillType().value).toEqual(getFillType(test.fillTypeOut).value);
// diff should be null if the paths are identical (modulo rounding)
if (diff) {
expect(`[${testName}] ${diff}`).toBe('');
addSVG('[Simplify] ' + testName, expected, simplified, diff);
}
} else if (test.expectMatch === 'flaky') {
// Don't worry about it, at least it didn't crash.
} else {
if (!diff) {
expect(`[${testName}] was expected to not match output`).not.toBe('');
}
}
expected.delete();
}
simplified.delete();
path.delete();
}
done();
});
});
});
});
});