Bug 1257738 - part3 : add test. draft
authorAlastor Wu <alwu@mozilla.com>
Wed, 01 Jun 2016 10:13:47 +0800
changeset 373689 280d161c1b386be777f9f642653454d424fec118
parent 373688 061e8ca103c5f26e1cc95e54bf1f1848fb1e46b7
child 373700 7ff551ce6361578083a3fe5a10df09246110dc94
push id19809
push useralwu@mozilla.com
push dateWed, 01 Jun 2016 02:14:07 +0000
bugs1257738
milestone49.0a1
Bug 1257738 - part3 : add test. MozReview-Commit-ID: 8Probxb7e7G
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_audioCompeting.js
toolkit/content/tests/browser/file_multipleAudio.html
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -1,13 +1,18 @@
 [DEFAULT]
 support-files =
   head.js
   file_contentTitle.html
   audio.ogg
+
+[browser_audioCompeting.js]
+tags = audiochannel
+support-files =
+  file_multipleAudio.html
 [browser_autoscroll_disabled.js]
 [browser_bug295977_autoscroll_overflow.js]
 [browser_bug451286.js]
 [browser_bug594509.js]
 [browser_bug982298.js]
 [browser_bug1198465.js]
 [browser_contentTitle.js]
 [browser_default_image_filename.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_audioCompeting.js
@@ -0,0 +1,114 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_multipleAudio.html";
+
+function* wait_for_tab_playing_event(tab, expectPlaying) {
+  if (tab.soundPlaying == expectPlaying) {
+    ok(true, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
+  } else {
+    yield BrowserTestUtils.waitForEvent(tab, "TabAttrModified", false, (event) => {
+      if (event.detail.changed.indexOf("soundplaying") >= 0) {
+        is(tab.soundPlaying, expectPlaying, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
+        return true;
+      }
+      return false;
+    });
+  }
+}
+
+function play_audio_from_invisible_tab () {
+  return new Promise(resolve => {
+    var autoPlay = content.document.getElementById('autoplay');
+    if (!autoPlay) {
+      ok(false, "Can't get the audio element!");
+    }
+
+    is(autoPlay.paused, true, "Audio in tab 1 was paused by audio competing.");
+    autoPlay.play();
+    autoPlay.onpause = function() {
+      autoPlay.onpause = null;
+      ok(true, "Audio in tab 1 can't playback when other tab is playing in foreground.");
+      resolve();
+    };
+  });
+}
+
+function audio_should_keep_playing_even_go_to_background () {
+  var autoPlay = content.document.getElementById('autoplay');
+  if (!autoPlay) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(autoPlay.paused, false, "Audio in tab 2 is still playing in the background.");
+}
+
+function play_non_autoplay_audio () {
+  return new Promise(resolve => {
+    var autoPlay = content.document.getElementById('autoplay');
+    var nonAutoPlay = content.document.getElementById('nonautoplay');
+    if (!autoPlay || !nonAutoPlay) {
+      ok(false, "Can't get the audio element!");
+    }
+
+    is(nonAutoPlay.paused, true, "Non-autoplay audio isn't started playing yet.");
+    nonAutoPlay.play();
+
+    nonAutoPlay.onplay = function() {
+      nonAutoPlay.onplay = null;
+      is(nonAutoPlay.paused, false, "Start Non-autoplay audio.");
+      is(autoPlay.paused, false, "Autoplay audio is still playing.");
+      resolve();
+    };
+  });
+}
+
+add_task(function* setup_test_preference() {
+  yield new Promise(resolve => {
+    SpecialPowers.pushPrefEnv({"set": [
+      ["dom.audiochannel.audioCompeting", true]
+    ]}, resolve);
+  });
+});
+
+add_task(function* cross_tabs_audio_competing () {
+  info("- open tab 1 in foreground -");
+  let tab1 = yield BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                         "about:blank");
+  tab1.linkedBrowser.loadURI(PAGE);
+  yield wait_for_tab_playing_event(tab1, true);
+
+  info("- open tab 2 in foreground -");
+  let tab2 = yield BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                        "about:blank");
+  tab2.linkedBrowser.loadURI(PAGE);
+  yield wait_for_tab_playing_event(tab1, false);
+
+  info("- open tab 3 in foreground -");
+  let tab3 = yield BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                         "about:blank");
+  yield ContentTask.spawn(tab2.linkedBrowser, null,
+                          audio_should_keep_playing_even_go_to_background);
+
+  info("- play audio from background tab 1 -");
+  yield ContentTask.spawn(tab1.linkedBrowser, null,
+                          play_audio_from_invisible_tab);
+
+  info("- remove tabs -");
+  yield BrowserTestUtils.removeTab(tab1);
+  yield BrowserTestUtils.removeTab(tab2);
+  yield BrowserTestUtils.removeTab(tab3);
+});
+
+add_task(function* within_one_tab_audio_competing () {
+  info("- open tab and play audio1 -");
+  let tab = yield BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                        "about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield wait_for_tab_playing_event(tab, true);
+
+  info("- play audio2 in the same tab -");
+  yield ContentTask.spawn(tab.linkedBrowser, null,
+                          play_non_autoplay_audio);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});
+
--- a/toolkit/content/tests/browser/file_multipleAudio.html
+++ b/toolkit/content/tests/browser/file_multipleAudio.html
@@ -1,7 +1,19 @@
 <!DOCTYPE html>
+<head>
+  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
+  <meta content="utf-8" http-equiv="encoding">
+</head>
 <body>
+<audio id="autoplay" src="audio.ogg"></audio>
+<audio id="nonautoplay" src="audio.ogg"></audio>
 <script type="text/javascript">
+
+// In linux debug on try server, sometimes the download process would fail, so
+// we can't activate the "auto-play" or playing after receving "oncanplay".
+// Therefore, we just call play here.
+var audio = document.getElementById("autoplay");
+audio.loop = true;
+audio.play();
+
 </script>
-<audio id="autoplay" src="audio.ogg" autoplay></audio>
-<audio id="nonautoplay" src="audio.ogg"></audio>
 </body>