Bug 980182 - more about:accounts tests. r=ttaubert
authorMark Hammond <mhammond@skippinet.com.au>
Mon, 10 Mar 2014 18:33:00 -0700
changeset 172839 7c69522060ebce3c975f39547b825ea7f7e1539f
parent 172838 470a1e4e9ac5b8d75dca28ae559f369994b32813
child 172840 5639010709ef2bfecb8171105fa4df94107db728
push id5574
push usermhammond@skippinet.com.au
push dateTue, 11 Mar 2014 01:34:14 +0000
treeherderfx-team@5639010709ef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert
bugs980182
milestone30.0a1
Bug 980182 - more about:accounts tests. r=ttaubert
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_aboutAccounts.js
browser/base/content/test/general/content_aboutAccounts.js
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -78,16 +78,18 @@ support-files =
   test_no_mcb_on_http_site_font2.html
   test_no_mcb_on_http_site_font2.css
   xul_tooltiptext.xhtml
 
 [browser_URLBarSetURI.js]
 skip-if = (os == "linux" || os == "mac") && debug # bug 970052, bug 970053
 [browser_aboutAccounts.js]
 skip-if = os == "linux" # Bug 958026
+support-files =
+  content_aboutAccounts.js
 [browser_aboutHealthReport.js]
 skip-if = os == "linux" # Bug 924307
 [browser_aboutHome.js]
 [browser_aboutSyncProgress.js]
 [browser_addKeywordSearch.js]
 [browser_alltabslistener.js]
 skip-if = os == "linux" # Intermittent failures, bug 951680
 [browser_backButtonFitts.js]
--- a/browser/base/content/test/general/browser_aboutAccounts.js
+++ b/browser/base/content/test/general/browser_aboutAccounts.js
@@ -4,32 +4,42 @@
 
 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
   "resource://gre/modules/Promise.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Task",
   "resource://gre/modules/Task.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "fxAccounts",
   "resource://gre/modules/FxAccounts.jsm");
 
