Bug 1421518 - add user-gestures test for the autoplay policy. r=jwwang
authorAlastor Wu <alwu@mozilla.com>
Tue, 05 Dec 2017 10:50:56 +0800
changeset 394982 213ae1ab70d470365ab5ee3a4778c225153074cb
parent 394981 b2db8b6d26d0828c6db93237e7050756ac0907be
child 394983 ce32d481dd08abaadfbe2712aef9b828d5ec7eb2
push id97987
push usernerli@mozilla.com
push dateTue, 05 Dec 2017 13:52:50 +0000
treeherdermozilla-inbound@8842dba7396b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwwang
bugs1421518
milestone59.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 1421518 - add user-gestures test for the autoplay policy. r=jwwang Mouse click or keyboard press are the events which could activate the page and allow the page to autoplay. Other events are not. MozReview-Commit-ID: FjhcBSH0mCX
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_autoplay_policy_user_gestures.js
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -27,16 +27,17 @@ support-files =
   doggy.png
   firebird.png
 
 [browser_audioCompeting.js]
 tags = audiochannel
 [browser_audioCompeting_onlyForActiveAgent.js]
 tags = audiochannel
 [browser_autoplay_policy_play_twice.js]
+[browser_autoplay_policy_user_gestures.js]
 [browser_autoscroll_disabled.js]
 [browser_block_autoplay_media.js]
 tags = audiochannel
 [browser_block_autoplay_media_pausedAfterPlay.js]
  tags = audiochannel
 [browser_block_autoplay_playAfterTabVisible.js]
 tags = audiochannel
 [browser_block_multipleMedia.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_autoplay_policy_user_gestures.js
@@ -0,0 +1,103 @@
+const FILE = "https://example.com/browser/toolkit/content/tests/browser/gizmo.mp4";
+
+var UserGestures = {
+  MOUSE_CLICK: "mouse-click",
+  MOUSE_MOVE: "mouse-move",
+  KEYBOARD_PRESS: "keyboard-press"
+};
+
+var UserGestureTests = [
+  {type: UserGestures.MOUSE_CLICK, isActivationGesture: true},
+  {type: UserGestures.MOUSE_MOVE, isActivationGesture: false},
+  {type: UserGestures.KEYBOARD_PRESS, isActivationGesture: true}
+];
+
+function setup_test_preference() {
+  return SpecialPowers.pushPrefEnv({"set": [
+    ["media.autoplay.enabled", false],
+    ["media.autoplay.enabled.user-gestures-needed", true]
+  ]});
+}
+
+function simulateUserGesture(gesture, targetBrowser) {
+  let targetElement = targetBrowser.contentDocument.documentElement;
+  info(`- simulate ${gesture.type} event -`);
+  switch (gesture.type) {
+    case UserGestures.MOUSE_CLICK:
+      return BrowserTestUtils.synthesizeMouseAtCenter(targetElement, {button: 0},
+                                                      targetBrowser);
+    case UserGestures.MOUSE_MOVE:
+      return BrowserTestUtils.synthesizeMouseAtCenter(targetElement, {type: "mousemove"},
+                                                      targetBrowser);
+    case UserGestures.KEYBOARD_PRESS:
+      return BrowserTestUtils.sendChar("a", targetBrowser);
+    default:
+      ok(false, "undefined user gesture");
+      return false;
+  }
+}
+
+async function test_play_without_user_gesture() {
+  info("- open new tab -");
+  let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                        "about:blank");
+  info("- create autoplay video -");
+  let document = tab.linkedBrowser.contentDocument;
+  let video = document.createElement("video");
+  video.src = FILE;
+  video.autoplay = true;
+  let canplayPromise = once(video, "canplaythrough");
+  document.body.appendChild(video);
+
+  info("- can't autoplay without user activation -");
+  await canplayPromise;
+  ok(video.paused, "video can't start without user input.");
+
+  info("- call play() without user activation -");
+  await video.play().catch(function() {
+      ok(video.paused, "video can't start play without user input.");
+  });
+
+  info("- remove tab -");
+  await BrowserTestUtils.removeTab(tab);
+}
+
+async function test_play_with_user_gesture(gesture) {
+  info("- open new tab -");
+  let tab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                        "about:blank");
+  info("- create autoplay video -");
+  let document = tab.linkedBrowser.contentDocument;
+  let video = document.createElement("video");
+  video.src = FILE;
+  document.body.appendChild(video);
+
+  info("- simulate user gesture -");
+  await simulateUserGesture(gesture, tab.linkedBrowser);
+
+  info("- call play() -");
+  try {
+    await video.play();
+    ok(gesture.isActivationGesture, "user gesture can activate the page");
+    ok(!video.paused, "video starts playing.");
+  } catch (e) {
+    ok(!gesture.isActivationGesture, "user gesture can not activate the page");
+    ok(video.paused, "video can not start playing.");
+  }
+
+  info("- remove tab -");
+  await BrowserTestUtils.removeTab(tab);
+}
+
+add_task(async function start_test() {
+  info("- setup test preference -");
+  await setup_test_preference();
+
+  info("- test play when page doesn't be activated -");
+  await test_play_without_user_gesture();
+
+  info("- test play after page got user gesture -");
+  for (let idx = 0; idx < UserGestureTests.length; idx++) {
+    await test_play_with_user_gesture(UserGestureTests[idx]);
+  }
+});