Bug 1300988 - Part 3: Move MockDocument to toolkit/modules and refactor unit test in passwordmgr. r=MattN
MozReview-Commit-ID: Gp4DFbdLSqP
--- a/browser/extensions/formautofill/moz.build
+++ b/browser/extensions/formautofill/moz.build
@@ -10,17 +10,13 @@ DEFINES['MOZ_APP_MAXVERSION'] = CONFIG['
FINAL_TARGET_FILES.features['formautofill@mozilla.org'] += [
'bootstrap.js'
]
FINAL_TARGET_PP_FILES.features['formautofill@mozilla.org'] += [
'install.rdf.in'
]
-TESTING_JS_MODULES += [
- 'test/unit/MockDocument.jsm'
-]
-
BROWSER_CHROME_MANIFESTS += ['test/browser/browser.ini']
XPCSHELL_TESTS_MANIFESTS += ['test/unit/xpcshell.ini']
JAR_MANIFESTS += ['jar.mn']
rename from browser/extensions/formautofill/test/unit/MockDocument.jsm
rename to testing/modules/MockDocument.jsm
--- a/toolkit/components/passwordmgr/test/unit/head.js
+++ b/toolkit/components/passwordmgr/test/unit/head.js
@@ -92,54 +92,16 @@ function getTempFile(aLeafName)
////////////////////////////////////////////////////////////////////////////////
const RecipeHelpers = {
initNewParent() {
return (new LoginRecipesParent({ defaults: null })).initializationPromise;
},
};
-const MockDocument = {
- /**
- * Create a document for the given URL containing the given HTML with the ownerDocument of all <form>s having a mocked location.
- */
- createTestDocument(aDocumentURL, aContent = "<form>", aType = "text/html") {
- let parser = Cc["@mozilla.org/xmlextras/domparser;1"].
- createInstance(Ci.nsIDOMParser);
- parser.init();
- let parsedDoc = parser.parseFromString(aContent, aType);
-
- for (let element of parsedDoc.forms) {
- this.mockOwnerDocumentProperty(element, parsedDoc, aDocumentURL);
- }
- return parsedDoc;
- },
-
- mockOwnerDocumentProperty(aElement, aDoc, aURL) {
- // Mock the document.location object so we can unit test without a frame. We use a proxy
- // instead of just assigning to the property since it's not configurable or writable.
- let document = new Proxy(aDoc, {
- get(target, property, receiver) {
- // document.location is normally null when a document is outside of a "browsing context".
- // See https://html.spec.whatwg.org/#the-location-interface
- if (property == "location") {
- return new URL(aURL);
- }
- return target[property];
- },
- });
-
- // Assign element.ownerDocument to the proxy so document.location works.
- Object.defineProperty(aElement, "ownerDocument", {
- value: document,
- });
- },
-
-};
-
//// Initialization functions common to all tests
add_task(function* test_common_initialize()
{
// Before initializing the service for the first time, we should copy the key
// file required to decrypt the logins contained in the SQLite databases used
// by migration tests. This file is not required for the other tests.
yield OS.File.copy(do_get_file("data/key3.db").path,
--- a/toolkit/components/passwordmgr/test/unit/test_context_menu.js
+++ b/toolkit/components/passwordmgr/test/unit/test_context_menu.js
@@ -1,16 +1,17 @@
/*
* Test the password manager context menu.
*/
"use strict";
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/LoginManagerContextMenu.jsm");
+Cu.import("resource://testing-common/MockDocument.jsm");
XPCOMUtils.defineLazyGetter(this, "_stringBundle", function() {
return Services.strings.
createBundle("chrome://passwordmgr/locale/passwordmgr.properties");
});
/**
* Prepare data for the following tests.
--- a/toolkit/components/passwordmgr/test/unit/test_getFormFields.js
+++ b/toolkit/components/passwordmgr/test/unit/test_getFormFields.js
@@ -2,16 +2,17 @@
* Test for LoginManagerContent._getFormFields.
*/
"use strict";
//Services.prefs.setBoolPref("signon.debug", true);
Cu.importGlobalProperties(["URL"]);
+Cu.import("resource://testing-common/MockDocument.jsm");
const LMCBackstagePass = Cu.import("resource://gre/modules/LoginManagerContent.jsm");
const { LoginManagerContent, FormLikeFactory } = LMCBackstagePass;
const TESTCASES = [
{
description: "1 password field outside of a <form>",
document: `<input id="pw1" type=password>`,
returnedFieldIDs: [null, "pw1", null],
--- a/toolkit/components/passwordmgr/test/unit/test_getPasswordFields.js
+++ b/toolkit/components/passwordmgr/test/unit/test_getPasswordFields.js
@@ -1,14 +1,15 @@
/*
* Test for LoginManagerContent._getPasswordFields using FormLikeFactory.
*/
"use strict";
+Cu.import("resource://testing-common/MockDocument.jsm");
const LMCBackstagePass = Cu.import("resource://gre/modules/LoginManagerContent.jsm");
const { LoginManagerContent, FormLikeFactory } = LMCBackstagePass;
const TESTCASES = [
{
description: "Empty document",
document: ``,
returnedFieldIDsByFormLike: [],
skipEmptyFields: undefined,
--- a/toolkit/components/passwordmgr/test/unit/test_recipes_content.js
+++ b/toolkit/components/passwordmgr/test/unit/test_recipes_content.js
@@ -2,16 +2,17 @@
* http://creativecommons.org/publicdomain/zero/1.0/ */
/**
* Test filtering recipes in LoginRecipesContent.
*/
"use strict";
+Cu.import("resource://testing-common/MockDocument.jsm");
Cu.importGlobalProperties(["URL"]);
add_task(function* test_getFieldOverrides() {
let recipes = new Set([
{ // path doesn't match but otherwise good
hosts: ["example.com:8080"],
passwordSelector: "#password",
pathRegex: /^\/$/,
copy from browser/extensions/formautofill/test/unit/MockDocument.jsm
copy to toolkit/modules/MockDocument.jsm
--- a/toolkit/modules/moz.build
+++ b/toolkit/modules/moz.build
@@ -5,16 +5,17 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
XPCSHELL_TESTS_MANIFESTS += ['tests/xpcshell/xpcshell.ini']
BROWSER_CHROME_MANIFESTS += ['tests/browser/browser.ini']
MOCHITEST_MANIFESTS += ['tests/mochitest/mochitest.ini']
MOCHITEST_CHROME_MANIFESTS += ['tests/chrome/chrome.ini']
TESTING_JS_MODULES += [
+ 'MockDocument.jsm',
'tests/PromiseTestUtils.jsm',
'tests/xpcshell/TestIntegration.jsm',
]
SPHINX_TREES['toolkit_modules'] = 'docs'
EXTRA_JS_MODULES += [
'addons/MatchPattern.jsm',