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-07 09:22:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:22:09 +0000
commit43a97878ce14b72f0981164f87f2e35e14151312 (patch)
tree620249daf56c0258faa40cbdcf9cfba06de2a846 /toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
parentInitial commit. (diff)
downloadfirefox-upstream.tar.xz
firefox-upstream.zip
Adding upstream version 110.0.1.upstream/110.0.1upstream
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.js112
1 files changed, 112 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..921f91207e
--- /dev/null
+++ b/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
@@ -0,0 +1,112 @@
+/* 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.import(
+ "resource://gre/modules/TaskScheduler.jsm"
+);
+const { MacOSImpl } = ChromeUtils.import(
+ "resource://gre/modules/TaskSchedulerMacOSImpl.jsm"
+);
+
+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)}`
+ );
+});