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 | |
41 exports.testElemHideAPI = function(test) | 41 exports.testElemHideAPI = function(test) |
42 { | 42 { |
| 43 withNAD(0, elemHide => |
43 { | 44 { |
44 let filter = Filter.fromText("###ads"); | 45 withNAD(0, filter => |
45 ElemHide.add(filter); | 46 { |
46 | 47 elemHide.add(filter); |
47 test.equal(filter.selectorDomain, ""); | 48 test.equal(filter.selectorDomain, ""); |
48 let unconditionals = ElemHide.getUnconditionalSelectors(); | 49 })(Filter.fromText("###ads")); |
49 test.equal(unconditionals.selectorCount, 1); | 50 |
50 test.equal(unconditionals.selectorAt(0), "#ads"); | 51 withNAD(0, unconditionals => |
51 test.equal(unconditionals.filterKeyAt(0), "###ads"); | 52 { |
52 filter.delete(); | 53 test.equal(unconditionals.selectorCount, 1); |
53 } | 54 test.equal(unconditionals.selectorAt(0), "#ads"); |
54 | 55 test.equal(unconditionals.filterKeyAt(0), "###ads"); |
55 { | 56 })(elemHide.getUnconditionalSelectors()); |
56 let filter2 = Filter.fromText("example.com##.foo"); | 57 |
57 ElemHide.add(filter2); | 58 withNAD(0, filter => |
58 test.equal(filter2.selectorDomain, "example.com"); | 59 { |
59 filter2.delete(); | 60 elemHide.add(filter); |
60 } | 61 test.equal(filter.selectorDomain, "example.com"); |
61 | 62 })(Filter.fromText("example.com##.foo")); |
62 let unconditionals = ElemHide.getUnconditionalSelectors(); | 63 |
63 test.equal(unconditionals.selectorCount, 1); | 64 withNAD( |
64 | 65 0, unconditionals => |
65 let selectors = ElemHide.getSelectorsForDomain("example.com", 1); | 66 test.equal(unconditionals.selectorCount, 1))(elemHide.getUnconditionalSe
lectors()); |
66 test.equal(selectors.selectorCount, 1); | 67 |
67 test.equal(selectors.selectorAt(0), ".foo"); | 68 withNAD(0, selectors => |
68 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); | 69 { |
69 | 70 test.equal(selectors.selectorCount, 1); |
70 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 71 test.equal(selectors.selectorAt(0), ".foo"); |
71 test.equal(selectors.selectorCount, 2); | 72 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); |
72 test.equal(selectors.selectorAt(0), "#ads"); | 73 })(elemHide.getSelectorsForDomain("example.com", 1)); |
73 test.equal(selectors.filterKeyAt(0), "###ads"); | 74 |
74 test.equal(selectors.selectorAt(1), ".foo"); | 75 withNAD(0, selectors => |
75 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); | 76 { |
76 | 77 test.equal(selectors.selectorCount, 2); |
77 let filter3 = Filter.fromText("example.com##.message"); | 78 test.equal(selectors.selectorAt(0), "#ads"); |
78 ElemHide.add(filter3); | 79 test.equal(selectors.filterKeyAt(0), "###ads"); |
79 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 80 test.equal(selectors.selectorAt(1), ".foo"); |
80 test.equal(selectors.selectorCount, 3); | 81 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); |
81 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 82 })(elemHide.getSelectorsForDomain("example.com", 0)); |
82 test.equal(selectors.selectorCount, 3); | 83 |
83 | 84 withNAD(0, filter3 => |
84 let filter4 = Filter.fromText("mail.example.com#@#.message"); | 85 { |
85 ElemHide.add(filter4); | 86 elemHide.add(filter3); |
86 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 87 |
87 test.equal(selectors.selectorCount, 3); | 88 withNAD( |
88 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 89 0, selectors => |
89 test.equal(selectors.selectorCount, 2); | 90 test.equal(selectors.selectorCount, 3))( |
90 | 91 elemHide.getSelectorsForDomain("example.com", 0)); |
91 unconditionals = ElemHide.getUnconditionalSelectors(); | 92 |
92 test.equal(unconditionals.selectorCount, 1); | 93 withNAD( |
93 | 94 0, selectors => |
94 ElemHide.remove(filter4); | 95 test.equal(selectors.selectorCount, 3))( |
95 filter4.delete(); | 96 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
96 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 97 |
97 test.equal(selectors.selectorCount, 3); | 98 withNAD(0, filter4 => |
98 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 99 { |
99 test.equal(selectors.selectorCount, 3); | 100 elemHide.add(filter4); |
100 | 101 withNAD( |
101 ElemHide.remove(filter3); | 102 0, selectors => |
102 filter3.delete(); | 103 test.equal(selectors.selectorCount, 3))( |
103 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 104 elemHide.getSelectorsForDomain("example.com", 0)); |
104 test.equal(selectors.selectorCount, 2); | 105 |
| 106 withNAD( |
| 107 0, selectors => |
| 108 test.equal(selectors.selectorCount, 2))( |
| 109 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 110 |
| 111 withNAD( |
| 112 0, |
| 113 unconditionals => |
| 114 test.equal(unconditionals.selectorCount, 1))(elemHide.getUncondition
alSelectors()); |
| 115 |
| 116 elemHide.remove(filter4); |
| 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()); |
105 | 137 |
106 test.done(); | 138 test.done(); |
107 }; | 139 }; |
108 | 140 |
109 exports.testDomainRestrictions = function(test) | 141 exports.testDomainRestrictions = function(test) |
110 { | 142 { |
111 function testSelectorMatches(description, filters, domain, expectedMatches) | 143 function testSelectorMatches(description, filters, domain, expectedMatches) |
112 { | 144 { |
113 for (let text of filters) | 145 withNAD([0, 1], (elemHide, elemHideEmulation) => |
114 { | 146 { |
115 let filter = Filter.fromText(text); | 147 let addFilter = withNAD(0, filter => |
116 if (filter instanceof ElemHideEmulationFilter) | 148 { |
117 ElemHideEmulation.add(filter); | 149 if (filter instanceof ElemHideEmulationFilter) |
118 else | 150 elemHideEmulation.add(filter); |
119 ElemHide.add(filter); | 151 else |
120 filter.delete(); | 152 elemHide.add(filter); |
121 } | 153 }); |
122 | 154 |
123 let rules = ElemHideEmulation.getRulesForDomain(domain); | 155 for (let text of filters) |
124 let matches = []; | 156 addFilter(Filter.fromText(text)); |
125 for (let i = 0; i < rules.filterCount; i++) | 157 |
126 { | 158 withNAD(0, rules => |
127 let filter = rules.filterAt(i); | 159 { |
128 matches.push(filter.text); | 160 let matches = []; |
129 filter.delete(); | 161 let push = withNAD(0, filter => matches.push(filter.text)); |
130 } | 162 |
131 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 163 for (let i = 0; i < rules.filterCount; i++) |
132 rules.delete(); | 164 push(rules.filterAt(i)); |
133 | 165 |
134 ElemHideEmulation.clear(); | 166 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
135 ElemHide.clear(); | 167 })(elemHideEmulation.getRulesForDomain(elemHide, domain)); |
| 168 |
| 169 elemHideEmulation.clear(); |
| 170 })(ElemHide.create(), ElemHideEmulation.create()); |
136 } | 171 } |
137 | 172 |
138 testSelectorMatches( | 173 testSelectorMatches( |
139 "Ignore generic filters", | 174 "Ignore generic filters", |
140 [ | 175 [ |
141 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", | 176 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", |
142 "~example.com##[-abp-properties='foo']" | 177 "~example.com##[-abp-properties='foo']" |
143 ], | 178 ], |
144 "example.com", | 179 "example.com", |
145 ["example.com##[-abp-properties='foo']"] | 180 ["example.com##[-abp-properties='foo']"] |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 ], | 214 ], |
180 "other.example.com", | 215 "other.example.com", |
181 ["other.example.com##[-abp-properties='foo']"] | 216 ["other.example.com##[-abp-properties='foo']"] |
182 ); | 217 ); |
183 | 218 |
184 test.done(); | 219 test.done(); |
185 }; | 220 }; |
186 | 221 |
187 exports.testElemHideEmulationFiltersContainer = function(test) | 222 exports.testElemHideEmulationFiltersContainer = function(test) |
188 { | 223 { |
189 function compareRules(description, domain, expectedMatches) | 224 withNAD([0, 1], (elemHide, elemHideEmulation) => |
190 { | 225 { |
191 let rules = ElemHideEmulation.getRulesForDomain(domain); | 226 function compareRules(description, domain, expectedMatches) |
192 let result = []; | 227 { |
193 for (let i = 0; i < rules.filterCount; i++) | 228 withNAD(0, rules => |
194 { | 229 { |
195 let filter = rules.filterAt(i); | 230 let result = []; |
196 result.push(filter.text); | 231 for (let i = 0; i < rules.filterCount; i++) |
197 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)); |
198 } | 237 } |
199 expectedMatches = expectedMatches.map(filter => filter.text); | 238 |
200 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 239 withNAD([0, 1, 2], (domainFilter, subdomainFilter, otherDomainFilter) => |
201 | 240 { |
202 rules.delete(); | 241 elemHideEmulation.add(domainFilter); |
203 } | 242 elemHideEmulation.add(subdomainFilter); |
204 | 243 elemHideEmulation.add(otherDomainFilter); |
205 let domainFilter = Filter.fromText("example.com##filter1"); | 244 compareRules( |
206 let subdomainFilter = Filter.fromText("www.example.com##filter2"); | 245 "Return all matching filters", |
207 let otherDomainFilter = Filter.fromText("other.example.com##filter3"); | 246 "www.example.com", |
208 | 247 [domainFilter, subdomainFilter] |
209 ElemHideEmulation.add(domainFilter); | 248 ); |
210 ElemHideEmulation.add(subdomainFilter); | 249 |
211 ElemHideEmulation.add(otherDomainFilter); | 250 elemHideEmulation.remove(domainFilter); |
212 compareRules( | 251 compareRules( |
213 "Return all matching filters", | 252 "Return all matching filters after removing one", |
214 "www.example.com", | 253 "www.example.com", |
215 [domainFilter, subdomainFilter] | 254 [subdomainFilter] |
216 ); | 255 ); |
217 | 256 |
218 ElemHideEmulation.remove(domainFilter); | 257 elemHideEmulation.clear(); |
219 compareRules( | 258 compareRules( |
220 "Return all matching filters after removing one", | 259 "Return no filters after clearing", |
221 "www.example.com", | 260 "www.example.com", |
222 [subdomainFilter] | 261 [] |
223 ); | 262 ); |
224 | 263 })(Filter.fromText("example.com##filter1"), |
225 ElemHideEmulation.clear(); | 264 Filter.fromText("www.example.com##filter2"), |
226 compareRules( | 265 Filter.fromText("other.example.com##filter3")); |
227 "Return no filters after clearing", | 266 })(ElemHide.create(), ElemHideEmulation.create()); |
228 "www.example.com", | |
229 [] | |
230 ); | |
231 | 267 |
232 test.done(); | 268 test.done(); |
233 }; | 269 }; |
LEFT | RIGHT |