mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-21 09:49:14 +00:00
Bug 1886518 - [remote] Add cached resource test for images and scripts r=webdriver-reviewers,Sasha
Differential Revision: https://phabricator.services.mozilla.com/D224505
This commit is contained in:
parent
df1da866ca
commit
b21e6d226e
@ -3,7 +3,7 @@ tags = "remote"
|
||||
subsuite = "remote"
|
||||
support-files = [
|
||||
"head.js",
|
||||
"cached_style.sjs",
|
||||
"cached_resource.sjs",
|
||||
]
|
||||
prefs = ["remote.messagehandler.modulecache.useBrowserTestRoot=true"]
|
||||
|
||||
|
@ -2,73 +2,181 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
const STYLESHEET_URL =
|
||||
"https://example.com/browser/remote/shared/listeners/test/browser/cached_style.sjs";
|
||||
const CACHED_RESOURCE_URL =
|
||||
"https://example.com/browser/remote/shared/listeners/test/browser/cached_resource.sjs";
|
||||
const STYLESHEET_URL = `${CACHED_RESOURCE_URL}?type=stylesheet`;
|
||||
const SCRIPT_URL = `${CACHED_RESOURCE_URL}?type=script`;
|
||||
const IMAGE_URL = `${CACHED_RESOURCE_URL}?type=image`;
|
||||
|
||||
add_task(async function test_only_for_observed_context() {
|
||||
// Clear the cache.
|
||||
Services.cache2.clear();
|
||||
add_task(async function test_stylesheet() {
|
||||
for (const type of ["stylesheet", "script", "image"]) {
|
||||
// Clear the cache.
|
||||
Services.cache2.clear();
|
||||
|
||||
const tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(
|
||||
gBrowser,
|
||||
"https://example.com/document-builder.sjs?html=cached_css_testpage"
|
||||
));
|
||||
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
|
||||
const topContext = tab.linkedBrowser.browsingContext;
|
||||
const tab = (gBrowser.selectedTab = BrowserTestUtils.addTab(
|
||||
gBrowser,
|
||||
`https://example.com/document-builder.sjs?html=cached_${type}_testpage`
|
||||
));
|
||||
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
|
||||
const topContext = tab.linkedBrowser.browsingContext;
|
||||
|
||||
// Setup the cached resource listener and load a stylesheet in a link tag, no
|
||||
// event should be received.
|
||||
await setupCachedListener(topContext);
|
||||
await loadStylesheet(topContext, STYLESHEET_URL);
|
||||
let cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(cachedEventCount, 0, "No cached event received for the initial load");
|
||||
// Setup the cached resource listener and load a resource, no event should
|
||||
// be received.
|
||||
await setupCachedListener(topContext);
|
||||
await loadCachedResource(topContext, type);
|
||||
let cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(
|
||||
cachedEventCount,
|
||||
0,
|
||||
`[${type}] No cached event received for the initial load`
|
||||
);
|
||||
|
||||
// Destroy listener before reloading.
|
||||
await destroyCachedListener(topContext);
|
||||
// Destroy listener before reloading.
|
||||
await destroyCachedListener(topContext);
|
||||
|
||||
// Reload, prepare the cached resource listener and load the same stylesheet
|
||||
// again.
|
||||
await BrowserTestUtils.reloadTab(tab);
|
||||
await setupCachedListener(topContext);
|
||||
await loadStylesheet(topContext, STYLESHEET_URL);
|
||||
// Reload, prepare the cached resource listener and load the same resource
|
||||
// again.
|
||||
await BrowserTestUtils.reloadTab(tab);
|
||||
await setupCachedListener(topContext);
|
||||
await loadCachedResource(topContext, type);
|
||||
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(cachedEventCount, 1, "1 cached event received for the second load");
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
(type === "script" ? todo_is : is)(
|
||||
cachedEventCount,
|
||||
1,
|
||||
`[${type}] 1 cached event received for the second load`
|
||||
);
|
||||
|
||||
const iframeContext = await createIframeContext(topContext);
|
||||
await setupCachedListener(iframeContext);
|
||||
await loadStylesheet(iframeContext, STYLESHEET_URL);
|
||||
const iframeContext = await createIframeContext(topContext);
|
||||
await setupCachedListener(iframeContext);
|
||||
await loadCachedResource(iframeContext, type);
|
||||
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(cachedEventCount, 1, "No new event for the top context");
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
(type === "script" ? todo_is : is)(
|
||||
cachedEventCount,
|
||||
1,
|
||||
`[${type}] No new event for the top context`
|
||||
);
|
||||
|
||||
let iframeCachedEventCount = await getCachedResourceEventCount(iframeContext);
|
||||
is(iframeCachedEventCount, 1, "1 event received for the frame context");
|
||||
let iframeCachedEventCount = await getCachedResourceEventCount(
|
||||
iframeContext
|
||||
);
|
||||
|
||||
// Destroy listeners.
|
||||
await destroyCachedListener(topContext);
|
||||
await destroyCachedListener(iframeContext);
|
||||
if (type === "image") {
|
||||
// For images, loading an image already cached in the parent document
|
||||
// will not trigger a fetch. We will have to perform additional requests.
|
||||
is(
|
||||
iframeCachedEventCount,
|
||||
0,
|
||||
`[${type}] No event received for the frame context`
|
||||
);
|
||||
|
||||
gBrowser.removeTab(tab);
|
||||
// Load the image with a url suffix to avoid reusing the cached version
|
||||
// from the top page.
|
||||
await loadCachedResource(iframeContext, type, { addIframeSuffix: true });
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(cachedEventCount, 1, `[${type}] No new event for the top context`);
|
||||
iframeCachedEventCount = await getCachedResourceEventCount(iframeContext);
|
||||
is(
|
||||
iframeCachedEventCount,
|
||||
0,
|
||||
`[${type}] Still no event for the frame context`
|
||||
);
|
||||
|
||||
// Perform another load of the image in the iframe, this time an event
|
||||
// should be emitted for the frame context.
|
||||
await loadCachedResource(iframeContext, type, { addIframeSuffix: true });
|
||||
cachedEventCount = await getCachedResourceEventCount(topContext);
|
||||
is(cachedEventCount, 1, `[${type}] No new event for the top context`);
|
||||
iframeCachedEventCount = await getCachedResourceEventCount(iframeContext);
|
||||
is(
|
||||
iframeCachedEventCount,
|
||||
1,
|
||||
`[${type}] 1 event received for the frame context`
|
||||
);
|
||||
} else {
|
||||
(type === "script" ? todo_is : is)(
|
||||
iframeCachedEventCount,
|
||||
1,
|
||||
`[${type}] 1 event received for the frame context`
|
||||
);
|
||||
}
|
||||
|
||||
// Destroy listeners.
|
||||
await destroyCachedListener(topContext);
|
||||
await destroyCachedListener(iframeContext);
|
||||
|
||||
gBrowser.removeTab(tab);
|
||||
}
|
||||
});
|
||||
|
||||
async function loadStylesheet(browsingContext, url) {
|
||||
info(`Load stylesheet for browsingContext ${browsingContext.id}`);
|
||||
await SpecialPowers.spawn(browsingContext, [url], async _url => {
|
||||
const head = content.document.getElementsByTagName("HEAD")[0];
|
||||
const link = content.document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.type = "text/css";
|
||||
link.href = _url;
|
||||
head.appendChild(link);
|
||||
async function loadCachedResource(browsingContext, type, options = {}) {
|
||||
info(`Load ${type} for browsingContext ${browsingContext.id}`);
|
||||
|
||||
info("Wait until the stylesheet has been loaded and applied");
|
||||
await ContentTaskUtils.waitForCondition(
|
||||
() =>
|
||||
content.getComputedStyle(content.document.body)["background-color"] ==
|
||||
"rgb(0, 0, 0)"
|
||||
);
|
||||
});
|
||||
const { addIframeSuffix = false } = options;
|
||||
const getResourceUrl = url => (addIframeSuffix ? url + "&for-iframe" : url);
|
||||
|
||||
switch (type) {
|
||||
case "stylesheet": {
|
||||
await SpecialPowers.spawn(
|
||||
browsingContext,
|
||||
[getResourceUrl(STYLESHEET_URL)],
|
||||
async url => {
|
||||
const head = content.document.getElementsByTagName("HEAD")[0];
|
||||
const link = content.document.createElement("link");
|
||||
link.rel = "stylesheet";
|
||||
link.type = "text/css";
|
||||
link.href = url;
|
||||
head.appendChild(link);
|
||||
|
||||
info("Wait until the stylesheet has been loaded and applied");
|
||||
await ContentTaskUtils.waitForCondition(
|
||||
() =>
|
||||
content.getComputedStyle(content.document.body)[
|
||||
"background-color"
|
||||
] == "rgb(0, 0, 0)"
|
||||
);
|
||||
}
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "script": {
|
||||
await SpecialPowers.spawn(
|
||||
browsingContext,
|
||||
[getResourceUrl(SCRIPT_URL)],
|
||||
async url => {
|
||||
const script = content.document.createElement("script");
|
||||
script.type = "text/javascript";
|
||||
script.src = url;
|
||||
content.document.body.appendChild(script);
|
||||
|
||||
info("Wait until the script has been loaded and applied");
|
||||
await ContentTaskUtils.waitForCondition(
|
||||
() => content.wrappedJSObject.scriptLoaded
|
||||
);
|
||||
}
|
||||
);
|
||||
break;
|
||||
}
|
||||
case "image": {
|
||||
await SpecialPowers.spawn(
|
||||
browsingContext,
|
||||
[getResourceUrl(IMAGE_URL)],
|
||||
async url => {
|
||||
const img = content.document.createElement("img");
|
||||
const loaded = new Promise(r => {
|
||||
img.addEventListener("load", r, { once: true });
|
||||
});
|
||||
img.src = url;
|
||||
|
||||
content.document.body.appendChild(img);
|
||||
|
||||
info("Wait until the image has been loaded");
|
||||
await loaded;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function setupCachedListener(browsingContext) {
|
||||
|
46
remote/shared/listeners/test/browser/cached_resource.sjs
Normal file
46
remote/shared/listeners/test/browser/cached_resource.sjs
Normal file
@ -0,0 +1,46 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
"use strict";
|
||||
|
||||
function handleRequest(request, response) {
|
||||
response.setHeader(
|
||||
"Cache-Control",
|
||||
"no-transform,public,max-age=300,s-maxage=900"
|
||||
);
|
||||
response.setHeader("Expires", "Thu, 01 Dec 2100 20:00:00 GMT");
|
||||
|
||||
const params = new Map(
|
||||
request.queryString
|
||||
.replace("?", "")
|
||||
.split("&")
|
||||
.map(s => s.split("="))
|
||||
);
|
||||
|
||||
switch (params.get("type")) {
|
||||
case "stylesheet": {
|
||||
response.setHeader("Content-Type", "text/css", false);
|
||||
response.write("body { background-color: black; }");
|
||||
break;
|
||||
}
|
||||
case "script": {
|
||||
response.setHeader("Content-Type", "text/javascript", false);
|
||||
response.write("window.scriptLoaded = true;");
|
||||
break;
|
||||
}
|
||||
case "image": {
|
||||
response.setHeader("Content-Type", "image/png", false);
|
||||
response.write(
|
||||
atob(
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12" +
|
||||
"P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
|
||||
)
|
||||
);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new Error(
|
||||
"Expecting type parameter to be one of script, stylesheet or image"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
/* Any copyright is dedicated to the Public Domain.
|
||||
http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
"use strict";
|
||||
|
||||
function handleRequest(request, response) {
|
||||
response.setHeader(
|
||||
"Cache-Control",
|
||||
"no-transform,public,max-age=300,s-maxage=900"
|
||||
);
|
||||
response.setHeader("Expires", "Thu, 01 Dec 2100 20:00:00 GMT");
|
||||
response.setHeader("Content-Type", "text/css", false);
|
||||
response.write("body { background-color: black; }");
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user