Bug 1343465 - part2 : add test. r=baku
authorAlastor Wu <alwu@mozilla.com>
Sat, 04 Mar 2017 16:52:21 +0800
changeset 394904 60d90be2e7bc6c6bd961c95fe3feae26ebe011c2
parent 394903 05312b954500198ffde27f4efe4c1d2205e14d93
child 394905 f0989ac3ad80618189d89591d7e78c18e210c358
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1343465
milestone54.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 1343465 - part2 : add test. r=baku MozReview-Commit-ID: HypzfD053xW
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_block_playMediaInMuteTab.js
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -22,16 +22,20 @@ tags = audiochannel
 support-files =
   file_silentAudioTrack.html
   silentAudioTrack.webm
 [browser_block_autoplay_media_pausedAfterPlay.js]
  tags = audiochannel
 support-files =
   file_blockMedia_shouldPlay.html
   file_blockMedia_shouldNotPlay.html
+[browser_block_playMediaInMuteTab.js]
+tags = audiochannel
+support-files =
+  file_nonAutoplayAudio.html
 [browser_block_autoplay_playAfterTabVisible.js]
 tags = audiochannel
 support-files =
   file_nonAutoplayAudio.html
 [browser_bug295977_autoscroll_overflow.js]
 [browser_bug451286.js]
 skip-if = !e10s
 [browser_bug594509.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_block_playMediaInMuteTab.js
@@ -0,0 +1,114 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_nonAutoplayAudio.html";
+
+var SuspendedType = {
+  NONE_SUSPENDED             : 0,
+  SUSPENDED_PAUSE            : 1,
+  SUSPENDED_BLOCK            : 2,
+  SUSPENDED_PAUSE_DISPOSABLE : 3
+};
+
+function wait_for_event(browser, event) {
+  return BrowserTestUtils.waitForEvent(browser, event, false, (event) => {
+    is(event.originalTarget, browser, "Event must be dispatched to correct browser.");
+    return true;
+  });
+}
+
+function check_audio_volume_and_mute(expectedMute) {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  let expectedVolume = expectedMute ? 0.0 : 1.0;
+  is(expectedVolume, audio.computedVolume, "Audio's volume is correct!");
+  is(expectedMute, audio.computedMuted, "Audio's mute state is correct!")
+}
+
+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(expectedPauseState) {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  is(audio.paused, expectedPauseState, "Audio is paused.");
+}
+
+function play_audio() {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  audio.play();
+  ok(audio.paused, "Can't play audio, because the tab was still blocked.");
+}
+
+add_task(function* setup_test_preference() {
+  yield SpecialPowers.pushPrefEnv({"set": [
+    ["media.useAudioChannelService.testing", true],
+    ["media.block-autoplay-until-in-foreground", true]
+  ]});
+});
+
+add_task(function* unblock_icon_should_disapear_after_resume_tab() {
+  info("- open new background tab -");
+  let tab = window.gBrowser.addTab("about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+  info("- audio doesn't be started in beginning -");
+  yield ContentTask.spawn(tab.linkedBrowser, true,
+                          check_audio_pause_state);
+
+  info("- audio shouldn't be muted or blocked -");
+  yield ContentTask.spawn(tab.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+  yield ContentTask.spawn(tab.linkedBrowser, false /* unmute */,
+                          check_audio_volume_and_mute);
+
+  info("- tab shouldn't display unblocking icon -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- mute tab -");
+  tab.linkedBrowser.mute();
+  ok(tab.linkedBrowser.audioMuted, "Audio should be muted now");
+
+  info("- try to start audio in background tab -");
+  yield ContentTask.spawn(tab.linkedBrowser, null,
+                          play_audio);
+
+  info("- audio should be muted and blocked -");
+  yield ContentTask.spawn(tab.linkedBrowser, SuspendedType.SUSPENDED_BLOCK,
+                          check_audio_suspended);
+  yield ContentTask.spawn(tab.linkedBrowser, true /* mute */,
+                          check_audio_volume_and_mute);
+
+  info("- tab should display unblocking icon -");
+  yield waitForTabBlockEvent(tab, true);
+
+  info("- select tab as foreground tab -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab);
+
+  info("- audio shoule be muted, but not be blocked -");
+  yield ContentTask.spawn(tab.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+  yield ContentTask.spawn(tab.linkedBrowser, true /* mute */,
+                          check_audio_volume_and_mute);
+
+  info("- tab should not display unblocking icon -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});