Index: chrome/test/data/extensions/api_test/permissions/optional/background.js |
=================================================================== |
--- chrome/test/data/extensions/api_test/permissions/optional/background.js (revision 0) |
+++ chrome/test/data/extensions/api_test/permissions/optional/background.js (revision 0) |
@@ -0,0 +1,314 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+var assertEq = chrome.test.assertEq; |
+var assertFalse = chrome.test.assertFalse; |
+var assertTrue = chrome.test.assertTrue; |
+var fail = chrome.test.callbackFail; |
+var pass = chrome.test.callbackPass; |
+var listenOnce = chrome.test.listenOnce; |
+ |
+var NOT_OPTIONAL_ERROR = |
+ "Optional permissions must be listed in extension manifest."; |
+ |
+var NO_TABS_PERMISSION = |
+ "You do not have permission to use 'windows.getAll'."; |
+ |
+var REQUIRED_ERROR = |
+ "You cannot remove required permissions."; |
+ |
+var NOT_WHITE_LISTED_ERROR = |
+ "The optional permissions API does not support '*'."; |
+ |
+var UNKNOWN_PERMISSIONS_ERROR = |
+ "'*' is not a recognized permission."; |
+ |
+var emptyPermissions = {permissions: [], origins: []}; |
+ |
+var initialPermissions = { |
+ permissions: ['management'], |
+ origins: ['http://a.com/*'] |
+}; |
+ |
+var permissionsWithTabs = { |
+ permissions: ['management', 'tabs'], |
+ origins: ['http://a.com/*'] |
+} |
+ |
+var permissionsWithOrigin = { |
+ permissions: ['management'], |
+ origins: ['http://a.com/*', 'http://*.c.com/*'] |
+} |
+ |
+function checkEqualSets(set1, set2) { |
+ if (set1.length != set2.length) |
+ return false; |
+ |
+ for (var x = 0; x < set1.length; x++) { |
+ if (!set2.some(function(v) { return v == set1[x]; })) |
+ return false; |
+ } |
+ |
+ return true; |
+} |
+ |
+function checkPermSetsEq(set1, set2) { |
+ return checkEqualSets(set1.permissions, set2.permissions) && |
+ checkEqualSets(set1.origins, set2.origins); |
+} |
+ |
+chrome.test.getConfig(function(config) { |
+ |
+ function doReq(domain, callback) { |
+ var req = new XMLHttpRequest(); |
+ var url = domain + ":PORT/files/extensions/test_file.txt"; |
+ url = url.replace(/PORT/, config.testServer.port); |
+ |
+ chrome.test.log("Requesting url: " + url); |
+ req.open("GET", url, true); |
+ |
+ req.onload = function() { |
+ assertEq(200, req.status); |
+ assertEq("Hello!", req.responseText); |
+ callback(true); |
+ }; |
+ |
+ req.onerror = function() { |
+ chrome.test.log("status: " + req.status); |
+ chrome.test.log("text: " + req.responseText); |
+ callback(false); |
+ }; |
+ |
+ req.send(null); |
+ } |
+ |
+ chrome.test.runTests([ |
+ function contains() { |
+ chrome.permissions.contains( |
+ {permissions: ['management'], origins: ['http://a.com/*']}, |
+ pass(function(result) { assertTrue(result); })); |
+ chrome.permissions.contains( |
+ {permissions: ['devtools'], origins: ['http://a.com/*']}, |
+ pass(function(result) { assertFalse(result); })); |
+ chrome.permissions.contains( |
+ {permissions: ['management']}, |
+ pass(function(result) { assertTrue(result); })); |
+ chrome.permissions.contains( |
+ {permissions: ['management']}, |
+ pass(function(result) { assertTrue(result); })); |
+ }, |
+ |
+ function getAll() { |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
+ })); |
+ }, |
+ |
+ // Nothing should happen if we request permission we already have |
+ function requestNoOp() { |
+ chrome.permissions.request( |
+ {permissions:['management'], origins:['http://a.com/*']}, |
+ pass(function(granted) { assertTrue(granted); })); |
+ }, |
+ |
+ // We should get an error when requesting permissions that haven't been |
+ // defined in "optional_permissions". |
+ function requestNonOptional() { |
+ chrome.permissions.request( |
+ {permissions: ['debugger']}, fail(NOT_OPTIONAL_ERROR)); |
+ chrome.permissions.request( |
+ {origins: ['http://*.b.com/*']}, fail(NOT_OPTIONAL_ERROR)); |
+ chrome.permissions.request( |
+ {permissions: ['tabs'], origins: ['http://*.b.com/*']}, |
+ fail(NOT_OPTIONAL_ERROR)); |
+ }, |
+ |
+ // We should be able to request the tabs API since it's in the granted |
+ // permissions list (see permissions_apitest.cc). |
+ function requestTabs() { |
+ try { |
+ chrome.windows.getAll({populate: true}, function() { |
+ chrome.test.fail("Should not have tabs API permission."); |
+ }); |
+ } catch (e) { |
+ assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); |
+ } |
+ listenOnce(chrome.permissions.onAdded, |
+ function(permissions) { |
+ assertTrue(permissions.permissions.length == 1); |
+ assertTrue(permissions.permissions[0] == 'tabs'); |
+ }); |
+ chrome.permissions.request( |
+ {permissions:['tabs']}, |
+ pass(function(granted) { |
+ assertTrue(granted); |
+ chrome.windows.getAll({populate: true}, pass(function(windows) { |
+ assertTrue(true); |
+ })); |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(permissionsWithTabs, permissions)); |
+ })); |
+ })); |
+ }, |
+ |
+ // You can't remove required permissions. |
+ function removeRequired() { |
+ chrome.permissions.remove( |
+ {permissions: ['management']}, fail(REQUIRED_ERROR)); |
+ chrome.permissions.remove( |
+ {origins: ['http://a.com/*']}, fail(REQUIRED_ERROR)); |
+ chrome.permissions.remove( |
+ {permissions: ['tabs'], origins: ['http://a.com/*']}, |
+ fail(REQUIRED_ERROR)); |
+ }, |
+ |
+ // You can remove permissions you don't have (nothing happens). |
+ function removeNoOp() { |
+ chrome.permissions.remove( |
+ {permissions:['background']}, |
+ pass(function(removed) { assertTrue(removed); })); |
+ chrome.permissions.remove( |
+ {origins:['http://*.c.com/*']}, |
+ pass(function(removed) { assertTrue(removed); })); |
+ chrome.permissions.remove( |
+ {permissions:['background'], origins:['http://*.c.com/*']}, |
+ pass(function(removed) { assertTrue(removed); })); |
+ }, |
+ |
+ function removeTabs() { |
+ chrome.windows.getAll({populate: true}, pass(function(windows) { |
+ assertTrue(true); |
+ })); |
+ listenOnce(chrome.permissions.onRemoved, |
+ function(permissions) { |
+ assertTrue(permissions.permissions.length == 1); |
+ assertTrue(permissions.permissions[0] == 'tabs'); |
+ }); |
+ chrome.permissions.remove( |
+ {permissions:['tabs']}, |
+ pass(function() { |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
+ })); |
+ try { |
+ chrome.windows.getAll({populate: true}, function() { |
+ chrome.test.fail("Should not have tabs API permission."); |
+ }); |
+ } catch (e) { |
+ assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); |
+ } |
+ })); |
+ }, |
+ |
+ // The user shouldn't have to approve permissions that have no warnings. |
+ function noPromptForNoWarnings() { |
+ chrome.permissions.request( |
+ {permissions: ['notifications']}, |
+ pass(function(granted) { |
+ assertTrue(granted); |
+ |
+ // Remove the notifications permission to return to normal. |
+ chrome.permissions.remove( |
+ {permissions: ['notifications']}, |
+ pass(function(removed) { assertTrue(removed); })); |
+ })); |
+ }, |
+ |
+ // Make sure you can only access the white listed permissions. |
+ function whitelist() { |
+ var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'chromeAuthPrivate'); |
+ chrome.permissions.request( |
+ {permissions: ['chromeAuthPrivate']}, fail(error_msg)); |
+ chrome.permissions.remove( |
+ {permissions: ['chromeAuthPrivate']}, fail(error_msg)); |
+ }, |
+ |
+ function unknownPermission() { |
+ var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf'); |
+ chrome.permissions.request( |
+ {permissions: ['asdf']}, fail(error_msg)); |
+ }, |
+ |
+ function requestOrigin() { |
+ doReq('http://c.com', pass(function(success) { assertFalse(success); })); |
+ |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
+ })); |
+ |
+ listenOnce(chrome.permissions.onAdded, |
+ function(permissions) { |
+ assertTrue(permissions.permissions.length == 0); |
+ assertTrue(permissions.origins.length == 1); |
+ assertTrue(permissions.origins[0] == 'http://*.c.com/*'); |
+ }); |
+ chrome.permissions.request( |
+ {origins: ['http://*.c.com/*']}, |
+ pass(function(granted) { |
+ assertTrue(granted); |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(permissionsWithOrigin, permissions)); |
+ })); |
+ chrome.permissions.contains( |
+ {origins:['http://*.c.com/*']}, |
+ pass(function(result) { assertTrue(result); })); |
+ doReq('http://c.com', pass(function(result) { assertTrue(result); })); |
+ })); |
+ }, |
+ |
+ function removeOrigin() { |
+ doReq('http://c.com', pass(function(result) { assertTrue(result); })); |
+ |
+ listenOnce(chrome.permissions.onRemoved, |
+ function(permissions) { |
+ assertTrue(permissions.permissions.length == 0); |
+ assertTrue(permissions.origins.length == 1); |
+ assertTrue(permissions.origins[0] == 'http://*.c.com/*'); |
+ }); |
+ chrome.permissions.remove( |
+ {origins: ['http://*.c.com/*']}, |
+ pass(function(removed) { |
+ assertTrue(removed); |
+ chrome.permissions.getAll(pass(function(permissions) { |
+ assertTrue(checkPermSetsEq(initialPermissions, permissions)); |
+ })); |
+ chrome.permissions.contains( |
+ {origins:['http://*.c.com/*']}, |
+ pass(function(result) { assertFalse(result); })); |
+ doReq('http://c.com', pass(function(result) { assertFalse(result); })); |
+ })); |
+ }, |
+ |
+ // Tests that the changed permissions have taken effect from inside the |
+ // onAdded and onRemoved event listeners. |
+ function eventListenerPermissions() { |
+ listenOnce(chrome.permissions.onAdded, |
+ function(permissions) { |
+ chrome.windows.getAll({populate: true}, pass(function() { |
+ assertTrue(true); |
+ })); |
+ }); |
+ listenOnce(chrome.permissions.onRemoved, |
+ function(permissions) { |
+ try { |
+ chrome.windows.getAll({populate: true}, function() { |
+ chrome.test.fail("Should not have tabs API permission."); |
+ }); |
+ } catch (e) { |
+ assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); |
+ } |
+ }); |
+ |
+ chrome.permissions.request( |
+ {permissions: ['tabs']}, pass(function(granted) { |
+ assertTrue(granted); |
+ chrome.permissions.remove( |
+ {permissions: ['tabs']}, pass(function() { |
+ assertTrue(true); |
+ })); |
+ })); |
+ } |
+ |
+ ]); |
+}); |