Bug 1131413 (part 1) - add tests for browser-syncui.js. r=adw a=readinglist
authorMark Hammond <mhammond@skippinet.com.au>
Thu, 26 Feb 2015 18:48:11 +1100
changeset 248481 7bf115416751c9ceda69c6d8df6a149b6f9f805a
parent 248480 cdda1e9c607daea0717287db445484bf9593fca5
child 248482 ce90526403d8b465c1cb751727e78890233c27bc
push id7837
push userjwein@mozilla.com
push dateFri, 27 Mar 2015 00:27:16 +0000
treeherdermozilla-aurora@cb0db44ce60e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw, readinglist
bugs1131413
milestone38.0a2
Bug 1131413 (part 1) - add tests for browser-syncui.js. r=adw a=readinglist
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_syncui.js
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -399,16 +399,18 @@ skip-if = buildapp == 'mulet' || e10s # 
 skip-if = e10s
 support-files =
   searchSuggestionUI.html
   searchSuggestionUI.js
 [browser_selectTabAtIndex.js]
 [browser_ssl_error_reports.js]
 [browser_star_hsts.js]
 [browser_subframe_favicons_not_used.js]
+[browser_syncui.js]
+skip-if = e10s # Bug 1137087 - browser_tabopen_reflows.js fails if this was previously run with e10s
 [browser_tabDrop.js]
 skip-if = buildapp == 'mulet' || e10s
 [browser_tabMatchesInAwesomebar.js]
 skip-if = e10s # Bug 1093206 - need to re-enable tests relying on swapFrameLoaders et al for e10s (test calls gBrowser.swapBrowsersAndCloseOther)
 [browser_tabMatchesInAwesomebar_perwindowpb.js]
 skip-if = e10s || os == 'linux' # Bug 1093373, bug 1104755
 [browser_tab_drag_drop_perwindow.js]
 skip-if = buildapp == 'mulet'
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/browser_syncui.js
@@ -0,0 +1,110 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+let {Weave} = Cu.import("resource://services-sync/main.js", {});
+let {Notifications} = Cu.import("resource://services-sync/notifications.js", {});
+
+let stringBundle = Cc["@mozilla.org/intl/stringbundle;1"]
+                   .getService(Ci.nsIStringBundleService)
+                   .createBundle("chrome://weave/locale/services/sync.properties");
+
+function promiseObserver(topic) {
+  return new Promise(resolve => {
+    let obs = (subject, topic, data) => {
+      Services.obs.removeObserver(obs, topic);
+      resolve(subject);
+    }
+    Services.obs.addObserver(obs, topic, false);
+  });
+}
+
+add_task(function* prepare() {
+  let xps = Components.classes["@mozilla.org/weave/service;1"]
+                              .getService(Components.interfaces.nsISupports)
+                              .wrappedJSObject;
+  yield xps.whenLoaded();
+  // mock out the "_needsSetup()" function so we don't short-circuit.
+  let oldNeedsSetup = window.gSyncUI._needsSetup;
+  window.gSyncUI._needsSetup = () => false;
+  registerCleanupFunction(() => {
+    window.gSyncUI._needsSetup = oldNeedsSetup;
+    // this test leaves the tab focused which can cause browser_tabopen_reflows
+    // to fail, so re-select the URLBar.
+    gURLBar.select();
+  });
+});
+
+add_task(function* testProlongedError() {
+  let promiseNotificationAdded = promiseObserver("weave:notification:added");
+  Assert.equal(Notifications.notifications.length, 0, "start with no notifications");
+
+  // Pretend we are in the "prolonged error" state.
+  Weave.Status.sync = Weave.PROLONGED_SYNC_FAILURE;
+  Weave.Status.login = Weave.LOGIN_SUCCEEDED;
+  Services.obs.notifyObservers(null, "weave:ui:sync:error", null);
+
+  let subject = yield promiseNotificationAdded;
+  let notification = subject.wrappedJSObject.object; // sync's observer abstraction is abstract!
+  Assert.equal(notification.title, stringBundle.GetStringFromName("error.sync.title"));
+  Assert.equal(Notifications.notifications.length, 1, "exactly 1 notification");
+
+  // Now pretend we just had a successful sync - the error notification should go away.
+  let promiseNotificationRemoved = promiseObserver("weave:notification:removed");
+  Weave.Status.sync = Weave.STATUS_OK;
+  Services.obs.notifyObservers(null, "weave:ui:sync:finish", null);
+  yield promiseNotificationRemoved;
+  Assert.equal(Notifications.notifications.length, 0, "no notifications left");
+});
+
+add_task(function* testLoginError() {
+  let promiseNotificationAdded = promiseObserver("weave:notification:added");
+  Assert.equal(Notifications.notifications.length, 0, "start with no notifications");
+
+  // Pretend we are in the "prolonged error" state.
+  Weave.Status.sync = Weave.LOGIN_FAILED;
+  Weave.Status.login = Weave.LOGIN_FAILED_LOGIN_REJECTED;
+  Services.obs.notifyObservers(null, "weave:ui:sync:error", null);
+
+  let subject = yield promiseNotificationAdded;
+  let notification = subject.wrappedJSObject.object; // sync's observer abstraction is abstract!
+  Assert.equal(notification.title, stringBundle.GetStringFromName("error.login.title"));
+  Assert.equal(Notifications.notifications.length, 1, "exactly 1 notification");
+  // Now pretend we just had a successful login - the error notification should go away.
+  Weave.Status.sync = Weave.STATUS_OK;
+  Weave.Status.login = Weave.LOGIN_SUCCEEDED;
+  let promiseNotificationRemoved = promiseObserver("weave:notification:removed");
+  Services.obs.notifyObservers(null, "weave:service:login:start", null);
+  Services.obs.notifyObservers(null, "weave:service:login:finish", null);
+  yield promiseNotificationRemoved;
+  Assert.equal(Notifications.notifications.length, 0, "no notifications left");
+});
+
+function testButtonActions(startNotification, endNotification) {
+  let button = document.getElementById("sync-button");
+  Assert.ok(button, "button exists");
+  let panelbutton = document.getElementById("PanelUI-fxa-status");
+  Assert.ok(panelbutton, "panel button exists");
+
+  // pretend a sync is starting.
+  Services.obs.notifyObservers(null, startNotification, null);
+  Assert.equal(button.getAttribute("status"), "active");
+  Assert.equal(panelbutton.getAttribute("syncstatus"), "active");
+
+  Services.obs.notifyObservers(null, endNotification, null);
+  Assert.ok(!button.hasAttribute("status"));
+  Assert.ok(!panelbutton.hasAttribute("syncstatus"));
+}
+
+add_task(function* testButtonActivities() {
+  // add the Sync button to the panel so we can get it!
+  CustomizableUI.addWidgetToArea("sync-button", CustomizableUI.AREA_PANEL);
+  // check the button's functionality
+  yield PanelUI.show();
+  try {
+    testButtonActions("weave:service:login:start", "weave:service:login:finish");
+    testButtonActions("weave:service:sync:start", "weave:ui:sync:finish");
+  } finally {
+    PanelUI.hide();
+    CustomizableUI.removeWidgetFromArea("sync-button");
+  }
+});