browser/components/preferences/tests/browser_permissions.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial