summaryrefslogtreecommitdiffstats
path: root/b2g/components/test/mochitest/test_sandbox_permission.html
blob: cd13599a3552f83d1d80c79fafb3b67887365a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=951997
-->
<head>
  <meta charset="utf-8">
  <title>Permission Prompt Test</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=951997">Permission prompt web content test</a>
<script type="application/javascript;version=1.8">

"use strict";

const APP_URL = "SandboxPromptTest.html";

var iframe;
var gUrl = SimpleTest.getTestFileURL("permission_handler_chrome.js");
var gScript = SpecialPowers.loadChromeScript(gUrl);
var gResult = [
  {
    "video-capture": ["back"],
  },
  {
    "audio-capture": [""],
    "video-capture": ["back"],
  },
  {
    "audio-capture": [""],
  },
  {
    "geolocation": [],
  },
  {
    "desktop-notification": [],
  }
];

function runNext() {
  if (gResult.length > 0) {
    // Put the requested permission in query string
    let requestedPermission = JSON.stringify(Object.keys(gResult[0]));
    info('request permissions for ' + requestedPermission);
    iframe.src = APP_URL + '?' + encodeURIComponent(requestedPermission);
  } else {
    info('test finished, teardown');
    gScript.sendAsyncMessage("teardown", "");
    gScript.destroy();
    SimpleTest.finish();
  }
}

// Create a sanbox iframe.
function loadBrowser() {
  iframe = document.createElement("iframe");
  SpecialPowers.wrap(iframe).mozbrowser = true;
  iframe.src = 'about:blank';
  document.body.appendChild(iframe);

  iframe.addEventListener("load", function onLoad() {
    iframe.removeEventListener("load", onLoad);
    runNext();
  });
}

gScript.addMessageListener("permission-request", function (detail) {
  let permissions = detail.permissions;
  let expectedValue = gResult.shift();
  let permissionTypes = Object.keys(permissions);

  is(permissionTypes.length, Object.keys(expectedValue).length, "expected number of permissions");

  for (let type of permissionTypes) {
    ok(expectedValue.hasOwnProperty(type), "expected permission type");
    for (let i in permissions[type]) {
      is(permissions[type][i], expectedValue[type][i], "expected permission option");
    }
  }
  runNext();
});

// Add permissions to this app. We use ALLOW_ACTION here. The ContentPermissionPrompt
// should prompt for permission, not allow it without prompt.
SpecialPowers.pushPrefEnv({"set": [["media.navigator.permission.disabled", false]]},
  function() {
    SpecialPowers.addPermission('video-capture',
      SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION, document);
    SpecialPowers.addPermission('audio-capture',
      SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION, document);
    SpecialPowers.addPermission('geolocation',
      SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION, document);
    SpecialPowers.addPermission('desktop-notification',
      SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION, document);
    loadBrowser();
  });

SimpleTest.waitForExplicitFinish();
</script>
</pre>
</body>
</html>