2 getRootDirectory(gTestPath).replace(
3 "chrome://mochitests/content",
5 ) + "file_blocking_image.html";
8 * Loading an image from https:// should work.
10 add_task(async function load_image_from_https_test() {
11 let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
12 await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
14 gBrowser.selectedTab = tab;
16 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
17 function imgListener(img) {
18 return new Promise((resolve, reject) => {
19 img.addEventListener("load", () => resolve());
20 img.addEventListener("error", () => reject());
24 let img = content.document.createElement("img");
25 img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
26 content.document.body.appendChild(img);
29 await imgListener(img);
36 gBrowser.removeTab(tab);
40 * Loading an image from http:// should be rejected.
42 add_task(async function load_image_from_http_test() {
43 let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
44 await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
46 gBrowser.selectedTab = tab;
48 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
49 function imgListener(img) {
50 return new Promise((resolve, reject) => {
51 img.addEventListener("load", () => reject());
52 img.addEventListener("error", () => resolve());
56 let img = content.document.createElement("img");
57 img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
58 content.document.body.appendChild(img);
61 await imgListener(img);
68 gBrowser.removeTab(tab);
72 * Loading an image from http:// immediately after loading from https://
73 * The load from https:// should be replaced.
75 add_task(async function load_https_and_http_test() {
76 let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
77 await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
79 // Clear image cache, otherwise in non-e10s mode the image might be cached by
80 // previous test, and make the image from https is loaded immediately.
81 let imgTools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
82 let imageCache = imgTools.getImgCacheForDocument(window.document);
83 imageCache.clearCache(false); // false=content
85 gBrowser.selectedTab = tab;
87 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
88 function imgListener(img) {
89 return new Promise((resolve, reject) => {
90 img.addEventListener("load", () => reject());
91 img.addEventListener("error", () => resolve());
95 let img = content.document.createElement("img");
96 img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
97 img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
99 content.document.body.appendChild(img);
102 await imgListener(img);
109 gBrowser.removeTab(tab);
113 * Loading an image from https.
114 * Then after we have size information of the image, we immediately change the
115 * location to a http:// site (hence should be blocked by CSP).
117 add_task(async function block_pending_request_test() {
118 let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
119 await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
121 gBrowser.selectedTab = tab;
123 await SpecialPowers.spawn(tab.linkedBrowser, [], async function() {
128 sizeAvailable(request) {
129 // In non-e10s mode the image may have already been cached, so sometimes
130 // sizeAvailable() is called earlier then waitUntilSizeAvailable().
134 this._sizeAvail = true;
138 waitUntilSizeAvailable() {
139 return new Promise(resolve => {
140 this._resolve = resolve;
141 if (this._sizeAvail) {
147 QueryInterface(aIID) {
148 if (aIID.equals(Ci.imgIScriptedNotificationObserver)) {
151 throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
155 let observer = Cc["@mozilla.org/image/tools;1"]
156 .getService(Ci.imgITools)
157 .createScriptedObserver(wrapper);
159 let img = content.document.createElement("img");
160 img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
162 let req = img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST);
163 img.addObserver(observer);
165 content.document.body.appendChild(img);
167 info("Wait until Size Available");
168 await wrapper.waitUntilSizeAvailable();
169 info("Size Available now!");
170 img.removeObserver(observer);
172 // Now we change to load from http:// site, which will be blocked.
173 img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
176 img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST),
178 "CURRENT_REQUEST shouldn't be replaced."
182 gBrowser.removeTab(tab);