Bug 1089753 - Make WindowSnapshot.js's assertSnapshots() print out the number of differing pixels and their maximum difference. r=dholbert

This commit is contained in:
Jonathan Watt 2014-10-27 20:33:22 +00:00
parent a12deb9cbc
commit f668ccda97

View File

@ -15,57 +15,63 @@ function snapshotWindow(win, withCaret) {
// If the two snapshots don't compare as expected (true for equal, false for
// unequal), returns their serializations as data URIs. In all cases, returns
// whether the comparison was as expected.
function compareSnapshots(s1, s2, expected, fuzz) {
var s1Str, s2Str;
var correct = false;
function compareSnapshots(s1, s2, expectEqual, fuzz) {
if (s1.width != s2.width || s1.height != s2.height) {
ok(false, "Snapshot canvases are not the same size - comparing them makes no sense");
return [false];
}
var passed = false;
var numDifferentPixels;
var maxDifference = { value: undefined };
if (gWindowUtils) {
// First, check that the canvases are the same size.
var equal;
if (s1.width != s2.width || s1.height != s2.height) {
equal = false;
} else {
try {
var maxDifference = {};
var numDifferentPixels = gWindowUtils.compareCanvases(s1, s2, maxDifference);
if (!fuzz) {
equal = (numDifferentPixels == 0);
} else {
equal = (numDifferentPixels <= fuzz.numDifferentPixels &&
maxDifference.value <= fuzz.maxDifference);
}
} catch (e) {
equal = false;
ok(false, "Exception thrown from compareCanvases: " + e);
try {
numDifferentPixels = gWindowUtils.compareCanvases(s1, s2, maxDifference);
if (!fuzz) {
equal = (numDifferentPixels == 0);
} else {
equal = (numDifferentPixels <= fuzz.numDifferentPixels &&
maxDifference.value <= fuzz.maxDifference);
}
passed = (equal == expectEqual);
} catch (e) {
ok(false, "Exception thrown from compareCanvases: " + e);
}
correct = (equal == expected);
}
if (!correct) {
s1Str = s1.toDataURL();
s2Str = s2.toDataURL();
var s1DataURI, s2DataURI;
if (!passed) {
s1DataURI = s1.toDataURL();
s2DataURI = s2.toDataURL();
if (!gWindowUtils) {
correct = ((s1Str == s2Str) == expected);
passed = ((s1DataURI == s2DataURI) == expectEqual);
}
}
return [correct, s1Str, s2Str];
return [passed, s1DataURI, s2DataURI, numDifferentPixels, maxDifference.value];
}
function assertSnapshots(s1, s2, expected, fuzz, s1name, s2name) {
var [correct, s1Str, s2Str] = compareSnapshots(s1, s2, expected, fuzz);
var sym = expected ? "==" : "!=";
ok(correct, "reftest comparison: " + sym + " " + s1name + " " + s2name);
if (!correct) {
var report = "REFTEST TEST-UNEXPECTED-FAIL | " + s1name + " | image comparison (" + sym + ")\n";
if (expected) {
report += "REFTEST IMAGE 1 (TEST): " + s1Str + "\n";
report += "REFTEST IMAGE 2 (REFERENCE): " + s2Str + "\n";
function assertSnapshots(s1, s2, expectEqual, fuzz, s1name, s2name) {
var [passed, s1DataURI, s2DataURI, numDifferentPixels, maxDifference] =
compareSnapshots(s1, s2, expectEqual, fuzz);
var sym = expectEqual ? "==" : "!=";
ok(passed, "reftest comparison: " + sym + " " + s1name + " " + s2name);
if (!passed) {
// The language / format in this message should match the failure messages
// displayed by reftest.js's "RecordResult()" method so that log output
// can be parsed by reftest-analyzer.xhtml
var report = "REFTEST TEST-UNEXPECTED-FAIL | " + s1name +
" | image comparison (" + sym + "), max difference: " +
numDifferentPixels + ", number of differing pixels: " +
maxDifference + "\n";
if (expectEqual) {
report += "REFTEST IMAGE 1 (TEST): " + s1DataURI + "\n";
report += "REFTEST IMAGE 2 (REFERENCE): " + s2DataURI + "\n";
} else {
report += "REFTEST IMAGE: " + s1Str + "\n";
report += "REFTEST IMAGE: " + s1DataURI + "\n";
}
dump(report);
}
return correct;
return passed;
}