Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: chrome/test/data/extensions/api_test/permissions/optional/background.js

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

Powered by Google App Engine
This is Rietveld 408576698