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
--- 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,23 +125,19 @@ 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.");
for (var host of disabledHosts)
pwmgr.setLoginSavingEnabled(host, true);
}
// Add a login that's used in multiple tests
@@ -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);
@@ -336,9 +343,26 @@ if (this.addMessageListener) {
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());
});
+
+ SimpleTest.registerCleanupFunction(() => {
+ runInParent(function cleanupParent() {
+ const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
+ Cu.import("resource://gre/modules/Services.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();
+ });
+ });
}
--- 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);