Bug 1274919 - part4 : add test. draft
authorAlastor Wu <alwu@mozilla.com>
Fri, 14 Jul 2017 16:19:54 +0800
changeset 608871 9fc5651e8fb518bcb46c51a459812afa3a055e80
parent 608870 94b24ee236da226f8170660e925cb3e14b007bed
child 608872 f0429765b0bfe8f3fadbedd0f4062e39fa069afb
push id68429
push useralwu@mozilla.com
push dateFri, 14 Jul 2017 08:20:09 +0000
bugs1274919
milestone56.0a1
Bug 1274919 - part4 : add test. MozReview-Commit-ID: 5wKkE5tphLl
toolkit/content/tests/browser/browser.ini
toolkit/content/tests/browser/browser_video_decoding_recovery.js
toolkit/content/tests/browser/file_silentAudioTrack.html
--- a/toolkit/content/tests/browser/browser.ini
+++ b/toolkit/content/tests/browser/browser.ini
@@ -83,8 +83,9 @@ tags = audiochannel
 [browser_quickfind_editable.js]
 [browser_save_resend_postdata.js]
 support-files =
   common/mockTransfer.js
   data/post_form_inner.sjs
   data/post_form_outer.sjs
 skip-if = e10s # Bug ?????? - test directly manipulates content (gBrowser.contentDocument.getElementById("postForm").submit();)
 [browser_saveImageURL.js]
+[browser_video_decoding_recovery.js]
new file mode 100644
--- /dev/null
+++ b/toolkit/content/tests/browser/browser_video_decoding_recovery.js
@@ -0,0 +1,117 @@
+const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_silentAudioTrack.html";
+
+async function check_video_decoding_state(isSuspended) {
+  let video = content.document.getElementById("autoplay");
+  if (!video) {
+    ok(false, "Can't get the video element!");
+  }
+
+  let state = isSuspended ? "suspended" : "resumed";
+  let event = isSuspended ? "mozentervideosuspend" : "mozexitvideosuspend";
+  return new Promise(resolve => {
+    video.addEventListener(event, function() {
+      ok(true, `Video decoding is ${state}.`);
+      resolve();
+    }, {once: true});
+  });
+}
+
+function check_browser_video_decoding_recovery_mode(browser, isModeOn) {
+  let state = isModeOn ? "on" : "off";
+  if (browser.videoDecodingRecoveryMode == isModeOn) {
+    ok(true, `video decoding recovery mode is ${state}.`)
+    return true;
+  }
+
+  info(`video decoding recovery mode is not be ${state} now, wait until it changes.`);
+  let msg = isModeOn ?
+    "MozVideoDecodingRecoveryMode:Start" : "MozVideoDecodingRecoveryMode:Stop";
+  return new Promise(resolve => {
+    browser.messageManager.addMessageListener(msg, function() {
+      ok(true, `video decoding recovery mode is ${state}.`)
+      resolve();
+    });
+  });
+}
+
+function get_video_decoding_suspend_promise(browser) {
+  return ContentTask.spawn(browser, true /* suspend */,
+                           check_video_decoding_state);
+}
+
+function get_video_decoding_resume_promise(browser) {
+  return ContentTask.spawn(browser, false /* resume */,
+                           check_video_decoding_state);
+}
+
+/**
+ * Because of bug1029451, we can't receive "mouseover" event correctly when
+ * we disable non-test mouse event. Therefore, we can't synthesize mouse event
+ * to simulate cursor hovering, so we temporarily use a hacky way to resume and
+ * suspend video decoding.
+ */
+function cursor_hover_over_tab_and_resume_video_decoding(browser) {
+  // TODO : simulate cursor hovering over the tab after fixing bug1029451.
+  browser.resumeBackgroundVideo();
+}
+
+function cursor_leave_tab_and_suspend_video_decoding(browser) {
+  // TODO : simulate cursor leaveing the tab after fixing bug1029451.
+  browser.suspendBackgroundVideo();
+}
+
+add_task(async function setup_test_preference() {
+  await SpecialPowers.pushPrefEnv({"set": [
+    ["media.block-autoplay-until-in-foreground", false],
+    ["media.suspend-bkgnd-video.enabled", true],
+    ["media.suspend-bkgnd-video.delay-ms", 0],
+    ["media.suspend-bkgnd-video.resume-when-tab-is-hovering-by-cursor", true],
+    ["media.suspend-bkgnd-video.suspend-after-cursor-leaves-tab-ms", 0]
+  ]});
+});
+
+/**
+ * TODO : add the following user-level tests after fixing bug1029451.
+ * test1 - only affect the unselected tab
+ * test2 - only affect the tab with suspended video
+ */
+add_task(async function resume_and_suspend_background_video_decoding() {
+  info("- open new background tab -");
+  let tab = window.gBrowser.addTab("about:blank");
+  let browser = tab.linkedBrowser;
+  browser.loadURI(PAGE);
+  await BrowserTestUtils.browserLoaded(browser);
+
+  info("- should suspend background video decoding -");
+  await get_video_decoding_suspend_promise(browser);
+  await check_browser_video_decoding_recovery_mode(browser, true /* mode on */);
+
+  info("- when cursor is hovering over the tab, resuming the video decoding -");
+  let promise = get_video_decoding_resume_promise(browser);
+  await cursor_hover_over_tab_and_resume_video_decoding(browser);
+  await promise;
+  await check_browser_video_decoding_recovery_mode(browser, true /* mode on */);
+
+  info("- when cursor leaves the tab, suspending the video decoding -");
+  promise = get_video_decoding_suspend_promise(browser);
+  await cursor_leave_tab_and_suspend_video_decoding(browser);
+  await promise;
+  await check_browser_video_decoding_recovery_mode(browser, true /* mode on */);
+
+  info("- select video's owner tab as foreground tab, should resume video -");
+  promise = get_video_decoding_resume_promise(browser);
+  await BrowserTestUtils.switchTab(window.gBrowser, tab);
+  await promise;
+  await check_browser_video_decoding_recovery_mode(browser, false /* mode off */);
+
+  info("- video's owner tab goes to background again, should suspend video -");
+  promise = get_video_decoding_suspend_promise(browser);
+  let blankTab = await BrowserTestUtils.openNewForegroundTab(window.gBrowser,
+                                                             "about:blank");
+  await promise;
+  await check_browser_video_decoding_recovery_mode(browser, true /* mode on */);
+
+  info("- remove tabs -");
+  await BrowserTestUtils.removeTab(tab);
+  await BrowserTestUtils.removeTab(blankTab);
+});
--- a/toolkit/content/tests/browser/file_silentAudioTrack.html
+++ b/toolkit/content/tests/browser/file_silentAudioTrack.html
@@ -1,18 +1,18 @@
 <!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="autoplay" src="silentAudioTrack.webm"></audio>
+<video id="autoplay" src="silentAudioTrack.webm"></video>
 <script type="text/javascript">
 
 // In linux debug on try server, sometimes the download process would fail, so
 // we can't activate the "auto-play" or playing after receving "oncanplay".
 // Therefore, we just call play here.
-var audio = document.getElementById("autoplay");
-audio.loop = true;
-audio.play();
+var video = document.getElementById("autoplay");
+video.loop = true;
+video.play();
 
 </script>
 </body>