Skip to content

Commit

Permalink
[test] Enable --rebaseline with browser reftests. NFC (#22600)
Browse files Browse the repository at this point in the history
These allows us to update the reference images quickly and easily
directly from the commandline.

To test that it works I ran `./test/runner
browser.test_webgl_multi_draw_arrays --rebaseline`
and checked in the resulting change.
  • Loading branch information
sbc100 authored Sep 20, 2024
1 parent df22009 commit 8eb432e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Binary file modified test/browser/webgl_multi_draw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/browser_reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ function reportResultToServer(result, port) {
}
}

function sendFileToServer(filename, contents) {
fetch(`http://localhost:8888/?file=${filename}`, {method: "POST", body: contents});
}

/**
* @param {number=} port
*/
Expand Down
51 changes: 37 additions & 14 deletions test/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1942,15 +1942,15 @@ def end_headers(self):
def do_POST(self):
urlinfo = urlparse(self.path)
query = parse_qs(urlinfo.query)
# Mirror behaviour of emrun which is to write POST'd files to dump_out/ by default
if query['file']:
print('do_POST: got file: %s' % query['file'])
ensure_dir('dump_out')
filename = os.path.join('dump_out', query['file'][0])
filename = query['file'][0]
contentLength = int(self.headers['Content-Length'])
create_file(filename, self.rfile.read(contentLength), binary=True)
self.send_response(200)
self.end_headers()
else:
print(f'do_POST: unexpected POST: {urlinfo.query}')

def do_GET(self):
if self.path == '/run_harness':
Expand Down Expand Up @@ -2237,15 +2237,32 @@ def make_reftest(self, expected, manually_trigger=False):
total += Math.abs(expected[y*width*4 + x*4 + 2] - actual[y*width*4 + x*4 + 2]);
}
}
var wrong = Math.floor(total / (img.width*img.height*3)); // floor, to allow some margin of error for antialiasing
// If the main JS file is in a worker, or modularize, then we need to supply our own reporting logic.
if (typeof reportResultToServer === 'undefined') {
(() => {
%s
reportResultToServer(wrong);
})();
// floor, to allow some margin of error for antialiasing
var wrong = Math.floor(total / (img.width*img.height*3));
function reportResult(result) {
// If the main JS file is in a worker, or modularize, then we need to supply our own
// reporting logic.
if (typeof reportResultToServer === 'undefined') {
(() => {
%s
reportResultToServer(result);
})();
} else {
reportResultToServer(result);
}
}
var rebaseline = %s;
if (wrong || rebaseline) {
// Generate a png of the actual rendered image and send it back
// to the server.
Module.canvas.toBlob((blob) => {
sendFileToServer('actual.png', blob);
reportResult(wrong);
})
} else {
reportResultToServer(wrong);
reportResult(wrong);
}
};
actualImage.src = actualUrl;
Expand All @@ -2256,7 +2273,8 @@ def make_reftest(self, expected, manually_trigger=False):
/** @suppress {uselessCode} */
function setupRefTest() {
// Automatically trigger the reftest?
if (!%s) {
var manuallyTrigger = %s;
if (!manuallyTrigger) {
// Yes, automatically
Module['postRun'] = doReftest;
Expand Down Expand Up @@ -2293,7 +2311,7 @@ def make_reftest(self, expected, manually_trigger=False):
}
setupRefTest();
''' % (reporting, int(manually_trigger)))
''' % (reporting, EMTEST_REBASELINE, int(manually_trigger)))

def compile_btest(self, filename, args, reporting=Reporting.FULL):
# Inject support code for reporting results. This adds an include a header so testcases can
Expand Down Expand Up @@ -2334,7 +2352,12 @@ def reftest(self, filename, reference, reference_slack=0, manual_reference=False
kwargs.setdefault('args', [])
kwargs['args'] += ['--pre-js', 'reftest.js', '-sGL_TESTING']

return self.btest(filename, expected=expected, *args, **kwargs)
try:
return self.btest(filename, expected=expected, *args, **kwargs)
finally:
if EMTEST_REBASELINE and os.path.exists('actual.png'):
print(f'overwriting expected image: {reference}')
self.run_process('pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB actual.png'.split() + [reference])

def btest_exit(self, filename, assert_returncode=0, *args, **kwargs):
"""Special case of `btest` that reports its result solely via exiting
Expand Down
2 changes: 1 addition & 1 deletion test/fetch/test_fetch_post.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void doGet() {
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY;
attr.onsuccess = onGetSuccess;
attr.onerror = onError;
emscripten_fetch(&attr, "dump_out/newfile.txt");
emscripten_fetch(&attr, "newfile.txt");
}

void onPostSuccess(emscripten_fetch_t *fetch) {
Expand Down

0 comments on commit 8eb432e

Please sign in to comment.