summaryrefslogtreecommitdiffstats
path: root/toolkit/components/taskscheduler/tests/xpcshell/test_TaskSchedulerMacOSImpl.js
blob: d087c77446da1eca811387f94c1ebb38956b646e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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)}`
  );
});