Bug 1414173 - Explicitly enqueue task to fix the non-conformant promising handling failures in video controls mochitests. r=jaws
authorRay Lin <ralin@mozilla.com>
Tue, 07 Nov 2017 18:34:52 +0800
changeset 435772 ead14db678204b84c56e6238f5069a93dfb0dadc
parent 435771 5ba750f62b36a270549c93c0bd5e91aad7e2178f
child 435773 f296220f41172e899e585354439d37877aada199
push id117
push userfmarier@mozilla.com
push dateTue, 28 Nov 2017 20:17:16 +0000
reviewersjaws
bugs1414173
milestone58.0a1
Bug 1414173 - Explicitly enqueue task to fix the non-conformant promising handling failures in video controls mochitests. r=jaws MozReview-Commit-ID: 11dStMrxlyH
toolkit/content/tests/widgets/test_bug1319301.html
toolkit/content/tests/widgets/test_videocontrols.html
toolkit/content/tests/widgets/test_videocontrols_error.html
toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
--- a/toolkit/content/tests/widgets/test_bug1319301.html
+++ b/toolkit/content/tests/widgets/test_bug1319301.html
@@ -1,63 +1,42 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>Video controls test - bug 1319301</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
   <script type="text/javascript" src="head.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <p id="display"></p>
 
 <div id="content">
   <video id="video" controls preload="auto"></video>
 </div>
 
 <pre id="test">
 <script clas="testbody" type="application/javascript">
-  SimpleTest.waitForExplicitFinish();
-
-  const domUtils = SpecialPowers.Cc["@mozilla.org/inspector/dom-utils;1"].
-    getService(SpecialPowers.Ci.inIDOMUtils);
-
   const video = document.getElementById("video");
   const controlsSpacer = getAnonElementWithinVideoByAttribute(video, "anonid", "controlsSpacer");
 
-  const testCases = [];
+  add_task(async function setup() {
+    await new Promise(resolve => window.addEventListener("load", resolve));
+    await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
+  });
 
-  testCases.push(() => new Promise(resolve => {
+  add_task(async function play_fadeout() {
     isnot(controlsSpacer.getAttribute("fadeout"), "true", "controlsSpacer should not fadeout before playing");
 
-    resolve();
-  }));
-
-  testCases.push(() => new Promise(resolve => {
-    video.src = "seek_with_sound.ogg";
-    video.addEventListener("play", resolve);
-    video.addEventListener("canplay", video.play);
-  }));
-
-  testCases.push(() => new Promise(resolve => {
-    is(controlsSpacer.getAttribute("fadeout"), "true", "controlsSpacer should fadeout once video starts playing");
+    await new Promise(resolve => {
+      video.addEventListener("canplaythrough", video.play);
+      video.addEventListener("play", () => SimpleTest.executeSoon(resolve));
+      video.src = "seek_with_sound.ogg";
+    })
 
-    resolve();
-  }));
-
-  function executeTestCases(tasks) {
-    return tasks.reduce((promise, task) => promise.then(task), Promise.resolve());
-  }
-
-  function startTest() {
-    executeTestCases(testCases).then(SimpleTest.finish);
-  }
-
-  function loadevent() {
-    SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]}, startTest);
-  }
-
-  window.addEventListener("load", loadevent);
+    is(controlsSpacer.getAttribute("fadeout"), "true", "controlsSpacer should fadeout once video starts playing");
+  });
 </script>
 </pre>
 </body>
 </html>
