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
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test for alerts with requireInteraction</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const chromeScript = SpecialPowers.loadChromeScript(_ => {
/* eslint-env mozilla/chrome-script */
const {clearTimeout, setTimeout} = ChromeUtils.importESModule(
"resource://gre/modules/Timer.sys.mjs"
);
addMessageListener("waitForXULAlert", function() {
var timer = setTimeout(function() {
Services.ww.unregisterNotification(windowObserver);
sendAsyncMessage("waitForXULAlert", false);
}, 2000);
var windowObserver = function(win, aTopic, aData) {
if (aTopic != "domwindowopened") {
return;
}
win.addEventListener("load", function() {
let windowType = win.document.documentElement.getAttribute("windowtype");
if (windowType == "alert:alert") {
clearTimeout(timer);
Services.ww.unregisterNotification(windowObserver);
sendAsyncMessage("waitForXULAlert", true);
}
}, {once: true});
};
Services.ww.registerNotification(windowObserver);
});
});
var cookie = 0;
function promiseCreateXULAlert(alertService, listener, name) {
return new Promise(resolve => {
chromeScript.addMessageListener("waitForXULAlert", function waitedForAlert(result) {
chromeScript.removeMessageListener("waitForXULAlert", waitedForAlert);
resolve(result);
});
chromeScript.sendAsyncMessage("waitForXULAlert");
alertService.showAlertNotification(null, "title", "body",
true, cookie++, listener, name, null, null, null,
null, false, true);
});
}
add_task(async function test_require_interaction() {
if (!("@mozilla.org/alerts-service;1" in Cc)) {
todo(false, "Alerts service does not exist in this application.");
return;
}
ok(true, "Alerts service exists in this application.");
var alertService;
try {
alertService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
ok(true, "Alerts service is available.");
} catch (ex) {
todo(false, "Alerts service is not available.");
return;
}
await SpecialPowers.pushPrefEnv({"set": [
[ "dom.webnotifications.requireinteraction.enabled", true ],
[ "dom.webnotifications.requireinteraction.count", 2 ],
]});
var expectedSequence = [
"first show",
"second show",
"second finished",
"second replacement show",
"third finished",
"first finished",
"third replacement show",
"second replacement finished",
"third replacement finished",
];
var actualSequence = [];
function createAlertListener(name, showCallback, finishCallback) {
return (subject, topic, data) => {
if (topic == "alertshow") {
actualSequence.push(name + " show");
if (showCallback) {
showCallback();
}
} else if (topic == "alertfinished") {
actualSequence.push(name + " finished");
if (finishCallback) {
finishCallback();
}
}
};
}
var xulAlertCreated = await promiseCreateXULAlert(alertService,
createAlertListener("first"), "first");
if (!xulAlertCreated) {
ok(true, "Platform does not use XUL alerts.");
alertService.closeAlert("first");
return;
}
xulAlertCreated = await promiseCreateXULAlert(alertService,
createAlertListener("second"), "second");
ok(xulAlertCreated, "Create XUL alert");
// Replace second alert
xulAlertCreated = await promiseCreateXULAlert(alertService,
createAlertListener("second replacement"), "second");
ok(xulAlertCreated, "Create XUL alert");
var testFinishResolve;
var testFinishPromise = new Promise((resolve) => { testFinishResolve = resolve; });
xulAlertCreated = await promiseCreateXULAlert(alertService,
createAlertListener("third"), "third");
ok(!xulAlertCreated, "XUL alert should not be visible");
// Replace the not-yet-visible third alert.
xulAlertCreated = await promiseCreateXULAlert(alertService,
createAlertListener("third replacement",
function showCallback() {
alertService.closeAlert("second");
alertService.closeAlert("third");
},
function finishCallback() {
// Check actual sequence of alert events compared to expected sequence.
for (var i = 0; i < actualSequence.length; i++) {
is(actualSequence[i], expectedSequence[i],
"Alert callback at index " + i + " should be in expected order.");
}
testFinishResolve();
}), "third");
ok(!xulAlertCreated, "XUL alert should not be visible");
alertService.closeAlert("first");
await testFinishPromise;
});
</script>
</pre>
</body>
</html>
|