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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=4 ts=4 sts=4 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 { BackgroundTasksUtils } = ChromeUtils.importESModule(
"resource://gre/modules/BackgroundTasksUtils.sys.mjs"
);
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ASRouterTargeting: "resource:///modules/asrouter/ASRouterTargeting.sys.mjs",
});
setupProfileService();
add_task(async function test_withProfileLock() {
let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
);
let profilePath = do_get_profile();
profilePath.append(`test_withProfileLock`);
let profile = profileService.createUniqueProfile(
profilePath,
"test_withProfileLock"
);
await BackgroundTasksUtils.withProfileLock(async lock => {
BackgroundTasksUtils._throwIfNotLocked(lock);
}, profile);
// In debug builds, an unlocked lock crashes via `NS_ERROR` when
// `.directory` is invoked. There's no way to check that the lock
// is unlocked, so we can't realistically test this scenario in
// debug builds.
if (!AppConstants.DEBUG) {
await Assert.rejects(
BackgroundTasksUtils.withProfileLock(async lock => {
lock.unlock();
BackgroundTasksUtils._throwIfNotLocked(lock);
}, profile),
/Profile is not locked/
);
}
});
add_task(async function test_readPreferences() {
let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
);
let profilePath = do_get_profile();
profilePath.append(`test_readPreferences`);
let profile = profileService.createUniqueProfile(
profilePath,
"test_readPreferences"
);
// Before we write any preferences, we fail to read.
await Assert.rejects(
BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readPreferences(null, lock),
profile
),
/NotFoundError/
);
let s = `user_pref("testPref.bool1", true);
user_pref("testPref.bool2", false);
user_pref("testPref.int1", 23);
user_pref("testPref.int2", -1236);
`;
let prefsFile = profile.rootDir.clone();
prefsFile.append("prefs.js");
await IOUtils.writeUTF8(prefsFile.path, s);
// Now we can read all the prefs.
let prefs = await BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readPreferences(null, lock),
profile
);
let expected = {
"testPref.bool1": true,
"testPref.bool2": false,
"testPref.int1": 23,
"testPref.int2": -1236,
};
Assert.deepEqual(prefs, expected, "Prefs read are correct");
// And we can filter the read as well.
prefs = await BackgroundTasksUtils.withProfileLock(
lock =>
BackgroundTasksUtils.readPreferences(name => name.endsWith("1"), lock),
profile
);
expected = {
"testPref.bool1": true,
"testPref.int1": 23,
};
Assert.deepEqual(prefs, expected, "Filtered prefs read are correct");
});
add_task(async function test_readTelemetryClientID() {
let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
);
let profilePath = do_get_profile();
profilePath.append(`test_readTelemetryClientID`);
let profile = profileService.createUniqueProfile(
profilePath,
"test_readTelemetryClientID"
);
// Before we write any state, we fail to read.
await Assert.rejects(
BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readTelemetryClientID(lock),
profile
),
/NotFoundError/
);
let expected = {
clientID: "9cc75672-6830-4cb6-a7fd-089d6c7ce34c",
ecosystemClientID: "752f9d53-73fc-4006-a93c-d3e2e427f238",
};
let prefsFile = profile.rootDir.clone();
prefsFile.append("datareporting");
prefsFile.append("state.json");
await IOUtils.writeUTF8(prefsFile.path, JSON.stringify(expected));
// Now we can read the state.
let state = await BackgroundTasksUtils.withProfileLock(
lock => BackgroundTasksUtils.readTelemetryClientID(lock),
profile
);
Assert.deepEqual(state, expected.clientID, "State read is correct");
});
add_task(
{
skip_if: () => AppConstants.MOZ_BUILD_APP !== "browser", // ASRouter is Firefox-only.
},
async function test_readFirefoxMessagingSystemTargetingSnapshot() {
let profileService = Cc[
"@mozilla.org/toolkit/profile-service;1"
].getService(Ci.nsIToolkitProfileService);
let profilePath = do_get_profile();
profilePath.append(`test_readFirefoxMessagingSystemTargetingSnapshot`);
let profile = profileService.createUniqueProfile(
profilePath,
"test_readFirefoxMessagingSystemTargetingSnapshot"
);
// Before we write any state, we fail to read.
await Assert.rejects(
BackgroundTasksUtils.withProfileLock(
lock =>
BackgroundTasksUtils.readFirefoxMessagingSystemTargetingSnapshot(
lock
),
profile
),
/NotFoundError/
);
// We can't take a full environment snapshot under `xpcshell`. Select a few
// items that do work.
let target = {
// `Date` instances and strings do not `deepEqual` each other.
currentDate: JSON.stringify(
await lazy.ASRouterTargeting.Environment.currentDate
),
firefoxVersion: lazy.ASRouterTargeting.Environment.firefoxVersion,
};
let expected = await lazy.ASRouterTargeting.getEnvironmentSnapshot({
targets: [target],
});
let snapshotFile = profile.rootDir.clone();
snapshotFile.append("targeting.snapshot.json");
await IOUtils.writeUTF8(snapshotFile.path, JSON.stringify(expected));
// Now we can read the state.
let state = await BackgroundTasksUtils.withProfileLock(
lock =>
BackgroundTasksUtils.readFirefoxMessagingSystemTargetingSnapshot(lock),
profile
);
Assert.deepEqual(state, expected, "State read is correct");
}
);
|