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
|
/* 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/. */
/**
* Tests the replace of alerts service with our own. This will let us check if we're
* prompting or not.
*/
var { alertHook } = ChromeUtils.import(
"resource:///modules/activity/alertHook.jsm"
);
var { MailServices } = ChromeUtils.import(
"resource:///modules/MailServices.jsm"
);
var { MockRegistrar } = ChromeUtils.importESModule(
"resource://testing-common/MockRegistrar.sys.mjs"
);
var { PromiseTestUtils } = ChromeUtils.import(
"resource://testing-common/mailnews/PromiseTestUtils.jsm"
);
var { PromiseUtils } = ChromeUtils.importESModule(
"resource://gre/modules/PromiseUtils.sys.mjs"
);
alertHook.init();
// Wait time of 1s for slow debug builds.
const TEST_WAITTIME = 1000;
var gMsgWindow = Cc["@mozilla.org/messenger/msgwindow;1"].createInstance(
Ci.nsIMsgWindow
);
var mockAlertsService;
var cid;
var mailnewsURL;
add_setup(function () {
// First register the mock alerts service.
mockAlertsService = new MockAlertsService();
cid = MockRegistrar.register(
"@mozilla.org/alerts-service;1",
mockAlertsService
);
// A random URL.
let uri = Services.io.newURI("news://localhost:80/1@regular.invalid");
mailnewsURL = uri.QueryInterface(Ci.nsIMsgMailNewsUrl);
});
add_task(async function test_not_shown_to_user_no_url_no_window() {
// Just text, no url or window => expect no error shown to user
MailServices.mailSession.alertUser("test error");
await Promise.race([
PromiseTestUtils.promiseDelay(TEST_WAITTIME).then(result => {
Assert.ok(true, "Alert is not shown with no window or no url present");
}),
mockAlertsService.promise.then(result => {
throw new Error(
"Alert is shown to the user although neither window nor url is present"
);
}),
]);
});
add_task(async function test_shown_to_user() {
// Reset promise state.
mockAlertsService.deferPromise();
// Set a window for the URL.
mailnewsURL.msgWindow = gMsgWindow;
// Text, url and window => expect error shown to user
MailServices.mailSession.alertUser("test error 2", mailnewsURL);
let alertShown = await mockAlertsService.promise;
Assert.ok(alertShown);
});
add_task(async function test_not_shown_to_user_no_window() {
// Reset promise state.
mockAlertsService.deferPromise();
// No window for the URL.
mailnewsURL.msgWindow = null;
// Text, url and no window => export no error shown to user
MailServices.mailSession.alertUser("test error 3", mailnewsURL);
await Promise.race([
PromiseTestUtils.promiseDelay(TEST_WAITTIME).then(result => {
Assert.ok(true, "Alert is not shown with no window but a url present");
}),
mockAlertsService.promise.then(result => {
throw new Error(
"Alert is shown to the user although no window in the mailnewsURL present"
);
}),
]);
});
add_task(function endTest() {
MockRegistrar.unregister(cid);
});
class MockAlertsService {
QueryInterface = ChromeUtils.generateQI(["nsIAlertsService"]);
constructor() {
this._deferredPromise = PromiseUtils.defer();
}
showAlert() {
this._deferredPromise.resolve(true);
}
deferPromise() {
this._deferredPromise = PromiseUtils.defer();
}
get promise() {
return this._deferredPromise.promise;
}
}
|