Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <script> | |
| 2 | |
| 3 var assertFalse = chrome.test.assertFalse; | |
| 4 var assertTrue = chrome.test.assertTrue; | |
| 5 var fail = chrome.test.callbackFail; | |
| 6 var pass = chrome.test.callbackPass; | |
| 7 var listenOnce = chrome.test.listenOnce; | |
| 8 | |
| 9 var NOT_OPTIONAL_ERROR = | |
| 10 "Optional permissions must be listed in extension manifest."; | |
| 11 | |
| 12 var NO_TABS_PERMISSION = | |
| 13 "You do not have permission to use 'windows.getAll'."; | |
| 14 | |
| 15 var REQUIRED_ERROR = | |
| 16 "You cannot remove required permissions."; | |
| 17 | |
| 18 var NOT_WHITE_LISTED_ERROR = | |
| 19 "The optional permissions API does not support '*'."; | |
| 20 | |
| 21 var UNKNOWN_PERMISSIONS_ERROR = | |
| 22 "'*' is not a recognized permission."; | |
| 23 | |
| 24 var empty_permissions = {apis: []}; | |
|
Mihai Parparita -not on Chrome
2011/07/20 22:03:43
I think we generally use the JS naming scheme in t
jstritar
2011/07/22 19:21:55
Done.
| |
| 25 | |
| 26 var initial_permissions = { | |
| 27 apis: ['management', 'permissions'] | |
| 28 }; | |
| 29 | |
| 30 var permissions_with_tabs = { | |
| 31 apis: ['management', 'permissions', 'tabs'] | |
| 32 } | |
| 33 | |
| 34 function checkEqualSets(set1, set2) { | |
| 35 if (set1.length != set2.length) | |
| 36 return false; | |
| 37 | |
| 38 for (var x = 0; x < set1.length; x++) { | |
| 39 if (!set2.some(function(v) { return v == set1[x]; })) | |
| 40 return false; | |
| 41 } | |
| 42 | |
| 43 return true; | |
| 44 } | |
| 45 | |
| 46 function checkPermSetsEq(set1, set2) { | |
| 47 return checkEqualSets(set1.apis, set2.apis); | |
| 48 } | |
| 49 | |
| 50 chrome.test.runTests([ | |
| 51 function contains() { | |
| 52 chrome.permissions.contains({apis: ['management']}, pass(function(result) { | |
| 53 assertTrue(result); | |
| 54 })); | |
| 55 chrome.permissions.contains({apis: ['devtools']}, pass(function(result) { | |
| 56 assertFalse(result); | |
| 57 })); | |
| 58 chrome.permissions.contains({ | |
| 59 apis: ['permissions', 'management'] | |
| 60 }, pass(function(result) { | |
|
Mihai Parparita -not on Chrome
2011/07/20 22:03:43
Seems like this should be intended more, since it'
jstritar
2011/07/22 19:21:55
Looked kind of strange that way, since the content
| |
| 61 assertTrue(result); | |
| 62 })); | |
| 63 chrome.permissions.contains({ | |
| 64 apis: ['management', 'permissions'] | |
| 65 }, pass(function(result) { | |
| 66 assertTrue(result); | |
| 67 })); | |
| 68 }, | |
| 69 | |
| 70 function getAll() { | |
| 71 chrome.permissions.getAll(pass(function(permissions) { | |
| 72 assertTrue(checkPermSetsEq(initial_permissions, permissions)); | |
| 73 })); | |
| 74 }, | |
| 75 | |
| 76 // Nothing should happen if we request permission we already have | |
| 77 function requestNoOp() { | |
| 78 chrome.permissions.request({apis:['management']}, pass(function(granted) { | |
| 79 assertTrue(granted); | |
| 80 })); | |
| 81 }, | |
| 82 | |
| 83 // We should get an error when requesting permissions that haven't been | |
| 84 // defined in "optional_permissions". | |
| 85 function requestNonOptional() { | |
| 86 chrome.permissions.request({apis:['debugger']}, fail(NOT_OPTIONAL_ERROR)); | |
| 87 }, | |
| 88 | |
| 89 // We should be able to request the tabs API since it's in the granted | |
| 90 // permissions list (see permissions_apitest.cc). | |
| 91 function requestTabs() { | |
| 92 try { | |
| 93 chrome.windows.getAll({populate: true}, function() { | |
| 94 assertTrue(false); | |
|
Mihai Parparita -not on Chrome
2011/07/20 22:03:43
Something like chrome.test.fail("Should not have t
jstritar
2011/07/22 19:21:55
Done.
| |
| 95 }); | |
| 96 } catch (e) { | |
| 97 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | |
| 98 } | |
| 99 listenOnce(chrome.permissions.onAdded, function(permissions) { | |
| 100 assertTrue(permissions.apis.length == 1); | |
| 101 assertTrue(permissions.apis[0] == 'tabs'); | |
| 102 }); | |
| 103 chrome.permissions.request({apis:['tabs']}, pass(function(granted) { | |
| 104 assertTrue(granted); | |
| 105 chrome.windows.getAll({populate: true}, pass(function(windows) { | |
| 106 assertTrue(true); | |
| 107 })); | |
| 108 chrome.permissions.getAll(pass(function(permissions) { | |
| 109 assertTrue(checkPermSetsEq(permissions_with_tabs, permissions)); | |
| 110 })); | |
| 111 })); | |
| 112 }, | |
| 113 | |
| 114 // You can't remove required permissions. | |
| 115 function removeRequired() { | |
| 116 chrome.permissions.remove({apis:['management']}, fail(REQUIRED_ERROR)); | |
| 117 }, | |
| 118 | |
| 119 // You can remove permissions you don't have (nothing happens). | |
| 120 function removeNoOp() { | |
| 121 chrome.permissions.remove({apis:['background']}, pass(function(removed) { | |
| 122 assertTrue(removed); | |
| 123 })); | |
| 124 }, | |
| 125 | |
| 126 function removeTabs() { | |
| 127 chrome.windows.getAll({populate: true}, pass(function(windows) { | |
| 128 assertTrue(true); | |
| 129 })); | |
| 130 listenOnce(chrome.permissions.onRemoved, function(permissions) { | |
| 131 assertTrue(permissions.apis.length == 1); | |
| 132 assertTrue(permissions.apis[0] == 'tabs'); | |
| 133 }); | |
| 134 chrome.permissions.remove({apis:['tabs']}, pass(function() { | |
| 135 chrome.permissions.getAll(pass(function(permissions) { | |
| 136 assertTrue(checkPermSetsEq(initial_permissions, permissions)); | |
| 137 })); | |
| 138 try { | |
| 139 chrome.windows.getAll({populate: true}, function() { | |
| 140 assertTrue(false); | |
|
Mihai Parparita -not on Chrome
2011/07/20 22:03:43
Ditto about using chrome.test.fail("Should not hav
jstritar
2011/07/22 19:21:55
Done.
| |
| 141 }); | |
| 142 } catch (e) { | |
| 143 assertTrue(e.message.indexOf(NO_TABS_PERMISSION) == 0); | |
| 144 } | |
| 145 })); | |
| 146 }, | |
| 147 | |
| 148 // The user shouldn't have to approve permissions that have no warnings. | |
| 149 // TODO(jstritar): move to its own test and set a low timeout | |
| 150 function noPromptForNoWarnings() { | |
| 151 chrome.permissions.request({apis: ['notifications']}, | |
| 152 pass(function(granted) { | |
| 153 assertTrue(granted); | |
| 154 })); | |
| 155 }, | |
| 156 | |
| 157 // Make sure you can only access the white listed permissions. | |
| 158 function whitelist() { | |
| 159 var error_msg = NOT_WHITE_LISTED_ERROR.replace('*', 'chromeAuthPrivate'); | |
| 160 chrome.permissions.request({apis: ['chromeAuthPrivate']}, fail(error_msg)); | |
| 161 chrome.permissions.remove({apis: ['chromeAuthPrivate']}, fail(error_msg)); | |
| 162 }, | |
| 163 | |
| 164 function unknownPermission() { | |
| 165 var error_msg = UNKNOWN_PERMISSIONS_ERROR.replace('*', 'asdf'); | |
| 166 chrome.permissions.request({apis: ['asdf']}, fail(error_msg)); | |
| 167 } | |
| 168 ]); | |
| 169 </script> | |
| OLD | NEW |