Bug 1339324 - add test cases for paused audio. r?ehsan draft
authorJohn Lin <jolin@mozilla.com>
Thu, 23 Feb 2017 17:53:45 +0800
changeset 488550 10ecce66a844bda74e223c03e0c2ae5c08daff09
parent 488549 480ce3bd132830827a2ff3535d7d176bd956da99
child 546771 db6a29f7e0155482f6d1929b2f64f5bd39854df2
push id46573
push userbmo:jolin@mozilla.com
push dateThu, 23 Feb 2017 09:55:46 +0000
reviewersehsan
bugs1339324
milestone54.0a1
Bug 1339324 - add test cases for paused audio. r?ehsan MozReview-Commit-ID: JVoN1XvxTir
dom/base/test/browser_timeout_throttling_with_audio_playback.js
dom/base/test/file_audioLoop.html
dom/base/test/file_audioLoopInIframe.html
dom/base/test/file_webaudioLoop.html
--- a/dom/base/test/browser_timeout_throttling_with_audio_playback.js
+++ b/dom/base/test/browser_timeout_throttling_with_audio_playback.js
@@ -4,52 +4,68 @@ var testURLs = [
   "http://mochi.test:8888/browser/dom/base/test/file_audioLoop.html",
   "http://mochi.test:8888/browser/dom/base/test/file_audioLoopInIframe.html",
   "http://mochi.test:8888/browser/dom/base/test/file_pluginAudio.html",
   "http://mochi.test:8888/browser/dom/base/test/file_webaudioLoop.html",
 ];
 
 // We want to ensure that while audio is being played back, a background tab is
 // treated the same as a foreground tab as far as timeout throttling is concerned.
-// So we use a 10ms minimum timeout value for foreground tabs and a 100,000 second
+// So we use a 10ms minimum timeout value for foreground tabs and a 3 second
 // minimum timeout value for background tabs.  This means that in case the test
 // fails, it will time out in practice, but just for sanity the test condition
 // ensures that the observed timeout delay falls in this range.
 const kMinTimeoutForeground = 10;
-const kMinTimeoutBackground = 100 * 1000 * 1000;
+const kMinTimeoutBackground = 3 * 1000;
 
 Services.scriptloader.loadSubScript(kPluginJS, this);
 
+function* asSoonAsPossible() {
+    return new Promise(resolve => {
+      let before = new Date();
+      content.window.setTimeout(function() {
+        let after = new Date();
+        // Sometimes due to rounding errors, we may get a result of 9ms here, so
+        // let's round up by 1 to protect against such intermittent failures.
+        resolve(after - before + 1);
+      }, 0);
+    });
+}
+
+// Wait for the UI to indicate that audio is stopped.
+function* waitForAudioStopped(tab) {
+  return BrowserTestUtils.waitForEvent(tab, "TabAttrModified", false, () => {
+          return !tab.hasAttribute('soundplaying');
+  });
+}
+
 function* runTest(url) {
   let currentTab = gBrowser.selectedTab;
   let newTab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, kBaseURI);
   let newBrowser = gBrowser.getBrowserForTab(newTab);
 
   // Wait for the UI to indicate that audio is being played back.
   let promise = BrowserTestUtils.waitForAttribute("soundplaying", newTab, "true");
   newBrowser.loadURI(url);
   yield promise;
 
   // Put the tab in the background.
   yield BrowserTestUtils.switchTab(gBrowser, currentTab);
 
-  let timeout = yield ContentTask.spawn(newBrowser, {}, function() {
-    return new Promise(resolve => {
-      let before = new Date();
-      content.window.setTimeout(function() {
-        let after = new Date();
-        // Sometimes due to rounding errors, we may get a result of 9ms here, so
-        // let's round up by 1 to protect against such intermittent failures.
-        resolve(after - before + 1);
-      }, 0);
-    });
-  });
+  let timeout = yield ContentTask.spawn(newBrowser, {}, asSoonAsPossible);
   ok(timeout >= kMinTimeoutForeground &&
      timeout <  kMinTimeoutBackground, `Got the correct timeout (${timeout})`);
 
+  ContentTask.spawn(newBrowser, {}, () => {
+    content.wrappedJSObject.stopAudio();
+  });
+  yield waitForAudioStopped(newTab);
+  timeout = yield ContentTask.spawn(newBrowser, {}, asSoonAsPossible);
+  ok(timeout >= kMinTimeoutBackground, `Got the correct timeout (${timeout})`);
+
   // All done.
   yield BrowserTestUtils.removeTab(newTab);
 }
 
 add_task(function* setup() {
   yield SpecialPowers.pushPrefEnv({"set": [
     ["dom.min_timeout_value", kMinTimeoutForeground],
     ["dom.min_background_timeout_value", kMinTimeoutBackground],
--- a/dom/base/test/file_audioLoop.html
+++ b/dom/base/test/file_audioLoop.html
@@ -1,2 +1,7 @@
 <!DOCTYPE html>
 <audio src="audio.ogg" autoplay="true" loop>
+<script>
+function stopAudio() {
+  document.querySelector('audio').pause();
+}
+</script>
\ No newline at end of file
--- a/dom/base/test/file_audioLoopInIframe.html
+++ b/dom/base/test/file_audioLoopInIframe.html
@@ -1,2 +1,7 @@
 <!DOCTYPE html>
 <iframe src="file_audioLoop.html"></iframe>
+<script>
+function stopAudio() {
+  document.querySelector('iframe').contentWindow.stopAudio();
+}
+</script>
\ No newline at end of file
--- a/dom/base/test/file_webaudioLoop.html
+++ b/dom/base/test/file_webaudioLoop.html
@@ -1,24 +1,25 @@
 <!DOCTYPE html>
 <script>
 var ac = new AudioContext();
+var src = undefined;
 var runningPromise = new Promise(resolve => {
   ac.onstatechange = event => {
     if (ac.state == "running") {
       resolve();
     }
   };
 });
 fetch("audio.ogg").then(response => {
   return response.arrayBuffer();
 }).then(ab => {
   return ac.decodeAudioData(ab);
 }).then(ab => {
-  var src = ac.createBufferSource();
+  src = ac.createBufferSource();
   src.buffer = ab;
   src.loop = true;
   src.loopStart = 0;
   src.loopEnd = ab.duration;
   src.start();
   src.connect(ac.destination);
 });
 
@@ -36,9 +37,15 @@ function resumeAC() {
   });
 }
 
 function closeAC() {
   resumePromise.then(() => {
     ac.close();
   });
 }
+
+function stopAudio() {
+  if (!!src) {
+    src.stop();
+  }
+}
 </script>