Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: test/filterClasses.js

Issue 29361668: Issue 4394 - Create a filter class for element hiding emulation filters (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Left Patch Set: Address kzar's comments Created Nov. 11, 2016, 11:44 a.m.
Right Patch Set: Improve compliance with the 80 column rule Created Nov. 21, 2016, 8:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « test/elemHideEmulation.js ('k') | test/filterListener.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 22 matching lines...) Expand all
33 33
34 let t = null; 34 let t = null;
35 let defaultTypes = null; 35 let defaultTypes = null;
36 36
37 exports.setUp = function(callback) 37 exports.setUp = function(callback)
38 { 38 {
39 let sandboxedRequire = createSandbox(); 39 let sandboxedRequire = createSandbox();
40 ( 40 (
41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
43 ElemHideException, ElemHideEmulationFilter} = sandboxedRequire("../lib/filt erClasses") 43 ElemHideException,
44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses")
44 ); 45 );
45 t = RegExpFilter.typeMap; 46 t = RegExpFilter.typeMap;
46 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | 47 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP |
47 t.GENERICHIDE | t.GENERICBLOCK); 48 t.GENERICHIDE | t.GENERICBLOCK);
48 49
49 callback(); 50 callback();
50 }; 51 };
51 52
52 function serializeFilter(filter) 53 function serializeFilter(filter)
53 { 54 {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 result.push("type=whitelist"); 99 result.push("type=whitelist");
99 } 100 }
100 } 101 }
101 else if (filter instanceof ElemHideBase) 102 else if (filter instanceof ElemHideBase)
102 { 103 {
103 if (filter instanceof ElemHideFilter) 104 if (filter instanceof ElemHideFilter)
104 result.push("type=elemhide"); 105 result.push("type=elemhide");
105 else if (filter instanceof ElemHideException) 106 else if (filter instanceof ElemHideException)
106 result.push("type=elemhideexception"); 107 result.push("type=elemhideexception");
107 else if (filter instanceof ElemHideEmulationFilter) 108 else if (filter instanceof ElemHideEmulationFilter)
108 {
109 result.push("type=elemhideemulation"); 109 result.push("type=elemhideemulation");
110 result.push("features=" + filter.features);
111 }
112 110
113 result.push("selectorDomain=" + (filter.selectorDomain || "")); 111 result.push("selectorDomain=" + (filter.selectorDomain || ""));
114 result.push("selector=" + filter.selector); 112 result.push("selector=" + filter.selector);
115 } 113 }
116 } 114 }
117 return result; 115 return result;
118 } 116 }
119 117
120 function addDefaults(expected) 118 function addDefaults(expected)
121 { 119 {
122 let type = null; 120 let type = null;
123 let hasProperty = {}; 121 let hasProperty = {};
124 for (let entry of expected) 122 for (let entry of expected)
125 { 123 {
126 if (/^type=(.*)/.test(entry)) 124 if (/^type=(.*)/.test(entry))
127 type = RegExp.$1; 125 type = RegExp.$1;
128 else if (/^(\w+)/.test(entry)) 126 else if (/^(\w+)/.test(entry))
129 hasProperty[RegExp.$1] = true; 127 hasProperty[RegExp.$1] = true;
130 } 128 }
131 129
132 function addProperty(prop, value) 130 function addProperty(prop, value)
133 { 131 {
134 if (!(prop in hasProperty)) 132 if (!(prop in hasProperty))
135 expected.push(prop + "=" + value); 133 expected.push(prop + "=" + value);
136 } 134 }
137 135
138 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "elemhideemulation") 136 if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
137 type == "elemhideexception" || type == "elemhideemulation")
139 { 138 {
140 addProperty("disabled", "false"); 139 addProperty("disabled", "false");
141 addProperty("lastHit", "0"); 140 addProperty("lastHit", "0");
142 addProperty("hitCount", "0"); 141 addProperty("hitCount", "0");
143 } 142 }
144 if (type == "whitelist" || type == "filterlist") 143 if (type == "whitelist" || type == "filterlist")
145 { 144 {
146 addProperty("contentType", 0x7FFFFFFF & ~( 145 addProperty("contentType", 0x7FFFFFFF & ~(
147 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 146 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
148 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 147 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
149 RegExpFilter.typeMap.GENERICBLOCK 148 RegExpFilter.typeMap.GENERICBLOCK
150 )); 149 ));
151 addProperty("matchCase", "false"); 150 addProperty("matchCase", "false");
152 addProperty("thirdParty", "null"); 151 addProperty("thirdParty", "null");
153 addProperty("domains", ""); 152 addProperty("domains", "");
154 addProperty("sitekeys", ""); 153 addProperty("sitekeys", "");
155 } 154 }
156 if (type == "filterlist") 155 if (type == "filterlist")
157 { 156 {
158 addProperty("collapse", "null"); 157 addProperty("collapse", "null");
159 } 158 }
160 if (type == "elemhide" || type == "elemhideexception" || type == "elemhideemul ation") 159 if (type == "elemhide" || type == "elemhideexception" ||
160 type == "elemhideemulation")
161 { 161 {
162 addProperty("selectorDomain", ""); 162 addProperty("selectorDomain", "");
163 addProperty("domains", ""); 163 addProperty("domains", "");
164 }
165 if (type == "elemhideemulation")
166 {
167 addProperty("features", "0");
168 } 164 }
169 } 165 }
170 166
171 function compareFilter(test, text, expected, postInit) 167 function compareFilter(test, text, expected, postInit)
172 { 168 {
173 addDefaults(expected); 169 addDefaults(expected);
174 170
175 let filter = Filter.fromText(text); 171 let filter = Filter.fromText(text);
176 if (postInit) 172 if (postInit)
177 postInit(filter) 173 postInit(filter)
(...skipping 27 matching lines...) Expand all
205 test.equal(typeof Filter, "function", "typeof Filter"); 201 test.equal(typeof Filter, "function", "typeof Filter");
206 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 202 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
207 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 203 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
208 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 204 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
209 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 205 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
210 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 206 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
211 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 207 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
212 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 208 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
213 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 209 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
214 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 210 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
215 test.equal(typeof ElemHideEmulationFilter, "function", "typeof ElemHideEmulati onFilter"); 211 test.equal(typeof ElemHideEmulationFilter, "function",
212 "typeof ElemHideEmulationFilter");
216 213
217 test.done(); 214 test.done();
218 }; 215 };
219 216
220 exports.testComments = function(test) 217 exports.testComments = function(test)
221 { 218 {
222 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 219 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
223 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 220 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
224 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 221 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
225 222
226 test.done(); 223 test.done();
227 }; 224 };
228 225
229 exports.testInvalidFilters = function(test) 226 exports.testInvalidFilters = function(test)
230 { 227 {
231 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); 228 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
232 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); 229 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]);
233 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]); 230 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]);
234 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]); 231 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
235 232
236 function checkElemHideEmulationFilterInvalid(domains) 233 function checkElemHideEmulationFilterInvalid(domains)
237 { 234 {
238 let filterText = domains + "##[-abp-properties='abc']"; 235 let filterText = domains + "##[-abp-properties='abc']";
239 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_elemhideemulation_nodomain"]); 236 compareFilter(test, filterText,
237 ["type=invalid", "text=" + filterText,
238 "reason=filter_elemhideemulation_nodomain"]);
240 } 239 }
241 checkElemHideEmulationFilterInvalid(""); 240 checkElemHideEmulationFilterInvalid("");
242 checkElemHideEmulationFilterInvalid("~foo.com"); 241 checkElemHideEmulationFilterInvalid("~foo.com");
243 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com"); 242 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
244 checkElemHideEmulationFilterInvalid("foo"); 243 checkElemHideEmulationFilterInvalid("foo");
245 checkElemHideEmulationFilterInvalid("~foo.com,bar"); 244 checkElemHideEmulationFilterInvalid("~foo.com,bar");
246 245
247 test.done(); 246 test.done();
248 }; 247 };
249 248
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]); 341 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]);
343 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 342 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
344 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 343 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
345 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]); 344 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
346 345
347 test.done(); 346 test.done();
348 }; 347 };
349 348
350 exports.testElemHideEmulationFilters = function(test) 349 exports.testElemHideEmulationFilters = function(test)
351 { 350 {
352 let featuresIsPropertySelector = "features=" +
353 ElemHideEmulationFilter.featureMap.PROPERTY_SELECTOR;
354 let featuresIsHasPseudoClass = "features=" +
355 ElemHideEmulationFilter.featureMap.HAS_PSEUDO_CLASS;
356 let featuresArePropertySelectorAndHasPseudoClass = "features=" +
357 (ElemHideEmulationFilter.featureMap.PROPERTY_SELECTOR |
358 ElemHideEmulationFilter.featureMap.HAS_PSEUDO_CLASS);
359
360 // Check valid domain combinations 351 // Check valid domain combinations
361 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM", featuresIsPropertySelector]); 352 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM"]);
362 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain= foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM", featur esIsPropertySelector]); 353 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain= foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM"]);
363 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=elemhideem ulation", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com" , "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR", featuresIsProperty Selector]); 354 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=elemhideem ulation", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com" , "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR"]);
364 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain= bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM", featur esIsPropertySelector]); 355 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain= bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM"]);
365 356
366 compareFilter(test, "##[-abp-properties='']", ["type=invalid", "text=##[-abp-p roperties='']", "reason=filter_elemhideemulation_nodomain"]); 357 compareFilter(test, "##[-abp-properties='']", ["type=invalid", "text=##[-abp-p roperties='']", "reason=filter_elemhideemulation_nodomain"]);
367 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); 358 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]);
368 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=fo o.com", "selector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM", featuresI sPropertySelector]); 359 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=fo o.com", "selector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM"]);
369 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=elemhideemulation", "text=foo.com##[-abp-properties='|background-ima ge: url(data:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|backg round-image: url(data:*)']", "domains=FOO.COM", featuresIsPropertySelector]); 360 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=elemhideemulation", "text=foo.com##[-abp-properties='|background-ima ge: url(data:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|backg round-image: url(data:*)']", "domains=FOO.COM"]);
370
371 // Test feature detection
372 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM", featuresIsPropertySelector]);
373 compareFilter(test, "foo.com##:has(#ddd)", ["type=elemhideemulation", "text=fo o.com##:has(#ddd)", "selectorDomain=foo.com", "selector=:has(#ddd)", "domains=FO O.COM", featuresIsHasPseudoClass]);
374 compareFilter(test, "foo.com##:has([-abp-properties='abc'])", ["type=elemhidee mulation", "text=foo.com##:has([-abp-properties='abc'])", "selectorDomain=foo.co m", "selector=:has([-abp-properties='abc'])", "domains=FOO.COM", featuresAreProp ertySelectorAndHasPseudoClass]);
375 361
376 test.done(); 362 test.done();
377 }; 363 };
378 364
379 exports.testEmptyElemHideDomains = function(test) 365 exports.testEmptyElemHideDomains = function(test)
380 { 366 {
381 let emptyDomainFilters = [ 367 let emptyDomainFilters = [
382 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 368 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
383 ",foo.com##selector", "foo.com,~##selector", 369 ",foo.com##selector", "foo.com,~##selector",
384 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 370 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
385 ]; 371 ];
386 372
387 for (let filterText of emptyDomainFilters) 373 for (let filterText of emptyDomainFilters)
388 { 374 {
389 let filter = Filter.fromText(filterText); 375 let filter = Filter.fromText(filterText);
390 test.ok(filter instanceof InvalidFilter); 376 test.ok(filter instanceof InvalidFilter);
391 test.equal(filter.reason, "filter_invalid_domain"); 377 test.equal(filter.reason, "filter_invalid_domain");
392 } 378 }
393 379
394 test.done(); 380 test.done();
395 }; 381 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld