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