Bug 1341062 - part1 : wait for audioplayback event to ensure the media element has been resumed. draft
authorAlastor Wu <alwu@mozilla.com>
Thu, 02 Mar 2017 17:38:53 +0800
changeset 491826 506068a33fa0d55b848c3dcc1eea11e7b57410c7
parent 490433 1bc2ad020aee2830e0a7941f10958dbec108c254
child 491827 1c7b1070710b18f1e0c55b8c4715682d3296491c
push id47431
push useralwu@mozilla.com
push dateThu, 02 Mar 2017 09:39:24 +0000
bugs1341062
milestone54.0a1
Bug 1341062 - part1 : wait for audioplayback event to ensure the media element has been resumed. 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
@@ -44,8 +44,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;
+    });
+  }
+}