gecko-dev/dom/identity/tests/mochitest/test_syntheticEvents.html

211 строки
6.2 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=971379
-->
<head>
<meta charset="utf-8">
<title>Certified/packaged apps may use synthetic events with FXA -- Bug 971379</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"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=971379">Mozilla Bug 971379</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript;version=1.8">
SimpleTest.waitForExplicitFinish();
Components.utils.import("resource://gre/modules/Promise.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/DOMIdentity.jsm");
Components.utils.import("resource://gre/modules/identity/jwcrypto.jsm");
Components.utils.import("resource://gre/modules/identity/FirefoxAccounts.jsm");
// Mock the Firefox Accounts manager to give a dummy assertion, just to confirm
// that we're making the trip through the dom/identity and toolkit/identity
// plumbing.
function MockFXAManager() {}
MockFXAManager.prototype = {
getAssertion: function(audience, options) {
if (options.silent) {
return Promise.resolve(null);
}
return Promise.resolve("here~you.go.dude");
}
};
let originalManager = FirefoxAccounts.fxAccountsManager;
FirefoxAccounts.fxAccountsManager = new MockFXAManager();
// Mock IdentityService (Persona) so we can test request() while not handling
// user input on an installed app. Since in this test suite, we have only this
// one test for Persona, we additionally cause request() to throw if invoked, as
// added security that nsDOMIdentity did not emit a request message.
let MockIdentityService = function() {
this.RP = this;
this.contexts = {};
}
MockIdentityService.prototype = {
watch: function(context) {
this.contexts[context.id] = context;
context.doReady();
},
request: function(message) {
ok(false, "nsDOMIdentity should block Persona request() in this test suite");
},
};
DOMIdentity._mockIdentityService = new MockIdentityService();
// The manifests for these apps are all declared in
// /testing/profiles/webapps_mochitest.json. They are injected into the profile
// by /testing/mochitest/runtests.py with the appropriate appStatus. So we don't
// have to manually install any apps.
let apps = [
{
title: "an installed app, which must request() in a native event",
manifest: "https://example.com/manifest.webapp",
origin: "https://example.com",
uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
wantIssuer: "", // default to persona
expected: {
success: false,
errors: [
"ERROR_REQUEST_WHILE_NOT_HANDLING_USER_INPUT",
],
},
},
{
title: "a privileged app, which may use synthetic events",
manifest: "https://example.com/manifest_priv.webapp",
origin: "https://example.com",
uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
wantIssuer: "firefox-accounts",
expected: {
success: true,
},
},
{
title: "a certified app, which may use synthetic events",
manifest: "https://example.com/manifest_cert.webapp",
origin: "https://example.com",
uri: "https://example.com/chrome/dom/identity/tests/mochitest/file_syntheticEvents.html",
wantIssuer: "firefox-accounts",
expected: {
success: true,
},
},
];
let eventsReceived = 0;
let testRunner = runTest();
function receiveMessage(event) {
dump("** Received response: " + event.data + "\n");
let result = JSON.parse(event.data);
let app = apps[result.appIndex];
if (app.received) {
return;
}
apps[result.appIndex].received = true;
let expected = app.expected;
let receivedErrors = [];
is(result.success, expected.success,
"Assertion request " + (expected.success ? "succeeds" : "fails"));
if (result.error) {
receivedErrors.push(result.error);
}
ok(receivedErrors.length === (expected.errors || []).length,
"Received errors should be equal to expected errors");
receivedErrors.forEach((error) => {
ok(expected.errors.indexOf(error) > -1,
"Received " + error + ". " +
"Expected errors are: " + JSON.stringify(expected.errors));
});
eventsReceived += 1;
if (eventsReceived === apps.length) {
window.removeEventListener("message", receiveMessage);
FirefoxAccounts.fxAccountsManager = originalManager;
SimpleTest.finish();
return;
}
testRunner.next();
}
window.addEventListener("message", receiveMessage, false, true);
function runTest() {
let index;
for (let i = 0; i < apps.length; i++) {
let app = apps[i];
dump("** Testing " + app.title + "\n");
let iframe = document.createElement("iframe");
iframe.setAttribute("mozapp", app.manifest);
iframe.setAttribute("mozbrowser", "true");
iframe.src = app.uri;
document.getElementById("content").appendChild(iframe);
index = i;
(function(_index) {
iframe.addEventListener("load", function onLoad() {
iframe.removeEventListener("load", onLoad);
SpecialPowers.addPermission(
"firefox-accounts",
SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION,
iframe.contentDocument
);
// Because the <iframe mozapp> can't parent its way back to us, we
// provide this handle it can use to postMessage to us.
Components.utils.exportFunction(window.postMessage.bind(window), iframe.contentWindow, {defineAs: 'doPostMessage'});
iframe.contentWindow.postMessage({
wantIssuer: app.wantIssuer,
appIndex: _index
}, "*");
}, false);
})(index);
yield undefined;
}
}
SpecialPowers.pushPrefEnv({"set":
[
["dom.mozBrowserFramesEnabled", true],
["dom.identity.enabled", true],
["identity.fxaccounts.enabled", true],
["toolkit.identity.debug", true],
["security.apps.privileged.CSP.default", "'inline-script';"],
["security.apps.certified.CSP.default", "'inline-script';"],
]},
function() {
testRunner.next();
}
);
</script>
</pre>
</body>
</html>