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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
var { FileUtils } = ChromeUtils.import("resource://gre/modules/FileUtils.jsm");
var { HandlerServiceTestUtils } = ChromeUtils.import(
"resource://testing-common/HandlerServiceTestUtils.jsm"
);
var gMimeSvc = Cc["@mozilla.org/mime;1"].getService(Ci.nsIMIMEService);
var gHandlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].getService(
Ci.nsIHandlerService
);
function createMockedHandlerApp() {
// Mock the executable
let mockedExecutable = FileUtils.getFile("TmpD", ["mockedExecutable"]);
if (!mockedExecutable.exists()) {
mockedExecutable.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o755);
}
// Mock the handler app
let mockedHandlerApp = Cc[
"@mozilla.org/uriloader/local-handler-app;1"
].createInstance(Ci.nsILocalHandlerApp);
mockedHandlerApp.executable = mockedExecutable;
mockedHandlerApp.detailedDescription = "Mocked handler app";
registerCleanupFunction(function() {
// remove the mocked executable from disk.
if (mockedExecutable.exists()) {
mockedExecutable.remove(true);
}
});
return mockedHandlerApp;
}
function createMockedObjects(createHandlerApp) {
// Mock the mime info
let internalMockedMIME = gMimeSvc.getFromTypeAndExtension(
"text/x-test-handler",
null
);
internalMockedMIME.alwaysAskBeforeHandling = true;
internalMockedMIME.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
internalMockedMIME.appendExtension("abc");
if (createHandlerApp) {
let mockedHandlerApp = createMockedHandlerApp();
internalMockedMIME.description = mockedHandlerApp.detailedDescription;
internalMockedMIME.possibleApplicationHandlers.appendElement(
mockedHandlerApp
);
internalMockedMIME.preferredApplicationHandler = mockedHandlerApp;
}
// Proxy for the mocked MIME info for faking the read-only attributes
let mockedMIME = new Proxy(internalMockedMIME, {
get(target, property) {
switch (property) {
case "hasDefaultHandler":
return true;
case "defaultDescription":
return "Default description";
default:
return target[property];
}
},
});
// Mock the launcher:
let mockedLauncher = {
MIMEInfo: mockedMIME,
source: Services.io.newURI("http://www.mozilla.org/"),
suggestedFileName: "test_download_dialog.abc",
targetFileIsExecutable: false,
saveToDisk() {},
cancel() {},
launchWithApplication() {},
setWebProgressListener() {},
saveDestinationAvailable() {},
contentLength: 42,
targetFile: null, // never read
// PRTime is microseconds since epoch, Date.now() returns milliseconds:
timeDownloadStarted: Date.now() * 1000,
QueryInterface: ChromeUtils.generateQI([
"nsICancelable",
"nsIHelperAppLauncher",
]),
};
registerCleanupFunction(function() {
// remove the mocked mime info from database.
let mockHandlerInfo = gMimeSvc.getFromTypeAndExtension(
"text/x-test-handler",
null
);
if (gHandlerSvc.exists(mockHandlerInfo)) {
gHandlerSvc.remove(mockHandlerInfo);
}
});
return mockedLauncher;
}
async function openHelperAppDialog(launcher) {
let helperAppDialog = Cc[
"@mozilla.org/helperapplauncherdialog;1"
].createInstance(Ci.nsIHelperAppLauncherDialog);
let helperAppDialogShownPromise = BrowserTestUtils.domWindowOpenedAndLoaded();
try {
helperAppDialog.show(launcher, window, "foopy");
} catch (ex) {
ok(
false,
"Trying to show unknownContentType.xhtml failed with exception: " + ex
);
Cu.reportError(ex);
}
let dlg = await helperAppDialogShownPromise;
is(
dlg.location.href,
"chrome://mozapps/content/downloads/unknownContentType.xhtml",
"Got correct dialog"
);
return dlg;
}
async function waitForSubDialog(browser, url, state) {
let eventStr = state ? "dialogopen" : "dialogclose";
let tabDialogBox = gBrowser.getTabDialogBox(browser);
let dialogStack = tabDialogBox.getTabDialogManager()._dialogStack;
let checkFn;
if (state) {
checkFn = dialogEvent => dialogEvent.detail.dialog?._openedURL == url;
}
let event = await BrowserTestUtils.waitForEvent(
dialogStack,
eventStr,
true,
checkFn
);
let { dialog } = event.detail;
// If the dialog is closing wait for it to be fully closed before resolving
if (!state) {
await dialog._closingPromise;
}
return event.detail.dialog;
}
/**
* Wait for protocol permission dialog open/close.
* @param {MozBrowser} browser - Browser element the dialog belongs to.
* @param {boolean} state - true: dialog open, false: dialog close
* @returns {Promise<SubDialog>} - Returns a promise which resolves with the
* SubDialog object of the dialog which closed or opened.
*/
async function waitForProtocolPermissionDialog(browser, state) {
return waitForSubDialog(
browser,
"chrome://mozapps/content/handling/permissionDialog.xhtml",
state
);
}
/**
* Wait for protocol app chooser dialog open/close.
* @param {MozBrowser} browser - Browser element the dialog belongs to.
* @param {boolean} state - true: dialog open, false: dialog close
* @returns {Promise<SubDialog>} - Returns a promise which resolves with the
* SubDialog object of the dialog which closed or opened.
*/
async function waitForProtocolAppChooserDialog(browser, state) {
return waitForSubDialog(
browser,
"chrome://mozapps/content/handling/appChooser.xhtml",
state
);
}
async function promiseDownloadFinished(list) {
return new Promise(resolve => {
list.addView({
onDownloadChanged(download) {
info("Download changed!");
if (download.succeeded || download.error) {
info("Download succeeded or errored");
list.removeView(this);
resolve(download);
}
},
});
});
}
function setupMailHandler() {
let mailHandlerInfo = HandlerServiceTestUtils.getHandlerInfo("mailto");
let gOldMailHandlers = [];
// Remove extant web handlers because they have icons that
// we fetch from the web, which isn't allowed in tests.
let handlers = mailHandlerInfo.possibleApplicationHandlers;
for (let i = handlers.Count() - 1; i >= 0; i--) {
try {
let handler = handlers.queryElementAt(i, Ci.nsIWebHandlerApp);
gOldMailHandlers.push(handler);
// If we get here, this is a web handler app. Remove it:
handlers.removeElementAt(i);
} catch (ex) {}
}
let previousHandling = mailHandlerInfo.alwaysAskBeforeHandling;
mailHandlerInfo.alwaysAskBeforeHandling = true;
// Create a dummy web mail handler so we always know the mailto: protocol.
// Without this, the test fails on VMs without a default mailto: handler,
// because no dialog is ever shown, as we ignore subframe navigations to
// protocols that cannot be handled.
let dummy = Cc["@mozilla.org/uriloader/web-handler-app;1"].createInstance(
Ci.nsIWebHandlerApp
);
dummy.name = "Handler 1";
dummy.uriTemplate = "https://example.com/first/%s";
mailHandlerInfo.possibleApplicationHandlers.appendElement(dummy);
gHandlerSvc.store(mailHandlerInfo);
registerCleanupFunction(() => {
// Re-add the original protocol handlers:
let mailHandlers = mailHandlerInfo.possibleApplicationHandlers;
for (let i = handlers.Count() - 1; i >= 0; i--) {
try {
// See if this is a web handler. If it is, it'll throw, otherwise,
// we will remove it.
mailHandlers.queryElementAt(i, Ci.nsIWebHandlerApp);
mailHandlers.removeElementAt(i);
} catch (ex) {}
}
for (let h of gOldMailHandlers) {
mailHandlers.appendElement(h);
}
mailHandlerInfo.alwaysAskBeforeHandling = previousHandling;
gHandlerSvc.store(mailHandlerInfo);
});
}
let gDownloadDir;
async function setDownloadDir() {
let tmpDir = await PathUtils.getTempDir();
tmpDir = PathUtils.join(
tmpDir,
"testsavedir" + Math.floor(Math.random() * 2 ** 32)
);
// Create this dir if it doesn't exist (ignores existing dirs)
await IOUtils.makeDirectory(tmpDir);
registerCleanupFunction(async function() {
try {
await IOUtils.remove(tmpDir, { recursive: true });
} catch (e) {
Cu.reportError(e);
}
});
Services.prefs.setIntPref("browser.download.folderList", 2);
Services.prefs.setCharPref("browser.download.dir", tmpDir);
return tmpDir;
}
add_task(async function test_common_initialize() {
gDownloadDir = await setDownloadDir();
Services.prefs.setCharPref("browser.download.loglevel", "Debug");
});
|