fune/toolkit/content/tests/browser/browser_saveImageURL.js
Kris Maglione 031076f2f3 Bug 1463291: Move docShell getter from Document to Window. r=bz
DocShells are associated with outer DOM Windows, rather than Documents, so
having the getter on the document is a bit odd to begin with. But it's also
considerably less convenient, since most of the times when we want a docShell
from JS, we're dealing most directly with a window, and have to detour through
the document to get it.

MozReview-Commit-ID: LUj1H9nG3QL

--HG--
extra : source : fcfb99baa0f0fb60a7c420a712c6ae7c72576871
extra : histedit_source : 5be9b7b29a52a4b8376ee0bdfc5c08b12e3c775a
2018-05-21 16:58:23 -07:00

89 lines
2.9 KiB
JavaScript

"use strict";
const IMAGE_PAGE = "https://example.com/browser/toolkit/content/tests/browser/image_page.html";
const PREF_UNSAFE_FORBIDDEN = "dom.ipc.cpows.forbid-unsafe-from-browser";
var MockFilePicker = SpecialPowers.MockFilePicker;
MockFilePicker.init(window);
MockFilePicker.returnValue = MockFilePicker.returnCancel;
registerCleanupFunction(function() {
MockFilePicker.cleanup();
});
function waitForFilePicker() {
return new Promise((resolve) => {
MockFilePicker.showCallback = () => {
MockFilePicker.showCallback = null;
ok(true, "Saw the file picker");
resolve();
};
});
}
/**
* Test that saveImageURL works when we pass in the aIsContentWindowPrivate
* argument instead of a document. This is the preferred API.
*/
add_task(async function preferred_API() {
await BrowserTestUtils.withNewTab({
gBrowser,
url: IMAGE_PAGE,
}, async function(browser) {
let url = await ContentTask.spawn(browser, null, async function() {
let image = content.document.getElementById("image");
return image.href;
});
saveImageURL(url, "image.jpg", null, true, false, null, null, null, null,
false, gBrowser.contentPrincipal);
let channel = gBrowser.contentWindowAsCPOW.docShell.currentDocumentChannel;
if (channel) {
ok(true, channel.QueryInterface(Ci.nsIHttpChannelInternal)
.channelIsForDownload);
// Throttleable is the only class flag assigned to downloads.
ok(channel.QueryInterface(Ci.nsIClassOfService).classFlags,
Ci.nsIClassOfService.Throttleable);
}
await waitForFilePicker();
});
});
/**
* Test that saveImageURL will still work when passed a document instead
* of the aIsContentWindowPrivate argument. This is the deprecated API, and
* will not work in apps using remote browsers having PREF_UNSAFE_FORBIDDEN
* set to true.
*/
add_task(async function deprecated_API() {
await BrowserTestUtils.withNewTab({
gBrowser,
url: IMAGE_PAGE,
}, async function(browser) {
await pushPrefs([PREF_UNSAFE_FORBIDDEN, false]);
let url = await ContentTask.spawn(browser, null, async function() {
let image = content.document.getElementById("image");
return image.href;
});
// Now get the document directly from content. If we run this test with
// e10s-enabled, this will be a CPOW, which is forbidden. We'll just
// pass the XUL document instead to test this interface.
let doc = document;
let channel = gBrowser.contentWindowAsCPOW.docShell.currentDocumentChannel;
if (channel) {
ok(true, channel.QueryInterface(Ci.nsIHttpChannelInternal)
.channelIsForDownload);
// Throttleable is the only class flag assigned to downloads.
ok(channel.QueryInterface(Ci.nsIClassOfService).classFlags,
Ci.nsIClassOfService.Throttleable);
}
saveImageURL(url, "image.jpg", null, true, false, null, doc, null, null);
await waitForFilePicker();
});
});