fune/browser/base/content/test/general/browser_gZipOfflineChild.js
Kris Maglione 25c94b46d8 Bug 1481021: Part 1 - Fix tests that rely on permissive COWs or SpecialPowers side-effects in frame script scopes. r=bz
Right now, a lot of test code relies on side-effects of SpecialPowers being
loaded into frame script globals. In particular:

- It forces permissive COWs from those scopes, which allows frame scripts to
  pass objects from those scopes to unprivileged content that they otherwise
  wouldn't.
- It imports a bunch of helper modules and WebIDL globals which would
  otherwise not be available.

Fortunately, this seems to only impact test code at this point. But there's a
real down-the-road risk of it impacting shipping code, which ends up working
in automation due to the side-effects of SpecialPowers, but failing in real
world use.

MozReview-Commit-ID: G27eSSOHymX

--HG--
extra : rebase_source : 1702e63fed719fc92def2bdbbb8a7c53572432db
extra : source : 41bedc526dd6ec6b7e8c7be1c832ac60c81d6263
2018-08-07 14:13:06 -07:00

87 lines
2.8 KiB
JavaScript

/**
* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
const URL = "http://mochi.test:8888/browser/browser/base/content/test/general/test_offline_gzip.html";
registerCleanupFunction(function() {
// Clean up after ourself
let uri = Services.io.newURI(URL);
let principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
Services.perms.removeFromPrincipal(principal, "offline-app");
Services.prefs.clearUserPref("offline-apps.allow_by_default");
});
//
// Handle "message" events which are posted from the iframe upon
// offline cache events.
//
function contentTask() {
ChromeUtils.import("resource://gre/modules/Timer.jsm");
let resolve;
let promise = new Promise(r => { resolve = r; });
var cacheCount = 0;
var intervalID = 0;
function handleMessageEvents(event) {
cacheCount++;
switch (cacheCount) {
case 1:
// This is the initial caching off offline data.
is(event.data, "oncache", "Child was successfully cached.");
// Reload the frame; this will generate an error message
// in the case of bug 501422.
event.source.location.reload();
// Use setInterval to repeatedly call a function which
// checks that one of two things has occurred: either
// the offline cache is udpated (which means our iframe
// successfully reloaded), or the string "error" appears
// in the iframe, as in the case of bug 501422.
intervalID = setInterval(function() {
// Sometimes document.body may not exist, and trying to access
// it will throw an exception, so handle this case.
try {
var bodyInnerHTML = event.source.document.body.innerHTML;
} catch (e) {
bodyInnerHTML = "";
}
if (cacheCount == 2 || bodyInnerHTML.includes("error")) {
clearInterval(intervalID);
is(cacheCount, 2, "frame not reloaded successfully");
if (cacheCount != 2) {
finish();
}
}
}, 100);
break;
case 2:
is(event.data, "onupdate", "Child was successfully updated.");
clearInterval(intervalID);
resolve();
break;
default:
// how'd we get here?
ok(false, "cacheCount not 1 or 2");
}
}
content.addEventListener("message", handleMessageEvents);
return promise;
}
function test() {
waitForExplicitFinish();
Services.prefs.setBoolPref("offline-apps.allow_by_default", true);
// Open a new tab.
gBrowser.selectedTab = BrowserTestUtils.addTab(gBrowser, URL);
registerCleanupFunction(() => gBrowser.removeCurrentTab());
BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser).then(() => {
ContentTask.spawn(gBrowser.selectedBrowser, null, contentTask).then(finish);
});
}