Bug 1251151 - Make test_autofill_password-only.html work for e10s. r=dolske draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Thu, 25 Feb 2016 15:14:16 -0800
changeset 334726 d3caf8291f52b12fe58a589dd65621922e07adb5
parent 334412 e0c974992fb39a471c92f108f0d20cf6e1e6b87e
child 514981 43de7cff2629974f93bce6be96fadd31bb3269ed
push id11622
push usermozilla@noorenberghe.ca
push dateThu, 25 Feb 2016 23:14:31 +0000
reviewersdolske
bugs1251151
milestone47.0a1
Bug 1251151 - Make test_autofill_password-only.html work for e10s. r=dolske MozReview-Commit-ID: Atwqt9mp6Da
toolkit/components/passwordmgr/test/mochitest.ini
toolkit/components/passwordmgr/test/mochitest/mochitest.ini
toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
toolkit/components/passwordmgr/test/pwmgr_common.js
toolkit/components/passwordmgr/test/test_autofill_password-only.html
--- a/toolkit/components/passwordmgr/test/mochitest.ini
+++ b/toolkit/components/passwordmgr/test/mochitest.ini
@@ -29,17 +29,16 @@ support-files =
   subtst_privbrowsing_4.html
   subtst_prompt_async.html
   auth2/authenticate.sjs
 
 [test_autofill_before_load.html]
 # This test doesn't pass because we can't ensure a cross-platform event that
 # occurs between DOMContentLoaded and Pageload
 skip-if = true
-[test_autofill_password-only.html]
 [test_basic_form.html]
 [test_basic_form_0pw.html]
 [test_basic_form_1pw.html]
 [test_basic_form_1pw_2.html]
 [test_basic_form_2pw_1.html]
 [test_basic_form_2pw_2.html]
 [test_basic_form_3pw_1.html]
 [test_basic_form_autocomplete.html]
--- a/toolkit/components/passwordmgr/test/mochitest/mochitest.ini
+++ b/toolkit/components/passwordmgr/test/mochitest/mochitest.ini
@@ -1,6 +1,7 @@
 [DEFAULT]
 skip-if = buildapp == 'mulet' || buildapp == 'b2g'
 support-files =
   ../pwmgr_common.js
 
+[test_autofill_password-only.html]
 [test_recipe_login_fields.html]
rename from toolkit/components/passwordmgr/test/test_autofill_password-only.html
rename to toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
--- a/toolkit/components/passwordmgr/test/test_autofill_password-only.html
+++ b/toolkit/components/passwordmgr/test/mochitest/test_autofill_password-only.html
@@ -5,52 +5,64 @@
   <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>
-commonInit();
+let pwmgrCommonScript = SpecialPowers.loadChromeScript(PWMGR_COMMON_URL);
+pwmgrCommonScript.sendSyncMessage("setupParent");
+
 SimpleTest.waitForExplicitFinish();
 
-const Ci = SpecialPowers.Ci;
-const Cc = SpecialPowers.Cc;
-pwmgr = Cc["@mozilla.org/login-manager;1"].
-        getService(Ci.nsILoginManager);
+let chromeScript = runFunctionInChrome(function chromeSetup() {
+  const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
+  let pwmgr = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
 
-login1A  = Cc["@mozilla.org/login-manager/loginInfo;1"].
-           createInstance(Ci.nsILoginInfo);
-login1B  = Cc["@mozilla.org/login-manager/loginInfo;1"].
-           createInstance(Ci.nsILoginInfo);
-login2A  = Cc["@mozilla.org/login-manager/loginInfo;1"].
-           createInstance(Ci.nsILoginInfo);
-login2B  = Cc["@mozilla.org/login-manager/loginInfo;1"].
-           createInstance(Ci.nsILoginInfo);
-login2C  = Cc["@mozilla.org/login-manager/loginInfo;1"].
-           createInstance(Ci.nsILoginInfo);
+  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"].
+                 createInstance(Ci.nsILoginInfo);
+  let login2B  = Cc["@mozilla.org/login-manager/loginInfo;1"].
+                 createInstance(Ci.nsILoginInfo);
+  let login2C  = Cc["@mozilla.org/login-manager/loginInfo;1"].
+                 createInstance(Ci.nsILoginInfo);
+
+  login1A.init("http://mochi.test:8888", "http://bug444968-1", null,
+               "testuser1A", "testpass1A", "", "");
+  login1B.init("http://mochi.test:8888", "http://bug444968-1", null,
+               "", "testpass1B", "", "");
 
-login1A.init("http://mochi.test:8888", "http://bug444968-1", null,
-            "testuser1A", "testpass1A", "", "");
-login1B.init("http://mochi.test:8888", "http://bug444968-1", null,
-            "", "testpass1B", "", "");
+  login2A.init("http://mochi.test:8888", "http://bug444968-2", null,
+               "testuser2A", "testpass2A", "", "");
+  login2B.init("http://mochi.test:8888", "http://bug444968-2", null,
+               "", "testpass2B", "", "");
+  login2C.init("http://mochi.test:8888", "http://bug444968-2", null,
+               "testuser2C", "testpass2C", "", "");
 
-login2A.init("http://mochi.test:8888", "http://bug444968-2", null,
-            "testuser2A", "testpass2A", "", "");
-login2B.init("http://mochi.test:8888", "http://bug444968-2", null,
-            "", "testpass2B", "", "");
-login2C.init("http://mochi.test:8888", "http://bug444968-2", null,
-            "testuser2C", "testpass2C", "", "");
+  pwmgr.addLogin(login1A);
+  pwmgr.addLogin(login1B);
+  pwmgr.addLogin(login2A);
+  pwmgr.addLogin(login2B);
+  pwmgr.addLogin(login2C);
 
-pwmgr.addLogin(login1A);
-pwmgr.addLogin(login1B);
-pwmgr.addLogin(login2A);
-pwmgr.addLogin(login2B);
-pwmgr.addLogin(login2C);
+  addMessageListener("removeLogins", function removeLogins() {
+    pwmgr.removeLogin(login1A);
+    pwmgr.removeLogin(login1B);
+    pwmgr.removeLogin(login2A);
+    pwmgr.removeLogin(login2B);
+    pwmgr.removeLogin(login2C);
+  });
+});
+
+registerRunTests();
 </script>
 
 <p id="display"></p>
 <div id="content" style="display: none">
   <!-- first 3 forms have matching user+pass and pass-only logins -->
 
   <!-- user+pass form. -->
   <form id="form1" action="http://bug444968-1">