+const CHROME_BASE = "chrome://mochitests/content/browser/browser/base/content/test/general/";
+// Preference helpers.
+let changedPrefs = new Set();
+
+function setPref(name, value) {
+  changedPrefs.add(name);
+  Services.prefs.setCharPref(name, value);
+}
+
 registerCleanupFunction(function() {
   // Ensure we don't pollute prefs for next tests.
-  Services.prefs.clearUserPref("identity.fxaccounts.remote.uri");
+  for (let name of changedPrefs) {
+    Services.prefs.clearUserPref(name);
+  }
 });
 
 let gTests = [
-
 {
   desc: "Test the remote commands",
-  setup: function ()
+  teardown: () => gBrowser.removeCurrentTab(),
+  run: function* ()
   {
-    Services.prefs.setCharPref("identity.fxaccounts.remote.uri",
-                               "https://example.com/browser/browser/base/content/test/general/accounts_testRemoteCommands.html");
-  },
-  run: function ()
-  {
+    setPref("identity.fxaccounts.remote.uri",
+            "https://example.com/browser/browser/base/content/test/general/accounts_testRemoteCommands.html");
+    yield promiseNewTabLoadEvent("about:accounts");
+
     let deferred = Promise.defer();
 
     let results = 0;
     try {
       let win = gBrowser.contentWindow;
       win.addEventListener("message", function testLoad(e) {
         if (e.data.type == "testResult") {
           ok(e.data.pass, e.data.info);
@@ -45,48 +55,112 @@ let gTests = [
 
     } catch(e) {
       ok(false, "Failed to get all commands");
       deferred.reject();
     }
     return deferred.promise.then(() => fxAccounts.signOut());
   }
 },
+{
+  desc: "Test action=signin",
+  teardown: () => gBrowser.removeCurrentTab(),
+  run: function* ()
+  {
+    const expected_url = "https://example.com/?is_sign_in";
+    setPref("identity.fxaccounts.remote.signin.uri", expected_url);
+    let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signin");
+    is(url, expected_url, "action=signin got the expected URL");
+  }
+},
+{
+  desc: "Test action=signup",
+  teardown: () => gBrowser.removeCurrentTab(),
+  run: function* ()
+  {
+    const expected_url = "https://example.com/?is_sign_up";
+    setPref("identity.fxaccounts.remote.uri", expected_url);
+    let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=signup");
+    is(url, expected_url, "action=signup got the expected URL");
+  },
+},
+{
+  desc: "Test action=reauth",
+  teardown: function* () {
+    gBrowser.removeCurrentTab();
+    yield fxAccounts.signOut();
+  },
+  run: function* ()
+  {
+    const expected_url = "https://example.com/?is_force_auth";
+    setPref("identity.fxaccounts.remote.force_auth.uri", expected_url);
+    let userData = {
+      email: "foo@example.com",
+      uid: "1234@lcip.org",
+      assertion: "foobar",
+      sessionToken: "dead",
+      kA: "beef",
+      kB: "cafe",
+      verified: true
+    };
 
+    yield fxAccounts.setSignedInUser(userData);
+    let [tab, url] = yield promiseNewTabWithIframeLoadEvent("about:accounts?action=reauth");
+    // The current user will be appended to the url
+    let expected = expected_url + "&email=foo%40example.com";
+    is(url, expected, "action=reauth got the expected URL");
+  },
+},
 
 ]; // gTests
 
 function test()
 {
   waitForExplicitFinish();
 
   Task.spawn(function () {
     for (let test of gTests) {
       info(test.desc);
-      test.setup();
-
-      yield promiseNewTabLoadEvent("about:accounts");
-
-      yield test.run();
-
-      gBrowser.removeCurrentTab();
+      try {
+        yield test.run();
+      } finally {
+        yield test.teardown();
+      }
     }
 
     finish();
   });
 }
 
 function promiseNewTabLoadEvent(aUrl)
 {
   let deferred = Promise.defer();
   let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl);
   let browser = tab.linkedBrowser;
+  let mm = browser.messageManager;
 
-  browser.addEventListener("load", function onLoad(event) {
-    let iframe = browser.contentDocument.getElementById("remote");
-    if (iframe && event.target == iframe.contentDocument) {
-      browser.removeEventListener("load", onLoad, true);
-      deferred.resolve();
-    }
-  }, true);
-
+  // give it an e10s-friendly content script to help with our tests.
+  mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true);
+  // and wait for it to tell us about the load.
+  mm.addMessageListener("test:document:load", function onLoad() {
+    mm.removeMessageListener("test:document:load", onLoad);
+    deferred.resolve(tab);
+  });
   return deferred.promise;
 }
+
+// Returns a promise which is resolved with the iframe's URL after a new
+// tab is created and the iframe in that tab loads.
+function promiseNewTabWithIframeLoadEvent(aUrl) {
+  let deferred = Promise.defer();
+  let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl);
+  let browser = tab.linkedBrowser;
+  let mm = browser.messageManager;
+
+  // give it an e10s-friendly content script to help with our tests.
+  mm.loadFrameScript(CHROME_BASE + "content_aboutAccounts.js", true);
+  // and wait for it to tell us about the iframe load.
+  mm.addMessageListener("test:iframe:load", function onFrameLoad(message) {
+    mm.removeMessageListener("test:iframe:load", onFrameLoad);
+    deferred.resolve([tab, message.data.url]);
+  });
+  return deferred.promise;
+}
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/content_aboutAccounts.js
@@ -0,0 +1,20 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// This file is loaded as a "content script" for browser_aboutAccounts tests
+"use strict";
+
+addEventListener("DOMContentLoaded", function domContentLoaded(event) {
+  removeEventListener("DOMContentLoaded", domContentLoaded);
+  sendAsyncMessage("test:document:load");
+  let iframe = content.document.getElementById("remote");
+  iframe.addEventListener("load", function iframeLoaded(event) {
+    if (iframe.contentWindow.location.href == "about:blank" ||
+        event.target != iframe) {
+      return;
+    }
+    iframe.removeEventListener("load", iframeLoaded, true);
+    sendAsyncMessage("test:iframe:load", {url: iframe.getAttribute("src")});
+  }, true);
+});