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
|
"use strict";
/* exported MockAlertsService */
function mockServicesChromeScript() {
/* eslint-env mozilla/chrome-script */
const MOCK_ALERTS_CID = Components.ID(
"{48068bc2-40ab-4904-8afd-4cdfb3a385f3}"
);
const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";
const { setTimeout } = ChromeUtils.importESModule(
"resource://gre/modules/Timer.sys.mjs"
);
const registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
let activeNotifications = Object.create(null);
const mockAlertsService = {
showPersistentNotification: function (
persistentData,
alert,
alertListener
) {
this.showAlert(alert, alertListener);
},
showAlert: function (alert, listener) {
activeNotifications[alert.name] = {
listener: listener,
cookie: alert.cookie,
title: alert.title,
};
// fake async alert show event
if (listener) {
setTimeout(function () {
listener.observe(null, "alertshow", alert.cookie);
}, 100);
}
},
showAlertNotification: function (
imageUrl,
title,
text,
textClickable,
cookie,
alertListener,
name
) {
this.showAlert(
{
name: name,
cookie: cookie,
title: title,
},
alertListener
);
},
closeAlert: function (name) {
let alertNotification = activeNotifications[name];
if (alertNotification) {
if (alertNotification.listener) {
alertNotification.listener.observe(
null,
"alertfinished",
alertNotification.cookie
);
}
delete activeNotifications[name];
}
},
QueryInterface: ChromeUtils.generateQI(["nsIAlertsService"]),
createInstance: function (iid) {
return this.QueryInterface(iid);
},
};
registrar.registerFactory(
MOCK_ALERTS_CID,
"alerts service",
ALERTS_SERVICE_CONTRACT_ID,
mockAlertsService
);
function clickNotifications(doClose) {
// Until we need to close a specific notification, just click them all.
for (let [name, notification] of Object.entries(activeNotifications)) {
let { listener, cookie } = notification;
listener.observe(null, "alertclickcallback", cookie);
if (doClose) {
mockAlertsService.closeAlert(name);
}
}
}
function closeAllNotifications() {
for (let alertName of Object.keys(activeNotifications)) {
mockAlertsService.closeAlert(alertName);
}
}
const { addMessageListener, sendAsyncMessage } = this;
addMessageListener("mock-alert-service:unregister", () => {
closeAllNotifications();
activeNotifications = null;
registrar.unregisterFactory(MOCK_ALERTS_CID, mockAlertsService);
sendAsyncMessage("mock-alert-service:unregistered");
});
addMessageListener(
"mock-alert-service:click-notifications",
clickNotifications
);
addMessageListener(
"mock-alert-service:close-notifications",
closeAllNotifications
);
sendAsyncMessage("mock-alert-service:registered");
}
const MockAlertsService = {
async register() {
if (this._chromeScript) {
throw new Error("MockAlertsService already registered");
}
this._chromeScript = SpecialPowers.loadChromeScript(
mockServicesChromeScript
);
await this._chromeScript.promiseOneMessage("mock-alert-service:registered");
},
async unregister() {
if (!this._chromeScript) {
throw new Error("MockAlertsService not registered");
}
this._chromeScript.sendAsyncMessage("mock-alert-service:unregister");
return this._chromeScript
.promiseOneMessage("mock-alert-service:unregistered")
.then(() => {
this._chromeScript.destroy();
this._chromeScript = null;
});
},
async clickNotifications() {
// Most implementations of the nsIAlertsService automatically close upon click.
await this._chromeScript.sendAsyncMessage(
"mock-alert-service:click-notifications",
true
);
},
async clickNotificationsWithoutClose() {
// The implementation on macOS does not automatically close the notification.
await this._chromeScript.sendAsyncMessage(
"mock-alert-service:click-notifications",
false
);
},
async closeNotifications() {
await this._chromeScript.sendAsyncMessage(
"mock-alert-service:close-notifications"
);
},
};
|