Bug 1322505 - part3 : add test. r=baku,jaws
☠☠ backed out by f1fef2537a70 ☠ ☠
authorAlastor Wu <alwu@mozilla.com>
Fri, 20 Jan 2017 11:30:25 +0800
changeset 375246 861f01ee0e6e7ece0f78befb91bffed400d575ca
parent 375245 ddf90342d71d320960a41ea5e1e315b086e8471f
child 375247 fc024a9abc9620b61391ad2002a4e6bed78891d5
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku, jaws
bugs1322505
milestone53.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 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
@@ -6987,16 +6987,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"></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;
+    });
+  }
+}
\ No newline at end of file