- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / permissions / optional / background.js
1 // Copyright (c) 2012 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 assertThrows = chrome.test.assertThrows;
9 var fail = chrome.test.callbackFail;
10 var pass = chrome.test.callbackPass;
11 var listenOnce = chrome.test.listenOnce;
12
13 var NOT_OPTIONAL_ERROR =
14     "Optional permissions must be listed in extension manifest.";
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 permissionsWithBookmarks = {
33   permissions: ['management', 'bookmarks'],
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/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: ['history']}, fail(NOT_OPTIONAL_ERROR));
118       chrome.permissions.request(
119           {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR));
120       chrome.permissions.request(
121           {permissions: ['history'], origins: ['http://*.b.com/*']},
122           fail(NOT_OPTIONAL_ERROR));
123     },
124
125     // We should be able to request the bookmarks API since it's in the granted
126     // permissions list (see permissions_apitest.cc).
127     function requestBookmarks() {
128       assertEq(undefined, chrome.bookmarks);
129       listenOnce(chrome.permissions.onAdded,
130                  function(permissions) {
131         assertTrue(permissions.permissions.length == 1);
132         assertTrue(permissions.permissions[0] == 'bookmarks');
133       });
134       chrome.permissions.request(
135           {permissions:['bookmarks']},
136           pass(function(granted) {
137             assertTrue(granted);
138             chrome.bookmarks.getTree(pass(function(result) {
139               assertTrue(true);
140             }));
141             chrome.permissions.getAll(pass(function(permissions) {
142               assertTrue(checkPermSetsEq(permissionsWithBookmarks,
143                                          permissions));
144             }));
145       }));
146     },
147
148     // These permissions should be on the granted list because they're on the
149     // extension's default permission list.
150     function requestGrantedPermission() {
151       chrome.permissions.request(
152           {permissions: ['management'], origins: ['http://a.com/*']},
153           pass(function(granted) {
154         assertTrue(granted);
155       }));
156     },
157
158     // You can't remove required permissions.
159     function removeRequired() {
160       chrome.permissions.remove(
161           {permissions: ['management']}, fail(REQUIRED_ERROR));
162       chrome.permissions.remove(
163           {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR));
164       chrome.permissions.remove(
165           {permissions: ['bookmarks'], origins: ['http://a.com/*']},
166           fail(REQUIRED_ERROR));
167     },
168
169     // You can remove permissions you don't have (nothing happens).
170     function removeNoOp() {
171       chrome.permissions.remove(
172           {permissions:['background']},
173           pass(function(removed) { assertTrue(removed); }));
174       chrome.permissions.remove(
175           {origins:['http://*.c.com/*']},
176           pass(function(removed) { assertTrue(removed); }));
177       chrome.permissions.remove(
178           {permissions:['background'], origins:['http://*.c.com/*']},
179           pass(function(removed) { assertTrue(removed); }));
180     },
181
182     function removeBookmarks() {
183       chrome.bookmarks.getTree(pass(function(result) {
184         assertTrue(true);
185       }));
186       listenOnce(chrome.permissions.onRemoved,
187                  function(permissions) {
188         assertTrue(permissions.permissions.length == 1);
189         assertTrue(permissions.permissions[0] == 'bookmarks');
190       });
191       chrome.permissions.remove(
192           {permissions:['bookmarks']},
193           pass(function() {
194             chrome.permissions.getAll(pass(function(permissions) {
195               assertTrue(checkPermSetsEq(initialPermissions, permissions));
196             }));
197             assertTrue(typeof chrome.bookmarks == 'object' &&
198                        chrome.bookmarks != null);
199             assertThrows(
200               chrome.bookmarks.getTree, [function(){}],
201               "'bookmarks' requires a different Feature that is not present.");
202           }
203       ));
204     },
205
206     // The user shouldn't have to approve permissions that have no warnings.
207     function noPromptForNoWarnings() {
208       chrome.permissions.request(
209           {permissions: ['notifications']},
210           pass(function(granted) {
211         assertTrue(granted);
212
213         // Remove the notifications permission to return to normal.
214         chrome.permissions.remove(
215             {permissions: ['notifications']},
216             pass(function(removed) { assertTrue(removed); }));
217       }));
218     },
219
220     // Make sure you can only access the white listed permissions.
221     function whitelist() {
222       var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'cloudPrintPrivate');
223       chrome.permissions.request(
224           {permissions: ['cloudPrintPrivate']}, fail(error_msg));
225       chrome.permissions.remove(
226           {permissions: ['cloudPrintPrivate']}, fail(error_msg));
227     },
228
229     function unknownPermission() {
230       var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf');
231       chrome.permissions.request(
232           {permissions: ['asdf']}, fail(error_msg));
233     },
234
235     function requestOrigin() {
236       doReq('http://c.com', pass(function(success) { assertFalse(success); }));
237
238       chrome.permissions.getAll(pass(function(permissions) {
239         assertTrue(checkPermSetsEq(initialPermissions, permissions));
240       }));
241
242       listenOnce(chrome.permissions.onAdded,
243                  function(permissions) {
244         assertTrue(permissions.permissions.length == 0);
245         assertTrue(permissions.origins.length == 1);
246         assertTrue(permissions.origins[0] == 'http://*.c.com/*');
247       });
248       chrome.permissions.request(
249           {origins: ['http://*.c.com/*']},
250           pass(function(granted) {
251         assertTrue(granted);
252         chrome.permissions.getAll(pass(function(permissions) {
253           assertTrue(checkPermSetsEq(permissionsWithOrigin, permissions));
254         }));
255         chrome.permissions.contains(
256             {origins:['http://*.c.com/*']},
257             pass(function(result) { assertTrue(result); }));
258         doReq('http://c.com', pass(function(result) { assertTrue(result); }));
259       }));
260     },
261
262     function removeOrigin() {
263       doReq('http://c.com', pass(function(result) { assertTrue(result); }));
264
265       listenOnce(chrome.permissions.onRemoved,
266                  function(permissions) {
267         assertTrue(permissions.permissions.length == 0);
268         assertTrue(permissions.origins.length == 1);
269         assertTrue(permissions.origins[0] == 'http://*.c.com/*');
270       });
271       chrome.permissions.remove(
272           {origins: ['http://*.c.com/*']},
273           pass(function(removed) {
274         assertTrue(removed);
275         chrome.permissions.getAll(pass(function(permissions) {
276           assertTrue(checkPermSetsEq(initialPermissions, permissions));
277         }));
278         chrome.permissions.contains(
279             {origins:['http://*.c.com/*']},
280             pass(function(result) { assertFalse(result); }));
281         doReq('http://c.com', pass(function(result) { assertFalse(result); }));
282       }));
283     },
284
285     // Tests that the changed permissions have taken effect from inside the
286     // onAdded and onRemoved event listeners.
287     function eventListenerPermissions() {
288       listenOnce(chrome.permissions.onAdded,
289                  function(permissions) {
290         chrome.bookmarks.getTree(pass(function() {
291           assertTrue(true);
292         }));
293       });
294       listenOnce(chrome.permissions.onRemoved,
295                  function(permissions) {
296         assertTrue(typeof chrome.bookmarks == 'object' &&
297                    chrome.bookmarks != null);
298         assertThrows(
299           chrome.bookmarks.getTree, [function(){}],
300           "'bookmarks' requires a different Feature that is not present.");
301       });
302
303       chrome.permissions.request(
304           {permissions: ['bookmarks', 'management']}, pass(function(granted) {
305         assertTrue(granted);
306         chrome.permissions.remove(
307             {permissions: ['bookmarks']}, pass(function() {
308           assertTrue(true);
309         }));
310       }));
311     }
312
313   ]);
314 });