browser/components/preferences/tests/browser_bug705422.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/preferences/tests/browser_bug705422.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +function test() {
     1.8 +  waitForExplicitFinish();
     1.9 +  // Allow all cookies, then actually set up the test
    1.10 +  SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, initTest);
    1.11 +}
    1.12 +
    1.13 +function initTest() {
    1.14 +    const searchTerm = "example";
    1.15 +    const dummyTerm = "elpmaxe";
    1.16 +
    1.17 +    var cm =  Components.classes["@mozilla.org/cookiemanager;1"]
    1.18 +                        .getService(Components.interfaces.nsICookieManager);
    1.19 +
    1.20 +    // delete all cookies (might be left over from other tests)
    1.21 +    cm.removeAll();
    1.22 +
    1.23 +    // data for cookies
    1.24 +    var vals = [[searchTerm+".com", dummyTerm, dummyTerm],          // match
    1.25 +                [searchTerm+".org", dummyTerm, dummyTerm],          // match
    1.26 +                [dummyTerm+".com", searchTerm, dummyTerm],          // match
    1.27 +                [dummyTerm+".edu", searchTerm+dummyTerm, dummyTerm],// match
    1.28 +                [dummyTerm+".net", dummyTerm, searchTerm],          // match
    1.29 +                [dummyTerm+".org", dummyTerm, searchTerm+dummyTerm],// match
    1.30 +                [dummyTerm+".int", dummyTerm, dummyTerm]];          // no match
    1.31 +
    1.32 +    // matches must correspond to above data
    1.33 +    const matches = 6;
    1.34 +
    1.35 +    var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.36 +                        .getService(Components.interfaces.nsIIOService);
    1.37 +    var cookieSvc = Components.classes["@mozilla.org/cookieService;1"]
    1.38 +                              .getService(Components.interfaces.nsICookieService);
    1.39 +    var v;
    1.40 +    // inject cookies
    1.41 +    for (v in vals) {
    1.42 +        let [host, name, value] = vals[v];    
    1.43 +        var cookieUri = ios.newURI("http://"+host, null, null);
    1.44 +        cookieSvc.setCookieString(cookieUri, null, name+"="+value+";", null);
    1.45 +    }
    1.46 +
    1.47 +    // open cookie manager
    1.48 +    var cmd = window.openDialog("chrome://browser/content/preferences/cookies.xul",
    1.49 +                                "Browser:Cookies", "", {});
    1.50 +    
    1.51 +    // when it has loaded, run actual tests
    1.52 +    cmd.addEventListener("load", function() {executeSoon(function() {runTest(cmd, searchTerm, vals.length, matches);});}, false);
    1.53 +}
    1.54 +
    1.55 +function isDisabled(win, expectation) {
    1.56 +    var disabled = win.document.getElementById("removeAllCookies").disabled;
    1.57 +    is(disabled, expectation, "Remove all cookies button has correct state: "+(expectation?"disabled":"enabled"));
    1.58 +}
    1.59 +
    1.60 +function runTest(win, searchTerm, cookies, matches) {
    1.61 +    var cm =  Components.classes["@mozilla.org/cookiemanager;1"]
    1.62 +                        .getService(Components.interfaces.nsICookieManager);
    1.63 +
    1.64 +
    1.65 +    // number of cookies should match injected cookies
    1.66 +    var cnt = 0,
    1.67 +        enumerator = cm.enumerator;
    1.68 +    while (enumerator.hasMoreElements()) {
    1.69 +        cnt++;
    1.70 +        enumerator.getNext();
    1.71 +    }
    1.72 +    is(cnt, cookies, "Number of cookies match injected cookies");
    1.73 +
    1.74 +    // "delete all cookies" should be enabled
    1.75 +    isDisabled(win, false);
    1.76 +
    1.77 +    // filter cookies and count matches
    1.78 +    win.gCookiesWindow.setFilter(searchTerm);
    1.79 +    is(win.gCookiesWindow._view.rowCount, matches, "Correct number of cookies shown after filter is applied");
    1.80 +
    1.81 +    // "delete all cookies" should be enabled
    1.82 +    isDisabled(win, false);
    1.83 +
    1.84 +
    1.85 +    // select first cookie and delete
    1.86 +    var tree = win.document.getElementById("cookiesList");
    1.87 +    var deleteButton = win.document.getElementById("removeCookie");
    1.88 +    var x = {}, y = {}, width = {}, height = {};
    1.89 +    tree.treeBoxObject.getCoordsForCellItem(0, tree.columns[0], "cell", x, y, width, height);
    1.90 +    EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win);
    1.91 +    EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win);
    1.92 +
    1.93 +    // count cookies should be matches-1
    1.94 +    is(win.gCookiesWindow._view.rowCount, matches-1, "Deleted selected cookie");
    1.95 +
    1.96 +    // select two adjacent cells and delete
    1.97 +    EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win);
    1.98 +    deleteButton = win.document.getElementById("removeCookies"); 
    1.99 +    var eventObj = {};
   1.100 +    if (navigator.platform.indexOf("Mac") >= 0)
   1.101 +        eventObj.metaKey = true;
   1.102 +    else
   1.103 +        eventObj.ctrlKey = true;
   1.104 +    tree.treeBoxObject.getCoordsForCellItem(1, tree.columns[0], "cell", x, y, width, height);
   1.105 +    EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, eventObj, win);
   1.106 +    EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win);
   1.107 +
   1.108 +    // count cookies should be matches-3
   1.109 +    is(win.gCookiesWindow._view.rowCount, matches-3, "Deleted selected two adjacent cookies");
   1.110 +
   1.111 +    // "delete all cookies" should be enabled
   1.112 +    isDisabled(win, false);
   1.113 +
   1.114 +    // delete all cookies and count
   1.115 +    var deleteAllButton = win.document.getElementById("removeAllCookies");
   1.116 +    EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win);
   1.117 +    is(win.gCookiesWindow._view.rowCount, 0, "Deleted all matching cookies");
   1.118 +
   1.119 +    // "delete all cookies" should be disabled
   1.120 +    isDisabled(win, true);
   1.121 +
   1.122 +    // clear filter and count should be cookies-matches
   1.123 +    win.gCookiesWindow.setFilter("");
   1.124 +    is(win.gCookiesWindow._view.rowCount, cookies-matches, "Unmatched cookies remain");
   1.125 +
   1.126 +    // "delete all cookies" should be enabled
   1.127 +    isDisabled(win, false);
   1.128 +
   1.129 +    // delete all cookies and count should be 0
   1.130 +    EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win);
   1.131 +    is(win.gCookiesWindow._view.rowCount, 0, "Deleted all cookies");
   1.132 +
   1.133 +    // check that datastore is also at 0
   1.134 +    var cnt = 0,
   1.135 +        enumerator = cm.enumerator;
   1.136 +    while (enumerator.hasMoreElements()) {
   1.137 +        cnt++;
   1.138 +        enumerator.getNext();
   1.139 +    }
   1.140 +    is(cnt, 0, "Zero cookies remain");
   1.141 +
   1.142 +    // "delete all cookies" should be disabled
   1.143 +    isDisabled(win, true);
   1.144 +
   1.145 +    // clean up
   1.146 +    win.close();
   1.147 +    finish();
   1.148 +}

mercurial