@@ -114,21 +126,17 @@ function startTest() {
   checkForm(2, "testpass1B");
   checkForm(3, "testuser1A", "testpass1A");
 
   checkUnmodifiedForm(4); // 2 logins match
   checkForm(5, "testpass2B");
   checkForm(6, "testuser2A", "testpass2A");
   checkForm(7, "testuser2C", "testpass2C");
 
-  pwmgr.removeLogin(login1A);
-  pwmgr.removeLogin(login1B);
-  pwmgr.removeLogin(login2A);
-  pwmgr.removeLogin(login2B);
-  pwmgr.removeLogin(login2C);
+  chromeScript.sendSyncMessage("removeLogins");
 
   SimpleTest.finish();
 }
 
 window.addEventListener("runTests", startTest);
 </script>
 </pre>
 </body>
--- a/toolkit/components/passwordmgr/test/pwmgr_common.js
+++ b/toolkit/components/passwordmgr/test/pwmgr_common.js
@@ -1,8 +1,10 @@
+const PWMGR_COMMON_URL = SimpleTest.getTestFileURL("pwmgr_common.js");
+
 /**
  * Returns the element with the specified |name| attribute.
  */
 function $_(formNum, name) {
   var form = document.getElementById("form" + formNum);
   if (!form) {
     logWarning("$_ couldn't find requested form " + formNum);
     return null;
@@ -155,16 +157,20 @@ 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();
+}
+
+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) => {
     var form = document.createElement('form');
     form.id = 'observerforcer';
     var username = document.createElement('input');
@@ -248,16 +254,19 @@ function dumpLogin(label, login) {
   loginText += login.usernameField;
   loginText += " / pfield: ";
   loginText += login.passwordField;
   ok(true, label + loginText);
 }
 
 function getRecipeParent() {
   var { LoginManagerParent } = SpecialPowers.Cu.import("resource://gre/modules/LoginManagerParent.jsm", {});
+  if (!LoginManagerParent.recipeParentPromise) {
+    return null;
+  }
   return LoginManagerParent.recipeParentPromise.then((recipeParent) => {
     return SpecialPowers.wrap(recipeParent);
   });
 }
 
 /**
  * Resolves when a specified number of forms have been processed.
  */
@@ -270,16 +279,30 @@ function promiseFormsProcessed(expectedC
         SpecialPowers.removeObserver(onProcessedForm, "passwordmgr-processed-form");
         resolve(subject, data);
       }
     }
     SpecialPowers.addObserver(onProcessedForm, "passwordmgr-processed-form", false);
   });
 }
 
+/**
+ * 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.
+ * @return {Object} - the return value of loadChromeScript providing message-related methods.
+ *                    @see loadChromeScript in specialpowersAPI.js
+ */
+function runFunctionInChrome(aFunction) {
+  let chromeScript = SpecialPowers.loadChromeScript(aFunction);
+  SimpleTest.registerCleanupFunction(() => {
+    chromeScript.destroy();
+  });
+  return chromeScript;
+}
+
 // 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 = () => {};
 
@@ -299,11 +322,16 @@ 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(() => {
-    getRecipeParent().then(recipeParent => recipeParent.reset());
+    let recipeParent = getRecipeParent();
+    if (!recipeParent) {
+      // No need to reset the recipes if the module wasn't even loaded.
+      return;
+    }
+    recipeParent.then(recipeParent => recipeParent.reset());
   });
 }