Bug 1496636 - add_task() should wait for document finishes loading before starting the test r=florian
authorTimothy Guan-tin Chien <timdream@gmail.com>
Mon, 08 Oct 2018 12:59:15 +0000
changeset 495719 48f36c285594a46e5433c96c58d6b05ced3d555e
parent 495718 ccf9864ee59f8f62787c28a73c4042ed5f304cee
child 495720 88544bf70da7924bf86c0f90d6e4d21a5c72fa11
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersflorian
bugs1496636
milestone64.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 1496636 - add_task() should wait for document finishes loading before starting the test r=florian Differential Revision: https://phabricator.services.mozilla.com/D7941
browser/components/resistfingerprinting/test/mochitest/test_bug863246_resource_uri.html
layout/style/test/chrome/test_display_mode.html
layout/style/test/chrome/test_display_mode_reflow.html
testing/mochitest/tests/SimpleTest/AddTask.js
toolkit/components/passwordmgr/test/mochitest/test_autocomplete_https_upgrade.html
toolkit/components/passwordmgr/test/mochitest/test_autofocus_js.html
toolkit/content/tests/widgets/test_videocontrols_error.html
toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
toolkit/content/tests/widgets/test_videocontrols_vtt.html
toolkit/content/tests/widgets/xbl/test_videocontrols_error.html
toolkit/content/tests/widgets/xbl/test_videocontrols_video_noaudio.html
toolkit/content/tests/widgets/xbl/test_videocontrols_vtt.html
--- a/browser/components/resistfingerprinting/test/mochitest/test_bug863246_resource_uri.html
+++ b/browser/components/resistfingerprinting/test/mochitest/test_bug863246_resource_uri.html
@@ -1,21 +1,15 @@
 <!DOCTYPE html>
 <meta charset="utf8">
 <script src="/tests/SimpleTest/SimpleTest.js"></script>
 <script src="/tests/SimpleTest/AddTask.js"></script>
 <script>
 /* global SimpleTest SpecialPowers add_task */
 
