OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 "use strict"; |
| 19 |
| 20 const {createSandbox} = require("./_common"); |
| 21 |
| 22 let ElemHide = null; |
| 23 let Filter = null; |
| 24 |
| 25 exports.setUp = function(callback) |
| 26 { |
| 27 let sandboxedRequire = createSandbox(); |
| 28 ( |
| 29 {ElemHide} = sandboxedRequire("../lib/elemHide"), |
| 30 {Filter} = sandboxedRequire("../lib/filterClasses") |
| 31 ); |
| 32 |
| 33 callback(); |
| 34 }; |
| 35 |
| 36 function normalizeSelectors(selectorList) |
| 37 { |
| 38 let selectors; |
| 39 |
| 40 if (Array.isArray(selectorList)) |
| 41 selectors = selectorList; |
| 42 else |
| 43 { |
| 44 selectors = []; |
| 45 for (let i = 0; i < selectorList.selectorCount; i++) |
| 46 selectors.push(selectorList.selectorAt(i)); |
| 47 } |
| 48 |
| 49 // getSelectorsForDomain is currently allowed to return duplicate selectors |
| 50 // for performance reasons, so we need to remove duplicates here. |
| 51 return selectors.sort().filter((selector, index, sortedSelectors) => |
| 52 { |
| 53 return index == 0 || selector != sortedSelectors[index - 1]; |
| 54 }); |
| 55 } |
| 56 |
| 57 function testResult(test, elemHide, domain, expectedSelectors, criteria) |
| 58 { |
| 59 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); |
| 60 |
| 61 test.deepEqual( |
| 62 normalizeSelectors(elemHide.getSelectorsForDomain(domain, criteria)), |
| 63 normalizedExpectedSelectors |
| 64 ); |
| 65 } |
| 66 |
| 67 exports.testGetSelectorsForDomain = function(test) |
| 68 { |
| 69 let elemHide = ElemHide.create(); |
| 70 |
| 71 let addFilter = filterText => |
| 72 { |
| 73 let filter = Filter.fromText(filterText); |
| 74 elemHide.add(filter); |
| 75 filter.delete(); |
| 76 }; |
| 77 let removeFilter = filterText => |
| 78 { |
| 79 let filter = Filter.fromText(filterText); |
| 80 elemHide.remove(filter); |
| 81 filter.delete(); |
| 82 }; |
| 83 |
| 84 testResult(test, elemHide, "", []); |
| 85 |
| 86 addFilter("~foo.example.com,example.com##foo"); |
| 87 testResult(test, elemHide, "barfoo.example.com", ["foo"]); |
| 88 testResult(test, elemHide, "bar.foo.example.com", []); |
| 89 testResult(test, elemHide, "foo.example.com", []); |
| 90 testResult(test, elemHide, "example.com", ["foo"]); |
| 91 testResult(test, elemHide, "com", []); |
| 92 testResult(test, elemHide, "", []); |
| 93 |
| 94 addFilter("foo.example.com##turnip"); |
| 95 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 96 testResult(test, elemHide, "example.com", ["foo"]); |
| 97 testResult(test, elemHide, "com", []); |
| 98 testResult(test, elemHide, "", []); |
| 99 |
| 100 addFilter("example.com#@#foo"); |
| 101 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 102 testResult(test, elemHide, "example.com", []); |
| 103 testResult(test, elemHide, "com", []); |
| 104 testResult(test, elemHide, "", []); |
| 105 |
| 106 addFilter("com##bar"); |
| 107 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 108 testResult(test, elemHide, "example.com", ["bar"]); |
| 109 testResult(test, elemHide, "com", ["bar"]); |
| 110 testResult(test, elemHide, "", []); |
| 111 |
| 112 addFilter("example.com#@#bar"); |
| 113 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 114 testResult(test, elemHide, "example.com", []); |
| 115 testResult(test, elemHide, "com", ["bar"]); |
| 116 testResult(test, elemHide, "", []); |
| 117 |
| 118 removeFilter("example.com#@#foo"); |
| 119 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 120 testResult(test, elemHide, "example.com", ["foo"]); |
| 121 testResult(test, elemHide, "com", ["bar"]); |
| 122 testResult(test, elemHide, "", []); |
| 123 |
| 124 removeFilter("example.com#@#bar"); |
| 125 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 126 testResult(test, elemHide, "example.com", ["foo", "bar"]); |
| 127 testResult(test, elemHide, "com", ["bar"]); |
| 128 testResult(test, elemHide, "", []); |
| 129 |
| 130 addFilter("##generic"); |
| 131 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "generic"]); |
| 132 testResult(test, elemHide, "example.com", ["foo", "bar", "generic"]); |
| 133 testResult(test, elemHide, "com", ["bar", "generic"]); |
| 134 testResult(test, elemHide, "", ["generic"]); |
| 135 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SPEC
IFIC_ONLY); |
| 136 testResult(test, elemHide, "example.com", ["foo", "bar"], ElemHide.SPECIFIC_ON
LY); |
| 137 testResult(test, elemHide, "com", ["bar"], ElemHide.SPECIFIC_ONLY); |
| 138 testResult(test, elemHide, "", [], ElemHide.SPECIFIC_ONLY); |
| 139 removeFilter("##generic"); |
| 140 |
| 141 addFilter("~adblockplus.org##example"); |
| 142 testResult(test, elemHide, "adblockplus.org", []); |
| 143 testResult(test, elemHide, "", ["example"]); |
| 144 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "example"]); |
| 145 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SPEC
IFIC_ONLY); |
| 146 removeFilter("~adblockplus.org##example"); |
| 147 |
| 148 removeFilter("~foo.example.com,example.com##foo"); |
| 149 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 150 testResult(test, elemHide, "example.com", ["bar"]); |
| 151 testResult(test, elemHide, "com", ["bar"]); |
| 152 testResult(test, elemHide, "", []); |
| 153 |
| 154 removeFilter("com##bar"); |
| 155 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 156 testResult(test, elemHide, "example.com", []); |
| 157 testResult(test, elemHide, "com", []); |
| 158 testResult(test, elemHide, "", []); |
| 159 |
| 160 removeFilter("foo.example.com##turnip"); |
| 161 testResult(test, elemHide, "foo.example.com", []); |
| 162 testResult(test, elemHide, "example.com", []); |
| 163 testResult(test, elemHide, "com", []); |
| 164 testResult(test, elemHide, "", []); |
| 165 |
| 166 addFilter("example.com##dupe"); |
| 167 addFilter("example.com##dupe"); |
| 168 testResult(test, elemHide, "example.com", ["dupe"]); |
| 169 removeFilter("example.com##dupe"); |
| 170 testResult(test, elemHide, "example.com", []); |
| 171 removeFilter("example.com##dupe"); |
| 172 |
| 173 addFilter("~foo.example.com,example.com##foo"); |
| 174 |
| 175 addFilter("##foo"); |
| 176 testResult(test, elemHide, "foo.example.com", ["foo"]); |
| 177 testResult(test, elemHide, "example.com", ["foo"]); |
| 178 testResult(test, elemHide, "com", ["foo"]); |
| 179 testResult(test, elemHide, "", ["foo"]); |
| 180 removeFilter("##foo"); |
| 181 |
| 182 addFilter("example.org##foo"); |
| 183 testResult(test, elemHide, "foo.example.com", []); |
| 184 testResult(test, elemHide, "example.com", ["foo"]); |
| 185 testResult(test, elemHide, "com", []); |
| 186 testResult(test, elemHide, "", []); |
| 187 removeFilter("example.org##foo"); |
| 188 |
| 189 addFilter("~example.com##foo"); |
| 190 testResult(test, elemHide, "foo.example.com", []); |
| 191 testResult(test, elemHide, "example.com", ["foo"]); |
| 192 testResult(test, elemHide, "com", ["foo"]); |
| 193 testResult(test, elemHide, "", ["foo"]); |
| 194 removeFilter("~example.com##foo"); |
| 195 |
| 196 removeFilter("~foo.example.com,example.com##foo"); |
| 197 |
| 198 // Test criteria |
| 199 addFilter("##hello"); |
| 200 addFilter("~example.com##world"); |
| 201 addFilter("foo.com##specific"); |
| 202 testResult(test, elemHide, "foo.com", ["specific"], ElemHide.SPECIFIC_ONLY); |
| 203 testResult(test, elemHide, "foo.com", ["specific", "world"], ElemHide.NO_UNCON
DITIONAL); |
| 204 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"], ElemHide
.ALL_MATCHING); |
| 205 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"]); |
| 206 removeFilter("foo.com##specific"); |
| 207 removeFilter("~example.com##world"); |
| 208 removeFilter("##hello"); |
| 209 testResult(test, elemHide, "foo.com", []); |
| 210 |
| 211 addFilter("##hello"); |
| 212 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 213 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 214 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 215 testResult(test, elemHide, "foo.com", ["hello"]); |
| 216 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 217 testResult(test, elemHide, "bar.com", [], ElemHide.NO_UNCONDITIONAL); |
| 218 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 219 testResult(test, elemHide, "bar.com", ["hello"]); |
| 220 addFilter("foo.com#@#hello"); |
| 221 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 222 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 223 testResult(test, elemHide, "foo.com", [], ElemHide.ALL_MATCHING); |
| 224 testResult(test, elemHide, "foo.com", []); |
| 225 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 226 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 227 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 228 testResult(test, elemHide, "bar.com", ["hello"]); |
| 229 removeFilter("foo.com#@#hello"); |
| 230 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 231 // Note: We don't take care to track conditional selectors which became |
| 232 // unconditional when a filter was removed. This was too expensive. |
| 233 // testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 234 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 235 testResult(test, elemHide, "foo.com", ["hello"]); |
| 236 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 237 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 238 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 239 testResult(test, elemHide, "bar.com", ["hello"]); |
| 240 removeFilter("##hello"); |
| 241 testResult(test, elemHide, "foo.com", []); |
| 242 testResult(test, elemHide, "bar.com", []); |
| 243 |
| 244 addFilter("##hello"); |
| 245 addFilter("foo.com##hello"); |
| 246 testResult(test, elemHide, "foo.com", ["hello"]); |
| 247 removeFilter("foo.com##hello"); |
| 248 testResult(test, elemHide, "foo.com", ["hello"]); |
| 249 removeFilter("##hello"); |
| 250 testResult(test, elemHide, "foo.com", []); |
| 251 |
| 252 addFilter("##hello"); |
| 253 addFilter("foo.com##hello"); |
| 254 testResult(test, elemHide, "foo.com", ["hello"]); |
| 255 removeFilter("##hello"); |
| 256 testResult(test, elemHide, "foo.com", ["hello"]); |
| 257 removeFilter("foo.com##hello"); |
| 258 testResult(test, elemHide, "foo.com", []); |
| 259 |
| 260 elemHide.delete(); |
| 261 test.done(); |
| 262 }; |
| 263 |
| 264 exports.testZeroFilterKey = function(test) |
| 265 { |
| 266 let elemHide = ElemHide.create(); |
| 267 |
| 268 let filter1 = Filter.fromText("##test"); |
| 269 let filter2 = Filter.fromText("foo.com#@#test"); |
| 270 elemHide.add(filter1); |
| 271 elemHide.add(filter2); |
| 272 testResult(test, elemHide, "foo.com", []); |
| 273 testResult(test, elemHide, "bar.com", ["test"]); |
| 274 |
| 275 filter2.delete(); |
| 276 filter1.delete(); |
| 277 elemHide.delete(); |
| 278 test.done(); |
| 279 }; |
OLD | NEW |