dom/permission/tests/test_permissions_api.html
author David Anderson <danderson@mozilla.com>
Fri, 01 Jul 2016 01:15:16 -0700
changeset 343414 887adb3d9482c267c5928f5d900252361f35ac25
parent 333827 d600bbea44caf0433fa8573ce28b2724c43c9474
child 352980 19c4f264ce379ca6d418c8cb69a971536cac7d7a
permissions -rw-r--r--
Move InProcessCompositorWidget to its own file. (bug 1281998 part 3, r=jimm)

<!--
  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 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 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(() => 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(revokePermissions)
    .then(() => checkPermissions('prompt'))
    .then(testInvalidRevoke)
    .then(SimpleTest.finish)
    .catch ((e) => {
      ok(false, 'Unexpected error ' + e);
      SimpleTest.finish();
    });
}
</script>
</pre>
</body>
</html>