summaryrefslogtreecommitdiffstats
path: root/testing/xpcshell/example/unit
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--testing/xpcshell/example/unit/check_profile.js44
-rw-r--r--testing/xpcshell/example/unit/file.txt1
-rw-r--r--testing/xpcshell/example/unit/import_module.sys.mjs9
-rw-r--r--testing/xpcshell/example/unit/load_subscript.js6
-rw-r--r--testing/xpcshell/example/unit/location_load.js8
-rw-r--r--testing/xpcshell/example/unit/prefs_test_common.js47
-rw-r--r--testing/xpcshell/example/unit/subdir/file.txt1
-rw-r--r--testing/xpcshell/example/unit/test_add_setup.js23
-rw-r--r--testing/xpcshell/example/unit/test_check_nsIException.js10
-rw-r--r--testing/xpcshell/example/unit/test_check_nsIException_failing.js10
-rw-r--r--testing/xpcshell/example/unit/test_do_check_matches.js14
-rw-r--r--testing/xpcshell/example/unit/test_do_check_matches_failing.js12
-rw-r--r--testing/xpcshell/example/unit/test_do_check_null.js6
-rw-r--r--testing/xpcshell/example/unit/test_do_check_null_failing.js6
-rw-r--r--testing/xpcshell/example/unit/test_do_get_tempdir.js14
-rw-r--r--testing/xpcshell/example/unit/test_execute_soon.js20
-rw-r--r--testing/xpcshell/example/unit/test_fail.js8
-rw-r--r--testing/xpcshell/example/unit/test_get_file.js31
-rw-r--r--testing/xpcshell/example/unit/test_get_idle.js24
-rw-r--r--testing/xpcshell/example/unit/test_import_module.js19
-rw-r--r--testing/xpcshell/example/unit/test_load.js20
-rw-r--r--testing/xpcshell/example/unit/test_load_httpd_js.js13
-rw-r--r--testing/xpcshell/example/unit/test_location.js13
-rw-r--r--testing/xpcshell/example/unit/test_multiple_setups.js13
-rw-r--r--testing/xpcshell/example/unit/test_multiple_tasks.js20
-rw-r--r--testing/xpcshell/example/unit/test_prefs_defaults.js18
-rw-r--r--testing/xpcshell/example/unit/test_prefs_defaults_and_file.js42
-rw-r--r--testing/xpcshell/example/unit/test_prefs_defaults_included.js16
-rw-r--r--testing/xpcshell/example/unit/test_prefs_no_defaults.js15
-rw-r--r--testing/xpcshell/example/unit/test_prefs_no_defaults_with_file.js15
-rw-r--r--testing/xpcshell/example/unit/test_profile.js11
-rw-r--r--testing/xpcshell/example/unit/test_profile_afterChange.js11
-rw-r--r--testing/xpcshell/example/unit/test_sample.js21
-rw-r--r--testing/xpcshell/example/unit/test_skip.js8
-rw-r--r--testing/xpcshell/example/unit/test_tasks_skip.js21
-rw-r--r--testing/xpcshell/example/unit/test_tasks_skipall.js23
-rw-r--r--testing/xpcshell/example/unit/xpcshell-included-with-prefs.ini5
-rw-r--r--testing/xpcshell/example/unit/xpcshell-with-prefs.ini16
-rw-r--r--testing/xpcshell/example/unit/xpcshell.ini60
39 files changed, 674 insertions, 0 deletions
diff --git a/testing/xpcshell/example/unit/check_profile.js b/testing/xpcshell/example/unit/check_profile.js
new file mode 100644
index 0000000000..57ecf5fd55
--- /dev/null
+++ b/testing/xpcshell/example/unit/check_profile.js
@@ -0,0 +1,44 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function check_profile_dir(profd) {
+ Assert.ok(profd.exists());
+ Assert.ok(profd.isDirectory());
+ let profd2 = Services.dirsvc.get("ProfD", Ci.nsIFile);
+ Assert.ok(profd2.exists());
+ Assert.ok(profd2.isDirectory());
+ // make sure we got the same thing back...
+ Assert.ok(profd.equals(profd2));
+}
+
+function check_do_get_profile(fireProfileAfterChange) {
+ const observedTopics = new Map([
+ ["profile-do-change", 0],
+ ["profile-after-change", 0],
+ ]);
+ const expectedTopics = new Map(observedTopics);
+
+ for (let [topic] of observedTopics) {
+ Services.obs.addObserver(() => {
+ let val = observedTopics.get(topic) + 1;
+ observedTopics.set(topic, val);
+ }, topic);
+ }
+
+ // Trigger profile creation.
+ let profd = do_get_profile();
+ check_profile_dir(profd);
+
+ // Check the observed topics
+ expectedTopics.set("profile-do-change", 1);
+ if (fireProfileAfterChange) {
+ expectedTopics.set("profile-after-change", 1);
+ }
+ Assert.deepEqual(observedTopics, expectedTopics);
+
+ // A second do_get_profile() should not trigger more notifications.
+ profd = do_get_profile();
+ check_profile_dir(profd);
+ Assert.deepEqual(observedTopics, expectedTopics);
+}
diff --git a/testing/xpcshell/example/unit/file.txt b/testing/xpcshell/example/unit/file.txt
new file mode 100644
index 0000000000..ce01362503
--- /dev/null
+++ b/testing/xpcshell/example/unit/file.txt
@@ -0,0 +1 @@
+hello
diff --git a/testing/xpcshell/example/unit/import_module.sys.mjs b/testing/xpcshell/example/unit/import_module.sys.mjs
new file mode 100644
index 0000000000..aba93afc86
--- /dev/null
+++ b/testing/xpcshell/example/unit/import_module.sys.mjs
@@ -0,0 +1,9 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Module used by test_import_module.js
+
+export const MODULE_IMPORTED = true;
+
+export const MODULE_URI = import.meta.url;
diff --git a/testing/xpcshell/example/unit/load_subscript.js b/testing/xpcshell/example/unit/load_subscript.js
new file mode 100644
index 0000000000..bb0c4400b3
--- /dev/null
+++ b/testing/xpcshell/example/unit/load_subscript.js
@@ -0,0 +1,6 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals subscriptLoaded:true */
+subscriptLoaded = true;
diff --git a/testing/xpcshell/example/unit/location_load.js b/testing/xpcshell/example/unit/location_load.js
new file mode 100644
index 0000000000..c198b2e7de
--- /dev/null
+++ b/testing/xpcshell/example/unit/location_load.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals __LOCATION__ */
+
+// Gets loaded via test_location.js
+Assert.equal(__LOCATION__.leafName, "location_load.js");
diff --git a/testing/xpcshell/example/unit/prefs_test_common.js b/testing/xpcshell/example/unit/prefs_test_common.js
new file mode 100644
index 0000000000..e12d3d5298
--- /dev/null
+++ b/testing/xpcshell/example/unit/prefs_test_common.js
@@ -0,0 +1,47 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function isValidPref(prefName) {
+ return Services.prefs.getPrefType(prefName) !== Services.prefs.PREF_INVALID;
+}
+
+// Check a pref that appears in testing/profiles/xpcshell/user.js
+// but NOT in StaticPrefList.yaml, modules/libpref/init/all.js
+function has_pref_from_xpcshell_user_js() {
+ return isValidPref("extensions.webextensions.warnings-as-errors");
+}
+
+// Test pref from xpcshell-with-prefs.ini
+function has_pref_from_manifest_defaults() {
+ return isValidPref("dummy.pref.from.test.manifest");
+}
+
+// Test pref set in xpcshell.ini and xpcshell-with-prefs.ini
+function has_pref_from_manifest_file_section() {
+ return isValidPref("dummy.pref.from.test.file");
+}
+
+function check_common_xpcshell_with_prefs() {
+ Assert.ok(
+ has_pref_from_xpcshell_user_js(),
+ "Should have pref from xpcshell's user.js"
+ );
+
+ Assert.ok(
+ has_pref_from_manifest_defaults(),
+ "Should have pref from DEFAULTS in xpcshell-with-prefs.ini"
+ );
+}
+
+function check_common_xpcshell_without_prefs() {
+ Assert.ok(
+ has_pref_from_xpcshell_user_js(),
+ "Should have pref from xpcshell's user.js"
+ );
+
+ Assert.ok(
+ !has_pref_from_manifest_defaults(),
+ "xpcshell.ini did not set any prefs in DEFAULTS"
+ );
+}
diff --git a/testing/xpcshell/example/unit/subdir/file.txt b/testing/xpcshell/example/unit/subdir/file.txt
new file mode 100644
index 0000000000..c4f6b5f708
--- /dev/null
+++ b/testing/xpcshell/example/unit/subdir/file.txt
@@ -0,0 +1 @@
+subdir hello
diff --git a/testing/xpcshell/example/unit/test_add_setup.js b/testing/xpcshell/example/unit/test_add_setup.js
new file mode 100644
index 0000000000..a647f108ee
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_add_setup.js
@@ -0,0 +1,23 @@
+/* Any copyright is dedicated to the Public Domain.
+ http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+let someVar = 1;
+
+add_task(() => {
+ Assert.ok(false, "I should not be called!");
+});
+
+/* eslint-disable mozilla/reject-addtask-only */
+add_task(() => {
+ Assert.equal(
+ someVar,
+ 2,
+ "Setup should have run, even though this is the only test."
+ );
+}).only();
+
+add_setup(() => {
+ someVar = 2;
+});
diff --git a/testing/xpcshell/example/unit/test_check_nsIException.js b/testing/xpcshell/example/unit/test_check_nsIException.js
new file mode 100644
index 0000000000..23889d1a97
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_check_nsIException.js
@@ -0,0 +1,10 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* import-globals-from ../../head.js */
+
+function run_test() {
+ do_check_throws_nsIException(function() {
+ Services.env.QueryInterface(Ci.nsIFile);
+ }, "NS_NOINTERFACE");
+}
diff --git a/testing/xpcshell/example/unit/test_check_nsIException_failing.js b/testing/xpcshell/example/unit/test_check_nsIException_failing.js
new file mode 100644
index 0000000000..5f559247d9
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_check_nsIException_failing.js
@@ -0,0 +1,10 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* import-globals-from ../../head.js */
+
+function run_test() {
+ do_check_throws_nsIException(function() {
+ throw Error("I find your relaxed dishabille unpalatable");
+ }, "NS_NOINTERFACE");
+}
diff --git a/testing/xpcshell/example/unit/test_do_check_matches.js b/testing/xpcshell/example/unit/test_do_check_matches.js
new file mode 100644
index 0000000000..44ef0096fc
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_do_check_matches.js
@@ -0,0 +1,14 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test() {
+ Assert.deepEqual({ x: 1 }, { x: 1 });
+
+ // Property order is irrelevant.
+ Assert.deepEqual({ x: "foo", y: "bar" }, { y: "bar", x: "foo" }); // pass
+
+ // Patterns nest.
+ Assert.deepEqual({ a: 1, b: { c: 2, d: 3 } }, { a: 1, b: { c: 2, d: 3 } });
+
+ Assert.deepEqual([3, 4, 5], [3, 4, 5]);
+}
diff --git a/testing/xpcshell/example/unit/test_do_check_matches_failing.js b/testing/xpcshell/example/unit/test_do_check_matches_failing.js
new file mode 100644
index 0000000000..c45ec3469b
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_do_check_matches_failing.js
@@ -0,0 +1,12 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test() {
+ Assert.deepEqual({ x: 1 }, {}); // fail: all pattern props required
+ Assert.deepEqual({ x: 1 }, { x: 2 }); // fail: values must match
+ Assert.deepEqual({ x: undefined }, {});
+
+ // 'length' property counts, even if non-enumerable.
+ Assert.deepEqual([3, 4, 5], [3, 5, 5]); // fail; value doesn't match
+ Assert.deepEqual([3, 4, 5], [3, 4, 5, 6]); // fail; length doesn't match
+}
diff --git a/testing/xpcshell/example/unit/test_do_check_null.js b/testing/xpcshell/example/unit/test_do_check_null.js
new file mode 100644
index 0000000000..97ad824353
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_do_check_null.js
@@ -0,0 +1,6 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test() {
+ Assert.equal(null, null);
+}
diff --git a/testing/xpcshell/example/unit/test_do_check_null_failing.js b/testing/xpcshell/example/unit/test_do_check_null_failing.js
new file mode 100644
index 0000000000..981f5c838c
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_do_check_null_failing.js
@@ -0,0 +1,6 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+function run_test() {
+ Assert.equal(null, 0);
+}
diff --git a/testing/xpcshell/example/unit/test_do_get_tempdir.js b/testing/xpcshell/example/unit/test_do_get_tempdir.js
new file mode 100644
index 0000000000..31c061f741
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_do_get_tempdir.js
@@ -0,0 +1,14 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/* This tests that do_get_tempdir returns a directory that we can write to. */
+
+function run_test() {
+ let tmpd = do_get_tempdir();
+ Assert.ok(tmpd.exists());
+ tmpd.append("testfile");
+ tmpd.create(Ci.nsIFile.NORMAL_FILE_TYPE, 600);
+ Assert.ok(tmpd.exists());
+}
diff --git a/testing/xpcshell/example/unit/test_execute_soon.js b/testing/xpcshell/example/unit/test_execute_soon.js
new file mode 100644
index 0000000000..d4fb954e46
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_execute_soon.js
@@ -0,0 +1,20 @@
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* ***** BEGIN LICENSE BLOCK *****
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/licenses/publicdomain/
+ * ***** END LICENSE BLOCK ***** */
+
+var complete = false;
+
+function run_test() {
+ dump("Starting test\n");
+ registerCleanupFunction(function() {
+ dump("Checking test completed\n");
+ Assert.ok(complete);
+ });
+
+ executeSoon(function execute_soon_callback() {
+ dump("do_execute_soon callback\n");
+ complete = true;
+ });
+}
diff --git a/testing/xpcshell/example/unit/test_fail.js b/testing/xpcshell/example/unit/test_fail.js
new file mode 100644
index 0000000000..0c203cd82e
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_fail.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ // This test expects to fail.
+ Assert.ok(false);
+}
diff --git a/testing/xpcshell/example/unit/test_get_file.js b/testing/xpcshell/example/unit/test_get_file.js
new file mode 100644
index 0000000000..213c9e7233
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_get_file.js
@@ -0,0 +1,31 @@
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ var lf = do_get_file("file.txt");
+ Assert.ok(lf.exists());
+ Assert.ok(lf.isFile());
+ // check that allowNonexistent works
+ lf = do_get_file("file.txt.notfound", true);
+ Assert.ok(!lf.exists());
+ // check that we can get a file from a subdirectory
+ lf = do_get_file("subdir/file.txt");
+ Assert.ok(lf.exists());
+ Assert.ok(lf.isFile());
+ // and that we can get a handle to a directory itself
+ lf = do_get_file("subdir/");
+ Assert.ok(lf.exists());
+ Assert.ok(lf.isDirectory());
+ // check that we can go up a level
+ lf = do_get_file("..");
+ Assert.ok(lf.exists());
+ lf.append("unit");
+ lf.append("file.txt");
+ Assert.ok(lf.exists());
+ // check that do_get_cwd works
+ lf = do_get_cwd();
+ Assert.ok(lf.exists());
+ Assert.ok(lf.isDirectory());
+}
diff --git a/testing/xpcshell/example/unit/test_get_idle.js b/testing/xpcshell/example/unit/test_get_idle.js
new file mode 100644
index 0000000000..ea01ce0247
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_get_idle.js
@@ -0,0 +1,24 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+function run_test() {
+ print("Init the fake idle service and check its identity.");
+ let fakeIdleService = Cc["@mozilla.org/widget/useridleservice;1"].getService(
+ Ci.nsIUserIdleService
+ );
+ try {
+ fakeIdleService.QueryInterface(Ci.nsIFactory);
+ } catch (ex) {
+ do_throw("The fake idle service implements nsIFactory.");
+ }
+ // We need at least one PASS, thus sanity check the idle time.
+ Assert.equal(fakeIdleService.idleTime, 0);
+
+ print("Init the real idle service and check its identity.");
+ let realIdleService = do_get_idle();
+ try {
+ realIdleService.QueryInterface(Ci.nsIFactory);
+ do_throw("The real idle service does not implement nsIFactory.");
+ } catch (ex) {}
+}
diff --git a/testing/xpcshell/example/unit/test_import_module.js b/testing/xpcshell/example/unit/test_import_module.js
new file mode 100644
index 0000000000..089ec34f8d
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_import_module.js
@@ -0,0 +1,19 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * Ensures that tests can import a module in the same folder through:
+ * ChromeUtils.importESModule("resource://test/module.jsm");
+ */
+
+function run_test() {
+ Assert.ok(typeof this.MODULE_IMPORTED == "undefined");
+ Assert.ok(typeof this.MODULE_URI == "undefined");
+ let uri = "resource://test/import_module.sys.mjs";
+ let exports = ChromeUtils.importESModule(uri);
+ Assert.ok(exports.MODULE_URI == uri);
+ Assert.ok(exports.MODULE_IMPORTED);
+}
diff --git a/testing/xpcshell/example/unit/test_load.js b/testing/xpcshell/example/unit/test_load.js
new file mode 100644
index 0000000000..ec9b043ffd
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_load.js
@@ -0,0 +1,20 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+var subscriptLoaded = false;
+
+function run_test() {
+ load("load_subscript.js");
+ Assert.ok(subscriptLoaded);
+ subscriptLoaded = false;
+ try {
+ load("file_that_does_not_exist.js");
+ subscriptLoaded = true;
+ } catch (ex) {
+ Assert.equal(ex.message.substring(0, 16), "cannot open file");
+ }
+ Assert.ok(!subscriptLoaded, "load() should throw an error");
+}
diff --git a/testing/xpcshell/example/unit/test_load_httpd_js.js b/testing/xpcshell/example/unit/test_load_httpd_js.js
new file mode 100644
index 0000000000..03a993730d
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_load_httpd_js.js
@@ -0,0 +1,13 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+
+function run_test() {
+ var httpserver = new HttpServer();
+ Assert.notEqual(httpserver, null);
+ Assert.notEqual(httpserver.QueryInterface(Ci.nsIHttpServer), null);
+}
diff --git a/testing/xpcshell/example/unit/test_location.js b/testing/xpcshell/example/unit/test_location.js
new file mode 100644
index 0000000000..3abd2f7910
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_location.js
@@ -0,0 +1,13 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* globals __LOCATION__ */
+
+function run_test() {
+ Assert.equal(__LOCATION__.leafName, "test_location.js");
+ // also check that __LOCATION__ works via load()
+ load("location_load.js");
+}
diff --git a/testing/xpcshell/example/unit/test_multiple_setups.js b/testing/xpcshell/example/unit/test_multiple_setups.js
new file mode 100644
index 0000000000..63d731c8a8
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_multiple_setups.js
@@ -0,0 +1,13 @@
+/* Any copyright is dedicated to the Public Domain.
+http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+let someVar = 0;
+
+add_setup(() => (someVar = 1));
+add_setup(() => (someVar = 2));
+
+add_task(async function test_setup_ordering() {
+ Assert.equal(someVar, 2, "Setups should have run in order.");
+});
diff --git a/testing/xpcshell/example/unit/test_multiple_tasks.js b/testing/xpcshell/example/unit/test_multiple_tasks.js
new file mode 100644
index 0000000000..46d1b21225
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_multiple_tasks.js
@@ -0,0 +1,20 @@
+/* Any copyright is dedicated to the Public Domain.
+http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+let someVar = 0;
+
+add_task(async function test_first() {
+ Assert.equal(someVar, 1, "I should run as the first test task.");
+ someVar++;
+});
+
+add_setup(function setup() {
+ Assert.equal(someVar, 0, "Should run setup first.");
+ someVar++;
+});
+
+add_task(async function test_second() {
+ Assert.equal(someVar, 2, "I should run as the second test task.");
+});
diff --git a/testing/xpcshell/example/unit/test_prefs_defaults.js b/testing/xpcshell/example/unit/test_prefs_defaults.js
new file mode 100644
index 0000000000..c6a93802e9
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_prefs_defaults.js
@@ -0,0 +1,18 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Bug 1781025 - ESLint's use of multi-ini doesn't fully cope with
+// mozilla-central's use of .ini files
+// eslint-disable-next-line no-unused-vars
+function run_test() {
+ /* import-globals-from prefs_test_common.js */
+ load("prefs_test_common.js");
+
+ check_common_xpcshell_with_prefs();
+
+ Assert.ok(
+ !has_pref_from_manifest_file_section(),
+ "Should not have pref that was only assigned to a different test"
+ );
+}
diff --git a/testing/xpcshell/example/unit/test_prefs_defaults_and_file.js b/testing/xpcshell/example/unit/test_prefs_defaults_and_file.js
new file mode 100644
index 0000000000..8a56d49c34
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_prefs_defaults_and_file.js
@@ -0,0 +1,42 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+// Bug 1781025 - ESLint's use of multi-ini doesn't fully cope with
+// mozilla-central's use of .ini files
+// eslint-disable-next-line no-unused-vars
+function run_test() {
+ /* import-globals-from prefs_test_common.js */
+ load("prefs_test_common.js");
+
+ check_common_xpcshell_with_prefs();
+
+ Assert.ok(
+ has_pref_from_manifest_file_section(),
+ "Should have pref set for file in xpcshell-with-prefs.ini"
+ );
+
+ Assert.equal(
+ Services.prefs.getIntPref("dummy.pref.from.test.file"),
+ 2,
+ "Value of pref that was set once at the file in xpcshell-with-prefs.ini"
+ );
+
+ Assert.equal(
+ Services.prefs.getStringPref("dummy.pref.from.test.duplicate"),
+ "final",
+ "The last pref takes precedence when duplicated"
+ );
+
+ Assert.equal(
+ Services.prefs.getIntPref("dummy.pref.from.test.manifest"),
+ 1337,
+ "File-specific pref takes precedence over manifest defaults"
+ );
+
+ Assert.equal(
+ Services.prefs.getStringPref("dummy.pref.from.test.ancestor"),
+ "Ancestor",
+ "Pref in manifest defaults without file-specific override should be set"
+ );
+}
diff --git a/testing/xpcshell/example/unit/test_prefs_defaults_included.js b/testing/xpcshell/example/unit/test_prefs_defaults_included.js
new file mode 100644
index 0000000000..c092faf5c3
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_prefs_defaults_included.js
@@ -0,0 +1,16 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ /* import-globals-from prefs_test_common.js */
+ load("prefs_test_common.js");
+
+ check_common_xpcshell_with_prefs();
+
+ Assert.equal(
+ Services.prefs.getStringPref("dummy.pref.from.test.ancestor"),
+ "ReplacedParent",
+ "Pref set in included test manifest takes precedence over ancestor"
+ );
+}
diff --git a/testing/xpcshell/example/unit/test_prefs_no_defaults.js b/testing/xpcshell/example/unit/test_prefs_no_defaults.js
new file mode 100644
index 0000000000..f4516158b3
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_prefs_no_defaults.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ /* import-globals-from prefs_test_common.js */
+ load("prefs_test_common.js");
+
+ check_common_xpcshell_without_prefs();
+
+ Assert.ok(
+ !has_pref_from_manifest_file_section(),
+ "Should not have pref that was only assigned to a different test"
+ );
+}
diff --git a/testing/xpcshell/example/unit/test_prefs_no_defaults_with_file.js b/testing/xpcshell/example/unit/test_prefs_no_defaults_with_file.js
new file mode 100644
index 0000000000..e098b5d05e
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_prefs_no_defaults_with_file.js
@@ -0,0 +1,15 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ /* import-globals-from prefs_test_common.js */
+ load("prefs_test_common.js");
+
+ check_common_xpcshell_without_prefs();
+
+ Assert.ok(
+ has_pref_from_manifest_file_section(),
+ "Should have pref set for file in xpcshell.ini"
+ );
+}
diff --git a/testing/xpcshell/example/unit/test_profile.js b/testing/xpcshell/example/unit/test_profile.js
new file mode 100644
index 0000000000..f235eb72fa
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_profile.js
@@ -0,0 +1,11 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ /* import-globals-from check_profile.js */
+ load("check_profile.js");
+ check_do_get_profile(false);
+}
diff --git a/testing/xpcshell/example/unit/test_profile_afterChange.js b/testing/xpcshell/example/unit/test_profile_afterChange.js
new file mode 100644
index 0000000000..292cb00eba
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_profile_afterChange.js
@@ -0,0 +1,11 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ /* import-globals-from check_profile.js */
+ load("check_profile.js");
+ check_do_get_profile(true);
+}
diff --git a/testing/xpcshell/example/unit/test_sample.js b/testing/xpcshell/example/unit/test_sample.js
new file mode 100644
index 0000000000..f0aa3df7c6
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_sample.js
@@ -0,0 +1,21 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* This is the most basic testcase. It makes some trivial assertions,
+ * then sets a timeout, and exits the test harness when that timeout
+ * fires. This is meant to demonstrate that there is a complete event
+ * system available to test scripts.
+ * Available functions are described at:
+ * http://developer.mozilla.org/en/docs/Writing_xpcshell-based_unit_tests
+ */
+function run_test() {
+ Assert.equal(57, 57);
+ Assert.notEqual(1, 2);
+ Assert.ok(true);
+
+ do_test_pending();
+ do_timeout(100, do_test_finished);
+}
diff --git a/testing/xpcshell/example/unit/test_skip.js b/testing/xpcshell/example/unit/test_skip.js
new file mode 100644
index 0000000000..0c203cd82e
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_skip.js
@@ -0,0 +1,8 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+function run_test() {
+ // This test expects to fail.
+ Assert.ok(false);
+}
diff --git a/testing/xpcshell/example/unit/test_tasks_skip.js b/testing/xpcshell/example/unit/test_tasks_skip.js
new file mode 100644
index 0000000000..99f3e8d2c2
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_tasks_skip.js
@@ -0,0 +1,21 @@
+"use strict";
+
+add_task(async function skipMeNot1() {
+ Assert.ok(true, "Well well well.");
+});
+
+add_task(async function skipMe1() {
+ Assert.ok(false, "Not skipped after all.");
+}).skip();
+
+add_task(async function skipMeNot2() {
+ Assert.ok(true, "Well well well.");
+});
+
+add_task(async function skipMeNot3() {
+ Assert.ok(true, "Well well well.");
+});
+
+add_task(async function skipMe2() {
+ Assert.ok(false, "Not skipped after all.");
+}).skip();
diff --git a/testing/xpcshell/example/unit/test_tasks_skipall.js b/testing/xpcshell/example/unit/test_tasks_skipall.js
new file mode 100644
index 0000000000..13290e58ba
--- /dev/null
+++ b/testing/xpcshell/example/unit/test_tasks_skipall.js
@@ -0,0 +1,23 @@
+"use strict";
+
+/* eslint-disable mozilla/reject-addtask-only */
+
+add_task(async function skipMe1() {
+ Assert.ok(false, "Not skipped after all.");
+});
+
+add_task(async function skipMe2() {
+ Assert.ok(false, "Not skipped after all.");
+}).skip();
+
+add_task(async function skipMe3() {
+ Assert.ok(false, "Not skipped after all.");
+}).only();
+
+add_task(async function skipMeNot() {
+ Assert.ok(true, "Well well well.");
+}).only();
+
+add_task(async function skipMe4() {
+ Assert.ok(false, "Not skipped after all.");
+});
diff --git a/testing/xpcshell/example/unit/xpcshell-included-with-prefs.ini b/testing/xpcshell/example/unit/xpcshell-included-with-prefs.ini
new file mode 100644
index 0000000000..dc18700c82
--- /dev/null
+++ b/testing/xpcshell/example/unit/xpcshell-included-with-prefs.ini
@@ -0,0 +1,5 @@
+# This file is included by xpcshell-with-prefs.ini
+[DEFAULT]
+prefs = dummy.pref.from.test.ancestor=ReplacedParent
+
+[test_prefs_defaults_included.js]
diff --git a/testing/xpcshell/example/unit/xpcshell-with-prefs.ini b/testing/xpcshell/example/unit/xpcshell-with-prefs.ini
new file mode 100644
index 0000000000..4d5944b272
--- /dev/null
+++ b/testing/xpcshell/example/unit/xpcshell-with-prefs.ini
@@ -0,0 +1,16 @@
+[DEFAULT]
+head =
+support-files = prefs_test_common.js
+prefs =
+ dummy.pref.from.test.ancestor=Ancestor
+ dummy.pref.from.test.manifest=1
+
+[test_prefs_defaults.js]
+[test_prefs_defaults_and_file.js]
+prefs = # Multiple prefs, for additional test coverage over xpcshell.ini
+ dummy.pref.from.test.file=2
+ dummy.pref.from.test.duplicate=first
+ dummy.pref.from.test.duplicate=final
+ dummy.pref.from.test.manifest=1337 # overrides manifest
+
+[include:xpcshell-included-with-prefs.ini]
diff --git a/testing/xpcshell/example/unit/xpcshell.ini b/testing/xpcshell/example/unit/xpcshell.ini
new file mode 100644
index 0000000000..38e3c98363
--- /dev/null
+++ b/testing/xpcshell/example/unit/xpcshell.ini
@@ -0,0 +1,60 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+[DEFAULT]
+head =
+support-files =
+ subdir/file.txt
+ file.txt
+ import_module.sys.mjs
+ load_subscript.js
+ location_load.js
+ check_profile.js
+ prefs_test_common.js
+# NOTE: Do NOT set prefs here. If you do, move test_prefs_no_defaults.js and
+# test_prefs_no_defaults_with_file.js to a new file without a pref definitions.
+
+[test_add_setup.js]
+[test_check_nsIException.js]
+skip-if = os == 'win' && debug
+[test_check_nsIException_failing.js]
+fail-if = true
+skip-if = os == 'win' && debug
+
+[test_do_get_tempdir.js]
+[test_execute_soon.js]
+[test_get_file.js]
+[test_get_idle.js]
+[test_import_module.js]
+[test_load.js]
+[test_load_httpd_js.js]
+[test_location.js]
+[test_multiple_setups.js]
+[test_multiple_tasks.js]
+[test_prefs_no_defaults.js]
+[test_prefs_no_defaults_with_file.js]
+prefs = dummy.pref.from.test.file=1
+[test_profile.js]
+[test_profile_afterChange.js]
+[test_sample.js]
+
+[test_fail.js]
+fail-if = true
+
+[test_skip.js]
+skip-if = true
+
+[test_do_check_null.js]
+skip-if = os == 'win' && debug
+
+[test_do_check_null_failing.js]
+fail-if = true
+skip-if = os == 'win' && debug
+
+[test_do_check_matches.js]
+[test_do_check_matches_failing.js]
+fail-if = true
+
+[test_tasks_skip.js]
+[test_tasks_skipall.js]