gecko/dom/permission/tests/test_permissions_api.html
Birunthan Mohanathas b5c61ef822 Bug 1221104 - Revoke 'midi' permission queries with TypeError. r=baku
According to the spec, we should reject unsupported permissions with a
TypeError. This removes the 'midi' entry entirely so that we get the expected
behaviour.
2015-12-18 15:02:50 +02:00

137 lines
4.1 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 setup() {
return new Promise((resolve, reject) => {
SpecialPowers.pushPrefEnv({'set': [
['dom.permissions.enabled', true],
]}, resolve);
});
}
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 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 }).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 runTests() {
setup()
.then(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(SimpleTest.finish)
.catch ((e) => {
ok(false, 'Unexpected error ' + e);
SimpleTest.finish();
});
}
</script>
</pre>
</body>
</html>