Bug 1347758 - part5 : add tests. draft
authorAlastor Wu <alwu@mozilla.com>
Fri, 24 Mar 2017 17:35:01 +0800
changeset 504503 b114bfe758152f983468564e4814995005dab605
parent 504487 404e60a081f5fb251a33855a5db4a07fdcb91a13
child 504504 3ba3095ff5ef1b52602d7d80fa7eaf355c78467d
push id50810
push useralwu@mozilla.com
push dateFri, 24 Mar 2017 09:38:06 +0000
bugs1347758
milestone55.0a1
Bug 1347758 - part5 : add tests. Add tests for non-in-tree media element, web-audio and plug-in. MozReview-Commit-ID: 2BMzUHPjKWX
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_block_notInTreeAudio.js
toolkit/content/tests/browser/browser_block_plugIn.js
toolkit/content/tests/browser/browser_block_webAudio.js
toolkit/content/tests/browser/file_nonAutoplayAudio.html
toolkit/content/tests/browser/file_plugIn.html
toolkit/content/tests/browser/file_webAudio.html
toolkit/content/tests/browser/head.js
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -7,34 +7,42 @@ support-files =
   file_mediaPlayback.html
   file_mediaPlayback2.html
   file_mediaPlaybackFrame.html
   file_mediaPlaybackFrame2.html
   file_multipleAudio.html
   file_multiplePlayingAudio.html
   file_nonAutoplayAudio.html
   file_silentAudioTrack.html
+  file_webAudio.html
+  file_plugIn.html
   audio.ogg
   silentAudioTrack.webm
 
 [browser_audioCompeting.js]
 tags = audiochannel
 [browser_audioCompeting_onlyForActiveAgent.js]
 tags = audiochannel
 [browser_autoscroll_disabled.js]
 [browser_block_autoplay_media.js]
 tags = audiochannel
 [browser_block_silentAudioTrack_media.js]
 tags = audiochannel
 [browser_block_autoplay_media_pausedAfterPlay.js]
  tags = audiochannel
+[browser_block_notInTreeAudio.js]
+  tags = audiochannel
 [browser_block_playMediaInMuteTab.js]
 tags = audiochannel
 [browser_block_autoplay_playAfterTabVisible.js]
 tags = audiochannel
