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