Bug 1200487 - Refactor playing and pausing a tab in browser_audioTabIcon.js; r=jaws a=test-only
authorEhsan Akhgari <ehsan@mozilla.com>
Mon, 31 Aug 2015 22:05:03 -0400
changeset 289066 91825878e01aad45013e040a2175a48433bc3e4a
parent 289065 40a77893341e14a1b118ae27ec6735ec74659960
child 289067 c667ca217aef2426ec5596ad9a3cab8c5c4b72d6
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, test-only
bugs1200487
milestone42.0a2
Bug 1200487 - Refactor playing and pausing a tab in browser_audioTabIcon.js; r=jaws a=test-only
browser/base/content/test/general/browser_audioTabIcon.js
--- a/browser/base/content/test/general/browser_audioTabIcon.js
+++ b/browser/base/content/test/general/browser_audioTabIcon.js
@@ -5,16 +5,36 @@ function* wait_for_tab_playing_event(tab
     if (event.detail.changed.indexOf("soundplaying") >= 0) {
       is(tab.hasAttribute("soundplaying"), expectPlaying, "The tab should " + (expectPlaying ? "" : "not ") + "be playing");
       return true;
     }
     return false;
   });
 }
 
+function* play(tab) {
+  let browser = tab.linkedBrowser;
+  yield ContentTask.spawn(browser, {}, function* () {
+    let audio = content.document.querySelector("audio");
+    audio.play();
+  });
+
+  yield wait_for_tab_playing_event(tab, true);
+}
+
+function* pause(tab) {
+  let browser = tab.linkedBrowser;
+  yield ContentTask.spawn(browser, {}, function* () {
+    let audio = content.document.querySelector("audio");
+    audio.pause();
+  });
+
+  yield wait_for_tab_playing_event(tab, false);
+}
+
 function disable_non_test_mouse(disable) {
   let utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
                     .getInterface(Ci.nsIDOMWindowUtils);
   utils.disableNonTestMouseEvents(disable);
 }
 
 function* hover_icon(icon, tooltip) {
   disable_non_test_mouse(true);
@@ -75,22 +95,17 @@ function get_tab_attributes(tab) {
   let {attributes} = JSON.parse(ss.getTabState(tab));
   return attributes;
 }
 
 function* test_playing_icon_on_tab(tab, browser, isPinned) {
   let icon = document.getAnonymousElementByAttribute(tab, "anonid",
                                                      isPinned ? "overlay-icon" : "soundplaying-icon");
 
-  yield ContentTask.spawn(browser, {}, function* () {
-    let audio = content.document.querySelector("audio");
-    audio.play();
-  });
-
-  yield wait_for_tab_playing_event(tab, true);
+  yield play(tab);
 
   yield test_tooltip(icon, "Mute tab");
 
   ok(!("muted" in get_tab_attributes(tab)), "No muted attribute should be persisted");
 
   yield test_mute_tab(tab, icon, true);
 
   ok("muted" in get_tab_attributes(tab), "Muted attribute should be persisted");
@@ -100,21 +115,17 @@ function* test_playing_icon_on_tab(tab, 
   yield test_mute_tab(tab, icon, false);
 
   ok(!("muted" in get_tab_attributes(tab)), "No muted attribute should be persisted");
 
   yield test_tooltip(icon, "Mute tab");
 
   yield test_mute_tab(tab, icon, true);
 
-  yield ContentTask.spawn(browser, {}, function* () {
-    let audio = content.document.querySelector("audio");
-    audio.pause();
-  });
-  yield wait_for_tab_playing_event(tab, false);
+  yield pause(tab);
 
   ok(tab.hasAttribute("muted") &&
      !tab.hasAttribute("soundplaying"), "Tab should still be muted but not playing");
 
   yield test_tooltip(icon, "Unmute tab");
 
   yield test_mute_tab(tab, icon, false);
 
@@ -152,21 +163,17 @@ function* test_swapped_browser(oldTab, n
   yield AudioPlaybackPromise;
 
   ok(newTab.hasAttribute("muted"), "Expected the correct muted attribute on the new tab");
   is(newTab.hasAttribute("soundplaying"), isPlaying, "Expected the correct soundplaying attribute on the new tab");
 }
 
 function* test_browser_swapping(tab, browser) {
   // First, test swapping with a playing but muted tab.
-  yield ContentTask.spawn(browser, {}, function* () {
-    let audio = content.document.querySelector("audio");
-    audio.play();
-  });
-  yield wait_for_tab_playing_event(tab, true);
+  yield play(tab);
 
   let icon = document.getAnonymousElementByAttribute(tab, "anonid",
                                                      "soundplaying-icon");
   yield test_mute_tab(tab, icon, true);
 
   yield BrowserTestUtils.withNewTab({
     gBrowser,
     url: "about:blank",
@@ -174,20 +181,17 @@ function* test_browser_swapping(tab, bro
     yield test_swapped_browser(tab, newBrowser, true)
 
     // FIXME: this is needed to work around bug 1190903.
     yield new Promise(resolve => setTimeout(resolve, 1000));
 
     // Now, test swapping with a muted but not playing tab.
     // Note that the tab remains muted, so we only need to pause playback.
     tab = gBrowser.getTabForBrowser(newBrowser);
-    yield ContentTask.spawn(newBrowser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.pause();
-    });
+    yield pause(tab);
 
     yield BrowserTestUtils.withNewTab({
       gBrowser,
       url: "about:blank",
     }, newBrowser => test_swapped_browser(tab, newBrowser, false));
   });
 }
 
@@ -197,34 +201,25 @@ function* test_click_on_pinned_tab_after
 
     gBrowser.selectedTab = originallySelectedTab;
     isnot(tab, gBrowser.selectedTab, "Sanity check, the tab should not be selected!");
 
     // Steps to reproduce the bug:
     //   Pin the tab.
     gBrowser.pinTab(tab);
 
-    //   Start playbak.
-    yield ContentTask.spawn(browser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.play();
-    });
-
-    //   Wait for playback to start.
-    yield wait_for_tab_playing_event(tab, true);
+    //   Start playback and wait for it to finish.
+    yield play(tab);
 
     //   Mute the tab.
     let icon = document.getAnonymousElementByAttribute(tab, "anonid", "overlay-icon");
     yield test_mute_tab(tab, icon, true);
 
-    //   Stop playback
-    yield ContentTask.spawn(browser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.pause();
-    });
+    // Pause playback and wait for it to finish.
+    yield pause(tab);
 
     // Unmute tab.
     yield test_mute_tab(tab, icon, false);
 
     // Now click on the tab.
     let image = document.getAnonymousElementByAttribute(tab, "anonid", "tab-icon-image");
     EventUtils.synthesizeMouseAtCenter(image, {button: 0});
 
@@ -253,48 +248,33 @@ function* test_mute_keybinding() {
     yield mutedPromise;
   }
   function* test_on_browser(browser) {
     let tab = gBrowser.getTabForBrowser(browser);
 
     // Make sure it's possible to mute before the tab is playing.
     yield test_muting_using_keyboard(tab);
 
-    // Start playback.
-    yield ContentTask.spawn(browser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.play();
-    });
-
-    // Wait for playback to start.
-    yield wait_for_tab_playing_event(tab, true);
+    //   Start playback and wait for it to finish.
+    yield play(tab);
 
     // Make sure it's possible to mute after the tab is playing.
     yield test_muting_using_keyboard(tab);
 
-    // Start playback.
-    yield ContentTask.spawn(browser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.pause();
-    });
+    // Pause playback and wait for it to finish.
+    yield pause(tab);
 
     // Make sure things work if the tab is pinned.
     gBrowser.pinTab(tab);
 
     // Make sure it's possible to mute before the tab is playing.
     yield test_muting_using_keyboard(tab);
 
-    // Start playback.
-    yield ContentTask.spawn(browser, {}, function* () {
-      let audio = content.document.querySelector("audio");
-      audio.play();
-    });
-
-    // Wait for playback to start.
-    yield wait_for_tab_playing_event(tab, true);
+    //   Start playback and wait for it to finish.
+    yield play(tab);
 
     // Make sure it's possible to mute after the tab is playing.
     yield test_muting_using_keyboard(tab);
 
     gBrowser.unpinTab(tab);
   }
 
   yield BrowserTestUtils.withNewTab({