/* * Test for LoginManagerContent._getFormFields. */ "use strict"; // Services.prefs.setBoolPref("signon.debug", true); XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]); const {LoginFormFactory} = ChromeUtils.import("resource://gre/modules/LoginFormFactory.jsm"); const LMCBackstagePass = ChromeUtils.import("resource://gre/modules/LoginManagerContent.jsm", null); const { LoginManagerContent } = LMCBackstagePass; const TESTCASES = [ { description: "1 password field outside of a
", document: ``, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "1 text field outside of a without a password field", document: ``, returnedFieldIDs: [null, null, null], skipEmptyFields: undefined, }, { description: "1 username & password field outside of a ", document: ` `, returnedFieldIDs: ["un1", "pw1", null], skipEmptyFields: undefined, }, { beforeGetFunction(doc, formLike) { // Access the formLike.elements lazy getter to have it cached. Assert.equal(formLike.elements.length, 2, "Check initial elements length"); doc.getElementById("un1").remove(); }, description: "1 username & password field outside of a , un1 removed", document: ` `, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "1 username & password field in a ", document: `
`, returnedFieldIDs: ["un1", "pw1", null], skipEmptyFields: undefined, }, { description: "4 empty password fields outside of a
", document: ` `, returnedFieldIDs: [null, null, null], skipEmptyFields: undefined, }, { description: "4 password fields outside of a (1 empty, 3 full) with skipEmpty", document: ` `, returnedFieldIDs: [null, null, null], skipEmptyFields: true, }, { description: "Form with 1 password field", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "Form with 2 password fields", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "1 password field in a form, 1 outside (not processed)", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "1 password field in a form, 1 text field outside (not processed)", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "1 text field in a form, 1 password field outside (not processed)", document: `
`, returnedFieldIDs: [null, null, null], skipEmptyFields: undefined, }, { description: "2 password fields outside of a
with 1 linked via @form", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: undefined, }, { description: "2 password fields outside of a
with 1 linked via @form + skipEmpty", document: `
`, returnedFieldIDs: [null, null, null], skipEmptyFields: true, }, { description: "2 password fields outside of a
with 1 linked via @form + skipEmpty with 1 empty", document: `
`, returnedFieldIDs: [null, "pw1", null], skipEmptyFields: true, }, ]; for (let tc of TESTCASES) { info("Sanity checking the testcase: " + tc.description); (function() { let testcase = tc; add_task(async function() { info("Starting testcase: " + testcase.description); let document = MockDocument.createTestDocument("http://localhost:8080/test/", testcase.document); let input = document.querySelector("input"); MockDocument.mockOwnerDocumentProperty(input, document, "http://localhost:8080/test/"); let formLike = LoginFormFactory.createFromField(input); if (testcase.beforeGetFunction) { await testcase.beforeGetFunction(document, formLike); } let actual = LoginManagerContent._getFormFields(formLike, testcase.skipEmptyFields, new Set()); Assert.strictEqual(testcase.returnedFieldIDs.length, 3, "_getFormFields returns 3 elements"); for (let i = 0; i < testcase.returnedFieldIDs.length; i++) { let expectedID = testcase.returnedFieldIDs[i]; if (expectedID === null) { Assert.strictEqual(actual[i], expectedID, "Check returned field " + i + " is null"); } else { Assert.strictEqual(actual[i].id, expectedID, "Check returned field " + i + " ID"); } } }); })(); }