зеркало из https://github.com/mozilla/gecko-dev.git
279 строки
7.9 KiB
HTML
279 строки
7.9 KiB
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<!--
|
|
https://bugzilla.mozilla.org/show_bug.cgi?id=1171917
|
|
|
|
The tests being added here are about testing the logic underneath the
|
|
about:serviceworkers panel in the settings app. That logic is a B2G dedicate
|
|
component.
|
|
The component let us update and unregister a Service Worker Registration. It
|
|
communicates with content through content/chrome events. We mock them so we can
|
|
send messages for updating/unregistering a particular Service Worker
|
|
Registration.
|
|
|
|
These tests install an app and the app registers a service worker. Through the
|
|
component described above we update the corresponding Service Worker
|
|
Registration, we test that the Service Worker gets updated and
|
|
then we unregister it.
|
|
-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Test for Bug 1171917</title>
|
|
<script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
|
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
|
|
<script type="application/javascript;version=1.7">
|
|
|
|
const ASW_CHROME_EVENT = "mozAboutServiceWorkersChromeEvent";
|
|
const ASW_CONTENT_EVENT = "mozAboutServiceWorkersContentEvent";
|
|
|
|
const { utils: Cu } = Components;
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
XPCOMUtils.defineLazyModuleGetter(this, "AboutServiceWorkers",
|
|
"resource://gre/modules/AboutServiceWorkers.jsm");
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
const gOrigin = "http://test/chrome/b2g/components/test/mochitest/app";
|
|
const appManifestURL = gOrigin + "/manifest.webapp";
|
|
let gApp;
|
|
|
|
const ORIGINAL_INIT = AboutServiceWorkers.init;
|
|
const ORIGINAL_SENDRESULT = AboutServiceWorkers.sendResult;
|
|
const ORIGINAL_SENDERROR = AboutServiceWorkers.sendError;
|
|
|
|
function debug(aMsg) {
|
|
dump("test_aboutserviceworkers.html - " + aMsg + "\n");
|
|
}
|
|
|
|
function mockInit() {
|
|
window.addEventListener(ASW_CONTENT_EVENT, AboutServiceWorkers);
|
|
}
|
|
|
|
function mockSendResult(aId, aResult) {
|
|
let result = {
|
|
detail: {
|
|
id: aId,
|
|
result: aResult
|
|
}
|
|
};
|
|
debug("mockSendResult, " + JSON.stringify(result));
|
|
let event = new CustomEvent(ASW_CHROME_EVENT, result);
|
|
window.dispatchEvent(event);
|
|
}
|
|
|
|
function mockSendError(aId, aError) {
|
|
let error = {
|
|
detail: {
|
|
id: aId,
|
|
error: aError
|
|
}
|
|
};
|
|
debug("mockSendError, " + JSON.stringify(error));
|
|
let event = new CustomEvent(ASW_CHROME_EVENT, error);
|
|
window.dispatchEvent(event);
|
|
}
|
|
|
|
function attachMocks() {
|
|
AboutServiceWorkers.init = mockInit;
|
|
AboutServiceWorkers.sendResult = mockSendResult;
|
|
AboutServiceWorkers.sendError = mockSendError;
|
|
return Promise.resolve();
|
|
}
|
|
|
|
function restoreMocks() {
|
|
AboutServiceWorkers.init = ORIGINAL_INIT;
|
|
AboutServiceWorkers.sendResult = ORIGINAL_SENDRESULT;
|
|
AboutServiceWorkers.sendError = ORIGINAL_SENDERROR;
|
|
return Promise.resolve();
|
|
}
|
|
|
|
function sendContentEvent(detail) {
|
|
let event = new CustomEvent(ASW_CONTENT_EVENT, {
|
|
detail: detail
|
|
});
|
|
window.dispatchEvent(event);
|
|
}
|
|
|
|
function chromeRequest(request) {
|
|
if (!request) {
|
|
return Promise.reject("InternalErrorMissingEventDetail");
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
let id = request.id;
|
|
window.addEventListener(ASW_CHROME_EVENT,
|
|
function onChromeEvent(event) {
|
|
window.removeEventListener(ASW_CHROME_EVENT, onChromeEvent);
|
|
let message = event.detail;
|
|
if (!message || !message.id || message.id != id) {
|
|
return reject('InternalErrorWrongChromeEvent');
|
|
}
|
|
|
|
if (message.error) {
|
|
reject(message.error);
|
|
} else if (message.result) {
|
|
resolve(message.result);
|
|
}
|
|
});
|
|
|
|
sendContentEvent(request);
|
|
});
|
|
}
|
|
|
|
function uninstallApp() {
|
|
return new Promise((resolve, reject) => {
|
|
if (!gApp) {
|
|
return reject();
|
|
}
|
|
let req = navigator.mozApps.mgmt.uninstall(gApp);
|
|
req.onsuccess = resolve;
|
|
req.onerror = reject;
|
|
});
|
|
}
|
|
|
|
function update() {
|
|
return Promise.resolve()
|
|
.then(() => {
|
|
return chromeRequest({id: "0", name: "init"});
|
|
})
|
|
.then((result) => {
|
|
return chromeRequest({
|
|
id: "1",
|
|
name: "update",
|
|
principal: result.registrations[0].principal,
|
|
scope: result.registrations[0].scope
|
|
});
|
|
});
|
|
}
|
|
|
|
function testApp() {
|
|
if (!gApp) {
|
|
ok(false, "No test application to launch");
|
|
return Promise.reject();
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
let iframe = document.createElement("iframe");
|
|
let domParent = document.getElementById("container");
|
|
iframe.setAttribute("mozbrowser", "true");
|
|
iframe.setAttribute("mozapp", gApp.manifestURL);
|
|
iframe.addEventListener("mozbrowsershowmodalprompt", function listener(e) {
|
|
let message = e.detail.message;
|
|
if (/KO/.exec(message)) {
|
|
ok(false, "Message from app: " + message);
|
|
} else if (/OK/.exec(message)) {
|
|
ok(true, "Message from app: " + message);
|
|
} else if (/UPDATE/.exec(message)) {
|
|
ok(true, "Message from app: " + message);
|
|
update();
|
|
} else if (/DONE/.exec(message)) {
|
|
ok(true, "Message from app: " + message);
|
|
iframe.src = "about:blank";
|
|
domParent.removeChild(iframe);
|
|
iframe = null;
|
|
resolve();
|
|
}
|
|
}, false);
|
|
domParent.appendChild(iframe);
|
|
ok(true, "origin " + gOrigin + gApp.manifest.launch_path);
|
|
SpecialPowers.wrap(iframe.contentWindow).location =
|
|
gOrigin + gApp.manifest.launch_path;
|
|
});
|
|
}
|
|
|
|
function installApp() {
|
|
return new Promise((resolve, reject) => {
|
|
let req = navigator.mozApps.install(appManifestURL);
|
|
req.onsuccess = function() {
|
|
gApp = req.result;
|
|
is(req.result.manifestURL, appManifestURL, "app installed");
|
|
if (req.result.installState == "installed") {
|
|
is(req.result.installState, "installed", "app downloaded");
|
|
resolve()
|
|
} else {
|
|
req.result.ondownloadapplied = function() {
|
|
is(req.result.installState, "installed", "app downloaded");
|
|
resolve();
|
|
}
|
|
}
|
|
}
|
|
req.onerror = reject;
|
|
});
|
|
}
|
|
|
|
function setup() {
|
|
info("Setting up");
|
|
return new Promise((resolve, reject) => {
|
|
SpecialPowers.setAllAppsLaunchable(true);
|
|
SpecialPowers.pushPrefEnv({"set": [
|
|
["dom.mozBrowserFramesEnabled", true],
|
|
["dom.serviceWorkers.exemptFromPerDomainMax", true],
|
|
["dom.serviceWorkers.enabled", true],
|
|
["dom.serviceWorkers.testing.enabled", true],
|
|
["dom.serviceWorkers.interception.enabled", true]
|
|
]}, () => {
|
|
SpecialPowers.pushPermissions([
|
|
{ "type": "webapps-manage", "allow": 1, "context": document },
|
|
{ "type": "browser", "allow": 1, "context": document },
|
|
{ "type": "embed-apps", "allow": 1, "context": document }
|
|
], () => {
|
|
SpecialPowers.autoConfirmAppInstall(() => {
|
|
SpecialPowers.autoConfirmAppUninstall(resolve);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function cleanUp() {
|
|
restoreMocks();
|
|
SimpleTest.finish();
|
|
}
|
|
|
|
function go() {
|
|
setup()
|
|
.then(attachMocks)
|
|
.then(() => {
|
|
AboutServiceWorkers.init();
|
|
return Promise.resolve();
|
|
})
|
|
.then(installApp)
|
|
.then(testApp)
|
|
.then(() => {
|
|
return chromeRequest({id: "2", name: "init"});
|
|
})
|
|
.then((result) => {
|
|
return chromeRequest({
|
|
id: "3",
|
|
name: "unregister",
|
|
principal: result.registrations[0].principal,
|
|
scope: result.registrations[0].scope
|
|
});
|
|
})
|
|
.then(() => {
|
|
return chromeRequest({id: "4", name: "init"});
|
|
})
|
|
.then((result) => {
|
|
ok(result.registrations && result.registrations.length === 0,
|
|
"Service worker registration was successfuly unregistered");
|
|
})
|
|
.then(uninstallApp)
|
|
.then(cleanUp)
|
|
.catch((e) => {
|
|
ok(false, "Unexpected error " + e);
|
|
cleanUp();
|
|
});
|
|
}
|
|
</script>
|
|
</head>
|
|
<body onload="go()">
|
|
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1171917">Mozilla Bug 1171917</a>
|
|
<p id="display"></p>
|
|
<div id="content" style="display: none">
|
|
</div>
|
|
<pre id="test">
|
|
</pre>
|
|
<div id="container"></div>
|
|
</body>
|
|
</html>
|