Bug 1135045: add tests to confirm that the tab-sharing infoBar is working as expected under various conditions. r=Standard8, a=lsblakk
authorMike de Boer <mdeboer@mozilla.com>
Thu, 12 Mar 2015 15:13:40 +0100
changeset 248124 b3d399af0d4c6137d8dd3b12a233ef1db6180e55
parent 248123 f4c0e3fd326a519f08cd6ccf21c662e32f8cd065
child 248125 0b78e34a3347962cb388c4198154a282ec03b5ec
push id7762
push usermdeboer@mozilla.com
push dateMon, 16 Mar 2015 15:07:09 +0000
treeherdermozilla-aurora@9b59f3a2743d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8, lsblakk
bugs1135045
milestone38.0a2
Bug 1135045: add tests to confirm that the tab-sharing infoBar is working as expected under various conditions. r=Standard8, a=lsblakk
browser/components/loop/test/mochitest/browser_mozLoop_sharingListeners.js
--- a/browser/components/loop/test/mochitest/browser_mozLoop_sharingListeners.js
+++ b/browser/components/loop/test/mochitest/browser_mozLoop_sharingListeners.js
@@ -32,17 +32,17 @@ function promiseWindowIdReceivedOnAdd(ha
   return new Promise(resolve => {
     handler.resolve = resolve;
     gMozLoopAPI.addBrowserSharingListener(handler.listener);
   });
 };
 
 let createdTabs = [];
 
-function promiseWindowIdReceivedNewTab(handlers) {
+function promiseWindowIdReceivedNewTab(handlers = []) {
   let promiseHandlers = [];
 
   handlers.forEach(handler => {
     promiseHandlers.push(new Promise(resolve => {
       handler.resolve = resolve;
     }));
   });
 
@@ -97,29 +97,95 @@ add_task(function* test_multipleListener
   Assert.equal(initialWindowId0, initialWindowId1, "window ids should be the same");
 
   // Check that a new tab updates the window id.
   yield promiseWindowIdReceivedNewTab(handlers);
 
   let newWindowId0 = handlers[0].windowId;
   let newWindowId1 = handlers[1].windowId;
 
+  Assert.ok(newWindowId0, "windowId should not be null anymore");
   Assert.equal(newWindowId0, newWindowId1, "Listeners should have the same windowId");
   Assert.notEqual(initialWindowId0, newWindowId0, "Tab contentWindow IDs shouldn't be the same");
 
   // Now remove the first listener.
   gMozLoopAPI.removeBrowserSharingListener(handlers[0].listener);
 
   // Check that a new tab updates the window id.
   yield promiseWindowIdReceivedNewTab([handlers[1]]);
 
   let nextWindowId0 = handlers[0].windowId;
   let nextWindowId1 = handlers[1].windowId;
 
+  Assert.ok(nextWindowId0, "windowId should not be null anymore");
   Assert.equal(newWindowId0, nextWindowId0, "First listener shouldn't have updated");
   Assert.notEqual(newWindowId1, nextWindowId1, "Second listener should have updated");
 
   // Cleanup.
   gMozLoopAPI.removeBrowserSharingListener(handlers[1].listener);
 
   removeTabs();
 });
 
+add_task(function* test_infoBar() {
+  const kNSXUL = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
+  const kBrowserSharingNotificationId = "loop-sharing-notification";
+  const kPrefBrowserSharingInfoBar = "loop.browserSharing.showInfoBar";
+
+  Services.prefs.setBoolPref(kPrefBrowserSharingInfoBar, true);
+
+  // First we add two tabs.
+  yield promiseWindowIdReceivedNewTab();
+  yield promiseWindowIdReceivedNewTab();
+  Assert.strictEqual(gBrowser.selectedTab, createdTabs[1],
+    "The second tab created should be selected now");
+
+  // Add one sharing listener, which should show the infobar.
+  yield promiseWindowIdReceivedOnAdd(handlers[0]);
+
+  let getInfoBar = function() {
+    let box = gBrowser.getNotificationBox(gBrowser.selectedTab.linkedBrowser);
+    return box.getNotificationWithValue(kBrowserSharingNotificationId);
+  };
+
+  let testBarProps = function() {
+    let bar = getInfoBar();
+
+    // Start with some basic assertions for the bar.
+    Assert.ok(bar, "The notification bar should be visible");
+    Assert.strictEqual(bar.hidden, false, "Again, the notification bar should be visible");
+
+    let button = bar.querySelector(".notification-button");
+    Assert.ok(button, "There should be a button present");
+    Assert.strictEqual(button.type, "menu-button", "We're expecting a menu-button");
+    Assert.strictEqual(button.getAttribute("anchor"), "dropmarker",
+      "The popup should be opening anchored to the dropmarker");
+    Assert.strictEqual(button.getElementsByTagNameNS(kNSXUL, "menupopup").length, 1,
+      "There should be a popup attached to the button");
+  }
+
+  testBarProps();
+
+  // When we switch tabs, the infobar should move along with it. We use `selectedIndex`
+  // here, because that's the setter that triggers the 'select' event. This event
+  // is what LoopUI listens to and moves the infobar.
+  gBrowser.selectedIndex = Array.indexOf(gBrowser.tabs, createdTabs[0]);
+
+  // We now know that the second tab is selected and should be displaying the
+  // infobar.
+  testBarProps();
+
+  // Test hiding the infoBar.
+  getInfoBar().querySelector(".notification-button")
+              .getElementsByTagNameNS(kNSXUL, "menuitem")[0].click();
+  Assert.equal(getInfoBar(), null, "The notification should be hidden now");
+  Assert.strictEqual(Services.prefs.getBoolPref(kPrefBrowserSharingInfoBar), false,
+    "The pref should be set to false when the menu item is clicked");
+
+  gBrowser.selectedIndex = Array.indexOf(gBrowser.tabs, createdTabs[1]);
+
+  Assert.equal(getInfoBar(), null, "The notification should still be hidden");
+
+  // Cleanup.
+  gMozLoopAPI.removeBrowserSharingListener(handlers[0].listener);
+  removeTabs();
+  Services.prefs.clearUserPref(kPrefBrowserSharingInfoBar);
+});