summaryrefslogtreecommitdiffstats
path: root/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js')
-rw-r--r--toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js b/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
new file mode 100644
index 0000000000..d087c77446
--- /dev/null
+++ b/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
@@ -0,0 +1,108 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+"use strict";
+
+// Unit tests for macOS scheduled task generation.
+
+const { AppConstants } = ChromeUtils.importESModule(
+ "resource://gre/modules/AppConstants.sys.mjs"
+);
+
+const { updateAppInfo } = ChromeUtils.importESModule(
+ "resource://testing-common/AppInfo.sys.mjs"
+);
+updateAppInfo();
+
+const { TaskScheduler } = ChromeUtils.importESModule(
+ "resource://gre/modules/TaskScheduler.sys.mjs"
+);
+const { MacOSImpl } = ChromeUtils.importESModule(
+ "resource://gre/modules/TaskSchedulerMacOSImpl.sys.mjs"
+);
+
+function getFirefoxExecutableFilename() {
+ if (AppConstants.platform === "win") {
+ return AppConstants.MOZ_APP_NAME + ".exe";
+ }
+ return AppConstants.MOZ_APP_NAME;
+}
+
+// Returns a nsIFile to the firefox.exe (really, application) executable file.
+function getFirefoxExecutableFile() {
+ let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
+ file = Services.dirsvc.get("GreBinD", Ci.nsIFile);
+
+ file.append(getFirefoxExecutableFilename());
+ return file;
+}
+
+const uuidGenerator = Services.uuid;
+
+function randomName() {
+ return (
+ "moz-taskschd-test-" + uuidGenerator.generateUUID().toString().slice(1, -1)
+ );
+}
+
+add_task(async function test_all() {
+ let labels;
+ Assert.notEqual(await MacOSImpl._uid(), 0, "Should not be running as root");
+
+ let id1 = randomName();
+ let id2 = randomName();
+ Assert.notEqual(id1, id2, "Random labels should not collide");
+
+ await MacOSImpl.registerTask(
+ id1,
+ getFirefoxExecutableFile().path,
+ TaskScheduler.MIN_INTERVAL_SECONDS,
+ { disabled: true }
+ );
+
+ await MacOSImpl.registerTask(
+ id2,
+ getFirefoxExecutableFile().path,
+ TaskScheduler.MIN_INTERVAL_SECONDS,
+ { disabled: true }
+ );
+
+ let label1 = MacOSImpl._formatLabelForThisApp(id1);
+ let label2 = MacOSImpl._formatLabelForThisApp(id2);
+
+ // We don't assert equality because there may be existing tasks, concurrent
+ // tests, etc. This also means we can't reasonably tests `deleteAllTasks()`.
+ labels = await MacOSImpl._listAllLabelsForThisApp();
+ Assert.ok(
+ labels.includes(label1),
+ `Task ${label1} should have been registered in ${JSON.stringify(labels)}`
+ );
+ Assert.ok(
+ labels.includes(label2),
+ `Task ${label2} should have been registered in ${JSON.stringify(labels)}`
+ );
+
+ Assert.ok(await MacOSImpl.deleteTask(id1));
+
+ labels = await MacOSImpl._listAllLabelsForThisApp();
+ Assert.ok(
+ !labels.includes(label1),
+ `Task ${label1} should no longer be registered in ${JSON.stringify(labels)}`
+ );
+ Assert.ok(
+ labels.includes(label2),
+ `Task ${label2} should still be registered in ${JSON.stringify(labels)}`
+ );
+
+ Assert.ok(await MacOSImpl.deleteTask(id2));
+
+ labels = await MacOSImpl._listAllLabelsForThisApp();
+ Assert.ok(
+ !labels.includes(label1),
+ `Task ${label1} should no longer be registered in ${JSON.stringify(labels)}`
+ );
+ Assert.ok(
+ !labels.includes(label2),
+ `Task ${label2} should no longer be registered in ${JSON.stringify(labels)}`
+ );
+});