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

115 lines
4.3 KiB
JavaScript
Raw Normal View History

describe('PathKit\'s SVG Behavior', function() {
// Note, don't try to print the PathKit object - it can cause Karma/Jasmine to lock up.
var PathKit = null;
const LoadPathKit = new Promise(function(resolve, reject) {
if (PathKit) {
resolve();
} else {
PathKitInit({
locateFile: (file) => '/pathkit/'+file,
}).then((_PathKit) => {
PathKit = _PathKit;
resolve();
});
}
});
it('can create a path from an SVG string', function(done) {
LoadPathKit.then(() => {
//.This is a parallelagram from
// https://upload.wikimedia.org/wikipedia/commons/e/e7/Simple_parallelogram.svg
let path = PathKit.FromSVGString('M 205,5 L 795,5 L 595,295 L 5,295 L 205,5 z');
let cmds = path.toCmds();
expect(cmds).toBeTruthy();
// 1 move, 4 lines, 1 close
// each element in cmds is an array, with index 0 being the verb, and the rest being args
expect(cmds.length).toBe(6);
expect(cmds).toEqual([[PathKit.MOVE_VERB, 205, 5],
[PathKit.LINE_VERB, 795, 5],
[PathKit.LINE_VERB, 595, 295],
[PathKit.LINE_VERB, 5, 295],
[PathKit.LINE_VERB, 205, 5],
[PathKit.CLOSE_VERB]]);
path.delete();
done();
});
});
it('can create an SVG string from a path', function(done) {
LoadPathKit.then(() => {
let cmds = [[PathKit.MOVE_VERB, 205, 5],
[PathKit.LINE_VERB, 795, 5],
[PathKit.LINE_VERB, 595, 295],
[PathKit.LINE_VERB, 5, 295],
[PathKit.LINE_VERB, 205, 5],
[PathKit.CLOSE_VERB]];
let [ptr, len] = PathKit.loadCmdsTypedArray(cmds);
let path = PathKit.FromCmds(ptr, len);
let svgStr = path.toSVGString();
// We output it in terse form, which is different than Wikipedia's version
expect(svgStr).toEqual('M205 5L795 5L595 295L5 295L205 5Z');
path.delete();
done();
});
});
it('can create an SVG string from hex values', function(done) {
LoadPathKit.then(() => {
let cmds = [[PathKit.MOVE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
[PathKit.LINE_VERB, 795, 5],
[PathKit.LINE_VERB, 595, 295],
[PathKit.LINE_VERB, 5, 295],
[PathKit.LINE_VERB, "0x15e80300", "0x400004dc"], // 9.37088e-26f, 2.0003f
[PathKit.CLOSE_VERB]];
let [ptr, len] = PathKit.loadCmdsTypedArray(cmds);
let path = PathKit.FromCmds(ptr, len);
let svgStr = path.toSVGString();
expect(svgStr).toEqual('M9.37088e-26 2.0003L795 5L595 295L5 295L9.37088e-26 2.0003Z');
path.delete();
done();
});
});
it('should have input and the output be the same', function(done) {
LoadPathKit.then(() => {
let testCases = [
'M0 0L1075 0L1075 242L0 242L0 0Z'
];
for(let svg of testCases) {
let path = PathKit.FromSVGString(svg);
let output = path.toSVGString();
expect(svg).toEqual(output);
path.delete();
}
done();
});
});
it('approximates arcs (conics) with quads', function(done) {
LoadPathKit.then(() => {
let path = PathKit.NewPath();
[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
path.moveTo(50, 120);
path.arc(50, 120, 45, 0, 1.75 * Math.PI);
path.lineTo(50, 120);
let svgStr = path.toSVGString();
// Q stands for quad. No need to check the whole path, as that's more
// what the gold correctness tests are for (can account for changes we make
// to the approximation algorithms).
expect(svgStr).toContain('Q');
path.delete();
[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
reportSVGString(svgStr, 'conics_quads_approx').then(() => {
done();
}).catch(reportError(done));
});
});
});