diff options
Diffstat (limited to 'toolkit/components/formautofill/test/xpcshell')
6 files changed, 208 insertions, 0 deletions
diff --git a/toolkit/components/formautofill/test/xpcshell/.eslintrc.js b/toolkit/components/formautofill/test/xpcshell/.eslintrc.js new file mode 100644 index 000000000..d35787cd2 --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + "extends": [ + "../../../../../testing/xpcshell/xpcshell.eslintrc.js" + ] +}; diff --git a/toolkit/components/formautofill/test/xpcshell/head.js b/toolkit/components/formautofill/test/xpcshell/head.js new file mode 100644 index 000000000..1cee023f2 --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/head.js @@ -0,0 +1,23 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Initialization specific to Form Autofill xpcshell tests. + * + * This file is loaded by "loader.js". + */ + +"use strict"; + +// The testing framework is fully initialized at this point, you can add +// xpcshell specific test initialization here. If you need shared functions or +// initialization that are not specific to xpcshell, consider adding them to +// "head_common.js" in the parent folder instead. + +add_task_in_parent_process(function* test_xpcshell_initialize_profile() { + // We need to send the profile-after-change notification manually to the + // startup component to ensure it has been initialized. + Cc["@mozilla.org/formautofill/startup;1"] + .getService(Ci.nsIObserver) + .observe(null, "profile-after-change", ""); +}); diff --git a/toolkit/components/formautofill/test/xpcshell/loader.js b/toolkit/components/formautofill/test/xpcshell/loader.js new file mode 100644 index 000000000..449989c8a --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/loader.js @@ -0,0 +1,46 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Infrastructure for the xpcshell tests located in this folder. + * + * See "loader_common.js" in the parent folder for a general overview. + * + * Unless you are adding new features to the framework, you shouldn't have to + * modify this file. Use "head_common.js" or "head.js" for shared code. + */ + +"use strict"; + +var { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; + +Cu.import("resource://gre/modules/XPCOMUtils.jsm", this); +Cu.import("resource://gre/modules/Services.jsm", this); + +Services.scriptloader.loadSubScript( + Services.io.newFileURI(do_get_file("loader_common.js")).spec, this); + +// Define output functions so they look the same across all frameworks. +var Output = { + print: do_print, +}; + +var executeSoon = do_execute_soon; +var setTimeout = (fn, delay) => do_timeout(delay, fn); + +// Define task registration functions, see description in "loader_common.js". +var add_task_in_parent_process = add_task; +var add_task_in_child_process = function () {}; +var add_task_in_both_processes = add_task; + +Services.scriptloader.loadSubScript( + Services.io.newFileURI(do_get_file("head_common.js")).spec, this); + +// Tests are always run asynchronously and with the profile loaded. +function run_test() { + do_get_profile(); + run_next_test(); +} + +// Reminder: unless you are adding new features to the framework, you shouldn't +// have to modify this file. Use "head_common.js" or "head.js" for shared code. diff --git a/toolkit/components/formautofill/test/xpcshell/test_infrastructure.js b/toolkit/components/formautofill/test/xpcshell/test_infrastructure.js new file mode 100644 index 000000000..af27cfdb5 --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/test_infrastructure.js @@ -0,0 +1,48 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Tests the local testing infrastructure. + */ + +"use strict"; + +/** + * Tests the truth assertion function. + */ +add_task(function* test_assert_truth() { + Assert.ok(1 != 2); +}); + +/** + * Tests the equality assertion function. + */ +add_task(function* test_assert_equality() { + Assert.equal(1 + 1, 2); +}); + +/** + * Uses some of the utility functions provided by the framework. + */ +add_task(function* test_utility_functions() { + // The "print" function is useful to log information that is not known before. + let randomString = "R" + Math.floor(Math.random() * 10); + Output.print("The random contents will be '" + randomString + "'."); + + // Create the text file with the random contents. + let path = yield TestUtils.getTempFile("test-infrastructure.txt"); + yield OS.File.writeAtomic(path, new TextEncoder().encode(randomString)); + + // Test a few utility functions. + yield TestUtils.waitForTick(); + yield TestUtils.waitMs(50); + + let promiseMyNotification = TestUtils.waitForNotification("my-topic"); + Services.obs.notifyObservers(null, "my-topic", ""); + yield promiseMyNotification; + + // Check the file size. The file will be deleted automatically later. + Assert.equal((yield OS.File.stat(path)).size, randomString.length); +}); + +add_task(terminationTaskFn); diff --git a/toolkit/components/formautofill/test/xpcshell/test_integration.js b/toolkit/components/formautofill/test/xpcshell/test_integration.js new file mode 100644 index 000000000..7707f3880 --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/test_integration.js @@ -0,0 +1,72 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Tests overriding the FormAutofillIntegration module functions. + */ + +"use strict"; + +/** + * The requestAutocomplete UI will not be displayed during these tests. + */ +add_task_in_parent_process(function* test_initialize() { + FormAutofillTest.requestAutocompleteResponse = { canceled: true }; +}); + +/** + * Registers and unregisters an integration override function. + */ +add_task(function* test_integration_override() { + let overrideCalled = false; + + let newIntegrationFn = base => ({ + createRequestAutocompleteUI: Task.async(function* () { + overrideCalled = true; + return yield base.createRequestAutocompleteUI.apply(this, arguments); + }), + }); + + FormAutofill.registerIntegration(newIntegrationFn); + try { + let ui = yield FormAutofill.integration.createRequestAutocompleteUI({}); + let result = yield ui.show(); + Assert.ok(result.canceled); + } finally { + FormAutofill.unregisterIntegration(newIntegrationFn); + } + + Assert.ok(overrideCalled); +}); + +/** + * Registers an integration override function that throws an exception, and + * ensures that this does not block other functions from being registered. + */ +add_task(function* test_integration_override_error() { + let overrideCalled = false; + + let errorIntegrationFn = base => { throw "Expected error." }; + + let newIntegrationFn = base => ({ + createRequestAutocompleteUI: Task.async(function* () { + overrideCalled = true; + return yield base.createRequestAutocompleteUI.apply(this, arguments); + }), + }); + + FormAutofill.registerIntegration(errorIntegrationFn); + FormAutofill.registerIntegration(newIntegrationFn); + try { + let ui = yield FormAutofill.integration.createRequestAutocompleteUI({}); + let result = yield ui.show(); + Assert.ok(result.canceled); + } finally { + FormAutofill.unregisterIntegration(errorIntegrationFn); + FormAutofill.unregisterIntegration(newIntegrationFn); + } + + Assert.ok(overrideCalled); +}); + +add_task(terminationTaskFn); diff --git a/toolkit/components/formautofill/test/xpcshell/xpcshell.ini b/toolkit/components/formautofill/test/xpcshell/xpcshell.ini new file mode 100644 index 000000000..711c03399 --- /dev/null +++ b/toolkit/components/formautofill/test/xpcshell/xpcshell.ini @@ -0,0 +1,12 @@ +[DEFAULT] +head = loader.js head.js +tail = +skip-if = toolkit == 'android' +# The following files starting with ".." are installed in the current folder. +# However, they cannot be referenced directly in the "head" directive above. +support-files = + ../head_common.js + ../loader_common.js + +[test_infrastructure.js] +[test_integration.js] |