--- a/toolkit/content/tests/widgets/test_videocontrols.html
+++ b/toolkit/content/tests/widgets/test_videocontrols.html
@@ -87,24 +87,24 @@ function verifyExpectedEvent(event) {
 
   if (event.type == checkingEvent) {
     ok(true, "checking event type: ", checkingEvent);
   } else {
     expectingEventPromise.reject(new Error(`Got event: ${event.type}, expected: ${checkingEvent}`));
   }
 
   if (expectingEvents.length == 0) {
-    expectingEventPromise.resolve();
+    SimpleTest.executeSoon(expectingEventPromise.resolve);
   }
 }
 
 async function waitForEvent(...eventTypes) {
   expectingEvents = eventTypes;
 
-  return new Promise((resolve, reject) => expectingEventPromise = {resolve, reject}).catch(e => {
+  await new Promise((resolve, reject) => expectingEventPromise = {resolve, reject}).catch(e => {
     // Throw error here to get the caller in error stack.
     ok(false, e);
   });
 }
 
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]});
   await new Promise(resolve => {
--- a/toolkit/content/tests/widgets/test_videocontrols_error.html
+++ b/toolkit/content/tests/widgets/test_videocontrols_error.html
@@ -25,30 +25,30 @@
   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", resolve);
+      video.addEventListener("loadedmetadata", () => SimpleTest.executeSoon(resolve));
     });
 
     ok(statusOverlay.hidden, "statusOverlay shoud not present without error");
     ok(!statusOverlay.hasAttribute("error"), "statusOverlay should not in error state");
     isnot(statusIcon.getAttribute("type"), "error", "should not show error icon");
   });
 
   add_task(async function invalid_source() {
     const errorType = "errorNoSource";
 
     await new Promise(resolve => {
       video.src = "invalid_source.ogg";
-      video.addEventListener("error", resolve)
+      video.addEventListener("error", () => SimpleTest.executeSoon(resolve));
     });
 
     ok(clickToPlay.hasAttribute("hidden"), `click to play button should hide`);
     ok(!statusOverlay.hidden, `statusOverlay should show when ${errorType}`);
     is(statusOverlay.getAttribute("error"), errorType, `statusOverlay should have correct error state: ${errorType}`);
     is(statusIcon.getAttribute("type"), "error", `should show error icon when ${errorType}`);
   });
 </script>
--- a/toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
+++ b/toolkit/content/tests/widgets/test_videocontrols_video_noaudio.html
@@ -1,66 +1,44 @@
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>Video controls test</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="head.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 <p id="display"></p>
 
 <div id="content">
   <video id="video" controls preload="auto"></video>
 </div>
 
 <pre id="test">
 <script clas="testbody" type="application/javascript">
-  SimpleTest.waitForExplicitFinish();
+  const video = document.getElementById("video");
+  const muteButton = getAnonElementWithinVideoByAttribute(video, "anonid", "muteButton");
+  const volumeStack = getAnonElementWithinVideoByAttribute(video, "anonid", "volumeStack");
 
-  const domUtils = SpecialPowers.Cc["@mozilla.org/inspector/dom-utils;1"].
-    getService(SpecialPowers.Ci.inIDOMUtils);
-  const video = document.getElementById("video");
-  const muteButton = getElementByAttribute("class", "muteButton");
-  const volumeStack = getElementByAttribute("class", "volumeStack");
-  const testCases = [];
+  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));
+    })
+  });
 
-  testCases.push(() => new Promise(resolve => {
+  add_task(async function mute_button_icon() {
     is(muteButton.getAttribute("noAudio"), "true");
     is(muteButton.getAttribute("disabled"), "true");
 
     if (volumeStack) {
       ok(volumeStack.hidden);
     }
-
-    resolve();
-  }));
-
-  function executeTestCases(tasks) {
-    return tasks.reduce((promise, task) => promise.then(task), Promise.resolve());
-  }
-
-  function getElementByAttribute(aName, aValue) {
-    const videoControl = domUtils.getChildrenForNode(video, true)[1];
-
-    return SpecialPowers.wrap(document)
-      .getAnonymousElementByAttribute(videoControl, aName, aValue);
-  }
-
-  function loadedmetadata() {
-    executeTestCases(testCases).then(SimpleTest.finish);
-  }
-
-  function startMediaLoad() {
-    video.src = "video.ogg";
-    video.addEventListener("loadedmetadata", loadedmetadata);
-  }
-
-  function loadevent() {
-    SpecialPowers.pushPrefEnv({"set": [["media.cache_size", 40000]]}, startMediaLoad);
-  }
-
-  window.addEventListener("load", loadevent);
+  });
 </script>
 </pre>
 </body>
 </html>