1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/preferences/tests/browser_chunk_permissions.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,150 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Components.utils.import("resource://gre/modules/PlacesUtils.jsm"); 1.8 +Components.utils.import("resource://gre/modules/NetUtil.jsm"); 1.9 +Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm"); 1.10 + 1.11 +const ABOUT_PERMISSIONS_SPEC = "about:permissions"; 1.12 + 1.13 +const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); 1.14 +const TEST_URI_2 = NetUtil.newURI("http://mozilla.org/"); 1.15 +const TEST_URI_3 = NetUtil.newURI("http://wikipedia.org/"); 1.16 + 1.17 +// values from DefaultPermissions object 1.18 +const PERM_UNKNOWN = 0; 1.19 +const PERM_ALLOW = 1; 1.20 +const PERM_DENY = 2; 1.21 + 1.22 +// used to set permissions on test sites 1.23 +const TEST_PERMS = { 1.24 + "password": PERM_ALLOW, 1.25 + "cookie": PERM_ALLOW, 1.26 + "geo": PERM_UNKNOWN, 1.27 + "indexedDB": PERM_UNKNOWN, 1.28 + "popup": PERM_DENY 1.29 +}; 1.30 + 1.31 +function test() { 1.32 + waitForExplicitFinish(); 1.33 + registerCleanupFunction(cleanUp); 1.34 + setup(function() { 1.35 + runNextTest(); 1.36 + }); 1.37 +} 1.38 + 1.39 +function setup(aCallback) { 1.40 + // add test history visit 1.41 + addVisits(TEST_URI_1, function() { 1.42 + // set permissions ourselves to avoid problems with different defaults 1.43 + // from test harness configuration 1.44 + for (let type in TEST_PERMS) { 1.45 + if (type == "password") { 1.46 + Services.logins.setLoginSavingEnabled(TEST_URI_2.prePath, true); 1.47 + } else { 1.48 + // set permissions on a site without history visits to test enumerateServices 1.49 + Services.perms.add(TEST_URI_2, type, TEST_PERMS[type]); 1.50 + } 1.51 + } 1.52 + 1.53 + Services.perms.add(TEST_URI_3, "popup", TEST_PERMS["popup"]); 1.54 + aCallback(); 1.55 + }); 1.56 +} 1.57 + 1.58 +function cleanUp() { 1.59 + for (let type in TEST_PERMS) { 1.60 + if (type != "password") { 1.61 + Services.perms.remove(TEST_URI_1.host, type); 1.62 + Services.perms.remove(TEST_URI_2.host, type); 1.63 + Services.perms.remove(TEST_URI_3.host, type); 1.64 + } 1.65 + } 1.66 +} 1.67 + 1.68 +function runNextTest() { 1.69 + if (gTestIndex == tests.length) { 1.70 + waitForClearHistory(finish); 1.71 + return; 1.72 + } 1.73 + 1.74 + let nextTest = tests[gTestIndex++]; 1.75 + info(nextTest.desc); 1.76 + 1.77 + function preinit_observer() { 1.78 + Services.obs.removeObserver(preinit_observer, "browser-permissions-preinit"); 1.79 + nextTest.preInit(); 1.80 + } 1.81 + Services.obs.addObserver(preinit_observer, "browser-permissions-preinit", false); 1.82 + 1.83 + function init_observer() { 1.84 + Services.obs.removeObserver(init_observer, "browser-permissions-initialized"); 1.85 + nextTest.run(); 1.86 + } 1.87 + Services.obs.addObserver(init_observer, "browser-permissions-initialized", false); 1.88 + 1.89 + // open about:permissions 1.90 + let tab = gBrowser.selectedTab = gBrowser.addTab("about:permissions"); 1.91 + registerCleanupFunction(function() { 1.92 + gBrowser.removeTab(tab); 1.93 + }); 1.94 +} 1.95 + 1.96 +var gSitesList; 1.97 + 1.98 +var gTestIndex = 0; 1.99 +var tests = [ 1.100 + // 'preInit' occurs after opening about:permissions, before sites-list is populated 1.101 + // 'run' occurs after sites-list is populated 1.102 + { 1.103 + desc: "test filtering before sites-list is fully constructed.", 1.104 + preInit: function() { 1.105 + let sitesFilter = gBrowser.contentDocument.getElementById("sites-filter"); 1.106 + sitesFilter.value = TEST_URI_2.host; 1.107 + sitesFilter.doCommand(); 1.108 + }, 1.109 + run: function() { 1.110 + let testSite1 = getSiteItem(TEST_URI_1.host); 1.111 + ok(testSite1.collapsed, "test site 1 is collapsed after early filtering"); 1.112 + let testSite2 = getSiteItem(TEST_URI_2.host); 1.113 + ok(!testSite2.collapsed, "test site 2 is not collapsed after early filtering"); 1.114 + let testSite3 = getSiteItem(TEST_URI_3.host); 1.115 + ok(testSite3.collapsed, "test site 3 is collapsed after early filtering"); 1.116 + 1.117 + runNextTest(); 1.118 + } 1.119 + }, 1.120 + { 1.121 + desc: "test removing from sites-list before it is fully constructed.", 1.122 + preInit: function() { 1.123 + ForgetAboutSite.removeDataFromDomain(TEST_URI_2.host); 1.124 + }, 1.125 + run: function() { 1.126 + let testSite1 = getSiteItem(TEST_URI_1.host); 1.127 + ok(!testSite2, "test site 1 was not removed from sites list"); 1.128 + let testSite2 = getSiteItem(TEST_URI_2.host); 1.129 + ok(!testSite2, "test site 2 was pre-removed from sites list"); 1.130 + let testSite3 = getSiteItem(TEST_URI_3.host); 1.131 + ok(!testSite2, "test site 3 was not removed from sites list"); 1.132 + 1.133 + runNextTest(); 1.134 + } 1.135 + } 1.136 +]; 1.137 + 1.138 +function getSiteItem(aHost) { 1.139 + return gBrowser.contentDocument. 1.140 + querySelector(".site[value='" + aHost + "']"); 1.141 +} 1.142 + 1.143 +// copied from toolkit/components/places/tests/head_common.js 1.144 +function waitForClearHistory(aCallback) { 1.145 + let observer = { 1.146 + observe: function(aSubject, aTopic, aData) { 1.147 + Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED); 1.148 + aCallback(); 1.149 + } 1.150 + }; 1.151 + Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); 1.152 + PlacesUtils.bhistory.removeAllPages(); 1.153 +}