Skip to content

Commit

Permalink
Bug 1724319 - Fix pdfjs component tests for when new download changes…
Browse files Browse the repository at this point in the history
… are enabled. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D127009
  • Loading branch information
sfoster committed Sep 30, 2021
1 parent c20f9eb commit 1a5c6e7
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 3 deletions.
123 changes: 120 additions & 3 deletions toolkit/components/pdfjs/test/browser_pdfjs_octet_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,16 @@ add_task(async function test_octet_stream_opens_pdfjs() {
});

/**
* Check that if the octet-stream thing is in a frame, we don't load it inside
* PDF.js
* Check that when the improvements_to_download_panel pref is false and
* if the octet-stream thing is in a frame, we don't load it inside PDF.js
*/
add_task(async function test_octet_stream_in_frame_downloads() {
await SpecialPowers.pushPrefEnv({ set: [["pdfjs.handleOctetStream", true]] });
await SpecialPowers.pushPrefEnv({
set: [
["pdfjs.handleOctetStream", true],
["browser.download.improvements_to_download_panel", false],
],
});

let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/pdf",
Expand Down Expand Up @@ -79,3 +84,115 @@ add_task(async function test_octet_stream_in_frame_downloads() {
}
);
});

/**
* Check that when the improvements_to_download_panel pref is true and
* if the octet-stream thing is in a frame, we don't load it inside PDF.js
*/
add_task(
async function test_octet_stream_in_frame_downloads_improvements_to_download_panel() {
await SpecialPowers.pushPrefEnv({
set: [
["pdfjs.handleOctetStream", true],
["browser.download.improvements_to_download_panel", true],
],
});

let handlerInfo = gMIMEService.getFromTypeAndExtension(
"application/pdf",
"pdf"
);

// Make sure pdf.js is the default handler.
is(
handlerInfo.alwaysAskBeforeHandling,
false,
"pdf handler defaults to always-ask is false"
);
is(
handlerInfo.preferredAction,
Ci.nsIHandlerInfo.handleInternally,
"pdf handler defaults to internal"
);

let downloadsPanelPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popupshown"
);

// Once downloaded, the PDF will be opened as a file:// URI in a new tab
let previewTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
url => {
let uri = NetUtil.newURI(url);
return uri.scheme == "file" && uri.spec.endsWith(".pdf");
},
false, // dont wait for load
true // any tab, not just the next one
);

await BrowserTestUtils.withNewTab(
{ gBrowser, url: `data:text/html,<iframe src='${PDF_URL}'>` },
async function(newTabBrowser) {
// wait until downloadsPanel opens before continuing with test
info("Waiting for download panel to open");
await downloadsPanelPromise;
is(
DownloadsPanel.panel.state,
"open",
"Check the download panel state is 'open'"
);
let downloadList = await Downloads.getList(Downloads.PUBLIC);
let [download] = downloadList._downloads;

// Verify the downloaded PDF opened in a new tab,
// with its download file URI
info("Waiting for preview tab");
let previewTab = await previewTabPromise;
ok(previewTab, "PDF opened in a new tab");

is(
DownloadsPanel.isPanelShowing,
true,
"DownloadsPanel should be open."
);
is(
downloadList._downloads.length,
1,
"File should be successfully downloaded."
);

await BrowserTestUtils.removeTab(previewTab);

info("cleaning up downloads");
try {
if (Services.appinfo.OS === "WINNT") {
// We need to make the file writable to delete it on Windows.
await IOUtils.setPermissions(download.target.path, 0o600);
}
await IOUtils.remove(download.target.path);
} catch (error) {
info(
"The file " + download.target.path + " is not removed, " + error
);
}
await downloadList.remove(download);
await download.finalize();

if (DownloadsPanel.panel.state !== "closed") {
let hiddenPromise = BrowserTestUtils.waitForEvent(
DownloadsPanel.panel,
"popuphidden"
);
DownloadsPanel.hidePanel();
await hiddenPromise;
}
is(
DownloadsPanel.panel.state,
"closed",
"Check that the download panel is closed"
);
}
);
}
);
10 changes: 10 additions & 0 deletions toolkit/components/pdfjs/test/browser_pdfjs_savedialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ const RELATIVE_DIR = "toolkit/components/pdfjs/test/";
const TESTROOT = "http://example.com/browser/" + RELATIVE_DIR;

function test() {
// When the download panel improvements pref is true, we expect the PDF to be simply downloaded
// So it only makes sense to run this test with the pref set false
Services.prefs.setBoolPref(
"browser.download.improvements_to_download_panel",
false
);
var oldAction = changeMimeHandler(Ci.nsIHandlerInfo.useSystemDefault, true);
var tab = BrowserTestUtils.addTab(gBrowser, TESTROOT + "file_pdfjs_test.pdf");

// Test: "Open with" dialog comes up when pdf.js is not selected as the default
// handler.
addWindowListener(
Expand All @@ -16,6 +23,9 @@ function test() {

waitForExplicitFinish();
registerCleanupFunction(function() {
Services.prefs.clearUserPref(
"browser.download.improvements_to_download_panel"
);
changeMimeHandler(oldAction[0], oldAction[1]);
gBrowser.removeTab(tab);
});
Expand Down

0 comments on commit 1a5c6e7

Please sign in to comment.