summaryrefslogtreecommitdiffstats
path: root/dom/permission/tests/test_cross_origin_iframe.html
blob: b024592e750ef7a98fe060791ebd97c13c194260 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!--
  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 src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>

<body>
  <pre id="test"></pre>
  <script type="application/javascript">
  /*globals SpecialPowers, SimpleTest, is, ok, */
  'use strict';

  function setPermission(type, allow) {
    return new Promise(resolve => {
      SpecialPowers.popPermissions(() => {
        SpecialPowers.pushPermissions(
          [{ type, allow, context: document }],
          resolve
        );
      });
    });
  }

  function checkPermission(aIFrame, aExpectedState, aName) {
    return SpecialPowers.spawn(
      aIFrame,
      [{name: aName, expectedState: aExpectedState}],
      async aInput => {
        try {
          let result = await content.navigator
                                    .permissions
                                    .query({ name: aInput.name });
          is(
            SpecialPowers.wrap(result).state,
            aInput.expectedState,
            `correct state for '${aInput.name}'`
          );
        } catch (e) {
          ok(false, `query should not have rejected for '${aInput.name}'`)
        }
      }
    );
  }

  function createIframe(aId, aAllow) {
    return new Promise((resolve) => {
      const iframe = document.createElement('iframe');
      iframe.id = aId;
      iframe.src = 'https://example.org/tests/dom/permission/tests/file_empty.html';
      if (aAllow) {
        iframe.allow = aAllow;
      }
      iframe.onload = () => resolve(iframe);
      document.body.appendChild(iframe);
    });
  }

  function removeIframe(aId) {
    return new Promise((resolve) => {
      document.body.removeChild(document.getElementById(aId));
      resolve();
    });
  }

  const {
    UNKNOWN_ACTION,
    PROMPT_ACTION,
    ALLOW_ACTION,
    DENY_ACTION
  } = SpecialPowers.Ci.nsIPermissionManager;

  const tests = [
    {
      id: 'query navigation top unknown',
      top: UNKNOWN_ACTION,
      name: 'geolocation',
      type: 'geo',
      expected: 'denied',
    },
    {
      id: 'query notifications top unknown',
      top: UNKNOWN_ACTION,
      name: 'notifications',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query push top unknown',
      top: UNKNOWN_ACTION,
      name: 'push',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query persistent-storage unknown',
      top: UNKNOWN_ACTION,
      name: 'persistent-storage',
      type: 'persistent-storage',
      expected: 'denied',
    },
    {
      id: 'query navigation top prompt',
      top: PROMPT_ACTION,
      name: 'geolocation',
      type: 'geo',
      expected: 'denied',
    },
    {
      id: 'query notifications top prompt',
      top: PROMPT_ACTION,
      name: 'notifications',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query push top prompt',
      top: PROMPT_ACTION,
      name: 'push',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query persistent-storage top prompt',
      top: PROMPT_ACTION,
      name: 'persistent-storage',
      type: 'persistent-storage',
      expected: 'denied',
    },
    {
      id: 'query navigation top denied',
      top: DENY_ACTION,
      name: 'geolocation',
      type: 'geo',
      expected: 'denied',
    },
    {
      id: 'query notifications top denied',
      top: DENY_ACTION,
      name: 'notifications',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query push top denied',
      top: DENY_ACTION,
      name: 'push',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query persistent-storage top denied',
      top: DENY_ACTION,
      name: 'persistent-storage',
      type: 'persistent-storage',
      expected: 'denied',
    },
    {
      id: 'query navigation top granted',
      top: ALLOW_ACTION,
      name: 'geolocation',
      type: 'geo',
      expected: 'denied',
    },
    {
      id: 'query notifications top granted',
      top: ALLOW_ACTION,
      name: 'notifications',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query push top granted',
      top: ALLOW_ACTION,
      name: 'push',
      type: 'desktop-notification',
      expected: 'denied',
    },
    {
      id: 'query persistent-storage top granted',
      top: ALLOW_ACTION,
      name: 'persistent-storage',
      type: 'persistent-storage',
      expected: 'denied',
    },
    {
      id: 'query navigation top denied, iframe has allow attribute',
      top: DENY_ACTION,
      allow: 'geolocation',
      name: 'geolocation',
      type: 'geo',
      expected: 'denied',
    },
    {
      id: 'query navigation top granted, iframe has allow attribute',
      top: ALLOW_ACTION,
      allow: 'geolocation',
      name: 'geolocation',
      type: 'geo',
      expected: 'granted',
    },
    {
      id: 'query navigation top prompt, iframe has allow attribute',
      top: PROMPT_ACTION,
      allow: 'geolocation',
      name: 'geolocation',
      type: 'geo',
      expected: 'prompt',
    },
    {
      id: 'query navigation top unknown, iframe has allow attribute',
      top: UNKNOWN_ACTION,
      allow: 'geolocation',
      name: 'geolocation',
      type: 'geo',
      expected: 'prompt',
    },

  ];

  SimpleTest.waitForExplicitFinish();

  async function nextTest() {
    if (!tests.length) {
      SimpleTest.finish();
      return;
    }

    let test = tests.shift();
    await setPermission(test.type, test.top)
      .then(() => createIframe(test.id, test.allow))
      .then(iframe => checkPermission(iframe, test.expected, test.name))
      .then(() => removeIframe(test.id));

    SimpleTest.executeSoon(nextTest);
  }

  SpecialPowers.pushPrefEnv({"set": [
    ["permissions.delegation.enabled", true],
  ]}).then(nextTest);
  </script>
</body>

</html>