Bug 1322505 - part3 : add test. r=baku,jaws
authorAlastor Wu <alwu@mozilla.com>
Mon, 23 Jan 2017 14:46:28 +0800
changeset 464951 726ee922483f6e3e12a2921fbcdbe7c5616aafe8
parent 464950 5e90eada67cf0c69eb35530bee6af8bbf0b094db
child 464952 f1f53d0897bfedc2ab269e338d5cc373bd880c69
child 465478 839e34051bf99b49f80893f80b40e4873a251ae1
push id42495
push userbmo:fliu@mozilla.com
push dateMon, 23 Jan 2017 12:03:50 +0000
reviewersbaku, jaws
bugs1322505
milestone53.0a1
Bug 1322505 - part3 : add test. r=baku,jaws MozReview-Commit-ID: 2YnWEO98M2e
browser/base/content/tabbrowser.xml
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_block_autoplay_media_pausedAfterPlay.js
toolkit/content/tests/browser/file_blockMedia_shouldNotPlay.html
toolkit/content/tests/browser/file_blockMedia_shouldPlay.html
toolkit/content/tests/browser/head.js
--- a/browser/base/content/tabbrowser.xml
+++ b/browser/base/content/tabbrowser.xml
@@ -6998,16 +6998,22 @@
       </property>
 
       <property name="soundPlaying" readonly="true">
         <getter>
           return this.getAttribute("soundplaying") == "true";
         </getter>
       </property>
 
+      <property name="soundBlocked" readonly="true">
+        <getter>
+          return this.getAttribute("blocked") == "true";
+        </getter>
+      </property>
+
       <property name="lastAccessed">
         <getter>
           return this._lastAccessed == Infinity ? Date.now() : this._lastAccessed;
         </getter>
       </property>
       <method name="updateLastAccessed">
         <parameter name="aDate"/>
         <body><![CDATA[
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -8,16 +8,21 @@ support-files =
 tags = audiochannel
 support-files =
   file_multipleAudio.html
 [browser_autoscroll_disabled.js]
 [browser_block_autoplay_media.js]
 tags = audiochannel
 support-files =
   file_multipleAudio.html
+[browser_block_autoplay_media_pausedAfterPlay.js]
+ tags = audiochannel
+support-files =
+  file_blockMedia_shouldPlay.html
+  file_blockMedia_shouldNotPlay.html
 [browser_bug295977_autoscroll_overflow.js]
 [browser_bug451286.js]
 skip-if = !e10s
 [browser_bug594509.js]
 [browser_bug982298.js]
 [browser_bug1198465.js]
 [browser_contentTitle.js]
 [browser_crash_previous_frameloader.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_block_autoplay_media_pausedAfterPlay.js
@@ -0,0 +1,82 @@
+const PAGE_SHOULD_PLAY = "https://example.com/browser/toolkit/content/tests/browser/file_blockMedia_shouldPlay.html";
+const PAGE_SHOULD_NOT_PLAY = "https://example.com/browser/toolkit/content/tests/browser/file_blockMedia_shouldNotPlay.html";
+
+var SuspendedType = {
+  NONE_SUSPENDED             : 0,
+  SUSPENDED_PAUSE            : 1,
+  SUSPENDED_BLOCK            : 2,
+  SUSPENDED_PAUSE_DISPOSABLE : 3
+};
+
+function check_audio_suspended(suspendedType) {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(audio.computedSuspended, suspendedType,
+     "The suspeded state of audio is correct.");
+}
+
+function check_audio_pause_state(expectPause) {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(audio.paused, expectPause,
+    "The pause state of audio is corret.")
+}
+
+add_task(function* setup_test_preference() {
+  yield SpecialPowers.pushPrefEnv({"set": [
+    ["media.useAudioChannelService.testing", true],
+    ["media.block-autoplay-until-in-foreground", true]
+  ]});
+});
+
+add_task(function* block_autoplay_media() {
+  info("- open new background tab1, and check tab1's media suspend type -");
+  let tab1 = window.gBrowser.addTab("about:blank");
+  tab1.linkedBrowser.loadURI(PAGE_SHOULD_NOT_PLAY);
+  yield BrowserTestUtils.browserLoaded(tab1.linkedBrowser);
+  yield ContentTask.spawn(tab1.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+
+  info("- the tab1 should not be blocked -");
+  yield waitForTabBlockEvent(tab1, false);
+
+  info("- open new background tab2, and check tab2's media suspend type -");
+  let tab2 = window.gBrowser.addTab("about:blank");
+  tab2.linkedBrowser.loadURI(PAGE_SHOULD_PLAY);
+  yield BrowserTestUtils.browserLoaded(tab2.linkedBrowser);
+  yield ContentTask.spawn(tab2.linkedBrowser, SuspendedType.SUSPENDED_BLOCK,
+                          check_audio_suspended);
+
+  info("- the tab2 should be blocked -");
+  yield waitForTabBlockEvent(tab2, true);
+
+  info("- select tab1 as foreground tab, and tab1's media should be paused -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab1);
+  yield ContentTask.spawn(tab1.linkedBrowser, true,
+                          check_audio_pause_state);
+
+  info("- the tab1 should not be blocked -");
+  yield waitForTabBlockEvent(tab1, false);
+
+  info("- select tab2 as foreground tab, and tab2's media should be playing -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab2);
+  yield ContentTask.spawn(tab2.linkedBrowser, false,
+                          check_audio_pause_state);
+
+  info("- the tab2 should not be blocked -");
+  yield waitForTabBlockEvent(tab2, false);
+
+  info("- check tab2's media suspend type -");
+  yield ContentTask.spawn(tab2.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+
+  info("- remove tabs -");
+  yield BrowserTestUtils.removeTab(tab1);
+  yield BrowserTestUtils.removeTab(tab2);
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_blockMedia_shouldNotPlay.html
@@ -0,0 +1,15 @@
+<!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="testAudio" src="audio.ogg" preload="none"></audio>
+<script type="text/javascript">
+
+var audio = document.getElementById("testAudio");
+audio.play();
+audio.pause();
+
+</script>
+</body>
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_blockMedia_shouldPlay.html
@@ -0,0 +1,16 @@
+<!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="testAudio" src="audio.ogg"></audio>
+<script type="text/javascript">
+
+var audio = document.getElementById("testAudio");
+audio.play();
+audio.pause();
+audio.play();
+
+</script>
+</body>
--- a/toolkit/content/tests/browser/head.js
+++ b/toolkit/content/tests/browser/head.js
@@ -26,8 +26,25 @@ function closeFindbarAndWait(findbar) {
   });
 }
 
 function pushPrefs(...aPrefs) {
   let deferred = Promise.defer();
   SpecialPowers.pushPrefEnv({"set": aPrefs}, deferred.resolve);
   return deferred.promise;
 }
+
+/**
+ * Used to check whether the audio unblocking icon is in the tab.
+ */
+function* waitForTabBlockEvent(tab, expectBlocked) {
+  if (tab.soundBlocked == expectBlocked) {
+    ok(true, "The tab should " + (expectBlocked ? "" : "not ") + "be blocked");
+  } else {
+    yield BrowserTestUtils.waitForEvent(tab, "TabAttrModified", false, (event) => {
+      if (event.detail.changed.indexOf("blocked") >= 0) {
+        is(tab.soundBlocked, expectBlocked, "The tab should " + (expectBlocked ? "" : "not ") + "be blocked");
+        return true;
+      }
+      return false;
+    });
+  }
+}