Bug 1258055 - E10S helpers for mochitest-plain password manager tests not involving UI. r=dolske
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Thu, 24 Mar 2016 23:09:16 -0400
changeset 290273 5585f64b747d73ca4e677936cd528c3393f6df8f
parent 290272 371244335c102f7e8223e522a7ed1ea98c482c3f
child 290274 b903047d9572a456eed53377f12b24b1354c1135
push id18365
push usermozilla@noorenberghe.ca
push dateFri, 25 Mar 2016 13:23:49 +0000
treeherderfx-team@d823452bea13 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske
bugs1258055
milestone48.0a1
Bug 1258055 - E10S helpers for mochitest-plain password manager tests not involving UI. r=dolske * Support changing `selfFilling` in the `setupParent` message since these tests aren't self-filling like the ones already working in e10s. ** Make selfFilling = false work with e10s using a registerRunTests message. * Register a common cleanup function to remove all logins, remove all disabled hosts, and clear the HTTP auth state in the parent. ** Actually check there are no existing logins in commonInit (uncomment code) ** Delete some removeLogin calls at the end of tests now that we removeAllLogins during cleanup * Consolidate loadParentTestFile with runInParent MozReview-Commit-ID: IS5pPMiM12U
toolkit/components/passwordmgr/test/chrome/test_formless_autofill.html
toolkit/components/passwordmgr/test/chrome/test_formless_submit.html
toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
toolkit/components/passwordmgr/test/mochitest/test_recipe_login_fields.html
toolkit/components/passwordmgr/test/pwmgr_common.js
toolkit/components/passwordmgr/test/test_xml_load.html
--- a/toolkit/components/passwordmgr/test/chrome/test_formless_autofill.html
+++ b/toolkit/components/passwordmgr/test/chrome/test_formless_autofill.html
@@ -17,17 +17,17 @@ const LMCBackstagePass = Cu.import("reso
 const { LoginManagerContent, FormLikeFactory } = LMCBackstagePass;
 
 let parentScriptURL = SimpleTest.getTestFileURL("pwmgr_common.js");
 let mm = SpecialPowers.loadChromeScript(parentScriptURL);
 
 document.addEventListener("DOMContentLoaded", () => {
   document.getElementById("loginFrame").addEventListener("load", (evt) => {
     // Tell the parent to setup test logins.
-    mm.sendAsyncMessage("setupParent");
+    mm.sendAsyncMessage("setupParent", { selfFilling: true });
   });
 });
 
 // When the setup is done, load a recipe for this test.
 mm.addMessageListener("doneSetup", function doneSetup() {
   mm.sendAsyncMessage("loadRecipes", {
     siteRecipes: [{
       hosts: ["mochi.test:8888"],
--- a/toolkit/components/passwordmgr/test/chrome/test_formless_submit.html
+++ b/toolkit/components/passwordmgr/test/chrome/test_formless_submit.html
@@ -17,17 +17,17 @@ const LMCBackstagePass = Cu.import("reso
 const { LoginManagerContent, FormLikeFactory } = LMCBackstagePass;
 
 let parentScriptURL = SimpleTest.getTestFileURL("pwmgr_common.js");
 let mm = SpecialPowers.loadChromeScript(parentScriptURL);
 
 document.addEventListener("DOMContentLoaded", () => {
   document.getElementById("loginFrame").addEventListener("load", (evt) => {
     // Tell the parent to setup test logins.
-    mm.sendAsyncMessage("setupParent");
+    mm.sendAsyncMessage("setupParent", { selfFilling: true });
   });
 });
 
 // When the setup is done, load a recipe for this test.
 mm.addMessageListener("doneSetup", function doneSetup() {
   mm.sendAsyncMessage("loadRecipes", {
     siteRecipes: [{
       hosts: ["test1.mochi.test:8888"],
--- a/toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
@@ -5,22 +5,22 @@
   <title>Test password-only forms should prefer a password-only login when present</title>
   <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
   <script type="text/javascript" src="pwmgr_common.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
 </head>
 <body>
 Login Manager test: Bug 444968
 <script>
-let pwmgrCommonScript = loadParentTestFile("pwmgr_common.js");
-pwmgrCommonScript.sendSyncMessage("setupParent");
+let pwmgrCommonScript = runInParent(SimpleTest.getTestFileURL("pwmgr_common.js"));
+pwmgrCommonScript.sendSyncMessage("setupParent", { selfFilling: true });
 
 SimpleTest.waitForExplicitFinish();
 
-let chromeScript = runFunctionInParent(function chromeSetup() {
+let chromeScript = runInParent(function chromeSetup() {
   const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
   let pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
 
   let login1A  = Cc["@mozilla.org/login-manager/loginInfo;1"].
                  createInstance(Ci.nsILoginInfo);
   let login1B  = Cc["@mozilla.org/login-manager/loginInfo;1"].
                  createInstance(Ci.nsILoginInfo);
   let login2A  = Cc["@mozilla.org/login-manager/loginInfo;1"].
--- a/toolkit/components/passwordmgr/test/mochitest/test_recipe_login_fields.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_recipe_login_fields.html
@@ -9,17 +9,17 @@
 <body>
 <script type="application/javascript;version=1.8">
 SimpleTest.waitForExplicitFinish();
 
 const PWMGR_COMMON_URL = SimpleTest.getTestFileURL("pwmgr_common.js");
 let pwmgrCommonScript = SpecialPowers.loadChromeScript(PWMGR_COMMON_URL);
 
 // Tell the parent to setup test logins.
-pwmgrCommonScript.sendAsyncMessage("setupParent");
+pwmgrCommonScript.sendAsyncMessage("setupParent", { selfFilling: true });
 
 // When the setup is done, load a recipe for this test.
 pwmgrCommonScript.addMessageListener("doneSetup", function doneSetup() {
   pwmgrCommonScript.sendAsyncMessage("loadRecipes", {
     siteRecipes: [{
       hosts: ["mochi.test:8888"],
       usernameSelector: "input[name='uname1']",
       passwordSelector: "input[name='pword2']",
--- a/toolkit/components/passwordmgr/test/pwmgr_common.js
+++ b/toolkit/components/passwordmgr/test/pwmgr_common.js
@@ -125,26 +125,22 @@ function doKey(aKey, modifier) {
  * the test can start checking filled-in values. Tests that check observer
  * notifications might be confused by this.
  */
 function commonInit(selfFilling) {
   var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"].
               getService(SpecialPowers.Ci.nsILoginManager);
   ok(pwmgr != null, "Access LoginManager");
 
-
   // Check that initial state has no logins
   var logins = pwmgr.getAllLogins();
-  if (logins.length) {
-    //todo(false, "Warning: wasn't expecting logins to be present.");
-    pwmgr.removeAllLogins();
-  }
+  is(logins.length, 0, "Not expecting logins to be present");
   var disabledHosts = pwmgr.getAllDisabledHosts();
   if (disabledHosts.length) {
-    //todo(false, "Warning: wasn't expecting disabled hosts to be present.");
+    ok(false, "Warning: wasn't expecting disabled hosts to be present.");
     for (var host of disabledHosts)
       pwmgr.setLoginSavingEnabled(host, true);
   }
 
   // Add a login that's used in multiple tests
   var login = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"].
               createInstance(SpecialPowers.Ci.nsILoginInfo);
   login.init("http://mochi.test:8888", "http://mochi.test:8888", null,
@@ -155,17 +151,21 @@ function commonInit(selfFilling) {
   logins = pwmgr.getAllLogins();
   is(logins.length, 1, "Checking for successful init login");
   disabledHosts = pwmgr.getAllDisabledHosts();
   is(disabledHosts.length, 0, "Checking for no disabled hosts");
 
   if (selfFilling)
     return;
 
-  registerRunTests();
+  if (this.sendAsyncMessage) {
+    sendAsyncMessage("registerRunTests");
+  } else {
+    registerRunTests();
+  }
 }
 
 function registerRunTests() {
   // We provide a general mechanism for our tests to know when they can
   // safely run: we add a final form that we know will be filled in, wait
   // for the login manager to tell us that it's filled in and then continue
   // with the rest of the tests.
   window.addEventListener("DOMContentLoaded", (event) => {
@@ -277,51 +277,58 @@ function promiseFormsProcessed(expectedC
         SpecialPowers.removeObserver(onProcessedForm, "passwordmgr-processed-form");
         resolve(subject, data);
       }
     }
     SpecialPowers.addObserver(onProcessedForm, "passwordmgr-processed-form", false);
   });
 }
 
-function loadParentTestFile(aRelativeFilePath) {
-  let fileURL = SimpleTest.getTestFileURL(aRelativeFilePath);
-  let testScript = SpecialPowers.loadChromeScript(fileURL);
-  SimpleTest.registerCleanupFunction(function destroyChromeScript() {
-    testScript.destroy();
-  });
-  return testScript;
-}
-
 /**
  * Run a function synchronously in the parent process and destroy it in the test cleanup function.
- * @param {Function} aFunction - function that will be stringified and run.
+ * @param {Function|String} aFunctionOrURL - either a function that will be stringified and run
+ *                                           or the URL to a JS file.
  * @return {Object} - the return value of loadChromeScript providing message-related methods.
  *                    @see loadChromeScript in specialpowersAPI.js
  */
-function runFunctionInParent(aFunction) {
-  let chromeScript = SpecialPowers.loadChromeScript(aFunction);
+function runInParent(aFunctionOrURL) {
+  let chromeScript = SpecialPowers.loadChromeScript(aFunctionOrURL);
   SimpleTest.registerCleanupFunction(() => {
     chromeScript.destroy();
   });
   return chromeScript;
 }
 
+/**
+ * Run commonInit synchronously in the parent then run the test function after the runTests event.
+ *
+ * @param {Function} aFunction The test function to run
+ */
+function runChecksAfterCommonInit(aFunction = null) {
+  SimpleTest.waitForExplicitFinish();
+  let pwmgrCommonScript = runInParent(SimpleTest.getTestFileURL("pwmgr_common.js"));
+  if (aFunction) {
+    window.addEventListener("runTests", aFunction);
+    pwmgrCommonScript.addMessageListener("registerRunTests", () => registerRunTests());
+  }
+  pwmgrCommonScript.sendSyncMessage("setupParent");
+}
+
 // Code to run when loaded as a chrome script in tests via loadChromeScript
 if (this.addMessageListener) {
   const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
   var SpecialPowers = { Cc, Ci, Cr, Cu, };
   var ok, is;
   // Ignore ok/is in commonInit since they aren't defined in a chrome script.
   ok = is = () => {}; // eslint-disable-line no-native-reassign
 
   Cu.import("resource://gre/modules/Task.jsm");
 
-  addMessageListener("setupParent", () => {
-    commonInit(true);
+  addMessageListener("setupParent", ({selfFilling = false} = {selfFilling: false}) => {
+    commonInit(selfFilling);
     sendAsyncMessage("doneSetup");
   });
 
   addMessageListener("loadRecipes", Task.async(function* loadRecipes(recipes) {
     var { LoginManagerParent } = Cu.import("resource://gre/modules/LoginManagerParent.jsm", {});
     var recipeParent = yield LoginManagerParent.recipeParentPromise;
     yield recipeParent.load(recipes);
     sendAsyncMessage("loadedRecipes", recipes);
@@ -329,16 +336,29 @@ if (this.addMessageListener) {
 
   var globalMM = Cc["@mozilla.org/globalmessagemanager;1"].getService(Ci.nsIMessageListenerManager);
   globalMM.addMessageListener("RemoteLogins:onFormSubmit", function onFormSubmit(message) {
     sendAsyncMessage("formSubmissionProcessed", message.data, message.objects);
   });
 } else {
   // Code to only run in the mochitest pages (not in the chrome script).
   SimpleTest.registerCleanupFunction(() => {
-    let recipeParent = getRecipeParent();
-    if (!recipeParent) {
-      // No need to reset the recipes if the module wasn't even loaded.
-      return;
-    }
-    recipeParent.then(recipeParent => recipeParent.reset());
+    runInParent(function cleanupParent() {
+      const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
+      Cu.import("resource://gre/modules/Services.jsm");
+      Cu.import("resource://gre/modules/LoginManagerParent.jsm");
+
+      // Remove all logins and disabled hosts
+      Services.logins.removeAllLogins();
+
+      let disabledHosts = Services.logins.getAllDisabledHosts();
+      disabledHosts.forEach(host => Services.logins.setLoginSavingEnabled(host, true));
+
+      let authMgr = Cc["@mozilla.org/network/http-auth-manager;1"].
+                    getService(Ci.nsIHttpAuthManager);
+      authMgr.clearAll();
+
+      if (LoginManagerParent._recipeManager) {
+        LoginManagerParent._recipeManager.reset();
+      }
+    });
   });
 }
--- a/toolkit/components/passwordmgr/test/test_xml_load.html
+++ b/toolkit/components/passwordmgr/test/test_xml_load.html
@@ -35,24 +35,16 @@ function initLogins() {
                "xmluser1", "xmlpass1", "", "");
   login2.init("http://mochi.test:8888", null, "xml2",
                "xmluser2", "xmlpass2", "", "");
 
   pwmgr.addLogin(login1);
   pwmgr.addLogin(login2);
 }
 
-function finishTest() {
-  ok(true, "finishTest removing testing logins...");
-  pwmgr.removeLogin(login1);
-  pwmgr.removeLogin(login2);
-
-  SimpleTest.finish();
-}
-
 function handleDialog(doc, testNum) {
   ok(true, "handleDialog running for test " + testNum);
 
   var clickOK = true;
   var userfield = doc.getElementById("loginTextbox");
   var passfield = doc.getElementById("password1Textbox");
   var username = userfield.getAttribute("value");
   var password = passfield.getAttribute("value");
@@ -160,17 +152,17 @@ function doTest() {
         // and making sure the prompt re-focuses the original tab when shown:
         newWin = window.open();
         newWin.focus();
         startCallbackTimer();
         makeRequest("authenticate.sjs?user=xmluser2&pass=xmlpass2&realm=xml2");
         break;
 
     default:
-        finishTest();
+        SimpleTest.finish();
   }
 }
 
 function makeRequest(uri) {
   var xmlDoc = document.implementation.createDocument("", "test", null);
 
   function documentLoaded(e) {
       xmlLoad(xmlDoc);