Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 1 | const 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. |
| 4 | const fail_on_no_gold = false; |
| 5 | |
| 6 | function reportCanvas(canvas, testname) { |
| 7 | let b64 = canvas.toDataURL('image/png'); |
| 8 | return _report(b64, 'canvas', testname); |
| 9 | } |
| 10 | |
| 11 | function 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 Lubick | 11194ab | 2018-08-17 13:52:56 -0400 | [diff] [blame] | 29 | }).catch((e) => reject(e)); |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 30 | }; |
| 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. |
| 38 | function 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 | |
Kevin Lubick | c6c48aa | 2018-08-17 15:00:43 -0400 | [diff] [blame^] | 57 | // Reports a canvas and then an SVG of this path. Puts it on a standard size canvas. |
| 58 | function reportPath(path, testname, done) { |
| 59 | let canvas = document.createElement('canvas'); |
| 60 | let canvasCtx = canvas.getContext('2d'); |
| 61 | // Set canvas size and make it a bit bigger to zoom in on the lines |
| 62 | standardizedCanvasSize(canvasCtx); |
| 63 | canvasCtx.stroke(path.toPath2D()); |
| 64 | |
| 65 | let svgStr = path.toSVGString(); |
| 66 | |
| 67 | return reportCanvas(canvas, testname).then(() => { |
| 68 | reportSVGString(svgStr, testname).then(() => { |
| 69 | done(); |
| 70 | }).catch(reportError(done)); |
| 71 | }).catch(reportError(done)); |
| 72 | } |
| 73 | |
Kevin Lubick | a0ba612 | 2018-08-15 13:45:28 -0400 | [diff] [blame] | 74 | function _report(data, outputType, testname) { |
| 75 | return fetch(REPORT_URL, { |
| 76 | method: 'POST', |
| 77 | mode: 'no-cors', |
| 78 | headers: { |
| 79 | 'Content-Type': 'application/json', |
| 80 | }, |
| 81 | body: JSON.stringify({ |
| 82 | 'output_type': outputType, |
| 83 | 'data': data, |
| 84 | 'test_name': testname, |
| 85 | }) |
| 86 | }).then(() => console.log(`Successfully reported ${testname} to gold aggregator`)); |
| 87 | } |
| 88 | |
| 89 | function reportError(done) { |
| 90 | return (e) => { |
| 91 | console.log("Error with fetching. Likely could not connect to aggegator server", e.message); |
| 92 | if (fail_on_no_gold) { |
| 93 | expect(e).toBeUndefined(); |
| 94 | } |
| 95 | done(); |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | function setCanvasSize(ctx, width, height) { |
| 100 | ctx.canvas.width = width; |
| 101 | ctx.canvas.height = height; |
| 102 | } |
| 103 | |
| 104 | function standardizedCanvasSize(ctx) { |
| 105 | setCanvasSize(ctx, 600, 600); |
| 106 | } |