const REPORT_URL = 'http://localhost:8081/report_gold_data'
const fail_on_no_gold = false;
function reportCanvas(canvas, testname, outputType='canvas') {
let b64 = canvas.toDataURL('image/png');
return _report(b64, outputType, testname);
}
function reportSVG(svg, testname) {
let svgStr = svg.outerHTML;
let tempImg = document.createElement('img');
let tempCanvas = document.createElement('canvas');
let canvasCtx = tempCanvas.getContext('2d');
setCanvasSize(canvasCtx, svg.getAttribute('width'), svg.getAttribute('height'));
return new Promise(function(resolve, reject) {
tempImg.onload = () => {
canvasCtx.drawImage(tempImg, 0, 0);
let b64 = tempCanvas.toDataURL('image/png');
_report(b64, 'svg', testname).then(() => {
resolve();
}).catch((e) => reject(e));
};
tempImg.setAttribute('src', 'data:image/svg+xml;,' + svgStr);
});
}
function reportSVGString(svgstr, testname, fillRule='nofill') {
let newPath = document.createElementNS('http://www.w3.org/2000/svg', 'path');
newPath.setAttribute('stroke', 'black');
if (fillRule !== 'nofill') {
newPath.setAttribute('fill', 'orange');
newPath.setAttribute('fill-rule', fillRule);
} else {
newPath.setAttribute('fill', 'rgba(255,255,255,0.0)');
}
newPath.setAttribute('d', svgstr);
let newSVG = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
newSVG.appendChild(newPath);
newSVG.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
newSVG.setAttribute('width', 600);
newSVG.setAttribute('height', 600);
return reportSVG(newSVG, testname);
}
function reportPath(path, testname, done) {
let canvas = document.createElement('canvas');
let canvasCtx = canvas.getContext('2d');
standardizedCanvasSize(canvasCtx);
canvasCtx.stroke(path.toPath2D());
let svgStr = path.toSVGString();
return reportCanvas(canvas, testname).then(() => {
reportSVGString(svgStr, testname).then(() => {
done();
}).catch(reportError(done));
}).catch(reportError(done));
}
function _report(data, outputType, testname) {
return fetch(REPORT_URL, {
method: 'POST',
mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
'output_type': outputType,
'data': data,
'test_name': testname,
})
}).then(() => console.log(`Successfully reported ${testname} to gold aggregator`));
}
function reportError(done) {
return (e) => {
console.log("Error with fetching. Likely could not connect to aggregator server", e.message);
if (fail_on_no_gold) {
expect(e).toBeUndefined();
}
done();
};
}
function setCanvasSize(ctx, width, height) {
ctx.canvas.width = width;
ctx.canvas.height = height;
}
function standardizedCanvasSize(ctx) {
setCanvasSize(ctx, 600, 600);
}
function catchException(done, fn) {
return () => {
try {
fn()
} catch (e) {
console.log('Failed with the following error', e);
expect(e).toBeFalsy();
debugger;
done();
}
}
}