Bug 1326189 part 2 - Rewrite test_visited_reftest mochitest with async function to make it clearer. r=dholbert
authorXidorn Quan <me@upsuper.org>
Fri, 20 Jan 2017 16:52:01 +1100
changeset 377877 d3a7a0669c3feaf4eb99f02bee625a654accb7df
parent 377876 2472110ff7862ce333a0d89615364e097ed0e970
child 377878 1adf8249feb259fd77b69667adc7313f2e3d7ab6
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdholbert
bugs1326189
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1326189 part 2 - Rewrite test_visited_reftest mochitest with async function to make it clearer. r=dholbert This patch is a refactor, and should not change any behavior other than providing a potentially better failure message. MozReview-Commit-ID: 9ow1XwBtGDZ
layout/style/test/test_visited_reftests.html
--- a/layout/style/test/test_visited_reftests.html
+++ b/layout/style/test/test_visited_reftests.html
@@ -80,131 +80,91 @@ var gTests = [
   // FIXME: commented out because dynamic changes on the non-first-line
   // part of the test don't work right when the link becomes visited.
   //"== first-line-1.html first-line-1-ref.html",
   "== white-to-transparent-1.html white-to-transparent-1-ref.html",
   "== link-root-1.xhtml link-root-1-ref.xhtml",
   "== mathml-links.html mathml-links-ref.html",
 ];
 
-// Maintain a reference count of how many things we're waiting for until
-// we can say the tests are done.
-var gDelayCount = 0;
-function AddFinishDependency()
-  { ++gDelayCount; }
-function RemoveFinishDependency()
-  { if (--gDelayCount == 0) SimpleTest.finish(); }
-
 // We record the maximum number of times we had to look at a test before
 // it switched to the passing state (though we assume it's 10 to start
 // rather than 0 so that we have a reasonable default).  Then we make a
 // test "time out" if it takes more than gTimeoutFactor times that
 // amount of time.  This allows us to report a test failure rather than
 // making a test failure just show up as a timeout.
 var gMaxPassingTries = 10;
 var gTimeoutFactor = 10;
 
-function loadVisitedPage()
-{
-  var element = document.createElement("iframe");
-  element.addEventListener("load", visitedPageLoad);
-  element.src = "css-visited/visited-page.html";
-  document.body.appendChild(element);
-  AddFinishDependency();
-}
-
-function visitedPageLoad()
-{
-  for (var i = 0; i < gTests.length; ++i) {
-    startTest(i);
-  }
-  RemoveFinishDependency();
-}
-
-function takeSnapshot(iframe_element)
-{
-  return snapshotWindow(iframe_element.contentWindow, false);
-}
-
-function passes(op, shot1, shot2)
-{
-  var [correct, s1, s2] = compareSnapshots(shot1, shot2, op == "==");
-  return correct;
-}
-
-function startTest(i)
-{
-  var testLine = gTests[i];
-  var splitData = testLine.split(" ");
-  var testData =
-    { op: splitData[0], test: splitData[1], reference: splitData[2] };
-  var tries = 0;
-
-  // Maintain state specific to this test in the closure exposed to all
-  // the functions nested inside this one.
-
-  function startIframe(url)
-  {
+function startIframe(url) {
+  return new Promise(resolve => {
     var element = document.createElement("iframe");
-    element.addEventListener("load", handleLoad);
+    element.addEventListener("load", () => {
+      resolve(element.contentWindow);
+    }, {once: true});
     // smaller than normal reftests, but enough for these
     element.setAttribute("style", "width: 30em; height: 10em");
     element.src = "css-visited/" + url;
     document.body.appendChild(element);
-    function handleLoad(event)
-    {
-      iframe.loaded = true;
-      if (iframe == reference) {
-        reference.snapshot = takeSnapshot(element);
+  });
+}
+
+async function runTests() {
+  SimpleTest.waitForExplicitFinish();
+  SimpleTest.requestFlakyTimeout("async link coloring");
+  await startIframe("visited-page.html");
+  await Promise.all(gTests.map(runTest));
+  SimpleTest.finish();
+}
+
+function passes(equal, shot1, shot2)
+{
+  let [correct] = compareSnapshots(shot1, shot2, equal);
+  return correct;
+}
+
+function waitFor100ms() {
+  return new Promise(resolve => setTimeout(resolve, 100));
+}
+
+async function runTest(testLine) {
+  var splitData = testLine.split(" ");
+  let isEqual = splitData[0] == "==";
+  let testFile = splitData[1];
+  let refFile = splitData[2];
+
+  let promiseTestWin = startIframe(testFile);
+  let promiseRefWin = startIframe(refFile);
+  let refSnapshot = snapshotWindow(await promiseRefWin);
+  let testWindow = await promiseTestWin;
+  // Always wait at least 100ms, so that any test that switches
+  // from passing to failing when the asynchronous link coloring
+  // happens should fail at least some of the time.
+  await waitFor100ms();
+
+  let tries;
+  let testSnapshot;
+  for (tries = 0; tries < gMaxPassingTries * gTimeoutFactor; ++tries) {
+    testSnapshot = snapshotWindow(testWindow);
+    if (passes(isEqual, testSnapshot, refSnapshot)) {
+      if (tries > gMaxPassingTries) {
+        gMaxPassingTries = tries;
       }
-      var other = (iframe == test) ? reference : test;
-      if (other.loaded) {
-        // Always wait at least 100ms, so that any test that switches
-        // from passing to failing when the asynchronous link coloring
-        // happens should fail at least some of the time.
-        setTimeout(checkTest, 100);
-      }
+      break;
     }
-    function checkTest()
-    {
-      var test_snapshot = takeSnapshot(test.element);
-      if (passes(testData.op, test_snapshot, reference.snapshot)) {
-        if (tries > gMaxPassingTries) {
-          gMaxPassingTries = tries;
-        }
-        report(true);
-      } else {
-        ++tries;
-        if (tries > gMaxPassingTries * gTimeoutFactor) {
-          info("Giving up after " + tries + " tries, " +
-               "maxp=" + gMaxPassingTries +
-               "fact=" + gTimeoutFactor);
-          report(false);
-        } else {
-          // Links might not have been colored yet.  Try again in 100ms.
-          setTimeout(checkTest, 100);
-        }
-      }
-    }
-    function report(result)
-    {
-      ok(result, "(" + i + ") " +
-                 testData.op + " " + testData.test + " " + testData.reference);
-      RemoveFinishDependency();
-    }
-    var iframe = { element: element, loaded: false };
-
-    return iframe;
+    // Links might not have been colored yet. Try again in 100ms.
+    await waitFor100ms();
   }
 
-  AddFinishDependency();
-  var test = startIframe(testData.test);
-  var reference = startIframe(testData.reference);
+  let result = assertSnapshots(testSnapshot, refSnapshot,
+                               isEqual, null, testFile, refFile);
+  if (!result) {
+    info(`Gave up after ${tries} tries, ` +
+         `maxp=${gMaxPassingTries}, fact=${gTimeoutFactor}`);
+  }
 }
 
-SimpleTest.waitForExplicitFinish();
-SimpleTest.requestFlakyTimeout("untriaged");
-loadVisitedPage();
+runTests();
 
 </script>
 </pre>
 </body>
 </html>