Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); |
michael@0 | 5 | Components.utils.import("resource://gre/modules/NetUtil.jsm"); |
michael@0 | 6 | |
michael@0 | 7 | const ABOUT_PERMISSIONS_SPEC = "about:permissions"; |
michael@0 | 8 | |
michael@0 | 9 | const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); |
michael@0 | 10 | const TEST_URI_2 = NetUtil.newURI("http://mozilla.org/"); |
michael@0 | 11 | |
michael@0 | 12 | const TEST_PRINCIPAL_1 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_1); |
michael@0 | 13 | const TEST_PRINCIPAL_2 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_2); |
michael@0 | 14 | |
michael@0 | 15 | // values from DefaultPermissions object |
michael@0 | 16 | const PERM_UNKNOWN = 0; |
michael@0 | 17 | const PERM_ALLOW = 1; |
michael@0 | 18 | const PERM_DENY = 2; |
michael@0 | 19 | // cookie specific permissions |
michael@0 | 20 | const PERM_FIRST_PARTY_ONLY = 9; |
michael@0 | 21 | |
michael@0 | 22 | // used to set permissions on test sites |
michael@0 | 23 | const TEST_PERMS = { |
michael@0 | 24 | "password": PERM_ALLOW, |
michael@0 | 25 | "cookie": PERM_ALLOW, |
michael@0 | 26 | "geo": PERM_UNKNOWN, |
michael@0 | 27 | "indexedDB": PERM_UNKNOWN, |
michael@0 | 28 | "popup": PERM_DENY, |
michael@0 | 29 | "fullscreen" : PERM_UNKNOWN, |
michael@0 | 30 | "camera": PERM_UNKNOWN, |
michael@0 | 31 | "microphone": PERM_UNKNOWN |
michael@0 | 32 | }; |
michael@0 | 33 | |
michael@0 | 34 | const NO_GLOBAL_ALLOW = [ |
michael@0 | 35 | "geo", |
michael@0 | 36 | "indexedDB", |
michael@0 | 37 | "fullscreen" |
michael@0 | 38 | ]; |
michael@0 | 39 | |
michael@0 | 40 | // number of managed permissions in the interface |
michael@0 | 41 | const TEST_PERMS_COUNT = 8; |
michael@0 | 42 | |
michael@0 | 43 | function test() { |
michael@0 | 44 | waitForExplicitFinish(); |
michael@0 | 45 | registerCleanupFunction(cleanUp); |
michael@0 | 46 | |
michael@0 | 47 | // add test history visit |
michael@0 | 48 | addVisits(TEST_URI_1, function() { |
michael@0 | 49 | // set permissions ourselves to avoid problems with different defaults |
michael@0 | 50 | // from test harness configuration |
michael@0 | 51 | for (let type in TEST_PERMS) { |
michael@0 | 52 | if (type == "password") { |
michael@0 | 53 | Services.logins.setLoginSavingEnabled(TEST_URI_2.prePath, true); |
michael@0 | 54 | } else { |
michael@0 | 55 | // set permissions on a site without history visits to test enumerateServices |
michael@0 | 56 | Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, type, TEST_PERMS[type]); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | // open about:permissions |
michael@0 | 61 | gBrowser.selectedTab = gBrowser.addTab("about:permissions"); |
michael@0 | 62 | }); |
michael@0 | 63 | |
michael@0 | 64 | function observer() { |
michael@0 | 65 | Services.obs.removeObserver(observer, "browser-permissions-initialized"); |
michael@0 | 66 | runNextTest(); |
michael@0 | 67 | } |
michael@0 | 68 | Services.obs.addObserver(observer, "browser-permissions-initialized", false); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | function cleanUp() { |
michael@0 | 72 | for (let type in TEST_PERMS) { |
michael@0 | 73 | if (type != "password") { |
michael@0 | 74 | Services.perms.removeFromPrincipal(TEST_PRINCIPAL_1, type); |
michael@0 | 75 | Services.perms.removeFromPrincipal(TEST_PRINCIPAL_2, type); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | gBrowser.removeTab(gBrowser.selectedTab); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | function runNextTest() { |
michael@0 | 83 | if (gTestIndex == tests.length) { |
michael@0 | 84 | waitForClearHistory(finish); |
michael@0 | 85 | return; |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | let nextTest = tests[gTestIndex++]; |
michael@0 | 89 | info("[" + nextTest.name + "] running test"); |
michael@0 | 90 | nextTest(); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | var gSitesList; |
michael@0 | 94 | var gHeaderDeck; |
michael@0 | 95 | var gSiteLabel; |
michael@0 | 96 | |
michael@0 | 97 | var gTestIndex = 0; |
michael@0 | 98 | var tests = [ |
michael@0 | 99 | function test_page_load() { |
michael@0 | 100 | is(gBrowser.currentURI.spec, ABOUT_PERMISSIONS_SPEC, "about:permissions loaded"); |
michael@0 | 101 | |
michael@0 | 102 | gSitesList = gBrowser.contentDocument.getElementById("sites-list"); |
michael@0 | 103 | ok(gSitesList, "got sites list"); |
michael@0 | 104 | |
michael@0 | 105 | gHeaderDeck = gBrowser.contentDocument.getElementById("header-deck"); |
michael@0 | 106 | ok(gHeaderDeck, "got header deck"); |
michael@0 | 107 | |
michael@0 | 108 | gSiteLabel = gBrowser.contentDocument.getElementById("site-label"); |
michael@0 | 109 | ok(gSiteLabel, "got site label"); |
michael@0 | 110 | |
michael@0 | 111 | runNextTest(); |
michael@0 | 112 | }, |
michael@0 | 113 | |
michael@0 | 114 | function test_sites_list() { |
michael@0 | 115 | is(gSitesList.firstChild.id, "all-sites-item", |
michael@0 | 116 | "all sites is the first item in the sites list"); |
michael@0 | 117 | |
michael@0 | 118 | ok(getSiteItem(TEST_URI_1.host), "site item from places db exists"); |
michael@0 | 119 | ok(getSiteItem(TEST_URI_2.host), "site item from enumerating services exists"); |
michael@0 | 120 | |
michael@0 | 121 | runNextTest(); |
michael@0 | 122 | }, |
michael@0 | 123 | |
michael@0 | 124 | function test_filter_sites_list() { |
michael@0 | 125 | // set filter to test host |
michael@0 | 126 | let sitesFilter = gBrowser.contentDocument.getElementById("sites-filter"); |
michael@0 | 127 | sitesFilter.value = TEST_URI_1.host; |
michael@0 | 128 | sitesFilter.doCommand(); |
michael@0 | 129 | |
michael@0 | 130 | // make sure correct sites are collapsed/showing |
michael@0 | 131 | let testSite1 = getSiteItem(TEST_URI_1.host); |
michael@0 | 132 | ok(!testSite1.collapsed, "test site 1 is not collapsed"); |
michael@0 | 133 | let testSite2 = getSiteItem(TEST_URI_2.host); |
michael@0 | 134 | ok(testSite2.collapsed, "test site 2 is collapsed"); |
michael@0 | 135 | |
michael@0 | 136 | // clear filter |
michael@0 | 137 | sitesFilter.value = ""; |
michael@0 | 138 | sitesFilter.doCommand(); |
michael@0 | 139 | |
michael@0 | 140 | runNextTest(); |
michael@0 | 141 | }, |
michael@0 | 142 | |
michael@0 | 143 | function test_all_sites() { |
michael@0 | 144 | // "All Sites" item should be selected when the page is first loaded |
michael@0 | 145 | is(gSitesList.selectedItem, gBrowser.contentDocument.getElementById("all-sites-item"), |
michael@0 | 146 | "all sites item is selected"); |
michael@0 | 147 | |
michael@0 | 148 | let defaultsHeader = gBrowser.contentDocument.getElementById("defaults-header"); |
michael@0 | 149 | is(defaultsHeader, gHeaderDeck.selectedPanel, |
michael@0 | 150 | "correct header shown for all sites"); |
michael@0 | 151 | |
michael@0 | 152 | ok(gBrowser.contentDocument.getElementById("passwords-count").hidden, |
michael@0 | 153 | "passwords count is hidden"); |
michael@0 | 154 | ok(gBrowser.contentDocument.getElementById("cookies-count").hidden, |
michael@0 | 155 | "cookies count is hidden"); |
michael@0 | 156 | |
michael@0 | 157 | // Test to make sure "Allow" items hidden for certain permission types |
michael@0 | 158 | NO_GLOBAL_ALLOW.forEach(function(aType) { |
michael@0 | 159 | let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW); |
michael@0 | 160 | ok(menuitem.hidden, aType + " allow menuitem hidden for all sites"); |
michael@0 | 161 | }); |
michael@0 | 162 | |
michael@0 | 163 | runNextTest(); |
michael@0 | 164 | }, |
michael@0 | 165 | |
michael@0 | 166 | function test_all_sites_permission() { |
michael@0 | 167 | // apply the old default of allowing all cookies |
michael@0 | 168 | Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); |
michael@0 | 169 | |
michael@0 | 170 | // there should be no user-set pref for cookie behavior |
michael@0 | 171 | is(Services.prefs.getIntPref("network.cookie.cookieBehavior"), PERM_UNKNOWN, |
michael@0 | 172 | "network.cookie.cookieBehavior is expected default"); |
michael@0 | 173 | |
michael@0 | 174 | // the default behavior is to allow cookies |
michael@0 | 175 | let cookieMenulist = getPermissionMenulist("cookie"); |
michael@0 | 176 | is(cookieMenulist.value, PERM_ALLOW, |
michael@0 | 177 | "menulist correctly shows that cookies are allowed"); |
michael@0 | 178 | |
michael@0 | 179 | // set the pref to block cookies |
michael@0 | 180 | Services.prefs.setIntPref("network.cookie.cookieBehavior", PERM_DENY); |
michael@0 | 181 | // check to make sure this change is reflected in the UI |
michael@0 | 182 | is(cookieMenulist.value, PERM_DENY, "menulist correctly shows that cookies are blocked"); |
michael@0 | 183 | |
michael@0 | 184 | // clear the pref |
michael@0 | 185 | Services.prefs.clearUserPref("network.cookie.cookieBehavior"); |
michael@0 | 186 | |
michael@0 | 187 | runNextTest(); |
michael@0 | 188 | }, |
michael@0 | 189 | |
michael@0 | 190 | function test_manage_all_passwords() { |
michael@0 | 191 | // make sure "Manage All Passwords..." button opens the correct dialog |
michael@0 | 192 | addWindowListener("chrome://passwordmgr/content/passwordManager.xul", runNextTest); |
michael@0 | 193 | gBrowser.contentDocument.getElementById("passwords-manage-all-button").doCommand(); |
michael@0 | 194 | |
michael@0 | 195 | }, |
michael@0 | 196 | |
michael@0 | 197 | function test_manage_all_cookies() { |
michael@0 | 198 | // make sure "Manage All Cookies..." button opens the correct dialog |
michael@0 | 199 | addWindowListener("chrome://browser/content/preferences/cookies.xul", runNextTest); |
michael@0 | 200 | gBrowser.contentDocument.getElementById("cookies-manage-all-button").doCommand(); |
michael@0 | 201 | }, |
michael@0 | 202 | |
michael@0 | 203 | function test_select_site() { |
michael@0 | 204 | // select the site that has the permissions we set at the beginning of the test |
michael@0 | 205 | let testSiteItem = getSiteItem(TEST_URI_2.host); |
michael@0 | 206 | gSitesList.selectedItem = testSiteItem; |
michael@0 | 207 | |
michael@0 | 208 | let siteHeader = gBrowser.contentDocument.getElementById("site-header"); |
michael@0 | 209 | is(siteHeader, gHeaderDeck.selectedPanel, |
michael@0 | 210 | "correct header shown for a specific site"); |
michael@0 | 211 | is(gSiteLabel.value, TEST_URI_2.host, "header updated for selected site"); |
michael@0 | 212 | |
michael@0 | 213 | ok(!gBrowser.contentDocument.getElementById("passwords-count").hidden, |
michael@0 | 214 | "passwords count is not hidden"); |
michael@0 | 215 | ok(!gBrowser.contentDocument.getElementById("cookies-count").hidden, |
michael@0 | 216 | "cookies count is not hidden"); |
michael@0 | 217 | |
michael@0 | 218 | // Test to make sure "Allow" items are *not* hidden for certain permission types |
michael@0 | 219 | NO_GLOBAL_ALLOW.forEach(function(aType) { |
michael@0 | 220 | let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW); |
michael@0 | 221 | ok(!menuitem.hidden, aType + " allow menuitem not hidden for single site"); |
michael@0 | 222 | }); |
michael@0 | 223 | |
michael@0 | 224 | runNextTest(); |
michael@0 | 225 | }, |
michael@0 | 226 | |
michael@0 | 227 | function test_permissions() { |
michael@0 | 228 | let menulists = gBrowser.contentDocument.getElementsByClassName("pref-menulist"); |
michael@0 | 229 | is(menulists.length, TEST_PERMS_COUNT, "got expected number of managed permissions"); |
michael@0 | 230 | |
michael@0 | 231 | for (let i = 0; i < menulists.length; i++) { |
michael@0 | 232 | let permissionMenulist = menulists.item(i); |
michael@0 | 233 | let permissionType = permissionMenulist.getAttribute("type"); |
michael@0 | 234 | |
michael@0 | 235 | // permissions should reflect what we set at the beginning of the test |
michael@0 | 236 | is(permissionMenulist.value, TEST_PERMS[permissionType], |
michael@0 | 237 | "got expected value for " + permissionType + " permission"); |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | runNextTest(); |
michael@0 | 241 | }, |
michael@0 | 242 | |
michael@0 | 243 | function test_permission_change() { |
michael@0 | 244 | let geoMenulist = getPermissionMenulist("geo"); |
michael@0 | 245 | is(geoMenulist.value, PERM_UNKNOWN, "menulist correctly shows that geolocation permission is unspecified"); |
michael@0 | 246 | |
michael@0 | 247 | // change a permission programatically |
michael@0 | 248 | Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, "geo", PERM_DENY); |
michael@0 | 249 | // check to make sure this change is reflected in the UI |
michael@0 | 250 | is(geoMenulist.value, PERM_DENY, "menulist shows that geolocation is blocked"); |
michael@0 | 251 | |
michael@0 | 252 | // change a permisssion in the UI |
michael@0 | 253 | let geoAllowItem = gBrowser.contentDocument.getElementById("geo-" + PERM_ALLOW); |
michael@0 | 254 | geoMenulist.selectedItem = geoAllowItem; |
michael@0 | 255 | geoMenulist.doCommand(); |
michael@0 | 256 | // check to make sure this change is reflected in the permission manager |
michael@0 | 257 | is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "geo"), PERM_ALLOW, |
michael@0 | 258 | "permission manager shows that geolocation is allowed"); |
michael@0 | 259 | |
michael@0 | 260 | |
michael@0 | 261 | // change a site-specific cookie permission, just for fun |
michael@0 | 262 | let cookieMenuList = getPermissionMenulist("cookie"); |
michael@0 | 263 | let cookieItem = gBrowser.contentDocument.getElementById("cookie-" + PERM_FIRST_PARTY_ONLY); |
michael@0 | 264 | cookieMenuList.selectedItem = cookieItem; |
michael@0 | 265 | cookieMenuList.doCommand(); |
michael@0 | 266 | is(cookieMenuList.value, PERM_FIRST_PARTY_ONLY, "menulist correctly shows that " + |
michael@0 | 267 | "first party only cookies are allowed"); |
michael@0 | 268 | is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "cookie"), |
michael@0 | 269 | PERM_FIRST_PARTY_ONLY, "permission manager shows that first party cookies " + |
michael@0 | 270 | "are allowed"); |
michael@0 | 271 | |
michael@0 | 272 | runNextTest(); |
michael@0 | 273 | }, |
michael@0 | 274 | |
michael@0 | 275 | function test_forget_site() { |
michael@0 | 276 | // click "Forget About This Site" button |
michael@0 | 277 | gBrowser.contentDocument.getElementById("forget-site-button").doCommand(); |
michael@0 | 278 | waitForClearHistory(function() { |
michael@0 | 279 | is(gSiteLabel.value, "", "site label cleared"); |
michael@0 | 280 | |
michael@0 | 281 | let allSitesItem = gBrowser.contentDocument.getElementById("all-sites-item"); |
michael@0 | 282 | is(gSitesList.selectedItem, allSitesItem, |
michael@0 | 283 | "all sites item selected after forgetting selected site"); |
michael@0 | 284 | |
michael@0 | 285 | // check to make sure site is gone from sites list |
michael@0 | 286 | let testSiteItem = getSiteItem(TEST_URI_2.host); |
michael@0 | 287 | ok(!testSiteItem, "site removed from sites list"); |
michael@0 | 288 | |
michael@0 | 289 | // check to make sure we forgot all permissions corresponding to site |
michael@0 | 290 | for (let type in TEST_PERMS) { |
michael@0 | 291 | if (type == "password") { |
michael@0 | 292 | ok(Services.logins.getLoginSavingEnabled(TEST_URI_2.prePath), |
michael@0 | 293 | "password saving should be enabled by default"); |
michael@0 | 294 | } else { |
michael@0 | 295 | is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, type), PERM_UNKNOWN, |
michael@0 | 296 | type + " permission should not be set for test site 2"); |
michael@0 | 297 | } |
michael@0 | 298 | } |
michael@0 | 299 | |
michael@0 | 300 | runNextTest(); |
michael@0 | 301 | }); |
michael@0 | 302 | } |
michael@0 | 303 | ]; |
michael@0 | 304 | |
michael@0 | 305 | function getPermissionMenulist(aType) { |
michael@0 | 306 | return gBrowser.contentDocument.getElementById(aType + "-menulist"); |
michael@0 | 307 | } |
michael@0 | 308 | |
michael@0 | 309 | function getSiteItem(aHost) { |
michael@0 | 310 | return gBrowser.contentDocument. |
michael@0 | 311 | querySelector(".site[value='" + aHost + "']"); |
michael@0 | 312 | } |
michael@0 | 313 | |
michael@0 | 314 | function addWindowListener(aURL, aCallback) { |
michael@0 | 315 | Services.wm.addListener({ |
michael@0 | 316 | onOpenWindow: function(aXULWindow) { |
michael@0 | 317 | info("window opened, waiting for focus"); |
michael@0 | 318 | Services.wm.removeListener(this); |
michael@0 | 319 | |
michael@0 | 320 | var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 321 | .getInterface(Ci.nsIDOMWindow); |
michael@0 | 322 | waitForFocus(function() { |
michael@0 | 323 | is(domwindow.document.location.href, aURL, "should have seen the right window open"); |
michael@0 | 324 | domwindow.close(); |
michael@0 | 325 | aCallback(); |
michael@0 | 326 | }, domwindow); |
michael@0 | 327 | }, |
michael@0 | 328 | onCloseWindow: function(aXULWindow) { }, |
michael@0 | 329 | onWindowTitleChange: function(aXULWindow, aNewTitle) { } |
michael@0 | 330 | }); |
michael@0 | 331 | } |
michael@0 | 332 | |
michael@0 | 333 | // copied from toolkit/components/places/tests/head_common.js |
michael@0 | 334 | function waitForClearHistory(aCallback) { |
michael@0 | 335 | let observer = { |
michael@0 | 336 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 337 | Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED); |
michael@0 | 338 | aCallback(); |
michael@0 | 339 | } |
michael@0 | 340 | }; |
michael@0 | 341 | Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); |
michael@0 | 342 | PlacesUtils.bhistory.removeAllPages(); |
michael@0 | 343 | } |