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 const {withNAD} = require("./_test-utils"); |
| 22 |
| 23 let ElemHide = null; |
| 24 let Filter = null; |
| 25 |
| 26 exports.setUp = function(callback) |
| 27 { |
| 28 let sandboxedRequire = createSandbox(); |
| 29 ( |
| 30 {ElemHide} = sandboxedRequire("../lib/elemHide"), |
| 31 {Filter} = sandboxedRequire("../lib/filterClasses") |
| 32 ); |
| 33 |
| 34 callback(); |
| 35 }; |
| 36 |
| 37 function normalizeSelectors(selectorList) |
| 38 { |
| 39 let selectors; |
| 40 |
| 41 if (Array.isArray(selectorList)) |
| 42 selectors = selectorList; |
| 43 else |
| 44 { |
| 45 selectors = []; |
| 46 for (let i = 0; i < selectorList.selectorCount; i++) |
| 47 selectors.push(selectorList.selectorAt(i)); |
| 48 } |
| 49 |
| 50 // getSelectorsForDomain is currently allowed to return duplicate selectors |
| 51 // for performance reasons, so we need to remove duplicates here. |
| 52 return selectors.sort().filter((selector, index, sortedSelectors) => |
| 53 { |
| 54 return index == 0 || selector != sortedSelectors[index - 1]; |
| 55 }); |
| 56 } |
| 57 |
| 58 function testResult(test, elemHide, domain, expectedSelectors, criteria) |
| 59 { |
| 60 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); |
| 61 |
| 62 test.deepEqual( |
| 63 normalizeSelectors(elemHide.getSelectorsForDomain(domain, criteria)), |
| 64 normalizedExpectedSelectors |
| 65 ); |
| 66 } |
| 67 |
| 68 exports.testGetSelectorsForDomain = function(test) |
| 69 { |
| 70 withNAD(0, elemHide => |
| 71 { |
| 72 let addFilter = filterText => withNAD( |
| 73 0, filter => elemHide.add(filter))(Filter.fromText(filterText)); |
| 74 let removeFilter = filterText => withNAD( |
| 75 0, filter => elemHide.remove(filter))(Filter.fromText(filterText)); |
| 76 |
| 77 testResult(test, elemHide, "", []); |
| 78 |
| 79 addFilter("~foo.example.com,example.com##foo"); |
| 80 testResult(test, elemHide, "barfoo.example.com", ["foo"]); |
| 81 testResult(test, elemHide, "bar.foo.example.com", []); |
| 82 testResult(test, elemHide, "foo.example.com", []); |
| 83 testResult(test, elemHide, "example.com", ["foo"]); |
| 84 testResult(test, elemHide, "com", []); |
| 85 testResult(test, elemHide, "", []); |
| 86 |
| 87 addFilter("foo.example.com##turnip"); |
| 88 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 89 testResult(test, elemHide, "example.com", ["foo"]); |
| 90 testResult(test, elemHide, "com", []); |
| 91 testResult(test, elemHide, "", []); |
| 92 |
| 93 addFilter("example.com#@#foo"); |
| 94 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 95 testResult(test, elemHide, "example.com", []); |
| 96 testResult(test, elemHide, "com", []); |
| 97 testResult(test, elemHide, "", []); |
| 98 |
| 99 addFilter("com##bar"); |
| 100 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 101 testResult(test, elemHide, "example.com", ["bar"]); |
| 102 testResult(test, elemHide, "com", ["bar"]); |
| 103 testResult(test, elemHide, "", []); |
| 104 |
| 105 addFilter("example.com#@#bar"); |
| 106 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 107 testResult(test, elemHide, "example.com", []); |
| 108 testResult(test, elemHide, "com", ["bar"]); |
| 109 testResult(test, elemHide, "", []); |
| 110 |
| 111 removeFilter("example.com#@#foo"); |
| 112 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 113 testResult(test, elemHide, "example.com", ["foo"]); |
| 114 testResult(test, elemHide, "com", ["bar"]); |
| 115 testResult(test, elemHide, "", []); |
| 116 |
| 117 removeFilter("example.com#@#bar"); |
| 118 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 119 testResult(test, elemHide, "example.com", ["foo", "bar"]); |
| 120 testResult(test, elemHide, "com", ["bar"]); |
| 121 testResult(test, elemHide, "", []); |
| 122 |
| 123 addFilter("##generic"); |
| 124 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "generic"]); |
| 125 testResult(test, elemHide, "example.com", ["foo", "bar", "generic"]); |
| 126 testResult(test, elemHide, "com", ["bar", "generic"]); |
| 127 testResult(test, elemHide, "", ["generic"]); |
| 128 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SP
ECIFIC_ONLY); |
| 129 testResult(test, elemHide, "example.com", ["foo", "bar"], ElemHide.SPECIFIC_
ONLY); |
| 130 testResult(test, elemHide, "com", ["bar"], ElemHide.SPECIFIC_ONLY); |
| 131 testResult(test, elemHide, "", [], ElemHide.SPECIFIC_ONLY); |
| 132 removeFilter("##generic"); |
| 133 |
| 134 addFilter("~adblockplus.org##example"); |
| 135 testResult(test, elemHide, "adblockplus.org", []); |
| 136 testResult(test, elemHide, "", ["example"]); |
| 137 testResult(test, elemHide, "foo.example.com", ["turnip", "bar", "example"]); |
| 138 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"], ElemHide.SP
ECIFIC_ONLY); |
| 139 removeFilter("~adblockplus.org##example"); |
| 140 |
| 141 removeFilter("~foo.example.com,example.com##foo"); |
| 142 testResult(test, elemHide, "foo.example.com", ["turnip", "bar"]); |
| 143 testResult(test, elemHide, "example.com", ["bar"]); |
| 144 testResult(test, elemHide, "com", ["bar"]); |
| 145 testResult(test, elemHide, "", []); |
| 146 |
| 147 removeFilter("com##bar"); |
| 148 testResult(test, elemHide, "foo.example.com", ["turnip"]); |
| 149 testResult(test, elemHide, "example.com", []); |
| 150 testResult(test, elemHide, "com", []); |
| 151 testResult(test, elemHide, "", []); |
| 152 |
| 153 removeFilter("foo.example.com##turnip"); |
| 154 testResult(test, elemHide, "foo.example.com", []); |
| 155 testResult(test, elemHide, "example.com", []); |
| 156 testResult(test, elemHide, "com", []); |
| 157 testResult(test, elemHide, "", []); |
| 158 |
| 159 addFilter("example.com##dupe"); |
| 160 addFilter("example.com##dupe"); |
| 161 testResult(test, elemHide, "example.com", ["dupe"]); |
| 162 removeFilter("example.com##dupe"); |
| 163 testResult(test, elemHide, "example.com", []); |
| 164 removeFilter("example.com##dupe"); |
| 165 |
| 166 addFilter("~foo.example.com,example.com##foo"); |
| 167 |
| 168 addFilter("##foo"); |
| 169 testResult(test, elemHide, "foo.example.com", ["foo"]); |
| 170 testResult(test, elemHide, "example.com", ["foo"]); |
| 171 testResult(test, elemHide, "com", ["foo"]); |
| 172 testResult(test, elemHide, "", ["foo"]); |
| 173 removeFilter("##foo"); |
| 174 |
| 175 addFilter("example.org##foo"); |
| 176 testResult(test, elemHide, "foo.example.com", []); |
| 177 testResult(test, elemHide, "example.com", ["foo"]); |
| 178 testResult(test, elemHide, "com", []); |
| 179 testResult(test, elemHide, "", []); |
| 180 removeFilter("example.org##foo"); |
| 181 |
| 182 addFilter("~example.com##foo"); |
| 183 testResult(test, elemHide, "foo.example.com", []); |
| 184 testResult(test, elemHide, "example.com", ["foo"]); |
| 185 testResult(test, elemHide, "com", ["foo"]); |
| 186 testResult(test, elemHide, "", ["foo"]); |
| 187 removeFilter("~example.com##foo"); |
| 188 |
| 189 removeFilter("~foo.example.com,example.com##foo"); |
| 190 |
| 191 // Test criteria |
| 192 addFilter("##hello"); |
| 193 addFilter("~example.com##world"); |
| 194 addFilter("foo.com##specific"); |
| 195 testResult(test, elemHide, "foo.com", ["specific"], ElemHide.SPECIFIC_ONLY); |
| 196 testResult(test, elemHide, "foo.com", ["specific", "world"], ElemHide.NO_UNC
ONDITIONAL); |
| 197 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"], ElemHi
de.ALL_MATCHING); |
| 198 testResult(test, elemHide, "foo.com", ["hello", "specific", "world"]); |
| 199 removeFilter("foo.com##specific"); |
| 200 removeFilter("~example.com##world"); |
| 201 removeFilter("##hello"); |
| 202 testResult(test, elemHide, "foo.com", []); |
| 203 |
| 204 addFilter("##hello"); |
| 205 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 206 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 207 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 208 testResult(test, elemHide, "foo.com", ["hello"]); |
| 209 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 210 testResult(test, elemHide, "bar.com", [], ElemHide.NO_UNCONDITIONAL); |
| 211 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 212 testResult(test, elemHide, "bar.com", ["hello"]); |
| 213 addFilter("foo.com#@#hello"); |
| 214 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 215 testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 216 testResult(test, elemHide, "foo.com", [], ElemHide.ALL_MATCHING); |
| 217 testResult(test, elemHide, "foo.com", []); |
| 218 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 219 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 220 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 221 testResult(test, elemHide, "bar.com", ["hello"]); |
| 222 removeFilter("foo.com#@#hello"); |
| 223 testResult(test, elemHide, "foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 224 // Note: We don't take care to track conditional selectors which became |
| 225 // unconditional when a filter was removed. This was too expensive. |
| 226 // testResult(test, elemHide, "foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 227 testResult(test, elemHide, "foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 228 testResult(test, elemHide, "foo.com", ["hello"]); |
| 229 testResult(test, elemHide, "bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 230 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); |
| 231 testResult(test, elemHide, "bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 232 testResult(test, elemHide, "bar.com", ["hello"]); |
| 233 removeFilter("##hello"); |
| 234 testResult(test, elemHide, "foo.com", []); |
| 235 testResult(test, elemHide, "bar.com", []); |
| 236 |
| 237 addFilter("##hello"); |
| 238 addFilter("foo.com##hello"); |
| 239 testResult(test, elemHide, "foo.com", ["hello"]); |
| 240 removeFilter("foo.com##hello"); |
| 241 testResult(test, elemHide, "foo.com", ["hello"]); |
| 242 removeFilter("##hello"); |
| 243 testResult(test, elemHide, "foo.com", []); |
| 244 |
| 245 addFilter("##hello"); |
| 246 addFilter("foo.com##hello"); |
| 247 testResult(test, elemHide, "foo.com", ["hello"]); |
| 248 removeFilter("##hello"); |
| 249 testResult(test, elemHide, "foo.com", ["hello"]); |
| 250 removeFilter("foo.com##hello"); |
| 251 testResult(test, elemHide, "foo.com", []); |
| 252 })(ElemHide.create()); |
| 253 |
| 254 test.done(); |
| 255 }; |
| 256 |
| 257 exports.testFilterException = function(test) |
| 258 { |
| 259 withNAD([0, 1, 2], (elemHide, filter, exception) => |
| 260 { |
| 261 elemHide.add(filter); |
| 262 elemHide.add(exception); |
| 263 testResult(test, elemHide, "foo.com", []); |
| 264 testResult(test, elemHide, "bar.com", ["test"]); |
| 265 })(ElemHide.create(), Filter.fromText("##test"), Filter.fromText("foo.com#@#te
st")); |
| 266 |
| 267 test.done(); |
| 268 }; |
OLD | NEW |