tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WindowSnapshot.js (3435B)


      1 var gWindowUtils;
      2 
      3 try {
      4  gWindowUtils = SpecialPowers.getDOMWindowUtils(window);
      5  if (gWindowUtils && !gWindowUtils.compareCanvases) {
      6    gWindowUtils = null;
      7  }
      8 } catch (e) {
      9  gWindowUtils = null;
     10 }
     11 
     12 function snapshotWindow(win, withCaret) {
     13  return SpecialPowers.snapshotWindow(win, withCaret);
     14 }
     15 
     16 function snapshotRect(win, rect) {
     17  return SpecialPowers.snapshotRect(win, rect);
     18 }
     19 
     20 // If the two snapshots don't compare as expected (true for equal, false for
     21 // unequal), returns their serializations as data URIs.  In all cases, returns
     22 // whether the comparison was as expected.
     23 function compareSnapshots(s1, s2, expectEqual, fuzz) {
     24  if (s1.width != s2.width || s1.height != s2.height) {
     25    ok(
     26      false,
     27      "Snapshot canvases are not the same size: " +
     28        s1.width +
     29        "x" +
     30        s1.height +
     31        " vs. " +
     32        s2.width +
     33        "x" +
     34        s2.height
     35    );
     36    return [false];
     37  }
     38  var passed = false;
     39  var numDifferentPixels;
     40  var maxDifference = { value: undefined };
     41  if (gWindowUtils) {
     42    var equal;
     43    try {
     44      numDifferentPixels = gWindowUtils.compareCanvases(s1, s2, maxDifference);
     45      if (!fuzz) {
     46        equal = numDifferentPixels == 0;
     47      } else {
     48        equal =
     49          numDifferentPixels <= fuzz.numDifferentPixels &&
     50          maxDifference.value <= fuzz.maxDifference;
     51      }
     52      passed = equal == expectEqual;
     53    } catch (e) {
     54      ok(false, "Exception thrown from compareCanvases: " + e);
     55    }
     56  }
     57 
     58  var s1DataURI, s2DataURI;
     59  if (!passed) {
     60    s1DataURI = s1.toDataURL();
     61    s2DataURI = s2.toDataURL();
     62 
     63    if (!gWindowUtils) {
     64      passed = (s1DataURI == s2DataURI) == expectEqual;
     65    }
     66  }
     67 
     68  return [
     69    passed,
     70    s1DataURI,
     71    s2DataURI,
     72    numDifferentPixels,
     73    maxDifference.value,
     74  ];
     75 }
     76 
     77 function assertSnapshots(s1, s2, expectEqual, fuzz, s1name, s2name) {
     78  var [passed, s1DataURI, s2DataURI, numDifferentPixels, maxDifference] =
     79    compareSnapshots(s1, s2, expectEqual, fuzz);
     80  var sym = expectEqual ? "==" : "!=";
     81  ok(passed, "reftest comparison: " + sym + " " + s1name + " " + s2name);
     82  if (!passed) {
     83    let status = "TEST-UNEXPECTED-FAIL";
     84    if (usesFailurePatterns() && recordIfMatchesFailurePattern(s1name)) {
     85      status = "TEST-KNOWN-FAIL";
     86    }
     87    // The language / format in this message should match the failure messages
     88    // displayed by reftest.js's "RecordResult()" method so that log output
     89    // can be parsed by reftest-analyzer.xhtml
     90    var report =
     91      "REFTEST " +
     92      status +
     93      " | " +
     94      s1name +
     95      " | image comparison (" +
     96      sym +
     97      "), max difference: " +
     98      maxDifference +
     99      ", number of differing pixels: " +
    100      numDifferentPixels +
    101      "\n";
    102    if (expectEqual) {
    103      report += "REFTEST   IMAGE 1 (TEST): " + s1DataURI + "\n";
    104      report += "REFTEST   IMAGE 2 (REFERENCE): " + s2DataURI + "\n";
    105    } else {
    106      report += "REFTEST   IMAGE: " + s1DataURI + "\n";
    107    }
    108    (info || dump)(report);
    109  }
    110  return passed;
    111 }
    112 
    113 function assertWindowPureColor(win, color) {
    114  const snapshot = SpecialPowers.snapshotRect(win);
    115  const canvas = document.createElement("canvas");
    116  canvas.width = snapshot.width;
    117  canvas.height = snapshot.height;
    118  const context = canvas.getContext("2d");
    119  context.fillStyle = color;
    120  context.fillRect(0, 0, canvas.width, canvas.height);
    121  assertSnapshots(snapshot, canvas, true, null, "snapshot", color);
    122 }