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

Delta Between Two Patch Sets: test/filterClasses.js

Issue 29595633: Issue 5870 - Implement the new ElemHideEmulation filter type (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Nov. 2, 2017, 11:42 p.m.
Right Patch Set: Deal with ill formed filters. Created Feb. 14, 2018, 5:05 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
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-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 10 matching lines...) Expand all
41 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
42 ElemHideException, ElemHideEmulationFilter 43 ElemHideException, ElemHideEmulationFilter
43 } = sandboxedRequire("../lib/filterClasses") 44 } = sandboxedRequire("../lib/filterClasses")
44 ); 45 );
45 ({FilterNotifier} = sandboxedRequire("../lib/filterNotifier")); 46 ({FilterNotifier} = sandboxedRequire("../lib/filterNotifier"));
46 callback(); 47 callback();
47 }; 48 };
48 49
49 exports.testFromText = function(test) 50 exports.testFromText = function(test)
50 { 51 {
52 // fields are:
53 // text, type, typeName, collapse (optional, BlockingFilter only)
51 let tests = [ 54 let tests = [
52 ["!asdf", CommentFilter, "comment"], 55 ["!asdf", CommentFilter, "comment"],
53 ["asdf", BlockingFilter, "blocking"], 56 ["asdf", BlockingFilter, "blocking", 1],
54 ["asdf$image,~collapse", BlockingFilter, "blocking"], 57 ["asdf$image,~collapse", BlockingFilter, "blocking", 0],
55 ["/asdf/", BlockingFilter, "blocking"], 58 ["/asdf/", BlockingFilter, "blocking", 1],
56 ["/asdf??+/", InvalidFilter, "invalid"], 59 ["/asdf??+/", InvalidFilter, "invalid"],
57 ["@@asdf", WhitelistFilter, "whitelist"], 60 ["@@asdf", WhitelistFilter, "whitelist"],
58 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], 61 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"],
59 ["@@/asdf/", WhitelistFilter, "whitelist"], 62 ["@@/asdf/", WhitelistFilter, "whitelist"],
60 ["@@/asdf??+/", InvalidFilter, "invalid"], 63 ["@@/asdf??+/", InvalidFilter, "invalid"],
61 ["##asdf", ElemHideFilter, "elemhide"], 64 ["##asdf", ElemHideFilter, "elemhide"],
62 ["#@#asdf", ElemHideException, "elemhideexception"], 65 ["#@#asdf", ElemHideException, "elemhideexception"],
63 ["foobar##asdf", ElemHideFilter, "elemhide"], 66 ["foobar##asdf", ElemHideFilter, "elemhide"],
64 ["foobar#@#asdf", ElemHideException, "elemhideexception"], 67 ["foobar#@#asdf", ElemHideException, "elemhideexception"],
65 ["foobar##a", ElemHideFilter, "elemhide"], 68 ["foobar##a", ElemHideFilter, "elemhide"],
66 ["foobar#@#a", ElemHideException, "elemhideexception"], 69 ["foobar#@#a", ElemHideException, "elemhideexception"],
67 70
68 ["foobar#asdf", BlockingFilter, "blocking"], 71 ["foobar#asdf", BlockingFilter, "blocking", 1],
69 ["foobar|foobas##asdf", BlockingFilter, "blocking"], 72 ["foobar|foobas##asdf", BlockingFilter, "blocking", 1],
70 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], 73 ["foobar##asdf{asdf}", ElemHideFilter, "elemhide"],
71 ["foobar##", BlockingFilter, "blocking"], 74 ["foobar##", BlockingFilter, "blocking", 1],
72 ["foobar#@#", BlockingFilter, "blocking"], 75 ["foobar#@#", BlockingFilter, "blocking", 1],
73 ["asdf$foobar", InvalidFilter, "invalid"], 76 ["asdf$foobar", InvalidFilter, "invalid"],
74 ["asdf$image,foobar", InvalidFilter, "invalid"], 77 ["asdf$image,foobar", InvalidFilter, "invalid"],
75 ["asdf$image=foobar", BlockingFilter, "blocking"], 78 ["asdf$image=foobar", BlockingFilter, "blocking", 1],
76 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], 79 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking", 0],
77 80
78 ["#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid"], 81 ["#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid"],
79 ["#@#foo:-abp-properties(something)bar", ElemHideException, "elemhideexcepti on"], 82 ["#@#foo:-abp-properties(something)bar", ElemHideException, "elemhideexcepti on"],
80 ["example.com#?#foo:-abp-properties(something)bar", ElemHideEmulationFilter, "elemhideemulation"], 83 ["example.com#?#foo:-abp-properties(something)bar", ElemHideEmulationFilter, "elemhideemulation"],
81 ["example.com#@#foo:-abp-properties(something)bar", ElemHideException, "elem hideexception"], 84 ["example.com#@#foo:-abp-properties(something)bar", ElemHideException, "elem hideexception"],
82 ["~example.com#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid "], 85 ["~example.com#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid "],
83 ["~example.com#@#foo:-abp-properties(something)bar", ElemHideException, "ele mhideexception"], 86 ["~example.com#@#foo:-abp-properties(something)bar", ElemHideException, "ele mhideexception"],
84 ["~example.com,~example.info#?#foo:-abp-properties(something)bar", InvalidFi lter, "invalid"], 87 ["~example.com,~example.info#?#foo:-abp-properties(something)bar", InvalidFi lter, "invalid"],
85 ["~example.com,~example.info#@#foo:-abp-properties(something)bar", ElemHideE xception, "elemhideexception"], 88 ["~example.com,~example.info#@#foo:-abp-properties(something)bar", ElemHideE xception, "elemhideexception"],
86 ["~sub.example.com,example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"], 89 ["~sub.example.com,example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"],
87 ["~sub.example.com,example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"], 90 ["~sub.example.com,example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"],
88 ["example.com,~sub.example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"], 91 ["example.com,~sub.example.com#?#foo:-abp-properties(something)bar", ElemHid eEmulationFilter, "elemhideemulation"],
89 ["example.com,~sub.example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"], 92 ["example.com,~sub.example.com#@#foo:-abp-properties(something)bar", ElemHid eException, "elemhideexception"],
90 ["example.com#?#:-abp-properties(something)", ElemHideEmulationFilter, "elem hideemulation"], 93 ["example.com#?#:-abp-properties(something)", ElemHideEmulationFilter, "elem hideemulation"],
91 ["example.com#@#:-abp-properties(something)", ElemHideException, "elemhideex ception"], 94 ["example.com#@#:-abp-properties(something)", ElemHideException, "elemhideex ception"],
92 ["example.com#?#:-abp-properties((something))", ElemHideEmulationFilter, "el emhideemulation"], 95 ["example.com#?#:-abp-properties((something))", ElemHideEmulationFilter, "el emhideemulation"],
93 ["example.com#@#:-abp-properties((something))", ElemHideException, "elemhide exception"] 96 ["example.com#@#:-abp-properties((something))", ElemHideException, "elemhide exception"]
94 ]; 97 ];
95 for (let [text, type, typeName] of tests) 98 for (let [text, type, typeName, collapse] of tests)
96 { 99 {
97 let filter = Filter.fromText(text); 100 withNAD(0, filter =>
98 test.ok(filter instanceof Filter, "Got filter for " + text); 101 {
99 test.equal(filter.text, text, "Correct filter text for " + text); 102 test.ok(filter instanceof Filter, "Got filter for " + text);
100 test.ok(filter instanceof type, "Correct filter type for " + text); 103 test.equal(filter.text, text, "Correct filter text for " + text);
101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam e); 104 test.ok(filter instanceof type, "Correct filter type for " + text);
102 if (type == InvalidFilter) 105 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeN ame);
103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); 106 if (filter instanceof BlockingFilter)
104 filter.delete(); 107 test.equal(filter.collapse, collapse);
105 } 108 else
109 test.equal(filter.collapse, undefined);
110 if (type == InvalidFilter)
111 test.ok(filter.reason, "Invalid filter " + text + " has a reason set");
112 })(Filter.fromText(text));
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
106 test.done(); 123 test.done();
107 }; 124 };
108 125
109 exports.testClassHierarchy = function(test) 126 exports.testClassHierarchy = function(test)
110 { 127 {
111 let allClasses = [ 128 let allClasses = [
112 Filter, InvalidFilter, CommentFilter, ActiveFilter, 129 Filter, InvalidFilter, CommentFilter, ActiveFilter,
113 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase, 130 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase,
114 ElemHideFilter, ElemHideException, ElemHideEmulationFilter 131 ElemHideFilter, ElemHideException, ElemHideEmulationFilter
115 ]; 132 ];
116 let tests = [ 133 let tests = [
117 ["/asdf??+/", Filter, InvalidFilter], 134 ["/asdf??+/", Filter, InvalidFilter],
118 ["!asdf", Filter, CommentFilter], 135 ["!asdf", Filter, CommentFilter],
119 ["asdf", Filter, ActiveFilter, RegExpFilter, BlockingFilter], 136 ["asdf", Filter, ActiveFilter, RegExpFilter, BlockingFilter],
120 ["@@asdf", Filter, ActiveFilter, RegExpFilter, WhitelistFilter], 137 ["@@asdf", Filter, ActiveFilter, RegExpFilter, WhitelistFilter],
121 ["##asdf", Filter, ActiveFilter, ElemHideBase, ElemHideFilter], 138 ["##asdf", Filter, ActiveFilter, ElemHideBase, ElemHideFilter],
122 ["#@#asdf", Filter, ActiveFilter, ElemHideBase, ElemHideException], 139 ["#@#asdf", Filter, ActiveFilter, ElemHideBase, ElemHideException],
123 ["example.com#?#[-abp-properties='something']", Filter, ActiveFilter, ElemHi deBase, ElemHideEmulationFilter] 140 ["example.com#?#:-abp-properties(something)", Filter, ActiveFilter, ElemHide Base, ElemHideEmulationFilter]
124 ]; 141 ];
125 142
126 for (let list of tests) 143 for (let list of tests)
127 { 144 {
128 let filter = Filter.fromText(list.shift()); 145 let filter = Filter.fromText(list.shift());
129 for (let cls of list) 146 for (let cls of list)
130 { 147 {
131 test.ok(filter instanceof cls, 148 test.ok(filter instanceof cls,
132 "Testing correct superclass for filter " + filter.text); 149 "Testing correct superclass for filter " + filter.text);
133 } 150 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 } 243 }
227 244
228 test.done(); 245 test.done();
229 }; 246 };
230 247
231 exports.testSerialize = function(test) 248 exports.testSerialize = function(test)
232 { 249 {
233 // Comment 250 // Comment
234 let filter = Filter.fromText("! serialize"); 251 let filter = Filter.fromText("! serialize");
235 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); 252 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n");
253 test.equal(filter.collapse, undefined);
236 filter.delete(); 254 filter.delete();
237 255
238 // Blocking filter 256 // Blocking filter
239 filter = Filter.fromText("serialize"); 257 filter = Filter.fromText("serialize");
240 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); 258 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n");
241 filter.disabled = true; 259 filter.disabled = true;
242 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); 260 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n");
243 filter.disabled = false; 261 filter.disabled = false;
244 filter.hitCount = 10; 262 filter.hitCount = 10;
245 filter.lastHit = 12; 263 filter.lastHit = 12;
246 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit =12\n"); 264 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit =12\n");
265 test.ok(filter.collapse);
247 filter.delete(); 266 filter.delete();
248 267
249 // Invalid filter 268 // Invalid filter
250 filter = Filter.fromText("serialize$foobar"); 269 filter = Filter.fromText("serialize$foobar");
251 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); 270 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n");
252 filter.delete(); 271 filter.delete();
253 272
254 // Element hiding filter 273 // Element hiding filter
255 filter = Filter.fromText("example.com##serialize"); 274 filter = Filter.fromText("example.com##serialize");
256 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); 275 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n");
257 filter.disabled = true; 276 filter.disabled = true;
258 filter.lastHit = 5; 277 filter.lastHit = 5;
259 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable d=true\nlastHit=5\n"); 278 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable d=true\nlastHit=5\n");
260 filter.delete(); 279 filter.delete();
261 280
262 test.done(); 281 test.done();
263 }; 282 };
264 283
265 exports.testInvalidReasons = function(test) 284 exports.testInvalidReasons = function(test)
266 { 285 {
267 let tests = [ 286 let tests = [
268 ["/??/", "filter_invalid_regexp"], 287 ["/??/", "filter_invalid_regexp"],
269 ["asd$foobar", "filter_unknown_option"], 288 ["asd$foobar", "filter_unknown_option"],
270 ["~foo.com#?#[-abp-properties='abc']", "filter_elemhideemulation_nodomain"] 289 ["~foo.com#?#:-abp-properties(abc)", "filter_elemhideemulation_nodomain"]
271 ]; 290 ];
272 291
273 for (let [text, reason] of tests) 292 for (let [text, reason] of tests)
274 { 293 {
275 let filter = Filter.fromText(text); 294 let filter = Filter.fromText(text);
276 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");
277 filter.delete(); 296 filter.delete();
278 } 297 }
279 298
280 test.done(); 299 test.done();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 filter.delete(); 374 filter.delete();
356 } 375 }
357 376
358 let tests = [ 377 let tests = [
359 ["##foobar", "foobar", ""], 378 ["##foobar", "foobar", ""],
360 ["~example.com##foobar", "foobar", ""], 379 ["~example.com##foobar", "foobar", ""],
361 ["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"],
362 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)", "xyz"], 381 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)", "xyz"],
363 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], 382 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"],
364 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], 383 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"],
365 ["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"],
366 385
367 // 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.
368 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"] 387 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"]
369 ]; 388 ];
370 389
371 for (let [text, selector, selectorDomain] of tests) 390 for (let [text, selector, selectorDomain] of tests)
372 { 391 {
373 doTest(text, selector, selectorDomain); 392 doTest(text, selector, selectorDomain);
374 doTest(text.replace("##", "#@#"), selector, selectorDomain); 393 doTest(text.replace("##", "#@#"), selector, selectorDomain);
375 } 394 }
395
396 test.done();
397 };
398
399 exports.testElemHideRulesWithBraces = function(test)
400 {
401 withNAD(0, filter =>
402 {
403 test.equal(filter.type, "elemhide");
404 test.equal(filter.selector, "#foo\\7B color: red\\7D ");
405 })(Filter.fromText("###foo{color: red}"));
406
407 // Filter conversion to the new syntax dealing with braces too.
408 withNAD(0, filter =>
409 {
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}/']"));
376 413
377 test.done(); 414 test.done();
378 }; 415 };
379 416
380 exports.testNotifications = function(test) 417 exports.testNotifications = function(test)
381 { 418 {
382 function checkNotifications(action, expected, message) 419 function checkNotifications(action, expected, message)
383 { 420 {
384 let result = null; 421 let result = null;
385 let listener = (topic, filter) => 422 let listener = (topic, filter) =>
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
427 filter.hitCount++; 464 filter.hitCount++;
428 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts"); 465 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts");
429 checkNotifications(() => 466 checkNotifications(() =>
430 { 467 {
431 filter.hitCount = 0; 468 filter.hitCount = 0;
432 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts"); 469 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts");
433 470
434 filter.delete(); 471 filter.delete();
435 test.done(); 472 test.done();
436 }; 473 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld