1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mochitest/tests/SimpleTest/WindowSnapshot.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,63 @@ 1.4 +var gWindowUtils; 1.5 + 1.6 +try { 1.7 + gWindowUtils = SpecialPowers.getDOMWindowUtils(window); 1.8 + if (gWindowUtils && !gWindowUtils.compareCanvases) 1.9 + gWindowUtils = null; 1.10 +} catch (e) { 1.11 + gWindowUtils = null; 1.12 +} 1.13 + 1.14 +function snapshotWindow(win, withCaret) { 1.15 + return SpecialPowers.snapshotWindow(win, withCaret); 1.16 +} 1.17 + 1.18 +// If the two snapshots don't compare as expected (true for equal, false for 1.19 +// unequal), returns their serializations as data URIs. In all cases, returns 1.20 +// whether the comparison was as expected. 1.21 +function compareSnapshots(s1, s2, expected) { 1.22 + var s1Str, s2Str; 1.23 + var correct = false; 1.24 + if (gWindowUtils) { 1.25 + // First, check that the canvases are the same size. 1.26 + var equal; 1.27 + if (s1.width != s2.width || s1.height != s2.height) { 1.28 + equal = false; 1.29 + } else { 1.30 + try { 1.31 + equal = (gWindowUtils.compareCanvases(s1, s2, {}) == 0); 1.32 + } catch (e) { 1.33 + equal = false; 1.34 + ok(false, "Exception thrown from compareCanvases: " + e); 1.35 + } 1.36 + } 1.37 + correct = (equal == expected); 1.38 + } 1.39 + 1.40 + if (!correct) { 1.41 + s1Str = s1.toDataURL(); 1.42 + s2Str = s2.toDataURL(); 1.43 + 1.44 + if (!gWindowUtils) { 1.45 + correct = ((s1Str == s2Str) == expected); 1.46 + } 1.47 + } 1.48 + 1.49 + return [correct, s1Str, s2Str]; 1.50 +} 1.51 + 1.52 +function assertSnapshots(s1, s2, expected, s1name, s2name) { 1.53 + var [correct, s1Str, s2Str] = compareSnapshots(s1, s2, expected); 1.54 + var sym = expected ? "==" : "!="; 1.55 + ok(correct, "reftest comparison: " + sym + " " + s1name + " " + s2name); 1.56 + if (!correct) { 1.57 + var report = "REFTEST TEST-UNEXPECTED-FAIL | " + s1name + " | image comparison (" + sym + ")\n"; 1.58 + if (expected) { 1.59 + report += "REFTEST IMAGE 1 (TEST): " + s1Str + "\n"; 1.60 + report += "REFTEST IMAGE 2 (REFERENCE): " + s2Str + "\n"; 1.61 + } else { 1.62 + report += "REFTEST IMAGE: " + s1Str + "\n"; 1.63 + } 1.64 + dump(report); 1.65 + } 1.66 +}