michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: // Allow all cookies, then actually set up the test michael@0: SpecialPowers.pushPrefEnv({"set": [["network.cookie.cookieBehavior", 0]]}, initTest); michael@0: } michael@0: michael@0: function initTest() { michael@0: const searchTerm = "example"; michael@0: const dummyTerm = "elpmaxe"; michael@0: michael@0: var cm = Components.classes["@mozilla.org/cookiemanager;1"] michael@0: .getService(Components.interfaces.nsICookieManager); michael@0: michael@0: // delete all cookies (might be left over from other tests) michael@0: cm.removeAll(); michael@0: michael@0: // data for cookies michael@0: var vals = [[searchTerm+".com", dummyTerm, dummyTerm], // match michael@0: [searchTerm+".org", dummyTerm, dummyTerm], // match michael@0: [dummyTerm+".com", searchTerm, dummyTerm], // match michael@0: [dummyTerm+".edu", searchTerm+dummyTerm, dummyTerm],// match michael@0: [dummyTerm+".net", dummyTerm, searchTerm], // match michael@0: [dummyTerm+".org", dummyTerm, searchTerm+dummyTerm],// match michael@0: [dummyTerm+".int", dummyTerm, dummyTerm]]; // no match michael@0: michael@0: // matches must correspond to above data michael@0: const matches = 6; michael@0: michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var cookieSvc = Components.classes["@mozilla.org/cookieService;1"] michael@0: .getService(Components.interfaces.nsICookieService); michael@0: var v; michael@0: // inject cookies michael@0: for (v in vals) { michael@0: let [host, name, value] = vals[v]; michael@0: var cookieUri = ios.newURI("http://"+host, null, null); michael@0: cookieSvc.setCookieString(cookieUri, null, name+"="+value+";", null); michael@0: } michael@0: michael@0: // open cookie manager michael@0: var cmd = window.openDialog("chrome://browser/content/preferences/cookies.xul", michael@0: "Browser:Cookies", "", {}); michael@0: michael@0: // when it has loaded, run actual tests michael@0: cmd.addEventListener("load", function() {executeSoon(function() {runTest(cmd, searchTerm, vals.length, matches);});}, false); michael@0: } michael@0: michael@0: function isDisabled(win, expectation) { michael@0: var disabled = win.document.getElementById("removeAllCookies").disabled; michael@0: is(disabled, expectation, "Remove all cookies button has correct state: "+(expectation?"disabled":"enabled")); michael@0: } michael@0: michael@0: function runTest(win, searchTerm, cookies, matches) { michael@0: var cm = Components.classes["@mozilla.org/cookiemanager;1"] michael@0: .getService(Components.interfaces.nsICookieManager); michael@0: michael@0: michael@0: // number of cookies should match injected cookies michael@0: var cnt = 0, michael@0: enumerator = cm.enumerator; michael@0: while (enumerator.hasMoreElements()) { michael@0: cnt++; michael@0: enumerator.getNext(); michael@0: } michael@0: is(cnt, cookies, "Number of cookies match injected cookies"); michael@0: michael@0: // "delete all cookies" should be enabled michael@0: isDisabled(win, false); michael@0: michael@0: // filter cookies and count matches michael@0: win.gCookiesWindow.setFilter(searchTerm); michael@0: is(win.gCookiesWindow._view.rowCount, matches, "Correct number of cookies shown after filter is applied"); michael@0: michael@0: // "delete all cookies" should be enabled michael@0: isDisabled(win, false); michael@0: michael@0: michael@0: // select first cookie and delete michael@0: var tree = win.document.getElementById("cookiesList"); michael@0: var deleteButton = win.document.getElementById("removeCookie"); michael@0: var x = {}, y = {}, width = {}, height = {}; michael@0: tree.treeBoxObject.getCoordsForCellItem(0, tree.columns[0], "cell", x, y, width, height); michael@0: EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win); michael@0: EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win); michael@0: michael@0: // count cookies should be matches-1 michael@0: is(win.gCookiesWindow._view.rowCount, matches-1, "Deleted selected cookie"); michael@0: michael@0: // select two adjacent cells and delete michael@0: EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, {}, win); michael@0: deleteButton = win.document.getElementById("removeCookies"); michael@0: var eventObj = {}; michael@0: if (navigator.platform.indexOf("Mac") >= 0) michael@0: eventObj.metaKey = true; michael@0: else michael@0: eventObj.ctrlKey = true; michael@0: tree.treeBoxObject.getCoordsForCellItem(1, tree.columns[0], "cell", x, y, width, height); michael@0: EventUtils.synthesizeMouse(tree.body, x.value + width.value / 2, y.value + height.value / 2, eventObj, win); michael@0: EventUtils.synthesizeMouseAtCenter(deleteButton, {}, win); michael@0: michael@0: // count cookies should be matches-3 michael@0: is(win.gCookiesWindow._view.rowCount, matches-3, "Deleted selected two adjacent cookies"); michael@0: michael@0: // "delete all cookies" should be enabled michael@0: isDisabled(win, false); michael@0: michael@0: // delete all cookies and count michael@0: var deleteAllButton = win.document.getElementById("removeAllCookies"); michael@0: EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win); michael@0: is(win.gCookiesWindow._view.rowCount, 0, "Deleted all matching cookies"); michael@0: michael@0: // "delete all cookies" should be disabled michael@0: isDisabled(win, true); michael@0: michael@0: // clear filter and count should be cookies-matches michael@0: win.gCookiesWindow.setFilter(""); michael@0: is(win.gCookiesWindow._view.rowCount, cookies-matches, "Unmatched cookies remain"); michael@0: michael@0: // "delete all cookies" should be enabled michael@0: isDisabled(win, false); michael@0: michael@0: // delete all cookies and count should be 0 michael@0: EventUtils.synthesizeMouseAtCenter(deleteAllButton, {}, win); michael@0: is(win.gCookiesWindow._view.rowCount, 0, "Deleted all cookies"); michael@0: michael@0: // check that datastore is also at 0 michael@0: var cnt = 0, michael@0: enumerator = cm.enumerator; michael@0: while (enumerator.hasMoreElements()) { michael@0: cnt++; michael@0: enumerator.getNext(); michael@0: } michael@0: is(cnt, 0, "Zero cookies remain"); michael@0: michael@0: // "delete all cookies" should be disabled michael@0: isDisabled(win, true); michael@0: michael@0: // clean up michael@0: win.close(); michael@0: finish(); michael@0: }