Bug 1338137 - part3 : add test. r=baku
authorAlastor Wu <alwu@mozilla.com>
Sat, 04 Mar 2017 01:16:40 +0800
changeset 493375 7f8d8f0964ff22d92ffb88abfd96246addd8a6b6
parent 493374 422571b302dab5db408a3fcc89571788646b8d68
child 493376 dc8b32d3d5c68e2ad7a247da875a696b06fa9a1c
push id47743
push userbzbarsky@mozilla.com
push dateFri, 03 Mar 2017 21:15:16 +0000
reviewersbaku
bugs1338137
milestone54.0a1
Bug 1338137 - part3 : add test. r=baku MozReview-Commit-ID: 7lpvIp5IOqV
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_block_autoplay_playAfterTabVisible.js
toolkit/content/tests/browser/file_nonAutoplayAudio.html
--- 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_autoplay_playAfterTabVisible.js]
+tags = audiochannel
+support-files =
+  file_nonAutoplayAudio.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_playAfterTabVisible.js
@@ -0,0 +1,84 @@
+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 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.")
+}
+
+function play_audio() {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  audio.play();
+  return new Promise(resolve => {
+    audio.onplay = function() {
+      audio.onplay = null;
+      ok(true, "Audio starts playing.");
+      resolve();
+    }
+  });
+}
+
+add_task(function* setup_test_preference() {
+  yield SpecialPowers.pushPrefEnv({"set": [
+    ["media.useAudioChannelService.testing", true],
+    ["media.block-autoplay-until-in-foreground", true]
+  ]});
+});
+
+
+/**
+ * This test is used for testing the visible tab which was not resumed yet.
+ * If the tab doesn't have any media component, it won't be resumed even it
+ * has already gone to foreground until we start audio.
+ */
+add_task(function* media_should_be_able_to_play_in_visible_tab() {
+  info("- open new background tab, and check tab's media pause state -");
+  let tab = window.gBrowser.addTab("about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+  yield ContentTask.spawn(tab.linkedBrowser, true,
+                          check_audio_pause_state);
+
+  info("- select tab as foreground tab, and tab's media should still be paused -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab);
+  yield ContentTask.spawn(tab.linkedBrowser, true,
+                          check_audio_pause_state);
+
+  info("- start audio in tab -");
+  yield ContentTask.spawn(tab.linkedBrowser, null,
+                          play_audio);
+
+  info("- audio should be playing -");
+  yield ContentTask.spawn(tab.linkedBrowser, false,
+                          check_audio_pause_state);
+  yield ContentTask.spawn(tab.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_nonAutoplayAudio.html
@@ -0,0 +1,7 @@
+<!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>
\ No newline at end of file