1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 add_task(async function test_file_opening() {
5 // Get a ref to the pdf we want to open.
6 let dirFileObj = getChromeDir(getResolvedURI(gTestPath));
7 dirFileObj.append("file_pdfjs_test.pdf");
9 // Change the defaults.
10 var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true);
12 // Test: "Open with" dialog should not come up, despite pdf.js not being
13 // the default - because files from disk should always use pdfjs, unless
14 // it is forcibly disabled.
15 let openedWindow = false;
16 const windowOpenedPromise = new Promise(resolve => {
18 "chrome://mozapps/content/downloads/unknownContentType.xhtml",
26 // Open the tab with a system principal:
27 var tab = await BrowserTestUtils.openNewForegroundTab(
35 ".textLayer .endOfContent",
36 "Wait for text layer."
39 ok(!openedWindow, "Shouldn't open an unknownContentType window!");
40 await waitForPdfJSClose(tab.linkedBrowser, /* closeTab = */ true);
42 // Now try opening it from the file directory:
43 tab = await BrowserTestUtils.openNewForegroundTab(
45 dirFileObj.parent.path
47 const pdfjsLoadedPromise = BrowserTestUtils.browserLoaded(
50 url => url.endsWith("test.pdf")
52 await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
53 content.document.querySelector("a[href$='test.pdf']").click();
55 await Promise.race([windowOpenedPromise, pdfjsLoadedPromise]);
58 "Shouldn't open an unknownContentType window for PDFs from file: links!"
61 await waitForSelector(
63 ".textLayer .endOfContent",
64 "Wait for text layer."
67 registerCleanupFunction(async () => {
68 if (listenerCleanup) {
71 changeMimeHandler(oldAction[0], oldAction[1]);
72 await waitForPdfJSClose(tab.linkedBrowser, /* closeTab = */ true);
77 function addWindowListener(aURL, aCallback) {
79 onOpenWindow(aXULWindow) {
80 info("window opened, waiting for focus");
82 listenerCleanup = null;
84 var domwindow = aXULWindow.docShell.domWindow;
85 waitForFocus(function () {
87 domwindow.document.location.href,
89 "should have seen the right window open"
97 Services.wm.addListener(listener);
98 listenerCleanup = () => Services.wm.removeListener(listener);