+[browser_block_plugIn.js]
+tags = audiochannel
+[browser_block_webAudio.js]
+tags = audiochannel
 [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_notInTreeAudio.js
@@ -0,0 +1,87 @@
+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_not_in_tree_audio() {
+  var audio = content.document.getElementById("testAudio");
+  if (!audio) {
+    ok(false, "Can't get the audio element!");
+  }
+
+  content.document.body.removeChild(audio);
+
+  // Add timeout to ensure the audio is removed from DOM tree.
+  setTimeout(function() {
+    info("Prepare to start playing audio.");
+    audio.play();
+  }, 1000);
+}
+
+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_not_in_tree_media() {
+  info("- open new background tab -");
+  let tab = window.gBrowser.addTab("about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+  info("- tab should not be blocked -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- check audio's suspend state -");
+  yield ContentTask.spawn(tab.linkedBrowser, SuspendedType.NONE_SUSPENDED,
+                          check_audio_suspended);
+
+  info("- check audio's playing state -");
+  yield ContentTask.spawn(tab.linkedBrowser, true,
+                          check_audio_pause_state);
+
+  info("- playing audio -");
+  yield ContentTask.spawn(tab.linkedBrowser, null,
+                          play_not_in_tree_audio);
+
+  info("- tab should be blocked -");
+  yield waitForTabBlockEvent(tab, true);
+
+  info("- switch tab -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab);
+
+  info("- tab should be resumed -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- tab should be audible -");
+  yield waitForTabPlayingEvent(tab, true);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_block_plugIn.js
@@ -0,0 +1,31 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_plugIn.html";
+
+add_task(function* setup_test_preference() {
+  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Test Plug-in");
+  yield SpecialPowers.pushPrefEnv({"set": [
+    ["media.useAudioChannelService.testing", true],
+    ["media.block-autoplay-until-in-foreground", true]
+  ]});
+});
+
+add_task(function* block_plug_in() {
+  info("- open new background tab -");
+  let tab = window.gBrowser.addTab("about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+  info("- tab should be blocked -");
+  yield waitForTabBlockEvent(tab, true);
+
+  info("- switch tab -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab);
+
+  info("- tab should be resumed -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- tab should be audible -");
+  yield waitForTabPlayingEvent(tab, true);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_block_webAudio.js
@@ -0,0 +1,30 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_webAudio.html";
+
+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_web_audio() {
+  info("- open new background tab -");
+  let tab = window.gBrowser.addTab("about:blank");
+  tab.linkedBrowser.loadURI(PAGE);
+  yield BrowserTestUtils.browserLoaded(tab.linkedBrowser);
+
+  info("- tab should be blocked -");
+  yield waitForTabBlockEvent(tab, true);
+
+  info("- switch tab -");
+  yield BrowserTestUtils.switchTab(window.gBrowser, tab);
+
+  info("- tab should be resumed -");
+  yield waitForTabBlockEvent(tab, false);
+
+  info("- tab should be audible -");
+  yield waitForTabPlayingEvent(tab, true);
+
+  info("- remove tab -");
+  yield BrowserTestUtils.removeTab(tab);
+});
--- a/toolkit/content/tests/browser/file_nonAutoplayAudio.html
+++ b/toolkit/content/tests/browser/file_nonAutoplayAudio.html
@@ -1,7 +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
+<audio id="testAudio" src="audio.ogg" loop></audio>
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_plugIn.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<embed type="application/x-test" width="200" height="200"></embed>
+<script>
+var plugin = document.querySelector("embed");
+onload = function() {
+  plugin.startAudioPlayback();
+};
+</script>
\ No newline at end of file
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/file_webAudio.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<head>
+  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
+  <meta content="utf-8" http-equiv="encoding">
+</head>
+<script type="text/javascript">
+  var ac = new AudioContext();
+  var dest = ac.destination;
+  var osc = ac.createOscillator();
+  osc.connect(dest);
+  osc.start();
+</script>
+<body>
+</body>
\ No newline at end of file
--- a/toolkit/content/tests/browser/head.js
+++ b/toolkit/content/tests/browser/head.js
@@ -62,8 +62,39 @@ function* waitForTabPlayingEvent(tab, ex
       if (event.detail.changed.indexOf("soundplaying") >= 0) {
         is(tab.soundPlaying, expectPlaying, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
         return true;
       }
       return false;
     });
   }
 }
+
+function getTestPlugin(pluginName) {
+  var ph = SpecialPowers.Cc["@mozilla.org/plugin/host;1"]
+                                 .getService(SpecialPowers.Ci.nsIPluginHost);
+  var tags = ph.getPluginTags();
+  var name = pluginName || "Test Plug-in";
+  for (var tag of tags) {
+    if (tag.name == name) {
+      return tag;
+    }
+  }
+
+  ok(false, "Could not find plugin tag with plugin name '" + name + "'");
+  return null;
+}
+
+function setTestPluginEnabledState(newEnabledState, pluginName) {
+  var oldEnabledState = SpecialPowers.setTestPluginEnabledState(newEnabledState, pluginName);
+  if (!oldEnabledState) {
+    return;
+  }
+  var plugin = getTestPlugin(pluginName);
+  while (plugin.enabledState != newEnabledState) {
+    // Run a nested event loop to wait for the preference change to
+    // propagate to the child. Yuck!
+    SpecialPowers.Services.tm.currentThread.processNextEvent(true);
+  }
+  SimpleTest.registerCleanupFunction(function() {
+    SpecialPowers.setTestPluginEnabledState(oldEnabledState, pluginName);
+  });
+}