summaryrefslogtreecommitdiffstats
path: root/testing/mochitest/tests/Harness_sanity/test_SpecialPowersPushPermissions.html
blob: 3aeebb22ffe6441d81921c8aeca42c5c990f7f99 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for SpecialPowers extension</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="starttest();">

<pre id="test">
<script class="testbody" type="text/javascript">
const ALLOW_ACTION = SpecialPowers.Ci.nsIPermissionManager.ALLOW_ACTION;
const DENY_ACTION = SpecialPowers.Ci.nsIPermissionManager.DENY_ACTION;
const UNKNOWN_ACTION = SpecialPowers.Ci.nsIPermissionManager.UNKNOWN_ACTION;
const PROMPT_ACTION = SpecialPowers.Ci.nsIPermissionManager.PROMPT_ACTION;
const ACCESS_SESSION = SpecialPowers.Ci.nsICookiePermission.ACCESS_SESSION;

const EXPIRE_TIME = SpecialPowers.Ci.nsIPermissionManager.EXPIRE_TIME;
// expire Setting:
//     start                 expire time point
//   ----|------------------------|-----------
//       <------------------------>
//                 PERIOD
var start;
// PR_Now() that called in PermissionManager to get the system time
// is sometimes 100ms~600s more than Date.now() on Android 4.3 API11.
// Thus, the PERIOD should be larger than 600ms in this test.
const PERIOD = 900;
var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('specialPowers_framescript.js'));
SimpleTest.requestFlakyTimeout("untriaged");

function starttest(){
  SpecialPowers.addPermission("pPROMPT", PROMPT_ACTION, document);
  SpecialPowers.addPermission("pALLOW", ALLOW_ACTION, document);
  SpecialPowers.addPermission("pDENY", DENY_ACTION, document);
  SpecialPowers.addPermission("pREMOVE", ALLOW_ACTION, document);
  SpecialPowers.addPermission("pSESSION", ACCESS_SESSION, document);

  setTimeout(test1, 0);
}

SimpleTest.waitForExplicitFinish();

async function test1() {
  if (!await SpecialPowers.testPermission('pALLOW', ALLOW_ACTION, document)) {
    dump('/**** allow not set ****/\n');
    setTimeout(test1, 0);
  } else if (!await SpecialPowers.testPermission('pDENY', DENY_ACTION, document)) {
    dump('/**** deny not set ****/\n');
    setTimeout(test1, 0);
  } else if (!await SpecialPowers.testPermission('pPROMPT', PROMPT_ACTION, document)) {
    dump('/**** prompt not set ****/\n');
    setTimeout(test1, 0);
  } else if (!await SpecialPowers.testPermission('pREMOVE', ALLOW_ACTION, document)) {
    dump('/**** remove not set ****/\n');
    setTimeout(test1, 0);
  } else if (!await SpecialPowers.testPermission('pSESSION', ACCESS_SESSION, document)) {
    dump('/**** ACCESS_SESSION not set ****/\n');
    setTimeout(test1, 0);
  } else {
    test2();
  }
}

async function test2() {
  ok(await SpecialPowers.testPermission('pUNKNOWN', UNKNOWN_ACTION, document), 'pUNKNOWN value should have UNKOWN permission');
  SpecialPowers.pushPermissions([
    {'type': 'pUNKNOWN', 'allow': true, 'context': document},
    {'type': 'pALLOW', 'allow': false, 'context': document},
    {'type': 'pDENY', 'allow': true, 'context': document},
    {'type': 'pPROMPT', 'allow': true, 'context': document},
    {'type': 'pSESSION', 'allow': true, 'context': document},
    {'type': 'pREMOVE', 'remove': true, 'context': document},
  ], test3);
}

async function test3() {
  ok(await SpecialPowers.testPermission('pUNKNOWN', ALLOW_ACTION, document), 'pUNKNOWN value should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pPROMPT', ALLOW_ACTION, document), 'pPROMPT value should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pALLOW', DENY_ACTION, document), 'pALLOW should have DENY permission');
  ok(await SpecialPowers.testPermission('pDENY', ALLOW_ACTION, document), 'pDENY should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pREMOVE', UNKNOWN_ACTION, document), 'pREMOVE should have REMOVE permission');
  ok(await SpecialPowers.testPermission('pSESSION', ALLOW_ACTION, document), 'pSESSION should have ALLOW permission');

  // only pPROMPT (last one) is different, the other stuff is just to see if it doesn't cause test failures
  SpecialPowers.pushPermissions([
    {'type': 'pUNKNOWN', 'allow': true, 'context': document},
    {'type': 'pALLOW', 'allow': false, 'context': document},
    {'type': 'pDENY', 'allow': true, 'context': document},
    {'type': 'pPROMPT', 'allow': false, 'context': document},
    {'type': 'pREMOVE', 'remove': true, 'context': document},
  ], test3b);
}

async function test3b() {
  ok(await SpecialPowers.testPermission('pPROMPT', DENY_ACTION, document), 'pPROMPT value should have DENY permission');
  SpecialPowers.pushPermissions([
    {'type': 'pUNKNOWN', 'allow': DENY_ACTION, 'context': document},
    {'type': 'pALLOW', 'allow': PROMPT_ACTION, 'context': document},
    {'type': 'pDENY', 'allow': PROMPT_ACTION, 'context': document},
    {'type': 'pPROMPT', 'allow': ALLOW_ACTION, 'context': document},
  ], test4);
}

