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

Side by Side Diff: test/filterClasses.js

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Last comment addressed. Created Feb. 13, 2018, 4:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 let {createSandbox} = require("./_common"); 20 let {createSandbox} = require("./_common");
21 const {withNAD} = require("./_test-utils");
21 22
22 let Filter = null; 23 let Filter = null;
23 let InvalidFilter = null; 24 let InvalidFilter = null;
24 let CommentFilter = null; 25 let CommentFilter = null;
25 let ActiveFilter = null; 26 let ActiveFilter = null;
26 let RegExpFilter = null; 27 let RegExpFilter = null;
27 let BlockingFilter = null; 28 let BlockingFilter = null;
28 let WhitelistFilter = null; 29 let WhitelistFilter = null;
29 let ElemHideBase = null; 30 let ElemHideBase = null;
30 let ElemHideFilter = null; 31 let ElemHideFilter = null;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 ["foobar#asdf", BlockingFilter, "blocking", 1], 71 ["foobar#asdf", BlockingFilter, "blocking", 1],
71 ["foobar|foobas##asdf", BlockingFilter, "blocking", 1], 72 ["foobar|foobas##asdf", BlockingFilter, "blocking", 1],
72 ["foobar##asdf{asdf}", ElemHideFilter, "elemhide"], 73 ["foobar##asdf{asdf}", ElemHideFilter, "elemhide"],
73 ["foobar##", BlockingFilter, "blocking", 1], 74 ["foobar##", BlockingFilter, "blocking", 1],
74 ["foobar#@#", BlockingFilter, "blocking", 1], 75 ["foobar#@#", BlockingFilter, "blocking", 1],
75 ["asdf$foobar", InvalidFilter, "invalid"], 76 ["asdf$foobar", InvalidFilter, "invalid"],
76 ["asdf$image,foobar", InvalidFilter, "invalid"], 77 ["asdf$image,foobar", InvalidFilter, "invalid"],
77 ["asdf$image=foobar", BlockingFilter, "blocking", 1], 78 ["asdf$image=foobar", BlockingFilter, "blocking", 1],
78 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking", 0], 79 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking", 0],
79 80
80 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], 81 ["#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid"],
81 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep tion"], 82 ["#@#foo:-abp-properties(something)bar", ElemHideException, "elemhideexcepti on"],
82 ["example.com##foo[-abp-properties='something']bar", ElemHideEmulationFilter , "elemhideemulation"], 83 ["example.com#?#foo:-abp-properties(something)bar", ElemHideEmulationFilter, "elemhideemulation"],
83 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el emhideexception"], 84 ["example.com#@#foo:-abp-properties(something)bar", ElemHideException, "elem hideexception"],
84 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali d"], 85 ["~example.com#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid "],
85 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e lemhideexception"], 86 ["~example.com#@#foo:-abp-properties(something)bar", ElemHideException, "ele mhideexception"],
86 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF ilter, "invalid"], 87 ["~example.com,~example.info#?#foo:-abp-properties(something)bar", InvalidFi lter, "invalid"],
87 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid eException, "elemhideexception"], 88 ["~example.com,~example.info#@#foo:-abp-properties(something)bar", ElemHideE xception, "elemhideexception"],
88 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", ElemHi deEmulationFilter, "elemhideemulation"], 89 ["~sub.example.com,example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"],
89 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"], 90 ["~sub.example.com,example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"],
90 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", ElemHi deEmulationFilter, "elemhideemulation"], 91 ["example.com,~sub.example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"],
91 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"], 92 ["example.com,~sub.example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"],
92 ["example.com##[-abp-properties='something']", ElemHideEmulationFilter, "ele mhideemulation"], 93 ["example.com#?#:-abp-properties(something)", ElemHideEmulationFilter, "elem hideemulation"],
93 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide exception"], 94 ["example.com#@#:-abp-properties(something)", ElemHideException, "elemhideex ception"],
94 ["example.com##[-abp-properties=\"something\"]", ElemHideEmulationFilter, "e lemhideemulation"], 95 ["example.com#?#:-abp-properties((something))", ElemHideEmulationFilter, "el emhideemulation"],
95 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi deexception"], 96 ["example.com#@#:-abp-properties((something))", ElemHideException, "elemhide exception"]
96 ["example.com##[-abp-properties=(something)]", ElemHideEmulationFilter, "ele mhideemulation"],
97 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide exception"]
98 ]; 97 ];
99 for (let [text, type, typeName, collapse] of tests) 98 for (let [text, type, typeName, collapse] of tests)
100 { 99 {
101 let filter = Filter.fromText(text); 100 withNAD(0, filter =>
102 test.ok(filter instanceof Filter, "Got filter for " + text); 101 {
103 test.equal(filter.text, text, "Correct filter text for " + text); 102 test.ok(filter instanceof Filter, "Got filter for " + text);
104 test.ok(filter instanceof type, "Correct filter type for " + text); 103 test.equal(filter.text, text, "Correct filter text for " + text);
105 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam e); 104 test.ok(filter instanceof type, "Correct filter type for " + text);
106 if (filter instanceof BlockingFilter) 105 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeN ame);
107 test.equal(filter.collapse, collapse); 106 if (filter instanceof BlockingFilter)
108 else 107 test.equal(filter.collapse, collapse);
109 test.equal(filter.collapse, undefined); 108 else
110 if (type == InvalidFilter) 109 test.equal(filter.collapse, undefined);
111 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); 110 if (type == InvalidFilter)
112 filter.delete(); 111 test.ok(filter.reason, "Invalid filter " + text + " has a reason set");
112 })(Filter.fromText(text));
113 } 113 }
114
115 // conversion from old syntax.
116 withNAD(0, filter =>
117 {
118 test.equal(filter.text, "example.com#?#:-abp-properties(something)");
119 test.ok(filter instanceof ElemHideEmulationFilter);
120 test.equal(filter.type, "elemhideemulation");
121 })(Filter.fromText("example.com##[-abp-properties='something']"));
122
114 test.done(); 123 test.done();
115 }; 124 };
116 125
117 exports.testClassHierarchy = function(test) 126 exports.testClassHierarchy = function(test)
118 { 127 {
119 let allClasses = [ 128 let allClasses = [
120 Filter, InvalidFilter, CommentFilter, ActiveFilter, 129 Filter, InvalidFilter, CommentFilter, ActiveFilter,
121 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase, 130 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase,
122 ElemHideFilter, ElemHideException, ElemHideEmulationFilter 131 ElemHideFilter, ElemHideException, ElemHideEmulationFilter
123 ]; 132 ];
124 let tests = [ 133 let tests = [
125 ["/asdf??+/", Filter, InvalidFilter], 134 ["/asdf??+/", Filter, InvalidFilter],
126 ["!asdf", Filter, CommentFilter], 135 ["!asdf", Filter, CommentFilter],
127 ["asdf", Filter, ActiveFilter, RegExpFilter, BlockingFilter], 136 ["asdf", Filter, ActiveFilter, RegExpFilter, BlockingFilter],
128 ["@@asdf", Filter, ActiveFilter, RegExpFilter, WhitelistFilter], 137 ["@@asdf", Filter, ActiveFilter, RegExpFilter, WhitelistFilter],
129 ["##asdf", Filter, ActiveFilter, ElemHideBase, ElemHideFilter], 138 ["##asdf", Filter, ActiveFilter, ElemHideBase, ElemHideFilter],
130 ["#@#asdf", Filter, ActiveFilter, ElemHideBase, ElemHideException], 139 ["#@#asdf", Filter, ActiveFilter, ElemHideBase, ElemHideException],
131 ["example.com##[-abp-properties='something']", Filter, ActiveFilter, ElemHid eBase, ElemHideEmulationFilter] 140 ["example.com#?#:-abp-properties(something)", Filter, ActiveFilter, ElemHide Base, ElemHideEmulationFilter]
132 ]; 141 ];
133 142
134 for (let list of tests) 143 for (let list of tests)
135 { 144 {
136 let filter = Filter.fromText(list.shift()); 145 let filter = Filter.fromText(list.shift());
137 for (let cls of list) 146 for (let cls of list)
138 { 147 {
139 test.ok(filter instanceof cls, 148 test.ok(filter instanceof cls,
140 "Testing correct superclass for filter " + filter.text); 149 "Testing correct superclass for filter " + filter.text);
141 } 150 }
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 filter.delete(); 279 filter.delete();
271 280
272 test.done(); 281 test.done();
273 }; 282 };
274 283
275 exports.testInvalidReasons = function(test) 284 exports.testInvalidReasons = function(test)
276 { 285 {
277 let tests = [ 286 let tests = [
278 ["/??/", "filter_invalid_regexp"], 287 ["/??/", "filter_invalid_regexp"],
279 ["asd$foobar", "filter_unknown_option"], 288 ["asd$foobar", "filter_unknown_option"],
280 ["~foo.com##[-abp-properties='abc']", "filter_elemhideemulation_nodomain"] 289 ["~foo.com#?#:-abp-properties(abc)", "filter_elemhideemulation_nodomain"]
281 ]; 290 ];
282 291
283 for (let [text, reason] of tests) 292 for (let [text, reason] of tests)
284 { 293 {
285 let filter = Filter.fromText(text); 294 let filter = Filter.fromText(text);
286 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali d"); 295 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali d");
287 filter.delete(); 296 filter.delete();
288 } 297 }
289 298
290 test.done(); 299 test.done();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
365 filter.delete(); 374 filter.delete();
366 } 375 }
367 376
368 let tests = [ 377 let tests = [
369 ["##foobar", "foobar", ""], 378 ["##foobar", "foobar", ""],
370 ["~example.com##foobar", "foobar", ""], 379 ["~example.com##foobar", "foobar", ""],
371 ["example.com##body > div:first-child", "body > div:first-child", "example.c om"], 380 ["example.com##body > div:first-child", "body > div:first-child", "example.c om"],
372 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)", "xyz"], 381 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)", "xyz"],
373 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], 382 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"],
374 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], 383 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"],
375 ["foo.com##x[-abp-properties='abc']y", "x[-abp-properties='abc']y", "foo.com "], 384 ["foo.com##x:-abp-properties(abc)y", "x:-abp-properties(abc)y", "foo.com"],
376 385
377 // Good idea to test this? Maybe consider behavior undefined in this case. 386 // Good idea to test this? Maybe consider behavior undefined in this case.
378 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"] 387 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"]
379 ]; 388 ];
380 389
381 for (let [text, selector, selectorDomain] of tests) 390 for (let [text, selector, selectorDomain] of tests)
382 { 391 {
383 doTest(text, selector, selectorDomain); 392 doTest(text, selector, selectorDomain);
384 doTest(text.replace("##", "#@#"), selector, selectorDomain); 393 doTest(text.replace("##", "#@#"), selector, selectorDomain);
385 } 394 }
386 395
387 test.done(); 396 test.done();
388 }; 397 };
389 398
390 exports.testElemHideRulesWithBraces = function(test) 399 exports.testElemHideRulesWithBraces = function(test)
391 { 400 {
392 let filter = Filter.fromText("###foo{color: red}"); 401 withNAD(0, filter =>
393 test.equal(filter.type, "elemhide"); 402 {
394 test.equal(filter.selector, "#foo\\7B color: red\\7D "); 403 test.equal(filter.type, "elemhide");
395 filter.delete(); 404 test.equal(filter.selector, "#foo\\7B color: red\\7D ");
405 })(Filter.fromText("###foo{color: red}"));
396 406
397 filter = Filter.fromText("foo.com##[-abp-properties='/margin: [3-4]{2}/']"); 407 // Filter conversion to the new syntax dealing with braces too.
398 test.equal(filter.type, "elemhideemulation"); 408 withNAD(0, filter =>
399 test.equal(filter.selector, "[-abp-properties='/margin: [3-4]\\7B 2\\7D /']"); 409 {
400 filter.delete(); 410 test.equal(filter.type, "elemhideemulation");
411 test.equal(filter.selector, ":-abp-properties(/margin: [3-4]\\7B 2\\7D /)");
412 })(Filter.fromText("foo.com##[-abp-properties='/margin: [3-4]{2}/']"));
401 413
402 test.done(); 414 test.done();
403 }; 415 };
404 416
405 exports.testNotifications = function(test) 417 exports.testNotifications = function(test)
406 { 418 {
407 function checkNotifications(action, expected, message) 419 function checkNotifications(action, expected, message)
408 { 420 {
409 let result = null; 421 let result = null;
410 let listener = (topic, filter) => 422 let listener = (topic, filter) =>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 filter.hitCount++; 464 filter.hitCount++;
453 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts"); 465 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts");
454 checkNotifications(() => 466 checkNotifications(() =>
455 { 467 {
456 filter.hitCount = 0; 468 filter.hitCount = 0;
457 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts"); 469 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts");
458 470
459 filter.delete(); 471 filter.delete();
460 test.done(); 472 test.done();
461 }; 473 };
OLDNEW

Powered by Google App Engine
This is Rietveld