/** * Test for LoginManagerChild.getUserNameAndPasswordFields */ "use strict"; XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]); const LMCBackstagePass = ChromeUtils.import( "resource://gre/modules/LoginManagerChild.jsm", null ); const { LoginManagerChild } = LMCBackstagePass; const TESTCASES = [ { description: "1 password field outside of a
`, returnedFieldIDs: ["un1", "pw1", null], }, { description: "5 empty password fields outside of a `, returnedFieldIDs: [null, "pw1", null], }, { description: "Form with 2 password fields", document: ``, returnedFieldIDs: [null, "pw1", null], }, { description: "1 password field in a form, 1 outside (not processed)", document: ``, returnedFieldIDs: [null, "pw1", null], }, { description: "1 password field in a form, 1 text field outside (not processed)", document: ``, returnedFieldIDs: [null, "pw1", null], }, { description: "1 text field in a form, 1 password field outside (not processed)", document: ``, returnedFieldIDs: [null, null, null], }, { description: "2 password fields outside of a `, returnedFieldIDs: [null, "pw1", null], }, ]; 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/" ); MockDocument.mockNodePrincipalProperty( input, "http://localhost:8080/test/" ); // Additional mock to cache recipes let win = {}; Object.defineProperty(document, "defaultView", { value: win, }); let formOrigin = LoginHelper.getLoginOrigin(document.documentURI); LoginRecipesContent.cacheRecipes(formOrigin, win, new Set()); let actual = new LoginManagerChild().getUserNameAndPasswordFields(input); Assert.strictEqual( testcase.returnedFieldIDs.length, 3, "getUserNameAndPasswordFields 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" ); } } }); })(); }