Fri, 16 Jan 2015 18:13:44 +0100
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 function test() {
5 waitForExplicitFinish();
6 // Allow all cookies, then actually set up the test
7 SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, initTest);
8 }
10 function initTest() {
11 const searchTerm = "example";
12 const dummyTerm = "elpmaxe";
14 var cm = Components.classes["@mozilla.org/cookiemanager;1"]
15 .getService(Components.interfaces.nsICookieManager);
17 // delete all cookies (might be left over from other tests)
18 cm.removeAll();
20 // data for cookies
21 var vals = [[searchTerm+".com", dummyTerm, dummyTerm], // match
22 [searchTerm+".org", dummyTerm, dummyTerm], // match
23 [dummyTerm+".com", searchTerm, dummyTerm], // match
24 [dummyTerm+".edu", searchTerm+dummyTerm, dummyTerm],// match
25 [dummyTerm+".net", dummyTerm, searchTerm], // match
26 [dummyTerm+".org", dummyTerm, searchTerm+dummyTerm],// match
27 [dummyTerm+".int", dummyTerm, dummyTerm]]; // no match
29 // matches must correspond to above data
30 const matches = 6;
32 var ios = Components.classes["@mozilla.org/network/io-service;1"]
33 .getService(Components.interfaces.nsIIOService);
34 var cookieSvc = Components.classes["@mozilla.org/cookieService;1"]
35 .getService(Components.interfaces.nsICookieService);
36 var v;
37 // inject cookies
38 for (v in vals) {
39 let [host, name, value] = vals[v];
40 var cookieUri = ios.newURI("http://"+host, null, null);
41 cookieSvc.setCookieString(cookieUri, null, name+"="+value+";", null);
42 }
44 // open cookie manager
45 var cmd = window.openDialog("chrome://browser/content/preferences/cookies.xul",
46 "Browser:Cookies", "", {});
48 // when it has loaded, run actual tests
49 cmd.addEventListener("load", function() {executeSoon(function() {runTest(cmd, searchTerm, vals.length, matches);});}, false);
50 }
52 function isDisabled(win, expectation) {
53 var disabled = win.document.getElementById("removeAllCookies").disabled;
54 is(disabled, expectation, "Remove all cookies button has correct state: "+(expectation?"disabled":"enabled"));
55 }
57 function runTest(win, searchTerm, cookies, matches) {
58 var cm = Components.classes["@mozilla.org/cookiemanager;1"]
59 .getService(Components.interfaces.nsICookieManager);
62 // number of cookies should match injected cookies
63 var cnt = 0,
64 enumerator = cm.enumerator;
65 while (enumerator.hasMoreElements()) {
66 cnt++;
67 enumerator.getNext();
68 }
69 is(cnt, cookies, "Number of cookies match injected cookies");
71 // "delete all cookies" should be enabled
72 isDisabled(win, false);
74 // filter cookies and count matches
75 win.gCookiesWindow.setFilter(searchTerm);
76 is(win.gCookiesWindow._view.rowCount, matches, "Correct number of cookies shown after filter is applied");
78 // "delete all cookies" should be enabled
79 isDisabled(win, false);
82 // select first cookie and delete
83 var tree = win.document.getElementById("cookiesList");
84 var deleteButton = win.document.getElementById("removeCookie");
85 var x = {}, y = {}, width = {}, height = {};
86 tree.treeBoxObject.getCoordsForCellItem(0, tree.columns[0], "cell", x, y, width, height);
87 EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win);
88 EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win);
90 // count cookies should be matches-1
91 is(win.gCookiesWindow._view.rowCount, matches-1, "Deleted selected cookie");
93 // select two adjacent cells and delete
94 EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win);
95 deleteButton = win.document.getElementById("removeCookies");
96 var eventObj = {};
97 if (navigator.platform.indexOf("Mac") >= 0)
98 eventObj.metaKey = true;
99 else
100 eventObj.ctrlKey = true;
101 tree.treeBoxObject.getCoordsForCellItem(1, tree.columns[0], "cell", x, y, width, height);
102 EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, eventObj, win);
103 EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win);
105 // count cookies should be matches-3
106 is(win.gCookiesWindow._view.rowCount, matches-3, "Deleted selected two adjacent cookies");
108 // "delete all cookies" should be enabled
109 isDisabled(win, false);
111 // delete all cookies and count
112 var deleteAllButton = win.document.getElementById("removeAllCookies");
113 EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win);
114 is(win.gCookiesWindow._view.rowCount, 0, "Deleted all matching cookies");
116 // "delete all cookies" should be disabled
117 isDisabled(win, true);
119 // clear filter and count should be cookies-matches
120 win.gCookiesWindow.setFilter("");
121 is(win.gCookiesWindow._view.rowCount, cookies-matches, "Unmatched cookies remain");
123 // "delete all cookies" should be enabled
124 isDisabled(win, false);
126 // delete all cookies and count should be 0
127 EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win);
128 is(win.gCookiesWindow._view.rowCount, 0, "Deleted all cookies");
130 // check that datastore is also at 0
131 var cnt = 0,
132 enumerator = cm.enumerator;
133 while (enumerator.hasMoreElements()) {
134 cnt++;
135 enumerator.getNext();
136 }
137 is(cnt, 0, "Zero cookies remain");
139 // "delete all cookies" should be disabled
140 isDisabled(win, true);
142 // clean up
143 win.close();
144 finish();
145 }