async function test4() {
  ok(await SpecialPowers.testPermission('pUNKNOWN', DENY_ACTION, document), 'pUNKNOWN value should have DENY permission');
  ok(await SpecialPowers.testPermission('pPROMPT', ALLOW_ACTION, document), 'pPROMPT value should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pALLOW', PROMPT_ACTION, document), 'pALLOW should have PROMPT permission');
  ok(await SpecialPowers.testPermission('pDENY', PROMPT_ACTION, document), 'pDENY should have PROMPT permission');
  //this should reset all the permissions to before all the pushPermissions calls
  SpecialPowers.flushPermissions(test5);
}

async function test5() {
  ok(await SpecialPowers.testPermission('pUNKNOWN', UNKNOWN_ACTION, document), 'pUNKNOWN should have UNKNOWN permission');
  ok(await SpecialPowers.testPermission('pALLOW', ALLOW_ACTION, document), 'pALLOW should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pDENY', DENY_ACTION, document), 'pDENY should have DENY permission');
  ok(await SpecialPowers.testPermission('pPROMPT', PROMPT_ACTION, document), 'pPROMPT should have PROMPT permission');
  ok(await SpecialPowers.testPermission('pREMOVE', ALLOW_ACTION, document), 'pREMOVE should have ALLOW permission');
  ok(await SpecialPowers.testPermission('pSESSION', ACCESS_SESSION, document), 'pSESSION should have ACCESS_SESSION permission');

  SpecialPowers.removePermission("pPROMPT", document);
  SpecialPowers.removePermission("pALLOW", document);
  SpecialPowers.removePermission("pDENY", document);
  SpecialPowers.removePermission("pREMOVE", document);
  SpecialPowers.removePermission("pSESSION", document);

  setTimeout(test6, 0);
}

async function test6() {
  if (!await SpecialPowers.testPermission('pALLOW', UNKNOWN_ACTION, document)) {
    dump('/**** allow still set ****/\n');
    setTimeout(test6, 0);
  } else if (!await SpecialPowers.testPermission('pDENY', UNKNOWN_ACTION, document)) {
    dump('/**** deny still set ****/\n');
    setTimeout(test6, 0);
  } else if (!await SpecialPowers.testPermission('pPROMPT', UNKNOWN_ACTION, document)) {
    dump('/**** prompt still set ****/\n');
    setTimeout(test6, 0);
  } else if (!await SpecialPowers.testPermission('pREMOVE', UNKNOWN_ACTION, document)) {
    dump('/**** remove still set ****/\n');
    setTimeout(test6, 0);
  } else if (!await SpecialPowers.testPermission('pSESSION', UNKNOWN_ACTION, document)) {
    dump('/**** pSESSION still set ****/\n');
    setTimeout(test6, 0);
  } else {
    test7();
  }
}

function test7() {
  afterPermissionChanged('pEXPIRE', 'deleted', test8);
  afterPermissionChanged('pEXPIRE', 'added', permissionPollingCheck);
  start = Number(Date.now());
  SpecialPowers.addPermission('pEXPIRE',
                              true,
                              document,
                              EXPIRE_TIME,
                              (start + PERIOD + getPlatformInfo().timeCompensation));
}

function test8() {
  afterPermissionChanged('pEXPIRE', 'deleted', SimpleTest.finish);
  afterPermissionChanged('pEXPIRE', 'added', permissionPollingCheck);
  start = Number(Date.now());
  SpecialPowers.pushPermissions([
    { 'type': 'pEXPIRE',
      'allow': true,
      'expireType': EXPIRE_TIME,
      'expireTime': (start + PERIOD + getPlatformInfo().timeCompensation),
      'context': document
    }], function() {
      info("Wait for permission-changed signal!");
    }
  );
}

function afterPermissionChanged(type, op, callback) {
  // handle the message from specialPowers_framescript.js
  gScript.addMessageListener('perm-changed', function onChange(msg) {
    if (msg.type == type && msg.op == op) {
      gScript.removeMessageListener('perm-changed', onChange);
      callback();
    }
  });
}

async function permissionPollingCheck() {
  var now = Number(Date.now());
  if (now < (start + PERIOD)) {
    if (await SpecialPowers.testPermission('pEXPIRE', ALLOW_ACTION, document)) {
      // To make sure that permission will be expired in next round,
      // the next permissionPollingCheck calling will be fired 100ms later after
      // permission is out-of-period.
      setTimeout(permissionPollingCheck, PERIOD + 100);
      return;
    }

    errorHandler('unexpired permission should be allowed!');
  }

  // The permission is already expired!
  if (await SpecialPowers.testPermission('pEXPIRE', ALLOW_ACTION, document)) {
    errorHandler('expired permission should be removed!');
  }
}

function getPlatformInfo() {
  var version = SpecialPowers.Services.sysinfo.getProperty('version');
  version = parseFloat(version);

  // PR_Now() that called in PermissionManager to get the system time and
  // Date.now() are out of sync on win32 platform(XP/win7). The PR_Now() is
  // 15~20ms less than Date.now(). Unfortunately, this time skew can't be
  // avoided, so it needs to add a time buffer to compensate.
  // Version 5.1 is win XP, 6.1 is win7
  if (navigator.platform.startsWith('Win32') && (version <= 6.1)) {
    return { platform: "Win32", timeCompensation: -100 };
  }

  return { platform: "NoMatter", timeCompensation: 0 };
}

function errorHandler(msg) {
  ok(false, msg);
  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>