OLD | NEW |
1 <script> | 1 <!-- |
2 | 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. Use of this |
3 var assertEq = chrome.test.assertEq; | 3 * source code is governed by a BSD-style license that can be found in the |
4 var assertFalse = chrome.test.assertFalse; | 4 * LICENSE file. |
5 var assertTrue = chrome.test.assertTrue; | 5 --> |
6 var fail = chrome.test.callbackFail; | 6 <script src="background.js"></script> |
7 var pass = chrome.test.callbackPass; | |
8 var listenOnce = chrome.test.listenOnce; | |
9 | |
10 var NOT_OPTIONAL_ERROR = | |
11 "Optional permissions must be listed in extension manifest."; | |
12 | |
13 var NO_TABS_PERMISSION = | |
14 "You do not have permission to use 'windows.getAll'."; | |
15 | |
16 var REQUIRED_ERROR = | |
17 "You cannot remove required permissions."; | |
18 | |
19 var NOT_WHITE_LISTED_ERROR = | |
20 "The optional permissions API does not support '*'."; | |
21 | |
22 var UNKNOWN_PERMISSIONS_ERROR = | |
23 "'*' is not a recognized permission."; | |
24 | |
25 var emptyPermissions = {permissions: [], origins: []}; | |
26 | |
27 var initialPermissions = { | |
28 permissions: ['management'], | |
29 origins: ['http://a.com/*'] | |
30 }; | |
31 | |
32 var permissionsWithTabs = { | |
33 permissions: ['management', 'tabs'], | |
34 origins: ['http://a.com/*'] | |
35 } | |
36 | |
37 var permissionsWithOrigin = { | |
38 permissions: ['management'], | |
39 origins: ['http://a.com/*', 'http://*.c.com/*'] | |
40 } | |
41 | |
42 function checkEqualSets(set1, set2) { | |
43 if (set1.length != set2.length) | |
44 return false; | |
45 | |
46 for (var x = 0; x < set1.length; x++) { | |
47 if (!set2.some(function(v) { return v == set1[x]; })) | |
48 return false; | |
49 } | |
50 | |
51 return true; | |
52 } | |
53 | |
54 function checkPermSetsEq(set1, set2) { | |
55 return checkEqualSets(set1.permissions, set2.permissions) && | |
56 checkEqualSets(set1.origins, set2.origins); | |
57 } | |
58 | |
59 chrome.test.getConfig(function(config) { | |
60 | |
61 function doReq(domain, callback) { | |
62 var req = new XMLHttpRequest(); | |
63 var url = domain + ":PORT/files/extensions/test_file.txt"; | |
64 url = url.replace(/PORT/, config.testServer.port); | |
65 | |
66 chrome.test.log("Requesting url: " + url); | |
67 req.open("GET", url, true); | |
68 | |
69 req.onload = function() { | |
70 assertEq(200, req.status); | |
71 assertEq("Hello!", req.responseText); | |
72 callback(true); | |
73 }; | |
74 | |
75 req.onerror = function() { | |
76 chrome.test.log("status: " + req.status); | |
77 chrome.test.log("text: " + req.responseText); | |
78 callback(false); | |
79 }; | |
80 | |
81 req.send(null); | |
82 } | |
83 | |
84 chrome.test.runTests([ | |
85 function contains() { | |
86 chrome.permissions.contains( | |
87 {permissions: ['management'], origins: ['http://a.com/*']}, | |
88 pass(function(result) { assertTrue(result); })); | |
89 chrome.permissions.contains( | |
90 {permissions: ['devtools'], origins: ['http://a.com/*']}, | |
91 pass(function(result) { assertFalse(result); })); | |
92 chrome.permissions.contains( | |
93 {permissions: ['management']}, | |
94 pass(function(result) { assertTrue(result); })); | |
95 chrome.permissions.contains( | |
96 {permissions: ['management']}, | |
97 pass(function(result) { assertTrue(result); })); | |
98 }, | |
99 | |
100 function getAll() { | |
101 chrome.permissions.getAll(pass(function(permissions) { | |
102 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | |
103 })); | |
104 }, | |
105 | |
106 // Nothing should happen if we request permission we already have | |
107 function requestNoOp() { | |
108 chrome.permissions.request( | |
109 {permissions:['management'], origins:['http://a.com/*']}, | |
110 pass(function(granted) { assertTrue(granted); })); | |
111 }, | |
112 | |
113 // We should get an error when requesting permissions that haven't been | |
114 // defined in "optional_permissions". | |
115 function requestNonOptional() { | |
116 chrome.permissions.request( | |
117 {permissions: ['debugger']}, fail(NOT_OPTIONAL_ERROR)); | |
118 chrome.permissions.request( | |
119 {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR)); | |
120 chrome.permissions.request( | |
121 {permissions: ['tabs'], origins: ['http://*.b.com/*']}, | |
122 fail(NOT_OPTIONAL_ERROR)); | |
123 }, | |
124 | |
125 // We should be able to request the tabs API since it's in the granted | |
126 // permissions list (see permissions_apitest.cc). | |
127 function requestTabs() { | |
128 try { | |
129 chrome.windows.getAll({populate: true}, function() { | |
130 chrome.test.fail("Should not have tabs API permission."); | |
131 }); | |
132 } catch (e) { | |
133 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | |
134 } | |
135 listenOnce(chrome.permissions.onAdded, | |
136 function(permissions) { | |
137 assertTrue(permissions.permissions.length == 1); | |
138 assertTrue(permissions.permissions[0] == 'tabs'); | |
139 }); | |
140 chrome.permissions.request( | |
141 {permissions:['tabs']}, | |
142 pass(function(granted) { | |
143 assertTrue(granted); | |
144 chrome.windows.getAll({populate: true}, pass(function(windows) { | |
145 assertTrue(true); | |
146 })); | |
147 chrome.permissions.getAll(pass(function(permissions) { | |
148 assertTrue(checkPermSetsEq(permissionsWithTabs, permissions)); | |
149 })); | |
150 })); | |
151 }, | |
152 | |
153 // You can't remove required permissions. | |
154 function removeRequired() { | |
155 chrome.permissions.remove( | |
156 {permissions: ['management']}, fail(REQUIRED_ERROR)); | |
157 chrome.permissions.remove( | |
158 {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR)); | |
159 chrome.permissions.remove( | |
160 {permissions: ['tabs'], origins: ['http://a.com/*']}, | |
161 fail(REQUIRED_ERROR)); | |
162 }, | |
163 | |
164 // You can remove permissions you don't have (nothing happens). | |
165 function removeNoOp() { | |
166 chrome.permissions.remove( | |
167 {permissions:['background']}, | |
168 pass(function(removed) { assertTrue(removed); })); | |
169 chrome.permissions.remove( | |
170 {origins:['http://*.c.com/*']}, | |
171 pass(function(removed) { assertTrue(removed); })); | |
172 chrome.permissions.remove( | |
173 {permissions:['background'], origins:['http://*.c.com/*']}, | |
174 pass(function(removed) { assertTrue(removed); })); | |
175 }, | |
176 | |
177 function removeTabs() { | |
178 chrome.windows.getAll({populate: true}, pass(function(windows) { | |
179 assertTrue(true); | |
180 })); | |
181 listenOnce(chrome.permissions.onRemoved, | |
182 function(permissions) { | |
183 assertTrue(permissions.permissions.length == 1); | |
184 assertTrue(permissions.permissions[0] == 'tabs'); | |
185 }); | |
186 chrome.permissions.remove( | |
187 {permissions:['tabs']}, | |
188 pass(function() { | |
189 chrome.permissions.getAll(pass(function(permissions) { | |
190 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | |
191 })); | |
192 try { | |
193 chrome.windows.getAll({populate: true}, function() { | |
194 chrome.test.fail("Should not have tabs API permission."); | |
195 }); | |
196 } catch (e) { | |
197 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | |
198 } | |
199 })); | |
200 }, | |
201 | |
202 // The user shouldn't have to approve permissions that have no warnings. | |
203 function noPromptForNoWarnings() { | |
204 chrome.permissions.request( | |
205 {permissions: ['notifications']}, | |
206 pass(function(granted) { | |
207 assertTrue(granted); | |
208 | |
209 // Remove the notifications permission to return to normal. | |
210 chrome.permissions.remove( | |
211 {permissions: ['notifications']}, | |
212 pass(function(removed) { assertTrue(removed); })); | |
213 })); | |
214 }, | |
215 | |
216 // Make sure you can only access the white listed permissions. | |
217 function whitelist() { | |
218 var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'chromeAuthPrivate'); | |
219 chrome.permissions.request( | |
220 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); | |
221 chrome.permissions.remove( | |
222 {permissions: ['chromeAuthPrivate']}, fail(error_msg)); | |
223 }, | |
224 | |
225 function unknownPermission() { | |
226 var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf'); | |
227 chrome.permissions.request( | |
228 {permissions: ['asdf']}, fail(error_msg)); | |
229 }, | |
230 | |
231 function requestOrigin() { | |
232 doReq('http://c.com', pass(function(success) { assertFalse(success); })); | |
233 | |
234 chrome.permissions.getAll(pass(function(permissions) { | |
235 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | |
236 })); | |
237 | |
238 listenOnce(chrome.permissions.onAdded, | |
239 function(permissions) { | |
240 assertTrue(permissions.permissions.length == 0); | |
241 assertTrue(permissions.origins.length == 1); | |
242 assertTrue(permissions.origins[0] == 'http://*.c.com/*'); | |
243 }); | |
244 chrome.permissions.request( | |
245 {origins: ['http://*.c.com/*']}, | |
246 pass(function(granted) { | |
247 assertTrue(granted); | |
248 chrome.permissions.getAll(pass(function(permissions) { | |
249 assertTrue(checkPermSetsEq(permissionsWithOrigin, permissions)); | |
250 })); | |
251 chrome.permissions.contains( | |
252 {origins:['http://*.c.com/*']}, | |
253 pass(function(result) { assertTrue(result); })); | |
254 doReq('http://c.com', pass(function(result) { assertTrue(result); })); | |
255 })); | |
256 }, | |
257 | |
258 function removeOrigin() { | |
259 doReq('http://c.com', pass(function(result) { assertTrue(result); })); | |
260 | |
261 listenOnce(chrome.permissions.onRemoved, | |
262 function(permissions) { | |
263 assertTrue(permissions.permissions.length == 0); | |
264 assertTrue(permissions.origins.length == 1); | |
265 assertTrue(permissions.origins[0] == 'http://*.c.com/*'); | |
266 }); | |
267 chrome.permissions.remove( | |
268 {origins: ['http://*.c.com/*']}, | |
269 pass(function(removed) { | |
270 assertTrue(removed); | |
271 chrome.permissions.getAll(pass(function(permissions) { | |
272 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | |
273 })); | |
274 chrome.permissions.contains( | |
275 {origins:['http://*.c.com/*']}, | |
276 pass(function(result) { assertFalse(result); })); | |
277 doReq('http://c.com', pass(function(result) { assertFalse(result); })); | |
278 })); | |
279 }, | |
280 | |
281 // Tests that the changed permissions have taken effect from inside the | |
282 // onAdded and onRemoved event listeners. | |
283 function eventListenerPermissions() { | |
284 listenOnce(chrome.permissions.onAdded, | |
285 function(permissions) { | |
286 chrome.windows.getAll({populate: true}, pass(function() { | |
287 assertTrue(true); | |
288 })); | |
289 }); | |
290 listenOnce(chrome.permissions.onRemoved, | |
291 function(permissions) { | |
292 try { | |
293 chrome.windows.getAll({populate: true}, function() { | |
294 chrome.test.fail("Should not have tabs API permission."); | |
295 }); | |
296 } catch (e) { | |
297 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | |
298 } | |
299 }); | |
300 | |
301 chrome.permissions.request( | |
302 {permissions: ['tabs']}, pass(function(granted) { | |
303 assertTrue(granted); | |
304 chrome.permissions.remove( | |
305 {permissions: ['tabs']}, pass(function() { | |
306 assertTrue(true); | |
307 })); | |
308 })); | |
309 } | |
310 | |
311 ]); | |
312 }); | |
313 </script> | |
OLD | NEW |