blob: add663426b32ad6434f9afaa9a0fb16c1a35692b [file] [log] [blame]
Kevin Lubicka0ba6122018-08-15 13:45:28 -04001const REPORT_URL = 'http://localhost:8081/report_gold_data'
2// Set this to enforce that the gold server must be up.
3// Typically used for debugging.
4const fail_on_no_gold = false;
5
6function reportCanvas(canvas, testname) {
7 let b64 = canvas.toDataURL('image/png');
8 return _report(b64, 'canvas', testname);
9}
10
11function reportSVG(svg, testname) {
12 // This converts an SVG to a base64 encoded PNG. It basically creates an
13 // <img> element that takes the inlined SVG and draws it on a canvas.
14 // The trick is we have to wait until the image is loaded, thus the Promise
15 // wrapping below.
16 let svgStr = svg.outerHTML;
17 let tempImg = document.createElement('img');
18
19 let tempCanvas = document.createElement('canvas');
20 let canvasCtx = tempCanvas.getContext('2d');
21 setCanvasSize(canvasCtx, svg.getAttribute('width'), svg.getAttribute('height'));
22
23 return new Promise(function(resolve, reject) {
24 tempImg.onload = () => {
25 canvasCtx.drawImage(tempImg, 0, 0);
26 let b64 = tempCanvas.toDataURL('image/png');
27 _report(b64, 'svg', testname).then(() => {
28 resolve();
Kevin Lubick11194ab2018-08-17 13:52:56 -040029 }).catch((e) => reject(e));
Kevin Lubicka0ba6122018-08-15 13:45:28 -040030 };
31 tempImg.setAttribute('src', 'data:image/svg+xml;,' + svgStr);
32 });
33}
34
35// For tests that just do a simple path and return it as a string, wrap it in
36// a proper svg and send it off. Supports fill (nofill means just stroke it).
37// This uses the "standard" size of 600x600.
38function reportSVGString(svgstr, testname, fillRule='nofill') {
39 let newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
40 newPath.setAttribute('stroke', 'black');
41 if (fillRule !== 'nofill') {
42 newPath.setAttribute('fill', 'orange');
43 newPath.setAttribute('fill-rule', fillRule);
44 } else {
45 newPath.setAttribute('fill', 'rgba(255,255,255,0.0)');
46 }
47 newPath.setAttribute('d', svgstr);
48 let newSVG = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
49 newSVG.appendChild(newPath);
50 // helps with the conversion to PNG.
51 newSVG.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
52 newSVG.setAttribute('width', 600);
53 newSVG.setAttribute('height', 600);
54 return reportSVG(newSVG, testname);
55}
56
57function _report(data, outputType, testname) {
58 return fetch(REPORT_URL, {
59 method: 'POST',
60 mode: 'no-cors',
61 headers: {
62 'Content-Type': 'application/json',
63 },
64 body: JSON.stringify({
65 'output_type': outputType,
66 'data': data,
67 'test_name': testname,
68 })
69 }).then(() => console.log(`Successfully reported ${testname} to gold aggregator`));
70}
71
72function reportError(done) {
73 return (e) => {
74 console.log("Error with fetching. Likely could not connect to aggegator server", e.message);
75 if (fail_on_no_gold) {
76 expect(e).toBeUndefined();
77 }
78 done();
79 };
80}
81
82function setCanvasSize(ctx, width, height) {
83 ctx.canvas.width = width;
84 ctx.canvas.height = height;
85}
86
87function standardizedCanvasSize(ctx) {
88 setCanvasSize(ctx, 600, 600);
89}