| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
| 13 * | 13 * |
| 14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ | 16 */ |
| 17 | 17 |
| 18 "use strict"; | 18 "use strict"; |
| 19 | 19 |
| 20 const {createSandbox} = require("./_common"); | 20 const {createSandbox} = require("./_common"); |
| 21 const {withNAD} = require("./_test-utils"); | |
| 21 | 22 |
| 22 let ElemHideEmulationFilter = null; | 23 let ElemHideEmulationFilter = null; |
| 23 let ElemHideEmulation = null; | 24 let ElemHideEmulation = null; |
| 24 let ElemHide = null; | 25 let ElemHide = null; |
| 25 let Filter = null; | 26 let Filter = null; |
| 26 | 27 |
| 27 exports.setUp = function(callback) | 28 exports.setUp = function(callback) |
| 28 { | 29 { |
| 29 let sandboxedRequire = createSandbox(); | 30 let sandboxedRequire = createSandbox(); |
| 30 ( | 31 ( |
| 31 {Filter, | 32 {Filter, |
| 32 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses"), | 33 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses"), |
| 33 {ElemHideEmulation} = sandboxedRequire("../lib/elemHideEmulation"), | 34 {ElemHideEmulation} = sandboxedRequire("../lib/elemHideEmulation"), |
| 34 {ElemHide} = sandboxedRequire("../lib/elemHide") | 35 {ElemHide} = sandboxedRequire("../lib/elemHide") |
| 35 ); | 36 ); |
| 36 | 37 |
| 37 callback(); | 38 callback(); |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 exports.testElemHideAPI = function(test) | 41 exports.testElemHideAPI = function(test) |
| 41 { | 42 { |
| 43 withNAD(0, elemHide => | |
| 42 { | 44 { |
| 43 let filter = Filter.fromText("###ads"); | 45 withNAD(0, filter => |
| 44 ElemHide.add(filter); | 46 { |
| 45 | 47 elemHide.add(filter); |
| 46 test.equal(filter.selectorDomain, ""); | 48 test.equal(filter.selectorDomain, ""); |
| 47 let unconditionals = ElemHide.getUnconditionalSelectors(); | 49 })(Filter.fromText("###ads")); |
| 48 test.equal(unconditionals.selectorCount, 1); | 50 |
| 49 test.equal(unconditionals.selectorAt(0), "#ads"); | 51 withNAD(0, unconditionals => |
| 50 test.equal(unconditionals.filterKeyAt(0), "###ads"); | 52 { |
| 51 filter.delete(); | 53 test.equal(unconditionals.selectorCount, 1); |
| 52 unconditionals.delete(); | 54 test.equal(unconditionals.selectorAt(0), "#ads"); |
| 53 } | 55 test.equal(unconditionals.filterKeyAt(0), "###ads"); |
| 54 | 56 })(elemHide.getUnconditionalSelectors()); |
| 55 { | 57 |
| 56 let filter2 = Filter.fromText("example.com##.foo"); | 58 withNAD(0, filter => |
| 57 ElemHide.add(filter2); | 59 { |
| 58 test.equal(filter2.selectorDomain, "example.com"); | 60 elemHide.add(filter); |
| 59 filter2.delete(); | 61 test.equal(filter.selectorDomain, "example.com"); |
| 60 } | 62 })(Filter.fromText("example.com##.foo")); |
| 61 | 63 |
| 62 let unconditionals = ElemHide.getUnconditionalSelectors(); | 64 withNAD( |
| 63 test.equal(unconditionals.selectorCount, 1); | 65 0, unconditionals => |
| 64 unconditionals.delete(); | 66 test.equal(unconditionals.selectorCount, 1))(elemHide.getUnconditionalSe lectors()); |
| 65 | 67 |
| 66 let selectors = ElemHide.getSelectorsForDomain("example.com", 1); | 68 withNAD(0, selectors => |
| 67 test.equal(selectors.selectorCount, 1); | 69 { |
| 68 test.equal(selectors.selectorAt(0), ".foo"); | 70 test.equal(selectors.selectorCount, 1); |
| 69 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); | 71 test.equal(selectors.selectorAt(0), ".foo"); |
| 70 selectors.delete(); | 72 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); |
| 71 | 73 })(elemHide.getSelectorsForDomain("example.com", 1)); |
| 72 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 74 |
| 73 test.equal(selectors.selectorCount, 2); | 75 withNAD(0, selectors => |
| 74 test.equal(selectors.selectorAt(0), "#ads"); | 76 { |
| 75 test.equal(selectors.filterKeyAt(0), "###ads"); | 77 test.equal(selectors.selectorCount, 2); |
| 76 test.equal(selectors.selectorAt(1), ".foo"); | 78 test.equal(selectors.selectorAt(0), "#ads"); |
| 77 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); | 79 test.equal(selectors.filterKeyAt(0), "###ads"); |
| 78 selectors.delete(); | 80 test.equal(selectors.selectorAt(1), ".foo"); |
| 79 | 81 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); |
| 80 let filter3 = Filter.fromText("example.com##.message"); | 82 })(elemHide.getSelectorsForDomain("example.com", 0)); |
| 81 ElemHide.add(filter3); | 83 |
| 82 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 84 withNAD(0, filter3 => |
| 83 test.equal(selectors.selectorCount, 3); | 85 { |
| 84 selectors.delete(); | 86 elemHide.add(filter3); |
| 85 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 87 |
| 86 test.equal(selectors.selectorCount, 3); | 88 withNAD( |
|
sergei
2018/01/25 16:04:01
IMO it would be better to check the actually retur
hub
2018/01/25 19:05:27
Done. (in the whole test, not just here).
| |
| 87 selectors.delete(); | 89 0, selectors => |
| 88 | 90 test.equal(selectors.selectorCount, 3))( |
| 89 let filter4 = Filter.fromText("mail.example.com#@#.message"); | 91 elemHide.getSelectorsForDomain("example.com", 0)); |
| 90 ElemHide.add(filter4); | 92 |
| 91 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 93 withNAD( |
| 92 test.equal(selectors.selectorCount, 3); | 94 0, selectors => |
| 93 selectors.delete(); | 95 test.equal(selectors.selectorCount, 3))( |
| 94 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 96 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 95 test.equal(selectors.selectorCount, 2); | 97 |
| 96 selectors.delete(); | 98 withNAD(0, filter4 => |
| 97 | 99 { |
| 98 unconditionals = ElemHide.getUnconditionalSelectors(); | 100 elemHide.add(filter4); |
| 99 test.equal(unconditionals.selectorCount, 1); | 101 withNAD( |
| 100 unconditionals.delete(); | 102 0, selectors => |
| 101 | 103 test.equal(selectors.selectorCount, 3))( |
| 102 ElemHide.remove(filter4); | 104 elemHide.getSelectorsForDomain("example.com", 0)); |
| 103 filter4.delete(); | 105 |
| 104 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 106 withNAD( |
| 105 test.equal(selectors.selectorCount, 3); | 107 0, selectors => |
| 106 selectors.delete(); | 108 test.equal(selectors.selectorCount, 2))( |
| 107 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 109 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 108 test.equal(selectors.selectorCount, 3); | 110 |
| 109 selectors.delete(); | 111 withNAD( |
| 110 | 112 0, |
| 111 ElemHide.remove(filter3); | 113 unconditionals => |
| 112 filter3.delete(); | 114 test.equal(unconditionals.selectorCount, 1))(elemHide.getUncondition alSelectors()); |
| 113 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 115 |
| 114 test.equal(selectors.selectorCount, 2); | 116 elemHide.remove(filter4); |
| 115 selectors.delete(); | 117 })(Filter.fromText("mail.example.com#@#.message")); |
| 118 | |
| 119 withNAD( | |
| 120 0, selectors => | |
| 121 test.equal(selectors.selectorCount, 3))( | |
| 122 elemHide.getSelectorsForDomain("example.com", 0)); | |
| 123 | |
| 124 withNAD( | |
| 125 0, selectors => | |
| 126 test.equal(selectors.selectorCount, 3))( | |
| 127 elemHide.getSelectorsForDomain("mail.example.com", 0)); | |
| 128 | |
| 129 elemHide.remove(filter3); | |
| 130 })(Filter.fromText("example.com##.message")); | |
| 131 | |
| 132 withNAD( | |
| 133 0, selectors => | |
| 134 test.equal(selectors.selectorCount, 2))( | |
| 135 elemHide.getSelectorsForDomain("example.com", 0)); | |
| 136 })(ElemHide.create()); | |
| 116 | 137 |
| 117 test.done(); | 138 test.done(); |
| 118 }; | 139 }; |
| 119 | 140 |
| 120 exports.testDomainRestrictions = function(test) | 141 exports.testDomainRestrictions = function(test) |
| 121 { | 142 { |
| 122 function testSelectorMatches(description, filters, domain, expectedMatches) | 143 function testSelectorMatches(description, filters, domain, expectedMatches) |
| 123 { | 144 { |
| 124 for (let text of filters) | 145 withNAD([0, 1], (elemHide, elemHideEmulation) => |
| 125 { | 146 { |
| 126 let filter = Filter.fromText(text); | 147 let addFilter = withNAD(0, filter => |
| 127 if (filter instanceof ElemHideEmulationFilter) | 148 { |
| 128 ElemHideEmulation.add(filter); | 149 if (filter instanceof ElemHideEmulationFilter) |
| 129 else | 150 elemHideEmulation.add(filter); |
| 130 ElemHide.add(filter); | 151 else |
| 131 filter.delete(); | 152 elemHide.add(filter); |
| 132 } | 153 }); |
| 133 | 154 |
| 134 let rules = ElemHideEmulation.getRulesForDomain(domain); | 155 for (let text of filters) |
| 135 let matches = []; | 156 addFilter(Filter.fromText(text)); |
| 136 for (let i = 0; i < rules.filterCount; i++) | 157 |
| 137 { | 158 withNAD(0, rules => |
| 138 let filter = rules.filterAt(i); | 159 { |
| 139 matches.push(filter.text); | 160 let matches = []; |
| 140 filter.delete(); | 161 let push = withNAD(0, filter => matches.push(filter.text)); |
| 141 } | 162 |
| 142 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 163 for (let i = 0; i < rules.filterCount; i++) |
| 143 rules.delete(); | 164 push(rules.filterAt(i)); |
| 144 | 165 |
| 145 ElemHideEmulation.clear(); | 166 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
| 146 ElemHide.clear(); | 167 })(elemHideEmulation.getRulesForDomain(elemHide, domain)); |
| 168 | |
| 169 elemHideEmulation.clear(); | |
| 170 })(ElemHide.create(), ElemHideEmulation.create()); | |
| 147 } | 171 } |
| 148 | 172 |
| 149 testSelectorMatches( | 173 testSelectorMatches( |
| 150 "Ignore generic filters", | 174 "Ignore generic filters", |
| 151 [ | 175 [ |
| 152 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", | 176 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", |
| 153 "~example.com##[-abp-properties='foo']" | 177 "~example.com##[-abp-properties='foo']" |
| 154 ], | 178 ], |
| 155 "example.com", | 179 "example.com", |
| 156 ["example.com##[-abp-properties='foo']"] | 180 ["example.com##[-abp-properties='foo']"] |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 190 ], | 214 ], |
| 191 "other.example.com", | 215 "other.example.com", |
| 192 ["other.example.com##[-abp-properties='foo']"] | 216 ["other.example.com##[-abp-properties='foo']"] |
| 193 ); | 217 ); |
| 194 | 218 |
| 195 test.done(); | 219 test.done(); |
| 196 }; | 220 }; |
| 197 | 221 |
| 198 exports.testElemHideEmulationFiltersContainer = function(test) | 222 exports.testElemHideEmulationFiltersContainer = function(test) |
| 199 { | 223 { |
| 200 function compareRules(description, domain, expectedMatches) | 224 withNAD([0, 1], (elemHide, elemHideEmulation) => |
| 201 { | 225 { |
| 202 let rules = ElemHideEmulation.getRulesForDomain(domain); | 226 function compareRules(description, domain, expectedMatches) |
| 203 let result = []; | 227 { |
| 204 for (let i = 0; i < rules.filterCount; i++) | 228 withNAD(0, rules => |
| 205 { | 229 { |
| 206 let filter = rules.filterAt(i); | 230 let result = []; |
| 207 result.push(filter.text); | 231 for (let i = 0; i < rules.filterCount; i++) |
| 208 filter.delete(); | 232 withNAD(0, filter => result.push(filter.text))(rules.filterAt(i)); |
| 233 | |
| 234 expectedMatches = expectedMatches.map(filter => filter.text); | |
| 235 test.deepEqual(result.sort(), expectedMatches.sort(), description); | |
| 236 })(elemHideEmulation.getRulesForDomain(elemHide, domain)); | |
| 209 } | 237 } |
| 210 expectedMatches = expectedMatches.map(filter => filter.text); | 238 |
| 211 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 239 withNAD([0, 1, 2], (domainFilter, subdomainFilter, otherDomainFilter) => |
| 212 | 240 { |
| 213 rules.delete(); | 241 elemHideEmulation.add(domainFilter); |
| 214 } | 242 elemHideEmulation.add(subdomainFilter); |
| 215 | 243 elemHideEmulation.add(otherDomainFilter); |
| 216 let domainFilter = Filter.fromText("example.com##filter1"); | 244 compareRules( |
| 217 let subdomainFilter = Filter.fromText("www.example.com##filter2"); | 245 "Return all matching filters", |
| 218 let otherDomainFilter = Filter.fromText("other.example.com##filter3"); | 246 "www.example.com", |
| 219 | 247 [domainFilter, subdomainFilter] |
| 220 ElemHideEmulation.add(domainFilter); | 248 ); |
| 221 ElemHideEmulation.add(subdomainFilter); | 249 |
| 222 ElemHideEmulation.add(otherDomainFilter); | 250 elemHideEmulation.remove(domainFilter); |
| 223 compareRules( | 251 compareRules( |
| 224 "Return all matching filters", | 252 "Return all matching filters after removing one", |
| 225 "www.example.com", | 253 "www.example.com", |
| 226 [domainFilter, subdomainFilter] | 254 [subdomainFilter] |
| 227 ); | 255 ); |
| 228 | 256 |
| 229 ElemHideEmulation.remove(domainFilter); | 257 elemHideEmulation.clear(); |
| 230 compareRules( | 258 compareRules( |
| 231 "Return all matching filters after removing one", | 259 "Return no filters after clearing", |
| 232 "www.example.com", | 260 "www.example.com", |
| 233 [subdomainFilter] | 261 [] |
| 234 ); | 262 ); |
| 235 | 263 })(Filter.fromText("example.com##filter1"), |
| 236 ElemHideEmulation.clear(); | 264 Filter.fromText("www.example.com##filter2"), |
| 237 compareRules( | 265 Filter.fromText("other.example.com##filter3")); |
| 238 "Return no filters after clearing", | 266 })(ElemHide.create(), ElemHideEmulation.create()); |
| 239 "www.example.com", | |
| 240 [] | |
| 241 ); | |
| 242 | 267 |
| 243 test.done(); | 268 test.done(); |
| 244 }; | 269 }; |
| LEFT | RIGHT |