Bug 1341062 - Part 1: Wait for audioplayback event to ensure the media element has been resumed. r=baku, a=test-only
authorAlastor Wu <alwu@mozilla.com>
Thu, 02 Mar 2017 17:38:53 +0800
changeset 376568 3b5887490771d13cd89cd23b2deb5dc985229478
parent 376567 8d9897c0157908c266df11a3dec5d08ec5dbbb67
child 376569 d5b97a00c7e41b500878d293c16140304e871482
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku, test-only
bugs1341062
milestone53.0a2
Bug 1341062 - Part 1: Wait for audioplayback event to ensure the media element has been resumed. r=baku, a=test-only Block-stop should be dispatched before audio-playback, so we can check block event first. Also add "loop" attribute for video to avoid getting the wrong pause state. MozReview-Commit-ID: 3WHuJGsZCPn
toolkit/content/tests/browser/browser_block_autoplay_media_pausedAfterPlay.js
toolkit/content/tests/browser/file_blockMedia_shouldPlay.html
toolkit/content/tests/browser/head.js
--- a/toolkit/content/tests/browser/browser_block_autoplay_media_pausedAfterPlay.js
+++ b/toolkit/content/tests/browser/browser_block_autoplay_media_pausedAfterPlay.js
@@ -59,24 +59,25 @@ add_task(function* block_autoplay_media(
   info("- select tab1 as foreground tab, and tab1's media should be paused -");
   yield BrowserTestUtils.switchTab(window.gBrowser, tab1);
   yield ContentTask.spawn(tab1.linkedBrowser, true,
                           check_audio_pause_state);
 
   info("- the tab1 should not be blocked -");
   yield waitForTabBlockEvent(tab1, false);
 
-  info("- select tab2 as foreground tab, and tab2's media should be playing -");
+  info("- select tab2 as foreground tab, and the tab2 should not be blocked -");
   yield BrowserTestUtils.switchTab(window.gBrowser, tab2);
+  yield waitForTabBlockEvent(tab2, false);
+
+  info("- tab2's media should be playing -");
+  yield waitForTabPlayingEvent(tab2, true);
   yield ContentTask.spawn(tab2.linkedBrowser, false,
                           check_audio_pause_state);
 
-  info("- the tab2 should not be blocked -");
-  yield waitForTabBlockEvent(tab2, false);
-
   info("- check tab2's media suspend type -");
   yield ContentTask.spawn(tab2.linkedBrowser, SuspendedType.NONE_SUSPENDED,
                           check_audio_suspended);
 
   info("- remove tabs -");
   yield BrowserTestUtils.removeTab(tab1);
   yield BrowserTestUtils.removeTab(tab2);
 });
--- a/toolkit/content/tests/browser/file_blockMedia_shouldPlay.html
+++ b/toolkit/content/tests/browser/file_blockMedia_shouldPlay.html
@@ -1,15 +1,15 @@
 <!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>
+<audio id="testAudio" src="audio.ogg" loop></audio>
 <script type="text/javascript">
 
 var audio = document.getElementById("testAudio");
 audio.play();
 audio.pause();
 audio.play();
 
 </script>
--- a/toolkit/content/tests/browser/head.js
+++ b/toolkit/content/tests/browser/head.js
@@ -43,8 +43,26 @@ function* waitForTabBlockEvent(tab, expe
       if (event.detail.changed.indexOf("blocked") >= 0) {
         is(tab.soundBlocked, expectBlocked, "The tab should " + (expectBlocked ? "" : "not ") + "be blocked");
         return true;
       }
       return false;
     });
   }
 }
+
+/**
+ * Used to check whether the tab has soundplaying attribute.
+ */
+function* waitForTabPlayingEvent(tab, expectPlaying) {
+  if (tab.soundPlaying == expectPlaying) {
+    ok(true, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
+  } else {
+    info("Playing state doens't match, wait for attributes changes.");
+    yield BrowserTestUtils.waitForEvent(tab, "TabAttrModified", false, (event) => {
+      if (event.detail.changed.indexOf("soundplaying") >= 0) {
+        is(tab.soundPlaying, expectPlaying, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
+        return true;
+      }
+      return false;
+    });
+  }
+}