| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 var assertEq = chrome.test.assertEq; | 5 var assertEq = chrome.test.assertEq; |
| 6 var assertFalse = chrome.test.assertFalse; | 6 var assertFalse = chrome.test.assertFalse; |
| 7 var assertTrue = chrome.test.assertTrue; | 7 var assertTrue = chrome.test.assertTrue; |
| 8 var fail = chrome.test.callbackFail; | 8 var fail = chrome.test.callbackFail; |
| 9 var pass = chrome.test.callbackPass; | 9 var pass = chrome.test.callbackPass; |
| 10 var listenOnce = chrome.test.listenOnce; | 10 var listenOnce = chrome.test.listenOnce; |
| 11 | 11 |
| 12 var NOT_OPTIONAL_ERROR = | 12 var NOT_OPTIONAL_ERROR = |
| 13 "Optional permissions must be listed in extension manifest."; | 13 "Optional permissions must be listed in extension manifest."; |
| 14 | 14 |
| 15 var NO_TABS_PERMISSION = | 15 var NO_BOOKMARKS_PERMISSION = |
| 16 "You do not have permission to use 'windows.getAll'."; | 16 "You do not have permission to use 'bookmarks.getTree'."; |
| 17 | 17 |
| 18 var REQUIRED_ERROR = | 18 var REQUIRED_ERROR = |
| 19 "You cannot remove required permissions."; | 19 "You cannot remove required permissions."; |
| 20 | 20 |
| 21 var NOT_WHITE_LISTED_ERROR = | 21 var NOT_WHITE_LISTED_ERROR = |
| 22 "The optional permissions API does not support '*'."; | 22 "The optional permissions API does not support '*'."; |
| 23 | 23 |
| 24 var UNKNOWN_PERMISSIONS_ERROR = | 24 var UNKNOWN_PERMISSIONS_ERROR = |
| 25 "'*' is not a recognized permission."; | 25 "'*' is not a recognized permission."; |
| 26 | 26 |
| 27 var emptyPermissions = {permissions: [], origins: []}; | 27 var emptyPermissions = {permissions: [], origins: []}; |
| 28 | 28 |
| 29 var initialPermissions = { | 29 var initialPermissions = { |
| 30 permissions: ['management'], | 30 permissions: ['management'], |
| 31 origins: ['http://a.com/*'] | 31 origins: ['http://a.com/*'] |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 var permissionsWithTabs = { | 34 var permissionsWithBookmarks = { |
| 35 permissions: ['management', 'tabs'], | 35 permissions: ['management', 'bookmarks'], |
| 36 origins: ['http://a.com/*'] | 36 origins: ['http://a.com/*'] |
| 37 } | 37 } |
| 38 | 38 |
| 39 var permissionsWithOrigin = { | 39 var permissionsWithOrigin = { |
| 40 permissions: ['management'], | 40 permissions: ['management'], |
| 41 origins: ['http://a.com/*', 'http://*.c.com/*'] | 41 origins: ['http://a.com/*', 'http://*.c.com/*'] |
| 42 } | 42 } |
| 43 | 43 |
| 44 function checkEqualSets(set1, set2) { | 44 function checkEqualSets(set1, set2) { |
| 45 if (set1.length != set2.length) | 45 if (set1.length != set2.length) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 function requestNoOp() { | 109 function requestNoOp() { |
| 110 chrome.permissions.request( | 110 chrome.permissions.request( |
| 111 {permissions:['management'], origins:['http://a.com/*']}, | 111 {permissions:['management'], origins:['http://a.com/*']}, |
| 112 pass(function(granted) { assertTrue(granted); })); | 112 pass(function(granted) { assertTrue(granted); })); |
| 113 }, | 113 }, |
| 114 | 114 |
| 115 // We should get an error when requesting permissions that haven't been | 115 // We should get an error when requesting permissions that haven't been |
| 116 // defined in "optional_permissions". | 116 // defined in "optional_permissions". |
| 117 function requestNonOptional() { | 117 function requestNonOptional() { |
| 118 chrome.permissions.request( | 118 chrome.permissions.request( |
| 119 {permissions: ['bookmarks']}, fail(NOT_OPTIONAL_ERROR)); | 119 {permissions: ['history']}, fail(NOT_OPTIONAL_ERROR)); |
| 120 chrome.permissions.request( | 120 chrome.permissions.request( |
| 121 {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR)); | 121 {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR)); |
| 122 chrome.permissions.request( | 122 chrome.permissions.request( |
| 123 {permissions: ['tabs'], origins: ['http://*.b.com/*']}, | 123 {permissions: ['history'], origins: ['http://*.b.com/*']}, |
| 124 fail(NOT_OPTIONAL_ERROR)); | 124 fail(NOT_OPTIONAL_ERROR)); |
| 125 }, | 125 }, |
| 126 | 126 |
| 127 // We should be able to request the tabs API since it's in the granted | 127 // We should be able to request the bookmarks API since it's in the granted |
| 128 // permissions list (see permissions_apitest.cc). | 128 // permissions list (see permissions_apitest.cc). |
| 129 function requestTabs() { | 129 function requestBookmarks() { |
| 130 // chrome.windows is a optional permission, so the API definition should | 130 // chrome.bookmarks is a optional permission, so the API definition should |
| 131 // exist but its use disallowed. | 131 // exist but its use disallowed. |
| 132 assertTrue(!!chrome.windows); | 132 assertTrue(!!chrome.bookmarks); |
| 133 try { | 133 try { |
| 134 chrome.windows.getAll({populate: true}, function() { | 134 chrome.bookmarks.getTree(function() { |
| 135 chrome.test.fail("Should not have tabs API permission."); | 135 chrome.test.fail("Should not have bookmarks API permission."); |
| 136 }); | 136 }); |
| 137 } catch (e) { | 137 } catch (e) { |
| 138 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | 138 assertTrue(e.message.indexOf(NO_BOOKMARKS_PERMISSION) == 0); |
| 139 } | 139 } |
| 140 listenOnce(chrome.permissions.onAdded, | 140 listenOnce(chrome.permissions.onAdded, |
| 141 function(permissions) { | 141 function(permissions) { |
| 142 assertTrue(permissions.permissions.length == 1); | 142 assertTrue(permissions.permissions.length == 1); |
| 143 assertTrue(permissions.permissions[0] == 'tabs'); | 143 assertTrue(permissions.permissions[0] == 'bookmarks'); |
| 144 }); | 144 }); |
| 145 chrome.permissions.request( | 145 chrome.permissions.request( |
| 146 {permissions:['tabs']}, | 146 {permissions:['bookmarks']}, |
| 147 pass(function(granted) { | 147 pass(function(granted) { |
| 148 assertTrue(granted); | 148 assertTrue(granted); |
| 149 chrome.windows.getAll({populate: true}, pass(function(windows) { | 149 chrome.bookmarks.getTree(pass(function(result) { |
| 150 assertTrue(true); | 150 assertTrue(true); |
| 151 })); | 151 })); |
| 152 chrome.permissions.getAll(pass(function(permissions) { | 152 chrome.permissions.getAll(pass(function(permissions) { |
| 153 assertTrue(checkPermSetsEq(permissionsWithTabs, permissions)); | 153 assertTrue(checkPermSetsEq(permissionsWithBookmarks, |
| 154 permissions)); |
| 154 })); | 155 })); |
| 155 })); | 156 })); |
| 156 }, | 157 }, |
| 157 | 158 |
| 158 // These permissions should be on the granted list because they're on the | 159 // These permissions should be on the granted list because they're on the |
| 159 // extension's default permission list. | 160 // extension's default permission list. |
| 160 function requestGrantedPermission() { | 161 function requestGrantedPermission() { |
| 161 chrome.permissions.request( | 162 chrome.permissions.request( |
| 162 {permissions: ['management'], origins: ['http://a.com/*']}, | 163 {permissions: ['management'], origins: ['http://a.com/*']}, |
| 163 pass(function(granted) { | 164 pass(function(granted) { |
| 164 assertTrue(granted); | 165 assertTrue(granted); |
| 165 })); | 166 })); |
| 166 }, | 167 }, |
| 167 | 168 |
| 168 // You can't remove required permissions. | 169 // You can't remove required permissions. |
| 169 function removeRequired() { | 170 function removeRequired() { |
| 170 chrome.permissions.remove( | 171 chrome.permissions.remove( |
| 171 {permissions: ['management']}, fail(REQUIRED_ERROR)); | 172 {permissions: ['management']}, fail(REQUIRED_ERROR)); |
| 172 chrome.permissions.remove( | 173 chrome.permissions.remove( |
| 173 {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR)); | 174 {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR)); |
| 174 chrome.permissions.remove( | 175 chrome.permissions.remove( |
| 175 {permissions: ['tabs'], origins: ['http://a.com/*']}, | 176 {permissions: ['bookmarks'], origins: ['http://a.com/*']}, |
| 176 fail(REQUIRED_ERROR)); | 177 fail(REQUIRED_ERROR)); |
| 177 }, | 178 }, |
| 178 | 179 |
| 179 // You can remove permissions you don't have (nothing happens). | 180 // You can remove permissions you don't have (nothing happens). |
| 180 function removeNoOp() { | 181 function removeNoOp() { |
| 181 chrome.permissions.remove( | 182 chrome.permissions.remove( |
| 182 {permissions:['background']}, | 183 {permissions:['background']}, |
| 183 pass(function(removed) { assertTrue(removed); })); | 184 pass(function(removed) { assertTrue(removed); })); |
| 184 chrome.permissions.remove( | 185 chrome.permissions.remove( |
| 185 {origins:['http://*.c.com/*']}, | 186 {origins:['http://*.c.com/*']}, |
| 186 pass(function(removed) { assertTrue(removed); })); | 187 pass(function(removed) { assertTrue(removed); })); |
| 187 chrome.permissions.remove( | 188 chrome.permissions.remove( |
| 188 {permissions:['background'], origins:['http://*.c.com/*']}, | 189 {permissions:['background'], origins:['http://*.c.com/*']}, |
| 189 pass(function(removed) { assertTrue(removed); })); | 190 pass(function(removed) { assertTrue(removed); })); |
| 190 }, | 191 }, |
| 191 | 192 |
| 192 function removeTabs() { | 193 function removeBookmarks() { |
| 193 chrome.windows.getAll({populate: true}, pass(function(windows) { | 194 chrome.bookmarks.getTree(pass(function(result) { |
| 194 assertTrue(true); | 195 assertTrue(true); |
| 195 })); | 196 })); |
| 196 listenOnce(chrome.permissions.onRemoved, | 197 listenOnce(chrome.permissions.onRemoved, |
| 197 function(permissions) { | 198 function(permissions) { |
| 198 assertTrue(permissions.permissions.length == 1); | 199 assertTrue(permissions.permissions.length == 1); |
| 199 assertTrue(permissions.permissions[0] == 'tabs'); | 200 assertTrue(permissions.permissions[0] == 'bookmarks'); |
| 200 }); | 201 }); |
| 201 chrome.permissions.remove( | 202 chrome.permissions.remove( |
| 202 {permissions:['tabs']}, | 203 {permissions:['bookmarks']}, |
| 203 pass(function() { | 204 pass(function() { |
| 204 chrome.permissions.getAll(pass(function(permissions) { | 205 chrome.permissions.getAll(pass(function(permissions) { |
| 205 assertTrue(checkPermSetsEq(initialPermissions, permissions)); | 206 assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
| 206 })); | 207 })); |
| 207 try { | 208 try { |
| 208 chrome.windows.getAll({populate: true}, function() { | 209 chrome.bookmarks.getTree(function() { |
| 209 chrome.test.fail("Should not have tabs API permission."); | 210 chrome.test.fail("Should not have bookmarks API permission."); |
| 210 }); | 211 }); |
| 211 } catch (e) { | 212 } catch (e) { |
| 212 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | 213 assertTrue(e.message.indexOf(NO_BOOKMARKS_PERMISSION) == 0); |
| 213 } | 214 } |
| 214 })); | 215 })); |
| 215 }, | 216 }, |
| 216 | 217 |
| 217 // The user shouldn't have to approve permissions that have no warnings. | 218 // The user shouldn't have to approve permissions that have no warnings. |
| 218 function noPromptForNoWarnings() { | 219 function noPromptForNoWarnings() { |
| 219 chrome.permissions.request( | 220 chrome.permissions.request( |
| 220 {permissions: ['notifications']}, | 221 {permissions: ['notifications']}, |
| 221 pass(function(granted) { | 222 pass(function(granted) { |
| 222 assertTrue(granted); | 223 assertTrue(granted); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 pass(function(result) { assertFalse(result); })); | 292 pass(function(result) { assertFalse(result); })); |
| 292 doReq('http://c.com', pass(function(result) { assertFalse(result); })); | 293 doReq('http://c.com', pass(function(result) { assertFalse(result); })); |
| 293 })); | 294 })); |
| 294 }, | 295 }, |
| 295 | 296 |
| 296 // Tests that the changed permissions have taken effect from inside the | 297 // Tests that the changed permissions have taken effect from inside the |
| 297 // onAdded and onRemoved event listeners. | 298 // onAdded and onRemoved event listeners. |
| 298 function eventListenerPermissions() { | 299 function eventListenerPermissions() { |
| 299 listenOnce(chrome.permissions.onAdded, | 300 listenOnce(chrome.permissions.onAdded, |
| 300 function(permissions) { | 301 function(permissions) { |
| 301 chrome.windows.getAll({populate: true}, pass(function() { | 302 chrome.bookmarks.getTree(pass(function() { |
| 302 assertTrue(true); | 303 assertTrue(true); |
| 303 })); | 304 })); |
| 304 }); | 305 }); |
| 305 listenOnce(chrome.permissions.onRemoved, | 306 listenOnce(chrome.permissions.onRemoved, |
| 306 function(permissions) { | 307 function(permissions) { |
| 307 try { | 308 try { |
| 308 chrome.windows.getAll({populate: true}, function() { | 309 chrome.bookmarks.getTree(function() { |
| 309 chrome.test.fail("Should not have tabs API permission."); | 310 chrome.test.fail("Should not have bookmakrs API permission."); |
| 310 }); | 311 }); |
| 311 } catch (e) { | 312 } catch (e) { |
| 312 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | 313 assertTrue(e.message.indexOf(NO_BOOKMARKS_PERMISSION) == 0); |
| 313 } | 314 } |
| 314 }); | 315 }); |
| 315 | 316 |
| 316 chrome.permissions.request( | 317 chrome.permissions.request( |
| 317 {permissions: ['tabs', 'management']}, pass(function(granted) { | 318 {permissions: ['bookmarks', 'management']}, pass(function(granted) { |
| 318 assertTrue(granted); | 319 assertTrue(granted); |
| 319 chrome.permissions.remove( | 320 chrome.permissions.remove( |
| 320 {permissions: ['tabs']}, pass(function() { | 321 {permissions: ['bookmarks']}, pass(function() { |
| 321 assertTrue(true); | 322 assertTrue(true); |
| 322 })); | 323 })); |
| 323 })); | 324 })); |
| 324 } | 325 } |
| 325 | 326 |
| 326 ]); | 327 ]); |
| 327 }); | 328 }); |
| OLD | NEW |