diff options
Diffstat (limited to 'toolkit/components/formautofill/test/chrome')
9 files changed, 336 insertions, 0 deletions
diff --git a/toolkit/components/formautofill/test/chrome/.eslintrc.js b/toolkit/components/formautofill/test/chrome/.eslintrc.js new file mode 100644 index 000000000..8c0f4f574 --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + "extends": [ + "../../../../../testing/mochitest/chrome.eslintrc.js" + ] +}; diff --git a/toolkit/components/formautofill/test/chrome/chrome.ini b/toolkit/components/formautofill/test/chrome/chrome.ini new file mode 100644 index 000000000..67b7869af --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/chrome.ini @@ -0,0 +1,17 @@ +[DEFAULT] +# The following files starting with ".." are installed in the current folder. +support-files = + ../head_common.js + ../loader_common.js + head.js + test_infrastructure.js + test_requestAutocomplete_cancel.js + loader_parent.js + loader.js + +# For each test defined below, the associated JavaScript file must be declared +# in the list above. This is required because a "support-files" declaration on +# the individual test would override the global list instead of adding entries. + +[test_infrastructure.html] +[test_requestAutocomplete_cancel.html] diff --git a/toolkit/components/formautofill/test/chrome/head.js b/toolkit/components/formautofill/test/chrome/head.js new file mode 100644 index 000000000..4110d5e7c --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/head.js @@ -0,0 +1,15 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Initialization specific to Form Autofill mochitest-chrome tests. + * + * This file is loaded by "loader.js". + */ + +"use strict"; + +// The testing framework is fully initialized at this point, you can add +// mochitest-chrome specific test initialization here. If you need shared +// functions or initialization that are not specific to mochitest-chrome, +// consider adding them to "head_common.js" in the parent folder instead. diff --git a/toolkit/components/formautofill/test/chrome/loader.js b/toolkit/components/formautofill/test/chrome/loader.js new file mode 100644 index 000000000..25b0e6ea3 --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/loader.js @@ -0,0 +1,116 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Infrastructure for the mochitest-chrome 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( + "chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", this); + +var sharedUrl = SimpleTest.getTestFileURL("loader_common.js"); +Services.scriptloader.loadSubScript(sharedUrl, this); + +var parentScript = SpecialPowers.loadChromeScript( + SimpleTest.getTestFileURL("loader_parent.js")); + +// Replace the extension of the loaded HTML file with ".js" +var testUrl = location.href.replace(/\.\w+$/, ".js"); + +// Start loading the test script in the parent process. +var promiseParentInitFinished = new Promise(function (resolve) { + parentScript.addMessageListener("finish_load_in_parent", resolve); +}); +parentScript.sendAsyncMessage("start_load_in_parent", { testUrl: testUrl }); + +// Define output functions so they look the same across all frameworks. +var Output = { + print: info, +}; + +// Define assertion functions so they look the same across all frameworks. +var Assert = { + ok: _mochitestAssert.ok, + equal: _mochitestAssert.equal, +}; + +var executeSoon = SimpleTest.executeSoon; + +var gTestTasks = []; + +// Define task registration functions, see description in "loader_common.js". +function add_task(taskFn) { + gTestTasks.push([taskFn, "content", taskFn.name]); +} +function add_task_in_parent_process(taskFn, taskIdOverride) { + let taskId = taskIdOverride || getTaskId(Components.stack.caller); + gTestTasks.push([taskFn, "parent", taskId]); +} +function add_task_in_both_processes(taskFn) { + // We need to define a task ID based on our direct caller. + add_task_in_parent_process(taskFn, getTaskId(Components.stack.caller)); + add_task(taskFn); +} +var add_task_in_child_process = add_task; + +window.addEventListener("load", function onLoad() { + window.removeEventListener("load", onLoad); + + Task.spawn(function* () { + try { + for (let [taskFn, taskType, taskId] of gTestTasks) { + if (taskType == "content") { + // This is a normal task executed in the current process. + info("Running " + taskFn.name); + yield Task.spawn(taskFn); + } else { + // This is a task executed in the parent process. + info("Running task in parent process: " + taskFn.name); + let promiseFinished = new Promise(function (resolve) { + parentScript.addMessageListener("finish_task_" + taskId, resolve); + }); + parentScript.sendAsyncMessage("start_task_" + taskId); + yield promiseFinished; + info("Finished task in parent process: " + taskFn.name); + } + } + } catch (ex) { + ok(false, ex); + } + + SimpleTest.finish(); + }); +}); + +// Wait for the test script to be loaded in the parent process. This means that +// test tasks are registered and ready, but have not been executed yet. +add_task(function* wait_loading_in_parent_process() { + yield promiseParentInitFinished; +}); + +var headUrl = SimpleTest.getTestFileURL("head_common.js"); +Services.scriptloader.loadSubScript(headUrl, this); + +Output.print("Loading test file: " + testUrl); +Services.scriptloader.loadSubScript(testUrl, this); + +// Register the execution of termination tasks after all other tasks. +add_task(terminationTaskFn); +add_task_in_parent_process(terminationTaskFn, terminationTaskFn.name); + +SimpleTest.waitForExplicitFinish(); + +// 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/chrome/loader_parent.js b/toolkit/components/formautofill/test/chrome/loader_parent.js new file mode 100644 index 000000000..bf823218e --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/loader_parent.js @@ -0,0 +1,77 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Infrastructure for the mochitest-chrome tests located in this folder, always + * executed in the parent process. + * + * 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); + +var sharedUrl = "chrome://mochitests/content/chrome/" + + "toolkit/components/formautofill/test/chrome/loader_common.js"; +Services.scriptloader.loadSubScript(sharedUrl, this); + +// Define output functions so they look the same across all frameworks. Since +// we don't have an output function available here, we report as TEST-PASS. +var Output = { + print: message => assert.ok(true, message), +}; + +// Define assertion functions so they look the same across all frameworks. +var Assert = { + ok: assert.ok, + equal: assert.equal, +}; + +// Define task registration functions, see description in "loader_common.js". +function add_task_in_parent_process(taskFn, taskIdOverride) { + let taskId = taskIdOverride || getTaskId(Components.stack.caller); + Output.print("Registering in the parent process: " + taskId); + addMessageListener("start_task_" + taskId, function () { + Task.spawn(function* () { + try { + Output.print("Running in the parent process " + taskId); + yield Task.spawn(taskFn); + } catch (ex) { + assert.ok(false, ex); + } + + sendAsyncMessage("finish_task_" + taskId, {}); + }); + }); +} +var add_task = function () {}; +var add_task_in_child_process = function () {}; +var add_task_in_both_processes = add_task_in_parent_process; + +// We need to wait for the child process to send us the path of the test file +// to load before we can actually start loading it. +var context = this; +addMessageListener("start_load_in_parent", function (message) { + Output.print("Starting loading infrastructure in parent process."); + let headUrl = "chrome://mochitests/content/chrome/" + + "toolkit/components/formautofill/test/chrome/head_common.js"; + Services.scriptloader.loadSubScript(headUrl, context); + + Services.scriptloader.loadSubScript(message.testUrl, context); + + // Register the execution of termination tasks after all other tasks. + add_task_in_parent_process(terminationTaskFn, terminationTaskFn.name); + + Output.print("Finished loading infrastructure in parent process."); + sendAsyncMessage("finish_load_in_parent", {}); +}); + +// 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/chrome/test_infrastructure.html b/toolkit/components/formautofill/test/chrome/test_infrastructure.html new file mode 100644 index 000000000..54f417f77 --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/test_infrastructure.html @@ -0,0 +1,8 @@ +<!DOCTYPE html><html><head><meta charset="utf-8"></head><body> +<script type="application/javascript;version=1.7" src="loader.js"></script> +<!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + +<p id="paragraph">Paragraph contents.</p> + +</body></html> diff --git a/toolkit/components/formautofill/test/chrome/test_infrastructure.js b/toolkit/components/formautofill/test/chrome/test_infrastructure.js new file mode 100644 index 000000000..c3b0b43ff --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/test_infrastructure.js @@ -0,0 +1,61 @@ +/* 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); +}); + +/** + * This type of test has access to the content declared above. + */ +add_task(function* test_content() { + Assert.equal($("paragraph").innerHTML, "Paragraph contents."); + + let promiseMyEvent = TestUtils.waitForEvent($("paragraph"), "MyEvent"); + + let event = document.createEvent("CustomEvent"); + event.initCustomEvent("MyEvent", true, false, {}); + $("paragraph").dispatchEvent(event); + + yield promiseMyEvent; +}); diff --git a/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.html b/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.html new file mode 100644 index 000000000..8ae7ffd4b --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.html @@ -0,0 +1,9 @@ +<!DOCTYPE html><html><head><meta charset="utf-8"></head><body> +<script type="application/javascript;version=1.7" src="loader.js"></script> +<!-- Any copyright is dedicated to the Public Domain. + - http://creativecommons.org/publicdomain/zero/1.0/ --> + +<form id="form"> +</form> + +</body></html> diff --git a/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.js b/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.js new file mode 100644 index 000000000..1ee12bd9a --- /dev/null +++ b/toolkit/components/formautofill/test/chrome/test_requestAutocomplete_cancel.js @@ -0,0 +1,26 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Tests the response sent when requestAutocomplete is canceled by the user. + */ + +"use strict"; + +/** + * The requestAutocomplete UI will not be displayed during these tests. + */ +add_task_in_parent_process(function* test_cancel_init() { + FormAutofillTest.requestAutocompleteResponse = { canceled: true }; +}); + +/** + * Tests the case where the feature is canceled. + */ +add_task(function* test_cancel() { + let promise = TestUtils.waitForEvent($("form"), "autocompleteerror"); + $("form").requestAutocomplete(); + let errorEvent = yield promise; + + Assert.equal(errorEvent.reason, "cancel"); +}); |