browser/components/preferences/tests/browser_permissions.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/preferences/tests/browser_permissions.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,343 @@
     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 +
    1.10 +const ABOUT_PERMISSIONS_SPEC = "about:permissions";
    1.11 +
    1.12 +const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/");
    1.13 +const TEST_URI_2 = NetUtil.newURI("http://mozilla.org/");
    1.14 +
    1.15 +const TEST_PRINCIPAL_1 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_1);
    1.16 +const TEST_PRINCIPAL_2 = Services.scriptSecurityManager.getNoAppCodebasePrincipal(TEST_URI_2);
    1.17 +
    1.18 +// values from DefaultPermissions object
    1.19 +const PERM_UNKNOWN = 0;
    1.20 +const PERM_ALLOW = 1;
    1.21 +const PERM_DENY = 2;
    1.22 +// cookie specific permissions
    1.23 +const PERM_FIRST_PARTY_ONLY = 9;
    1.24 +
    1.25 +// used to set permissions on test sites
    1.26 +const TEST_PERMS = {
    1.27 +  "password": PERM_ALLOW,
    1.28 +  "cookie": PERM_ALLOW,
    1.29 +  "geo": PERM_UNKNOWN,
    1.30 +  "indexedDB": PERM_UNKNOWN,
    1.31 +  "popup": PERM_DENY,
    1.32 +  "fullscreen" : PERM_UNKNOWN,
    1.33 +  "camera": PERM_UNKNOWN,
    1.34 +  "microphone": PERM_UNKNOWN
    1.35 +};
    1.36 +
    1.37 +const NO_GLOBAL_ALLOW = [
    1.38 +  "geo",
    1.39 +  "indexedDB",
    1.40 +  "fullscreen"
    1.41 +];
    1.42 +
    1.43 +// number of managed permissions in the interface
    1.44 +const TEST_PERMS_COUNT = 8;
    1.45 +
    1.46 +function test() {
    1.47 +  waitForExplicitFinish();
    1.48 +  registerCleanupFunction(cleanUp);
    1.49 +
    1.50 +  // add test history visit
    1.51 +  addVisits(TEST_URI_1, function() {
    1.52 +    // set permissions ourselves to avoid problems with different defaults
    1.53 +    // from test harness configuration
    1.54 +    for (let type in TEST_PERMS) {
    1.55 +      if (type == "password") {
    1.56 +        Services.logins.setLoginSavingEnabled(TEST_URI_2.prePath, true);
    1.57 +      } else {
    1.58 +        // set permissions on a site without history visits to test enumerateServices
    1.59 +        Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, type, TEST_PERMS[type]);
    1.60 +      }
    1.61 +    }
    1.62 +
    1.63 +    // open about:permissions
    1.64 +    gBrowser.selectedTab = gBrowser.addTab("about:permissions");
    1.65 +  });
    1.66 +
    1.67 +  function observer() {
    1.68 +    Services.obs.removeObserver(observer, "browser-permissions-initialized");
    1.69 +    runNextTest();
    1.70 +  }
    1.71 +  Services.obs.addObserver(observer, "browser-permissions-initialized", false);
    1.72 +}
    1.73 +
    1.74 +function cleanUp() {
    1.75 +  for (let type in TEST_PERMS) {
    1.76 +    if (type != "password") {
    1.77 +      Services.perms.removeFromPrincipal(TEST_PRINCIPAL_1, type);
    1.78 +      Services.perms.removeFromPrincipal(TEST_PRINCIPAL_2, type);
    1.79 +    }
    1.80 +  }
    1.81 +
    1.82 +  gBrowser.removeTab(gBrowser.selectedTab);
    1.83 +}
    1.84 +
    1.85 +function runNextTest() {
    1.86 +  if (gTestIndex == tests.length) {
    1.87 +    waitForClearHistory(finish);
    1.88 +    return;
    1.89 +  }
    1.90 +
    1.91 +  let nextTest = tests[gTestIndex++];
    1.92 +  info("[" + nextTest.name + "] running test");
    1.93 +  nextTest();
    1.94 +}
    1.95 +
    1.96 +var gSitesList;
    1.97 +var gHeaderDeck;
    1.98 +var gSiteLabel;
    1.99 +
   1.100 +var gTestIndex = 0;
   1.101 +var tests = [
   1.102 +  function test_page_load() {
   1.103 +    is(gBrowser.currentURI.spec, ABOUT_PERMISSIONS_SPEC, "about:permissions loaded");
   1.104 +
   1.105 +    gSitesList = gBrowser.contentDocument.getElementById("sites-list");
   1.106 +    ok(gSitesList, "got sites list");
   1.107 +
   1.108 +    gHeaderDeck = gBrowser.contentDocument.getElementById("header-deck");
   1.109 +    ok(gHeaderDeck, "got header deck");
   1.110 +
   1.111 +    gSiteLabel = gBrowser.contentDocument.getElementById("site-label");
   1.112 +    ok(gSiteLabel, "got site label");
   1.113 +
   1.114 +    runNextTest();
   1.115 +  },
   1.116 +
   1.117 +  function test_sites_list() {
   1.118 +    is(gSitesList.firstChild.id, "all-sites-item",
   1.119 +       "all sites is the first item in the sites list");
   1.120 +
   1.121 +    ok(getSiteItem(TEST_URI_1.host), "site item from places db exists");
   1.122 +    ok(getSiteItem(TEST_URI_2.host), "site item from enumerating services exists");
   1.123 +
   1.124 +    runNextTest();
   1.125 +  },
   1.126 +
   1.127 +  function test_filter_sites_list() {
   1.128 +    // set filter to test host
   1.129 +    let sitesFilter = gBrowser.contentDocument.getElementById("sites-filter");
   1.130 +    sitesFilter.value = TEST_URI_1.host;
   1.131 +    sitesFilter.doCommand();
   1.132 +
   1.133 +    // make sure correct sites are collapsed/showing
   1.134 +    let testSite1 = getSiteItem(TEST_URI_1.host);
   1.135 +    ok(!testSite1.collapsed, "test site 1 is not collapsed");
   1.136 +    let testSite2 = getSiteItem(TEST_URI_2.host);
   1.137 +    ok(testSite2.collapsed, "test site 2 is collapsed");
   1.138 +
   1.139 +    // clear filter
   1.140 +    sitesFilter.value = "";
   1.141 +    sitesFilter.doCommand();
   1.142 +
   1.143 +    runNextTest();
   1.144 +  },
   1.145 +
   1.146 +  function test_all_sites() {
   1.147 +    // "All Sites" item should be selected when the page is first loaded
   1.148 +    is(gSitesList.selectedItem, gBrowser.contentDocument.getElementById("all-sites-item"),
   1.149 +       "all sites item is selected");
   1.150 +
   1.151 +    let defaultsHeader = gBrowser.contentDocument.getElementById("defaults-header");
   1.152 +    is(defaultsHeader, gHeaderDeck.selectedPanel,
   1.153 +       "correct header shown for all sites");
   1.154 +
   1.155 +    ok(gBrowser.contentDocument.getElementById("passwords-count").hidden,
   1.156 +       "passwords count is hidden");
   1.157 +    ok(gBrowser.contentDocument.getElementById("cookies-count").hidden,
   1.158 +       "cookies count is hidden");
   1.159 +
   1.160 +    // Test to make sure "Allow" items hidden for certain permission types
   1.161 +    NO_GLOBAL_ALLOW.forEach(function(aType) {
   1.162 +      let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW);
   1.163 +      ok(menuitem.hidden, aType + " allow menuitem hidden for all sites");
   1.164 +    });
   1.165 +
   1.166 +    runNextTest();
   1.167 +  },
   1.168 +
   1.169 +  function test_all_sites_permission() {
   1.170 +    // apply the old default of allowing all cookies
   1.171 +    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
   1.172 +
   1.173 +    // there should be no user-set pref for cookie behavior
   1.174 +    is(Services.prefs.getIntPref("network.cookie.cookieBehavior"), PERM_UNKNOWN,
   1.175 +       "network.cookie.cookieBehavior is expected default");
   1.176 +
   1.177 +    // the default behavior is to allow cookies
   1.178 +    let cookieMenulist = getPermissionMenulist("cookie");
   1.179 +    is(cookieMenulist.value, PERM_ALLOW,
   1.180 +       "menulist correctly shows that cookies are allowed");
   1.181 +
   1.182 +    // set the pref to block cookies
   1.183 +    Services.prefs.setIntPref("network.cookie.cookieBehavior", PERM_DENY);
   1.184 +    // check to make sure this change is reflected in the UI
   1.185 +    is(cookieMenulist.value, PERM_DENY, "menulist correctly shows that cookies are blocked");
   1.186 +
   1.187 +    // clear the pref
   1.188 +    Services.prefs.clearUserPref("network.cookie.cookieBehavior");
   1.189 +
   1.190 +    runNextTest();
   1.191 +  },
   1.192 +
   1.193 +  function test_manage_all_passwords() {
   1.194 +    // make sure "Manage All Passwords..." button opens the correct dialog
   1.195 +    addWindowListener("chrome://passwordmgr/content/passwordManager.xul", runNextTest);
   1.196 +    gBrowser.contentDocument.getElementById("passwords-manage-all-button").doCommand();
   1.197 +
   1.198 +  },
   1.199 +
   1.200 +  function test_manage_all_cookies() {
   1.201 +    // make sure "Manage All Cookies..." button opens the correct dialog
   1.202 +    addWindowListener("chrome://browser/content/preferences/cookies.xul", runNextTest);
   1.203 +    gBrowser.contentDocument.getElementById("cookies-manage-all-button").doCommand();
   1.204 +  },
   1.205 +
   1.206 +  function test_select_site() {
   1.207 +    // select the site that has the permissions we set at the beginning of the test
   1.208 +    let testSiteItem = getSiteItem(TEST_URI_2.host);
   1.209 +    gSitesList.selectedItem = testSiteItem;
   1.210 +
   1.211 +    let siteHeader = gBrowser.contentDocument.getElementById("site-header");
   1.212 +    is(siteHeader, gHeaderDeck.selectedPanel,
   1.213 +       "correct header shown for a specific site");
   1.214 +    is(gSiteLabel.value, TEST_URI_2.host, "header updated for selected site");
   1.215 +
   1.216 +    ok(!gBrowser.contentDocument.getElementById("passwords-count").hidden,
   1.217 +       "passwords count is not hidden");
   1.218 +    ok(!gBrowser.contentDocument.getElementById("cookies-count").hidden,
   1.219 +       "cookies count is not hidden");
   1.220 +
   1.221 +    // Test to make sure "Allow" items are *not* hidden for certain permission types
   1.222 +    NO_GLOBAL_ALLOW.forEach(function(aType) {
   1.223 +      let menuitem = gBrowser.contentDocument.getElementById(aType + "-" + PERM_ALLOW);
   1.224 +      ok(!menuitem.hidden, aType  + " allow menuitem not hidden for single site");
   1.225 +    });
   1.226 +
   1.227 +    runNextTest();
   1.228 +  },
   1.229 +
   1.230 +  function test_permissions() {
   1.231 +    let menulists = gBrowser.contentDocument.getElementsByClassName("pref-menulist");
   1.232 +    is(menulists.length, TEST_PERMS_COUNT, "got expected number of managed permissions");
   1.233 +
   1.234 +    for (let i = 0; i < menulists.length; i++) {
   1.235 +      let permissionMenulist = menulists.item(i);
   1.236 +      let permissionType = permissionMenulist.getAttribute("type");
   1.237 +
   1.238 +      // permissions should reflect what we set at the beginning of the test
   1.239 +      is(permissionMenulist.value, TEST_PERMS[permissionType],
   1.240 +        "got expected value for " + permissionType + " permission");
   1.241 +    }
   1.242 +
   1.243 +    runNextTest();
   1.244 +  },
   1.245 +
   1.246 +  function test_permission_change() {
   1.247 +    let geoMenulist = getPermissionMenulist("geo");
   1.248 +    is(geoMenulist.value, PERM_UNKNOWN, "menulist correctly shows that geolocation permission is unspecified");
   1.249 +
   1.250 +    // change a permission programatically
   1.251 +    Services.perms.addFromPrincipal(TEST_PRINCIPAL_2, "geo", PERM_DENY);
   1.252 +    // check to make sure this change is reflected in the UI
   1.253 +    is(geoMenulist.value, PERM_DENY, "menulist shows that geolocation is blocked");
   1.254 +
   1.255 +    // change a permisssion in the UI
   1.256 +    let geoAllowItem = gBrowser.contentDocument.getElementById("geo-" + PERM_ALLOW);
   1.257 +    geoMenulist.selectedItem = geoAllowItem;
   1.258 +    geoMenulist.doCommand();
   1.259 +    // check to make sure this change is reflected in the permission manager
   1.260 +    is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "geo"), PERM_ALLOW,
   1.261 +       "permission manager shows that geolocation is allowed");
   1.262 +
   1.263 +
   1.264 +    // change a site-specific cookie permission, just for fun
   1.265 +    let cookieMenuList = getPermissionMenulist("cookie");
   1.266 +    let cookieItem = gBrowser.contentDocument.getElementById("cookie-" + PERM_FIRST_PARTY_ONLY);
   1.267 +    cookieMenuList.selectedItem = cookieItem;
   1.268 +    cookieMenuList.doCommand();
   1.269 +    is(cookieMenuList.value, PERM_FIRST_PARTY_ONLY, "menulist correctly shows that " +
   1.270 +       "first party only cookies are allowed");
   1.271 +    is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, "cookie"),
   1.272 +       PERM_FIRST_PARTY_ONLY, "permission manager shows that first party cookies " +
   1.273 +       "are allowed");
   1.274 +
   1.275 +    runNextTest();
   1.276 +  },
   1.277 +
   1.278 +  function test_forget_site() {
   1.279 +    // click "Forget About This Site" button
   1.280 +    gBrowser.contentDocument.getElementById("forget-site-button").doCommand();
   1.281 +    waitForClearHistory(function() {
   1.282 +      is(gSiteLabel.value, "", "site label cleared");
   1.283 +
   1.284 +      let allSitesItem = gBrowser.contentDocument.getElementById("all-sites-item");
   1.285 +      is(gSitesList.selectedItem, allSitesItem,
   1.286 +         "all sites item selected after forgetting selected site");
   1.287 +
   1.288 +      // check to make sure site is gone from sites list
   1.289 +      let testSiteItem = getSiteItem(TEST_URI_2.host);
   1.290 +      ok(!testSiteItem, "site removed from sites list");
   1.291 +
   1.292 +      // check to make sure we forgot all permissions corresponding to site
   1.293 +      for (let type in TEST_PERMS) {
   1.294 +        if (type == "password") {
   1.295 +          ok(Services.logins.getLoginSavingEnabled(TEST_URI_2.prePath),
   1.296 +             "password saving should be enabled by default");
   1.297 +        } else {
   1.298 +          is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, type), PERM_UNKNOWN,
   1.299 +             type + " permission should not be set for test site 2");
   1.300 +        }
   1.301 +      }
   1.302 +
   1.303 +      runNextTest();
   1.304 +    });
   1.305 +  }
   1.306 +];
   1.307 +
   1.308 +function getPermissionMenulist(aType) {
   1.309 +  return gBrowser.contentDocument.getElementById(aType + "-menulist");
   1.310 +}
   1.311 +
   1.312 +function getSiteItem(aHost) {
   1.313 +  return gBrowser.contentDocument.
   1.314 +                  querySelector(".site[value='" + aHost + "']");
   1.315 +}
   1.316 +
   1.317 +function addWindowListener(aURL, aCallback) {
   1.318 +  Services.wm.addListener({
   1.319 +    onOpenWindow: function(aXULWindow) {
   1.320 +      info("window opened, waiting for focus");
   1.321 +      Services.wm.removeListener(this);
   1.322 +
   1.323 +      var domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
   1.324 +                                .getInterface(Ci.nsIDOMWindow);
   1.325 +      waitForFocus(function() {
   1.326 +        is(domwindow.document.location.href, aURL, "should have seen the right window open");
   1.327 +        domwindow.close();
   1.328 +        aCallback();
   1.329 +      }, domwindow);
   1.330 +    },
   1.331 +    onCloseWindow: function(aXULWindow) { },
   1.332 +    onWindowTitleChange: function(aXULWindow, aNewTitle) { }
   1.333 +  });
   1.334 +}
   1.335 +
   1.336 +// copied from toolkit/components/places/tests/head_common.js
   1.337 +function waitForClearHistory(aCallback) {
   1.338 +  let observer = {
   1.339 +    observe: function(aSubject, aTopic, aData) {
   1.340 +      Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
   1.341 +      aCallback();
   1.342 +    }
   1.343 +  };
   1.344 +  Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
   1.345 +  PlacesUtils.bhistory.removeAllPages();
   1.346 +}

mercurial