mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 06:11:37 +00:00
161 lines
5.2 KiB
HTML
161 lines
5.2 KiB
HTML
<!--
|
|
Any copyright is dedicated to the Public Domain.
|
|
http://creativecommons.org/publicdomain/zero/1.0/
|
|
-->
|
|
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Test for Permissions API</title>
|
|
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
|
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
|
|
</head>
|
|
<body onload='runTests()'>
|
|
<pre id="test">
|
|
<script type="application/javascript;version=1.8">
|
|
'use strict';
|
|
|
|
let { UNKNOWN_ACTION, PROMPT_ACTION, ALLOW_ACTION, DENY_ACTION } =
|
|
SpecialPowers.Ci.nsIPermissionManager;
|
|
|
|
SimpleTest.waitForExplicitFinish();
|
|
|
|
const PERMISSIONS = [
|
|
{ name: 'geolocation', perm: 'geo' },
|
|
{ name: 'notifications', perm: 'desktop-notification' },
|
|
{ name: 'push', perm: 'desktop-notification' },
|
|
];
|
|
|
|
const UNSUPPORTED_PERMISSIONS = [
|
|
'foobarbaz', // Not in spec, for testing only.
|
|
'midi'
|
|
];
|
|
|
|
function setPermissions(action) {
|
|
let permissions = PERMISSIONS.map(x => {
|
|
return { 'type': x.perm, 'allow': action, 'context': document };
|
|
});
|
|
return new Promise((resolve, reject) => {
|
|
SpecialPowers.popPermissions(() => {
|
|
SpecialPowers.pushPermissions(permissions, resolve);
|
|
});
|
|
});
|
|
}
|
|
|
|
function revokePermissions(action) {
|
|
return Promise.all(PERMISSIONS.map(x =>
|
|
navigator.permissions.revoke({ name: x.name }).then(
|
|
result => is(result.state, "prompt", `correct state for '${x.name}'`),
|
|
error => ok(false, `revoke should not have rejected for '${x.name}'`))
|
|
));
|
|
}
|
|
|
|
function revokeUnsupportedPermissions() {
|
|
return Promise.all(UNSUPPORTED_PERMISSIONS.map(name =>
|
|
navigator.permissions.revoke({ name: name }).then(
|
|
result => ok(false, `revoke should not have resolved for '${name}'`),
|
|
error => is(error.name, 'TypeError', `revoke should have thrown TypeError for '${name}'`))
|
|
));
|
|
}
|
|
|
|
function revokeUserVisiblePushPermission() {
|
|
return navigator.permissions.revoke({ name: 'push', userVisible: true }).then(
|
|
result => ok(false, `revoke should not have resolved for userVisible push`),
|
|
error => ok(true, `revoke should have rejected for userVisible push`));
|
|
}
|
|
|
|
function checkPermissions(state) {
|
|
return Promise.all(PERMISSIONS.map(x => {
|
|
return navigator.permissions.query({ name: x.name }).then(
|
|
result => is(result.state, state, `correct state for '${x.name}'`),
|
|
error => ok(false, `query should not have rejected for '${x.name}'`));
|
|
}));
|
|
}
|
|
|
|
function checkUnsupportedPermissions() {
|
|
return Promise.all(UNSUPPORTED_PERMISSIONS.map(name => {
|
|
return navigator.permissions.query({ name: name }).then(
|
|
result => ok(false, `query should not have resolved for '${name}'`),
|
|
error => {
|
|
is(error.name, 'TypeError',
|
|
`query should have thrown TypeError for '${name}'`);
|
|
});
|
|
}));
|
|
}
|
|
|
|
function checkUserVisiblePushPermission() {
|
|
return navigator.permissions.query({ name: 'push', userVisible: true }).then(
|
|
result => ok(false, `query should not have resolved for userVisible push`),
|
|
error => ok(true, `query should have rejected for userVisible push`));
|
|
}
|
|
|
|
function promiseStateChanged(name, state) {
|
|
return navigator.permissions.query({ name }).then(
|
|
status => {
|
|
return new Promise((resolve, reject) => {
|
|
status.onchange = () => {
|
|
status.onchange = null;
|
|
is(status.state, state, `state changed for '${name}'`);
|
|
resolve();
|
|
};
|
|
});
|
|
},
|
|
error => ok(false, `query should not have rejected for '${name}'`));
|
|
}
|
|
|
|
function testStatusOnChange() {
|
|
return new Promise((resolve, reject) => {
|
|
SpecialPowers.popPermissions(() => {
|
|
let permission = 'geolocation';
|
|
let promiseGranted = promiseStateChanged(permission, 'granted');
|
|
setPermissions(ALLOW_ACTION);
|
|
promiseGranted.then(() => {
|
|
let promisePrompt = promiseStateChanged(permission, 'prompt');
|
|
SpecialPowers.popPermissions();
|
|
return promisePrompt;
|
|
}).then(resolve);
|
|
});
|
|
});
|
|
}
|
|
|
|
function testInvalidQuery() {
|
|
navigator.permissions.query({ name: 'invalid' }).then(
|
|
result => ok(false, 'invalid query should not have resolved'),
|
|
error => ok(true, 'invalid query should have rejected'));
|
|
}
|
|
|
|
function testInvalidRevoke() {
|
|
navigator.permissions.revoke({ name: 'invalid' }).then(
|
|
result => ok(false, 'invalid revoke should not have resolved'),
|
|
error => ok(true, 'invalid revoke should have rejected'));
|
|
}
|
|
|
|
function runTests() {
|
|
checkUnsupportedPermissions()
|
|
.then(checkUserVisiblePushPermission)
|
|
.then(() => setPermissions(UNKNOWN_ACTION))
|
|
.then(() => checkPermissions('prompt'))
|
|
.then(() => setPermissions(PROMPT_ACTION))
|
|
.then(() => checkPermissions('prompt'))
|
|
.then(() => setPermissions(ALLOW_ACTION))
|
|
.then(() => checkPermissions('granted'))
|
|
.then(() => setPermissions(DENY_ACTION))
|
|
.then(() => checkPermissions('denied'))
|
|
.then(testStatusOnChange)
|
|
.then(testInvalidQuery)
|
|
.then(revokeUnsupportedPermissions)
|
|
.then(revokeUserVisiblePushPermission)
|
|
.then(revokePermissions)
|
|
.then(() => checkPermissions('prompt'))
|
|
.then(testInvalidRevoke)
|
|
.then(SimpleTest.finish)
|
|
.catch ((e) => {
|
|
ok(false, 'Unexpected error ' + e);
|
|
SimpleTest.finish();
|
|
});
|
|
}
|
|
</script>
|
|
</pre>
|
|
</body>
|
|
</html>
|