Bug 1513733 - part3 : add test. r=karlt
☠☠ backed out by 6a89226d8c15 ☠ ☠
authoralwu <alwu@mozilla.com>
Wed, 02 Jan 2019 16:33:51 +0000
changeset 452334 5ce7c992bd81c2426c5be3c3be57e7dfa72fdd9c
parent 452333 7ab6eb45e6b8ca96d5bc890bfc96af96cb6e0ded
child 452335 b63d0e63975e1faaea2d94b65592b65e13955ff4
push id75402
push useralwu@mozilla.com
push dateWed, 02 Jan 2019 23:26:23 +0000
treeherderautoland@5ce7c992bd81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt
bugs1513733
milestone66.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 1513733 - part3 : add test. r=karlt Differential Revision: https://phabricator.services.mozilla.com/D14913
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_autoplay_policy_web_audio_mediaElementAudioSourceNode.js
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -50,16 +50,20 @@ support-files =
 [browser_autoplay_policy_user_gestures.js]
 support-files =
   gizmo.mp4
   file_video.html
 [browser_autoplay_policy_touchScroll.js]
 [browser_autoplay_policy_web_audio.js]
 support-files =
   file_empty.html
+[browser_autoplay_policy_web_audio_mediaElementAudioSourceNode.js]
+support-files =
+  file_video.html
+  gizmo.mp4
 [browser_autoplay_policy_webRTC_permission.js]
 support-files =
   file_empty.html
   gizmo.mp4
 [browser_autoplay_videoDocument.js]
 [browser_autoscroll_disabled.js]
 skip-if = true # Bug 1312652
 [browser_block_autoplay_media.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_autoplay_policy_web_audio_mediaElementAudioSourceNode.js
@@ -0,0 +1,100 @@
+/**
+ * This test is used to ensure blocked AudioContext would be resumed when the
+ * source media element of MediaElementAudioSouceNode which has been created and
+ * connected to destinationnode has started.
+ */
+"use strict";
+
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_video.html";
+
+function setup_test_preference() {
+  return SpecialPowers.pushPrefEnv({"set": [
+    ["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.BLOCKED],
+    ["media.autoplay.enabled.user-gestures-needed", true],
+    ["media.autoplay.block-webaudio", true],
+    ["media.autoplay.block-event.enabled", true],
+  ]});
+}
+
+function createAudioContext() {
+  content.ac = new content.AudioContext();
+  const ac = content.ac;
+
+  ac.allowedToStart = new Promise(resolve => {
+    ac.addEventListener("statechange", function() {
+      if (ac.state === "running") {
+        resolve();
+      }
+    }, {once: true});
+  });
+
+  ac.notAllowedToStart = new Promise(resolve => {
+    ac.addEventListener("blocked", function() {
+      resolve();
+    }, {once: true});
+  });
+}
+
+async function checkIfAudioContextIsAllowedToStart(isAllowedToStart) {
+  const ac = content.ac;
+  if (isAllowedToStart) {
+    await ac.allowedToStart;
+    ok(ac.state === "running", `AudioContext is running.`);
+  } else {
+    await ac.notAllowedToStart;
+    ok(ac.state === "suspended", `AudioContext is not started yet.`);
+  }
+}
+
+async function createMediaElementSourceNode() {
+  const ac = content.ac;
+  const video = content.document.getElementById("v");
+  if (!video) {
+    ok(false, `can not get video element`);
+  }
+  let source = ac.createMediaElementSource(video);
+  source.connect(ac.destination);
+}
+
+async function playVideo() {
+  const video = content.document.getElementById("v");
+  if (!video) {
+    ok(false, `can not get video element`);
+  }
+  // simulate user gesture in order to start video.
+  content.document.notifyUserGestureActivation();
+  ok(await video.play().then(() => true, () => false), `video started playing`);
+}
+
+async function testResumeAudioContextWhenMediaElementSourceStarted() {
+  const tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser, PAGE);
+  const browser = tab.linkedBrowser;
+
+  info(`- create audio context -`);
+  loadFrameScript(browser, createAudioContext);
+
+  info(`- check AudioContext status -`);
+  let isAllowedToStart = false;
+  await ContentTask.spawn(browser, isAllowedToStart,
+                          checkIfAudioContextIsAllowedToStart);
+
+  info(`- create and start MediaElementAudioSourceNode -`);
+  await ContentTask.spawn(browser, null, createMediaElementSourceNode);
+  await ContentTask.spawn(browser, null, playVideo);
+
+  info(`- AudioContext should be resumed after MediaElementAudioSourceNode started -`);
+  isAllowedToStart = true;
+  await ContentTask.spawn(browser, isAllowedToStart,
+                          checkIfAudioContextIsAllowedToStart);
+
+  info(`- remove tab -`);
+  await BrowserTestUtils.removeTab(tab);
+}
+
+add_task(async function start_tests() {
+  info(`- setup test preference -`);
+  await setup_test_preference();
+
+  info(`- start 'testResumeAudioContextWhenMediaElementSourceStarted' -`);
+  await testResumeAudioContextWhenMediaElementSourceStarted();
+});