michael@0: /* michael@0: * $_ michael@0: * michael@0: * Returns the element with the specified |name| attribute. michael@0: */ michael@0: function $_(formNum, name) { michael@0: var form = document.getElementById("form" + formNum); michael@0: if (!form) { michael@0: logWarning("$_ couldn't find requested form " + formNum); michael@0: return null; michael@0: } michael@0: michael@0: var element = form.elements.namedItem(name); michael@0: if (!element) { michael@0: logWarning("$_ couldn't find requested element " + name); michael@0: return null; michael@0: } michael@0: michael@0: // Note that namedItem is a bit stupid, and will prefer an michael@0: // |id| attribute over a |name| attribute when looking for michael@0: // the element. Login Mananger happens to use .namedItem michael@0: // anyway, but let's rigorously check it here anyway so michael@0: // that we don't end up with tests that mistakenly pass. michael@0: michael@0: if (element.getAttribute("name") != name) { michael@0: logWarning("$_ got confused."); michael@0: return null; michael@0: } michael@0: michael@0: return element; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * checkForm michael@0: * michael@0: * Check a form for expected values. If an argument is null, a field's michael@0: * expected value will be the default value. michael@0: * michael@0: *
michael@0: * checkForm(#, "foo"); michael@0: */ michael@0: function checkForm(formNum, val1, val2, val3) { michael@0: var e, form = document.getElementById("form" + formNum); michael@0: ok(form, "Locating form " + formNum); michael@0: michael@0: var numToCheck = arguments.length - 1; michael@0: michael@0: if (!numToCheck--) michael@0: return; michael@0: e = form.elements[0]; michael@0: if (val1 == null) michael@0: is(e.value, e.defaultValue, "Test default value of field " + e.name + michael@0: " in form " + formNum); michael@0: else michael@0: is(e.value, val1, "Test value of field " + e.name + michael@0: " in form " + formNum); michael@0: michael@0: michael@0: if (!numToCheck--) michael@0: return; michael@0: e = form.elements[1]; michael@0: if (val2 == null) michael@0: is(e.value, e.defaultValue, "Test default value of field " + e.name + michael@0: " in form " + formNum); michael@0: else michael@0: is(e.value, val2, "Test value of field " + e.name + michael@0: " in form " + formNum); michael@0: michael@0: michael@0: if (!numToCheck--) michael@0: return; michael@0: e = form.elements[2]; michael@0: if (val3 == null) michael@0: is(e.value, e.defaultValue, "Test default value of field " + e.name + michael@0: " in form " + formNum); michael@0: else michael@0: is(e.value, val3, "Test value of field " + e.name + michael@0: " in form " + formNum); michael@0: michael@0: } michael@0: michael@0: michael@0: /* michael@0: * checkUnmodifiedForm michael@0: * michael@0: * Check a form for unmodified values from when page was loaded. michael@0: * michael@0: * michael@0: * checkUnmodifiedForm(#); michael@0: */ michael@0: function checkUnmodifiedForm(formNum) { michael@0: var form = document.getElementById("form" + formNum); michael@0: ok(form, "Locating form " + formNum); michael@0: michael@0: for (var i = 0; i < form.elements.length; i++) { michael@0: var ele = form.elements[i]; michael@0: michael@0: // No point in checking form submit/reset buttons. michael@0: if (ele.type == "submit" || ele.type == "reset") michael@0: continue; michael@0: michael@0: is(ele.value, ele.defaultValue, "Test to default value of field " + michael@0: ele.name + " in form " + formNum); michael@0: } michael@0: } michael@0: michael@0: michael@0: // Mochitest gives us a sendKey(), but it's targeted to a specific element. michael@0: // This basically sends an untargeted key event, to whatever's focused. michael@0: function doKey(aKey, modifier) { michael@0: var keyName = "DOM_VK_" + aKey.toUpperCase(); michael@0: var key = KeyEvent[keyName]; michael@0: michael@0: // undefined --> null michael@0: if (!modifier) michael@0: modifier = null; michael@0: michael@0: // Window utils for sending fake sey events. michael@0: var wutils = SpecialPowers.wrap(window). michael@0: QueryInterface(SpecialPowers.Ci.nsIInterfaceRequestor). michael@0: getInterface(SpecialPowers.Ci.nsIDOMWindowUtils); michael@0: michael@0: if (wutils.sendKeyEvent("keydown", key, 0, modifier)) { michael@0: wutils.sendKeyEvent("keypress", key, 0, modifier); michael@0: } michael@0: wutils.sendKeyEvent("keyup", key, 0, modifier); michael@0: } michael@0: michael@0: // Init with a common login michael@0: function commonInit() { michael@0: var pwmgr = SpecialPowers.Cc["@mozilla.org/login-manager;1"]. michael@0: getService(SpecialPowers.Ci.nsILoginManager); michael@0: ok(pwmgr != null, "Access LoginManager"); michael@0: michael@0: michael@0: // Check that initial state has no logins michael@0: var logins = pwmgr.getAllLogins(); michael@0: if (logins.length) { michael@0: //todo(false, "Warning: wasn't expecting logins to be present."); michael@0: pwmgr.removeAllLogins(); michael@0: } michael@0: var disabledHosts = pwmgr.getAllDisabledHosts(); michael@0: if (disabledHosts.length) { michael@0: //todo(false, "Warning: wasn't expecting disabled hosts to be present."); michael@0: for (var host of disabledHosts) michael@0: pwmgr.setLoginSavingEnabled(host, true); michael@0: } michael@0: michael@0: // Add a login that's used in multiple tests michael@0: var login = SpecialPowers.Cc["@mozilla.org/login-manager/loginInfo;1"]. michael@0: createInstance(SpecialPowers.Ci.nsILoginInfo); michael@0: login.init("http://mochi.test:8888", "http://mochi.test:8888", null, michael@0: "testuser", "testpass", "uname", "pword"); michael@0: pwmgr.addLogin(login); michael@0: michael@0: // Last sanity check michael@0: logins = pwmgr.getAllLogins(); michael@0: is(logins.length, 1, "Checking for successful init login"); michael@0: disabledHosts = pwmgr.getAllDisabledHosts(); michael@0: is(disabledHosts.length, 0, "Checking for no disabled hosts"); michael@0: } michael@0: michael@0: const masterPassword = "omgsecret!"; michael@0: michael@0: function enableMasterPassword() { michael@0: setMasterPassword(true); michael@0: } michael@0: michael@0: function disableMasterPassword() { michael@0: setMasterPassword(false); michael@0: } michael@0: michael@0: function setMasterPassword(enable) { michael@0: var oldPW, newPW; michael@0: if (enable) { michael@0: oldPW = ""; michael@0: newPW = masterPassword; michael@0: } else { michael@0: oldPW = masterPassword; michael@0: newPW = ""; michael@0: } michael@0: // Set master password. Note that this does not log you in, so the next michael@0: // invocation of pwmgr can trigger a MP prompt. michael@0: michael@0: var pk11db = Cc["@mozilla.org/security/pk11tokendb;1"]. michael@0: getService(Ci.nsIPK11TokenDB) michael@0: var token = pk11db.findTokenByName(""); michael@0: ok(true, "change from " + oldPW + " to " + newPW); michael@0: token.changePassword(oldPW, newPW); michael@0: } michael@0: michael@0: function logoutMasterPassword() { michael@0: var sdr = Cc["@mozilla.org/security/sdr;1"]. michael@0: getService(Ci.nsISecretDecoderRing); michael@0: sdr.logoutAndTeardown(); michael@0: } michael@0: michael@0: function dumpLogins(pwmgr) { michael@0: var logins = pwmgr.getAllLogins(); michael@0: ok(true, "----- dumpLogins: have " + logins.length + " logins. -----"); michael@0: for (var i = 0; i < logins.length; i++) michael@0: dumpLogin("login #" + i + " --- ", logins[i]); michael@0: } michael@0: michael@0: function dumpLogin(label, login) { michael@0: loginText = ""; michael@0: loginText += "host: "; michael@0: loginText += login.hostname; michael@0: loginText += " / formURL: "; michael@0: loginText += login.formSubmitURL; michael@0: loginText += " / realm: "; michael@0: loginText += login.httpRealm; michael@0: loginText += " / user: "; michael@0: loginText += login.username; michael@0: loginText += " / pass: "; michael@0: loginText += login.password; michael@0: loginText += " / ufield: "; michael@0: loginText += login.usernameField; michael@0: loginText += " / pfield: "; michael@0: loginText += login.passwordField; michael@0: ok(true, label + loginText); michael@0: }