-function waitForDOMContentLoaded() {
-  return new Promise((aResolve) => {
-    document.addEventListener("DOMContentLoaded", aResolve);
-  });
-}
-
 function testResourceUri(aTest, aUri, aContentAccessible) {
   return new Promise((aResolve) => {
     let link = document.createElement("link");
     link.rel = "stylesheet";
     link.onload = () => {
       SimpleTest.ok(aContentAccessible, aTest);
       aResolve();
     };
@@ -24,17 +18,16 @@ function testResourceUri(aTest, aUri, aC
       aResolve();
     };
     link.href = aUri;
     document.head.appendChild(link);
   });
 }
 
 add_task(async function() {
-  await waitForDOMContentLoaded();
   await testResourceUri(
       "resource://content-accessible is content-accessible",
       "resource://content-accessible/viewsource.css",
       true);
   await testResourceUri(
       "resource://gre-resources is not content-accessible",
       "resource://gre-resources/html.css",
       false);
--- a/layout/style/test/chrome/test_display_mode.html
+++ b/layout/style/test/chrome/test_display_mode.html
@@ -25,18 +25,16 @@ function waitOneEvent(element, name) {
   });
 }
 
 function promiseNextTick() {
   return new Promise(resolve => setTimeout(resolve, 0));
 }
 
 add_task(async function() {
-  await waitOneEvent(window, "load");
-
   var iframe = document.getElementById("subdoc");
   var subdoc = iframe.contentDocument;
   var style = subdoc.getElementById("style");
   var bodyComputedStyled = subdoc.defaultView.getComputedStyle(subdoc.body);
   var win = Services.wm.getMostRecentWindow("navigator:browser");
 
   function queryApplies(q) {
     style.setAttribute("media", q);
--- a/layout/style/test/chrome/test_display_mode_reflow.html
+++ b/layout/style/test/chrome/test_display_mode_reflow.html
@@ -25,18 +25,16 @@ function waitOneEvent(element, name) {
   });
 }
 
 function promiseNextTick() {
   return new Promise(resolve => setTimeout(resolve, 0));
 }
 
 add_task(async function() {
-  await waitOneEvent(window, "load");
-
   var iframe = document.getElementById("subdoc");
   var subdoc = iframe.contentDocument;
   var style = subdoc.getElementById("style");
   var bodyComputedStyled = subdoc.defaultView.getComputedStyle(subdoc.body);
   var win = Services.wm.getMostRecentWindow("navigator:browser");
 
   var secondDiv = subdoc.getElementById("b");
   var offsetTop = secondDiv.offsetTop;
--- a/testing/mochitest/tests/SimpleTest/AddTask.js
+++ b/testing/mochitest/tests/SimpleTest/AddTask.js
@@ -22,17 +22,30 @@ var add_task = (function () {
         throw new Error("SimpleTest not available.");
       }
       // Don't stop tests until asynchronous tasks are finished.
       SimpleTest.waitForExplicitFinish();
       // Because the client is using add_task for this set of tests,
       // we need to spawn a "master task" that calls each task in succesion.
       // Use setTimeout to ensure the master task runs after the client
       // script finishes.
-      setTimeout(function () {
+      setTimeout(function nextTick() {
+        // If we are in a HTML document, we should wait for the document
+        // to be fully loaded.
+        // These checks ensure that we are in an HTML document without
+        // throwing TypeError; also I am told that readyState in XUL documents
+        // are totally bogus so we don't try to do this there.
+        if (typeof window !== "undefined" &&
+            typeof HTMLDocument !== "undefined" &&
+            window.document instanceof HTMLDocument &&
+            window.document.readyState !== "complete") {
+          setTimeout(nextTick);
+          return;
+        }
+
         (async () => {
           // Allow for a task to be skipped; we need only use the structured logger
           // for this, whilst deactivating log buffering to ensure that messages
           // are always printed to stdout.
           function skipTask(name) {
             let logger = parentRunner && parentRunner.structuredLogger;
             if (!logger) {
               info("AddTask.js | Skipping test " + name);
--- a/toolkit/components/passwordmgr/test/mochitest/test_autocomplete_https_upgrade.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_autocomplete_https_upgrade.html
@@ -45,17 +45,17 @@ runInParent(function addLogins() {
     assert.ok(false, "addLogin threw: " + e);
   }
 });
 </script>
 <p id="display"></p>
 
 <!-- we presumably can't hide the content for this test. -->
 <div id="content">
-  <iframe src="https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_basic.html"></iframe>
+  <iframe></iframe>
 </div>
 
 <pre id="test">
 <script class="testbody" type="text/javascript">
 let iframe = SpecialPowers.wrap(document.getElementsByTagName("iframe")[0]);
 let iframeDoc;
 let uname;
 let pword;
@@ -73,16 +73,17 @@ function checkACForm(expectedUsername, e
   let formID = uname.parentNode.id;
   is(uname.value, expectedUsername, "Checking " + formID + " username");
   is(pword.value, expectedPassword, "Checking " + formID + " password");
 }
 
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({"set": [["signon.schemeUpgrades", true]]});
 
+  iframe.src = "https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_basic.html";
   await new Promise(resolve => {
     iframe.addEventListener("load", function() {
       resolve();
     }, {once: true});
   });
 
   iframeDoc = iframe.contentDocument;
   uname = iframeDoc.getElementById("form-basic-username");
--- a/toolkit/components/passwordmgr/test/mochitest/test_autofocus_js.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_autofocus_js.html
@@ -33,26 +33,27 @@ runInParent(function addLogins() {
   } catch (e) {
     assert.ok(false, "addLogin threw: " + e);
   }
 });
 </script>
 <p id="display"></p>
 
 <div id="content">
-  <iframe src="https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_autofocus_js.html"></iframe>
+  <iframe></iframe>
 </div>
 
 <pre id="test">
 <script class="testbody" type="text/javascript">
 
 let iframe = SpecialPowers.wrap(document.getElementsByTagName("iframe")[0]);
 let iframeDoc;
 
 add_task(async function setup() {
+  iframe.src = "https://example.org/tests/toolkit/components/passwordmgr/test/mochitest/form_autofocus_js.html";
   await new Promise(resolve => {
     iframe.addEventListener("load", function() {
       resolve();
     }, {once: true});
   });
 
   iframeDoc = iframe.contentDocument;
 
--- a/toolkit/content/tests/widgets/test_videocontrols_error.html
+++ b/toolkit/content/tests/widgets/test_videocontrols_error.html
@@ -18,17 +18,16 @@
 <pre id="test">
 <script clas="testbody" type="application/javascript">
   const video = document.getElementById("video");
   const statusOverlay = getElementWithinVideo(video, "statusOverlay");
   const statusIcon = getElementWithinVideo(video, "statusIcon");
   const statusLabelErrorNoSource = getElementWithinVideo(video, "errorNoSource");
 
   add_task(async function setup() {
-    await new Promise(resolve => window.addEventListener("load", resolve, {once: true}));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
   });
 
   add_task(async function check_normal_status() {
     await new Promise(resolve => {
       video.src = "seek_with_sound.ogg";
       video.addEventListener("loadedmetadata", () => SimpleTest.executeSoon(resolve));
     });
--- a/toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
+++ b/toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
@@ -17,17 +17,16 @@
 
 <pre id="test">
 <script clas="testbody" type="application/javascript">
   const video = document.getElementById("video");
   const muteButton = getElementWithinVideo(video, "muteButton");
   const volumeStack = getElementWithinVideo(video, "volumeStack");
 
   add_task(async function setup() {
-    await new Promise(resolve => window.addEventListener("load", resolve));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
     await new Promise(resolve => {
       video.src = "video.ogg";
       video.addEventListener("loadedmetadata", () => SimpleTest.executeSoon(resolve));
     });
   });
 
   add_task(async function mute_button_icon() {
--- a/toolkit/content/tests/widgets/test_videocontrols_vtt.html
+++ b/toolkit/content/tests/widgets/test_videocontrols_vtt.html
@@ -19,17 +19,16 @@
 <script clas="testbody" type="application/javascript">
   SimpleTest.waitForExplicitFinish();
 
   const video = document.getElementById("video");
   const ccBtn = getElementWithinVideo(video, "closedCaptionButton");
   const ttList = getElementWithinVideo(video, "textTrackList");
 
   add_task(async function wait_for_media_ready() {
-    await new Promise(resolve => window.addEventListener("load", resolve, {once: true}));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
     await new Promise(resolve => {
       video.src = "seek_with_sound.ogg";
       video.addEventListener("loadedmetadata", resolve);
     });
   });
 
   add_task(async function check_inital_state() {
--- a/toolkit/content/tests/widgets/xbl/test_videocontrols_error.html
+++ b/toolkit/content/tests/widgets/xbl/test_videocontrols_error.html
@@ -17,17 +17,16 @@
 
 <pre id="test">
 <script clas="testbody" type="application/javascript">
   const video = document.getElementById("video");
   const statusOverlay = getAnonElementWithinVideoByAttribute(video, "anonid", "statusOverlay");
   const statusIcon = getAnonElementWithinVideoByAttribute(video, "anonid", "statusIcon");
 
   add_task(async function setup() {
-    await new Promise(resolve => window.addEventListener("load", resolve, {once: true}));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
   });
 
   add_task(async function check_normal_status() {
     await new Promise(resolve => {
       video.src = "seek_with_sound.ogg";
       video.addEventListener("loadedmetadata", () => SimpleTest.executeSoon(resolve));
     });
--- a/toolkit/content/tests/widgets/xbl/test_videocontrols_video_noaudio.html
+++ b/toolkit/content/tests/widgets/xbl/test_videocontrols_video_noaudio.html
@@ -17,17 +17,16 @@
 
 <pre id="test">
 <script clas="testbody" type="application/javascript">
   const video = document.getElementById("video");
   const muteButton = getAnonElementWithinVideoByAttribute(video, "anonid", "muteButton");
   const volumeStack = getAnonElementWithinVideoByAttribute(video, "anonid", "volumeStack");
 
   add_task(async function setup() {
-    await new Promise(resolve => window.addEventListener("load", resolve));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
     await new Promise(resolve => {
       video.src = "video.ogg";
       video.addEventListener("loadedmetadata", () => SimpleTest.executeSoon(resolve));
     });
   });
 
   add_task(async function mute_button_icon() {
--- a/toolkit/content/tests/widgets/xbl/test_videocontrols_vtt.html
+++ b/toolkit/content/tests/widgets/xbl/test_videocontrols_vtt.html
@@ -19,17 +19,16 @@
 <script clas="testbody" type="application/javascript">
   SimpleTest.waitForExplicitFinish();
 
   const video = document.getElementById("video");
   const ccBtn = getAnonElementWithinVideoByAttribute(video, "anonid", "closedCaptionButton");
   const ttList = getAnonElementWithinVideoByAttribute(video, "anonid", "textTrackList");
 
   add_task(async function wait_for_media_ready() {
-    await new Promise(resolve => window.addEventListener("load", resolve, {once: true}));
     await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
     await new Promise(resolve => {
       video.src = "seek_with_sound.ogg";
       video.addEventListener("loadedmetadata", resolve);
     });
   });
 
   add_task(async function check_inital_state() {