Skip to content

Commit 2d4436b

Browse files
authored
[test] Move reftest.js code to a separate file. NFC (#22674)
1 parent 5882f41 commit 2d4436b

File tree

2 files changed

+113
-106
lines changed

2 files changed

+113
-106
lines changed

test/common.py

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,112 +2208,9 @@ def make_reftest(self, expected):
22082208
# make sure the pngs used here have no color correction, using e.g.
22092209
# pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB infile outfile
22102210
shutil.copy(expected, 'expected.png')
2211-
create_file('reftest.js', '''
2212-
// We have some tests that don't perform rendering during `main` so
2213-
// the normal process of performing `doReftest` in `postRun` doesn't
2214-
// work. These tests can delay the call to `doReftest` by calling
2215-
// `reftestBlock` and then calling `reftestUnblock` once they have
2216-
// done their rendering.
2217-
var reftestBlocked = false;
2218-
function reftestBlock() {
2219-
reftestBlocked = true;
2220-
}
2221-
function reftestUnblock() {
2222-
reftestBlocked = false;
2223-
doReftest();
2224-
}
2225-
function doReftest() {
2226-
if (reftestBlocked) return;
2227-
if (doReftest.done) return;
2228-
doReftest.done = true;
2229-
console.log('doReftest');
2230-
var img = new Image();
2231-
img.onload = () => {
2232-
assert(img.width == Module.canvas.width, `Invalid width: ${Module.canvas.width}, should be ${img.width}`);
2233-
assert(img.height == Module.canvas.height, `Invalid height: ${Module.canvas.height}, should be ${img.height}`);
2234-
2235-
var canvas = document.createElement('canvas');
2236-
canvas.width = img.width;
2237-
canvas.height = img.height;
2238-
var ctx = canvas.getContext('2d');
2239-
ctx.drawImage(img, 0, 0);
2240-
var expected = ctx.getImageData(0, 0, img.width, img.height).data;
2241-
2242-
var actualUrl = Module.canvas.toDataURL();
2243-
var actualImage = new Image();
2244-
actualImage.onload = () => {
2245-
/*
2246-
document.body.appendChild(img); // for comparisons
2247-
var div = document.createElement('div');
2248-
div.innerHTML = '^=expected, v=actual';
2249-
document.body.appendChild(div);
2250-
document.body.appendChild(actualImage); // to grab it for creating the test reference
2251-
*/
2252-
2253-
var actualCanvas = document.createElement('canvas');
2254-
actualCanvas.width = actualImage.width;
2255-
actualCanvas.height = actualImage.height;
2256-
var actualCtx = actualCanvas.getContext('2d');
2257-
actualCtx.drawImage(actualImage, 0, 0);
2258-
var actual = actualCtx.getImageData(0, 0, actualImage.width, actualImage.height).data;
2259-
2260-
var total = 0;
2261-
var width = img.width;
2262-
var height = img.height;
2263-
for (var x = 0; x < width; x++) {
2264-
for (var y = 0; y < height; y++) {
2265-
total += Math.abs(expected[y*width*4 + x*4 + 0] - actual[y*width*4 + x*4 + 0]);
2266-
total += Math.abs(expected[y*width*4 + x*4 + 1] - actual[y*width*4 + x*4 + 1]);
2267-
total += Math.abs(expected[y*width*4 + x*4 + 2] - actual[y*width*4 + x*4 + 2]);
2268-
}
2269-
}
2270-
// floor, to allow some margin of error for antialiasing
2271-
var wrong = Math.floor(total / (img.width*img.height*3));
2272-
var rebaseline = %s;
2273-
if (wrong || rebaseline) {
2274-
// Generate a png of the actual rendered image and send it back
2275-
// to the server.
2276-
Module.canvas.toBlob((blob) => {
2277-
sendFileToServer('actual.png', blob);
2278-
reportResultToServer(wrong);
2279-
})
2280-
} else {
2281-
reportResultToServer(wrong);
2282-
}
2283-
};
2284-
actualImage.src = actualUrl;
2285-
}
2286-
img.src = 'expected.png';
2287-
};
2288-
2289-
/** @suppress {uselessCode} */
2290-
function setupRefTest() {
2291-
Module['postRun'] = doReftest;
2292-
2293-
if (typeof WebGLClient !== 'undefined') {
2294-
// trigger reftest from RAF as well, needed for workers where there is no pre|postRun on the main thread
2295-
var realRAF = window.requestAnimationFrame;
2296-
/** @suppress{checkTypes} */
2297-
window.requestAnimationFrame = (func) => {
2298-
return realRAF(() => {
2299-
func();
2300-
realRAF(doReftest);
2301-
});
2302-
};
2303-
2304-
// trigger reftest from canvas render too, for workers not doing GL
2305-
var realWOM = worker.onmessage;
2306-
worker.onmessage = (event) => {
2307-
realWOM(event);
2308-
if (event.data.target === 'canvas' && event.data.op === 'render') {
2309-
realRAF(doReftest);
2310-
}
2311-
};
2312-
}
2313-
}
2314-
2315-
setupRefTest();
2316-
''' % EMTEST_REBASELINE)
2211+
create_file('reftest.js', f'''
2212+
const reftestRebaseline = {EMTEST_REBASELINE};
2213+
''' + read_file(test_file('reftest.js')))
23172214

23182215
def compile_btest(self, filename, args, reporting=Reporting.FULL):
23192216
# Inject support code for reporting results. This adds an include a header so testcases can

test/reftest.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Code for performing browser reftest where we compare generated canvas
2+
// image with an expected output image.
3+
//
4+
// This code expects the reftestRebaseline global to exist.
5+
// See make_reftest in test/common.py.
6+
7+
// We have some tests that don't perform rendering during `main` so
8+
// the normal process of performing `doReftest` in `postRun` doesn't
9+
// work. These tests can delay the call to `doReftest` by calling
10+
// `reftestBlock` and then calling `reftestUnblock` once they have
11+
// done their rendering.
12+
var reftestBlocked = false;
13+
14+
function reftestBlock() {
15+
reftestBlocked = true;
16+
}
17+
18+
function reftestUnblock() {
19+
reftestBlocked = false;
20+
doReftest();
21+
}
22+
23+
function doReftest() {
24+
if (reftestBlocked) return;
25+
if (doReftest.done) return;
26+
doReftest.done = true;
27+
var img = new Image();
28+
img.onload = () => {
29+
assert(img.width == Module.canvas.width, `Invalid width: ${Module.canvas.width}, should be ${img.width}`);
30+
assert(img.height == Module.canvas.height, `Invalid height: ${Module.canvas.height}, should be ${img.height}`);
31+
32+
var canvas = document.createElement('canvas');
33+
canvas.width = img.width;
34+
canvas.height = img.height;
35+
var ctx = canvas.getContext('2d');
36+
ctx.drawImage(img, 0, 0);
37+
var expected = ctx.getImageData(0, 0, img.width, img.height).data;
38+
39+
var actualUrl = Module.canvas.toDataURL();
40+
var actualImage = new Image();
41+
actualImage.onload = () => {
42+
/*
43+
document.body.appendChild(img); // for comparisons
44+
var div = document.createElement('div');
45+
div.innerHTML = '^=expected, v=actual';
46+
document.body.appendChild(div);
47+
document.body.appendChild(actualImage); // to grab it for creating the test reference
48+
*/
49+
50+
var actualCanvas = document.createElement('canvas');
51+
actualCanvas.width = actualImage.width;
52+
actualCanvas.height = actualImage.height;
53+
var actualCtx = actualCanvas.getContext('2d');
54+
actualCtx.drawImage(actualImage, 0, 0);
55+
var actual = actualCtx.getImageData(0, 0, actualImage.width, actualImage.height).data;
56+
57+
var total = 0;
58+
var width = img.width;
59+
var height = img.height;
60+
for (var x = 0; x < width; x++) {
61+
for (var y = 0; y < height; y++) {
62+
total += Math.abs(expected[y*width*4 + x*4 + 0] - actual[y*width*4 + x*4 + 0]);
63+
total += Math.abs(expected[y*width*4 + x*4 + 1] - actual[y*width*4 + x*4 + 1]);
64+
total += Math.abs(expected[y*width*4 + x*4 + 2] - actual[y*width*4 + x*4 + 2]);
65+
}
66+
}
67+
// floor, to allow some margin of error for antialiasing
68+
var wrong = Math.floor(total / (img.width*img.height*3));
69+
if (wrong || reftestRebaseline) {
70+
// Generate a png of the actual rendered image and send it back
71+
// to the server.
72+
Module.canvas.toBlob((blob) => {
73+
sendFileToServer('actual.png', blob);
74+
reportResultToServer(wrong);
75+
})
76+
} else {
77+
reportResultToServer(wrong);
78+
}
79+
};
80+
actualImage.src = actualUrl;
81+
}
82+
img.src = 'expected.png';
83+
};
84+
85+
function setupRefTest() {
86+
Module['postRun'] = doReftest;
87+
88+
if (typeof WebGLClient !== 'undefined') {
89+
// trigger reftest from RAF as well, needed for workers where there is no pre|postRun on the main thread
90+
var realRAF = window.requestAnimationFrame;
91+
/** @suppress{checkTypes} */
92+
window.requestAnimationFrame = (func) => {
93+
return realRAF(() => {
94+
func();
95+
realRAF(doReftest);
96+
});
97+
};
98+
99+
// trigger reftest from canvas render too, for workers not doing GL
100+
var realWOM = worker.onmessage;
101+
worker.onmessage = (event) => {
102+
realWOM(event);
103+
if (event.data.target === 'canvas' && event.data.op === 'render') {
104+
realRAF(doReftest);
105+
}
106+
};
107+
}
108+
}
109+
110+
setupRefTest();

0 commit comments

Comments
 (0)