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

Side by Side Diff: test/filterClasses.js

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Updated unit test framework to the current state of the repository Created Nov. 24, 2016, 3:40 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
« no previous file with comments | « test/domainRestrictions.js ('k') | test/regexpFilters_matching.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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-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
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 { 20 let {createSandbox} = require("./_common");
21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 21
22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 22 let Filter = null;
23 ElemHideException, CSSPropertyFilter 23 let InvalidFilter = null;
24 } = require("../lib/filterClasses"); 24 let CommentFilter = null;
25 25 let ActiveFilter = null;
26 function serializeFilter(filter) 26 let RegExpFilter = null;
27 { 27 let BlockingFilter = null;
28 // Filter serialization only writes out essential properties, need to do a ful l serialization here 28 let WhitelistFilter = null;
29 let result = []; 29 let ElemHideBase = null;
30 result.push("text=" + filter.text); 30 let ElemHideFilter = null;
31 if (filter instanceof InvalidFilter) 31 let ElemHideException = null;
32 { 32 let CSSPropertyFilter = null;
33 result.push("type=invalid"); 33
34 result.push("reason=" + filter.reason); 34 exports.setUp = function(callback)
35 } 35 {
36 else if (filter instanceof CommentFilter) 36 let sandboxedRequire = createSandbox();
37 { 37 (
38 result.push("type=comment"); 38 {
39 } 39 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
40 else if (filter instanceof ActiveFilter) 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
41 { 41 ElemHideException, CSSPropertyFilter
42 result.push("disabled=" + filter.disabled); 42 } = sandboxedRequire("../lib/filterClassesNew")
43 result.push("lastHit=" + filter.lastHit); 43 );
44 result.push("hitCount=" + filter.hitCount); 44 callback();
45 45 };
46 let domains = []; 46
47 if (filter.domains) 47 exports.testFromText = function(test)
48 { 48 {
49 for (let domain in filter.domains) 49 let tests = [
50 if (domain != "") 50 ["!asdf", CommentFilter, "comment"],
51 domains.push(filter.domains[domain] ? domain : "~" + domain); 51 ["asdf", BlockingFilter, "blocking"],
52 ["asdf$image,~collapse", BlockingFilter, "blocking"],
53 ["/asdf/", BlockingFilter, "blocking"],
54 ["/asdf??+/", InvalidFilter, "invalid"],
55 ["@@asdf", WhitelistFilter, "whitelist"],
56 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"],
57 ["@@/asdf/", WhitelistFilter, "whitelist"],
58 ["@@/asdf??+/", InvalidFilter, "invalid"],
59 ["##asdf", ElemHideFilter, "elemhide"],
60 ["#@#asdf", ElemHideException, "elemhideexception"],
61 ["foobar##asdf", ElemHideFilter, "elemhide"],
62 ["foobar#@#asdf", ElemHideException, "elemhideexception"],
63 ["foobar##a", ElemHideFilter, "elemhide"],
64 ["foobar#@#a", ElemHideException, "elemhideexception"],
65
66 ["foobar#asdf", BlockingFilter, "blocking"],
67 ["foobar|foobas##asdf", BlockingFilter, "blocking"],
68 ["foobar##asdf{asdf}", BlockingFilter, "blocking"],
69 ["foobar##", BlockingFilter, "blocking"],
70 ["foobar#@#", BlockingFilter, "blocking"],
71 ["asdf$foobar", InvalidFilter, "invalid"],
72 ["asdf$image,foobar", InvalidFilter, "invalid"],
73 ["asdf$image=foobar", BlockingFilter, "blocking"],
74 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"],
75
76 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"],
77 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep tion"],
78 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css property"],
79 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el emhideexception"],
80 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali d"],
81 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e lemhideexception"],
82 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF ilter, "invalid"],
83 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid eException, "elemhideexception"],
84 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro pertyFilter, "cssproperty"],
85 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"],
86 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", CSSPro pertyFilter, "cssproperty"],
87 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"],
88 ["example.com##[-abp-properties='something']", CSSPropertyFilter, "cssproper ty"],
89 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide exception"],
90 ["example.com##[-abp-properties=\"something\"]", CSSPropertyFilter, "cssprop erty"],
91 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi deexception"],
92 ["example.com##[-abp-properties=(something)]", ElemHideFilter, "elemhide"],
93 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide exception"],
94 ];
95 for (let [text, type, typeName, location] of tests)
96 {
97 let filter = Filter.fromText(text);
98 test.ok(filter instanceof Filter, "Got filter for " + text);
99 test.equal(filter.text, text, "Correct filter text for " + text);
100 test.ok(filter instanceof type, "Correct filter type for " + text);
101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam e);
102 if (type == InvalidFilter)
103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set");
104 filter.delete();
105 }
106 test.done();
107 };
108
109 exports.testClassHierarchy = function(test)
110 {
111 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter",
112 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase",
113 "ElemHideFilter", "ElemHideException", "CSSPropertyFilter"];
114 let tests = [
115 ["/asdf??+/", "Filter", "InvalidFilter"],
116 ["!asdf", "Filter", "CommentFilter"],
117 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"],
118 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"],
119 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"],
120 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"],
121 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El emHideBase", "CSSPropertyFilter"],
122 ];
123
124 for (let list of tests)
125 {
126 let filter = Filter.fromText(list.shift());
127 for (let cls of list)
128 {
129 test.ok(filter instanceof eval(cls),
130 "Filter " + filter.text + " is an instance of " + cls);
52 } 131 }
53 result.push("domains=" + domains.sort().join("|")); 132
54 133 for (let cls of allClasses)
55 if (filter instanceof RegExpFilter) 134 {
56 { 135 if (list.indexOf(cls) < 0)
57 result.push("regexp=" + filter.regexp.source);
58 result.push("contentType=" + filter.contentType);
59 result.push("matchCase=" + filter.matchCase);
60
61 let sitekeys = filter.sitekeys || [];
62 result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
63
64 result.push("thirdParty=" + filter.thirdParty);
65 if (filter instanceof BlockingFilter)
66 { 136 {
67 result.push("type=filterlist"); 137 test.ok(!(filter instanceof eval(cls)),
68 result.push("collapse=" + filter.collapse); 138 "Filter " + filter.text + " isn't an instance of " + cls);
69 }
70 else if (filter instanceof WhitelistFilter)
71 {
72 result.push("type=whitelist");
73 } 139 }
74 } 140 }
75 else if (filter instanceof ElemHideBase) 141 filter.delete();
76 { 142 }
77 if (filter instanceof ElemHideFilter) 143
78 result.push("type=elemhide"); 144 test.done();
79 else if (filter instanceof ElemHideException) 145 };
80 result.push("type=elemhideexception"); 146
81 else if (filter instanceof CSSPropertyFilter) 147 exports.testGetKnownFilter = function(test)
82 { 148 {
83 result.push("type=cssrule"); 149 let filter1 = Filter.getKnownFilter("someknownfilter");
84 result.push("prefix=" + (filter.selectorPrefix || "")); 150 test.ok(!filter1, "Unknown filter returns null");
85 result.push("regexp=" + filter.regexpString); 151
86 result.push("suffix=" + (filter.selectorSuffix || "")); 152 let filter2 = Filter.fromText("someknownfilter");
87 } 153 filter1 = Filter.getKnownFilter("someknownfilter");
88 154 test.ok(filter1, "Known filter returned");
89 result.push("selectorDomain=" + (filter.selectorDomain || "")); 155
90 result.push("selector=" + filter.selector); 156 filter2.hitCount = 432;
157 test.equal(filter1.hitCount, 432, "Changes on previous instance are reflected on new instance");
158
159 filter1.delete();
160 filter2.delete();
161
162 test.done();
163 };
164
165 exports.testNormalize = function(test)
166 {
167 let tests = [
168 [" ! comment something ", "! comment something"],
169 [" ! \n comment something ", "! comment something"],
170 [" foo bar ", "foobar"],
171 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"] ,
172 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b ar"],
173 ["foOBar"],
174 ["foOBar#xyz"],
175 ["foOBar$iMaGe,object_subrequest,~coLLapse", "foOBar$image,object-subrequest ,~collapse"],
176 ["foOBar$doMain=EXample.COM|~exAMPLE.РФ", "foOBar$domain=example.com|~exampl e.рф"],
177 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"],
178 ["exampLE.com##fooBAr", "example.com##fooBAr"],
179 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"],
180 ["exampLE.РФ#@#fooBAr", "example.рф#@#fooBAr"],
181 ];
182
183 for (let [text, expected, selectorDomain, selector] of tests)
184 {
185 if (!expected)
186 expected = text;
187
188 let filter1 = Filter.fromText(text);
189 let filter2 = Filter.fromText(expected);
190
191 test.equal(filter1.text, expected, "Filter text " + text + " got normalized" );
192 test.equal(filter2.text, expected, "Already normalized text " + expected + " didn't change");
193
194 if (filter1 instanceof ActiveFilter)
195 {
196 filter1.hitCount = 567;
197 test.equal(filter1.hitCount, filter2.hitCount, "Property changes on filter " + text + " get reflected on filter " + expected);
91 } 198 }
92 } 199
93 return result; 200 if (selectorDomain)
201 {
202 let expectedDomains = selectorDomain.split(",").sort().join(",");
203 let actualDomains1 = filter1.selectorDomain.split(",").sort().join(",");
204 let actualDomains2 = filter2.selectorDomain.split(",").sort().join(",");
205 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f ilter " + text);
206 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f ilter " + expected);
207
208 test.equal(filter1.selector, selector, "Correct selector for filter " + te xt);
209 test.equal(filter2.selector, selector, "Correct selector for filter " + ex pected);
210 }
211
212 filter1.delete();
213 filter2.delete();
214 }
215
216 test.done();
217 };
218
219 exports.testSerialize = function(test)
220 {
221 // Comment
222 let filter = Filter.fromText("! serialize");
223 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n");
224 filter.delete();
225
226 // Blocking filter
227 filter = Filter.fromText("serialize");
228 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n");
229 filter.disabled = true;
230 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n");
231 filter.disabled = false;
232 filter.hitCount = 10;
233 filter.lastHit = 12;
234 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit =12\n");
235 filter.delete();
236
237 // Invalid filter
238 filter = Filter.fromText("serialize$foobar");
239 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n");
240 filter.delete();
241
242 // Element hiding filter
243 filter = Filter.fromText("example.com##serialize");
244 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n");
245 filter.disabled = true;
246 filter.lastHit = 5;
247 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable d=true\nlastHit=5\n");
248 filter.delete();
249
250 test.done();
251 };
252
253 exports.testInvalidReasons = function(test)
254 {
255 let tests = [
256 ["/??/", "filter_invalid_regexp"],
257 ["asd$foobar", "filter_unknown_option"],
258 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"],
259 ];
260
261 for (let [text, reason] of tests)
262 {
263 let filter = Filter.fromText(text);
264 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali d");
265 filter.delete();
266 }
267
268 test.done();
269 };
270
271 exports.testActiveFilter = function(test)
272 {
273 let filter1 = Filter.fromText("asdf");
274 let filter1copy = Filter.fromText("asdf");
275 let filter2 = Filter.fromText("##foobar");
276
277 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt ers are initially enabled");
278 filter1.disabled = true;
279 test.ok(filter1.disabled, "Disabling filter works");
280 test.ok(filter1copy.disabled, "Filter copies are also disabled");
281 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others");
282
283 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou nt === 0, "Filters have no hit initially");
284 filter1.hitCount = 5;
285 test.equal(filter1.hitCount, 5, "Changing hit count works");
286 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change d");
287 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected");
288
289 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit === 0, "Filters have no last hit time initially");
290 filter1.lastHit = 10;
291 test.equal(filter1.lastHit, 10, "Changing last hit time works");
292 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch anged");
293 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected" );
294
295 filter1.delete();
296 filter1copy.delete();
297 filter2.delete();
298
299 test.done();
300 };
301
302 exports.testIsGeneric = function(test)
303 {
304 let tests = [
305 ["asfd", true],
306 ["|http://example.com/asdf", true],
307 ["||example.com/asdf", true],
308 ["asfd$third-party", true],
309 ["asdf$domain=com", false],
310 ["asdf$domain=example.com", false],
311 ["asdf$image,domain=example.com", false],
312 ["asdf$~image,domain=example.com", false],
313 ["asdf$third-party,domain=example.com", false],
314 ["||example.com/asdf$~coLLapse,domain=example.com", false],
315 ["||example.com/asdf$domain=~example.com", true],
316 ["||example.com/asdf$third-party,domain=~example.com", true],
317 ["asdf$domain=foo.example.com|~example.com", false],
318 ["asdf$domain=foo.com|~example.com", false],
319 ["asdf$domain=~foo.com|~example.com", true],
320 ];
321
322 for (let [text, generic] of tests)
323 {
324 let filter = Filter.fromText(text);
325 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic");
326 filter.delete();
327 }
328
329 test.done();
94 } 330 }
95 331
96 function addDefaults(expected) 332 exports.testElemHideSelector = function(test)
97 { 333 {
98 let type = null; 334 function doTest(text, selector, selectorDomain)
99 let hasProperty = {}; 335 {
100 for (let entry of expected) 336 let filter = Filter.fromText(text);
101 { 337 test.equal(filter.selector, selector, "Correct selector for " + text);
102 if (/^type=(.*)/.test(entry)) 338
103 type = RegExp.$1; 339 let actualDomains = filter.selectorDomain.split(",").sort().join(",");
104 else if (/^(\w+)/.test(entry)) 340 let expectedDomains = selectorDomain.split(",").sort().join(",");
105 hasProperty[RegExp.$1] = true; 341 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex t);
106 } 342
107 343 filter.delete();
108 function addProperty(prop, value) 344 }
109 { 345
110 if (!(prop in hasProperty)) 346 let tests = [
111 expected.push(prop + "=" + value); 347 ["##foobar", "foobar", ""],
112 } 348 ["~example.com##foobar", "foobar", ""],
113 349 ["example.com##body > div:first-child", "body > div:first-child", "example.c om"],
114 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule") 350 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"],
115 { 351 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"],
116 addProperty("disabled", "false"); 352 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"],
117 addProperty("lastHit", "0"); 353
118 addProperty("hitCount", "0"); 354 // Good idea to test this? Maybe consider behavior undefined in this case.
119 } 355 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"],
120 if (type == "whitelist" || type == "filterlist") 356 ];
121 { 357
122 addProperty("contentType", 0x7FFFFFFF & ~( 358 for (let [text, selector, selectorDomain] of tests)
123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 359 {
124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 360 doTest(text, selector, selectorDomain);
125 RegExpFilter.typeMap.GENERICBLOCK 361 doTest(text.replace("##", "#@#"), selector, selectorDomain);
126 )); 362 }
127 addProperty("matchCase", "false"); 363
128 addProperty("thirdParty", "null"); 364 test.done();
129 addProperty("domains", ""); 365 };
130 addProperty("sitekeys", ""); 366
131 } 367 exports.testCSSRules = function(test)
132 if (type == "filterlist") 368 {
133 { 369 let tests = [
134 addProperty("collapse", "null"); 370 ["foo.com##[-abp-properties='abc']", "abc", "", ""],
135 } 371 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""],
136 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") 372 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""],
137 { 373 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""],
138 addProperty("selectorDomain", ""); 374 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"],
139 addProperty("domains", ""); 375 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background \\-image\\:\\ url\\(data\\:.*\\)", "", ""],
140 } 376 ];
141 if (type == "cssrule") 377
142 { 378 for (let [text, regexp, prefix, suffix] of tests)
143 addProperty("regexp", ""); 379 {
144 addProperty("prefix", ""); 380 let filter = Filter.fromText(text);
145 addProperty("suffix", ""); 381 test.equal(filter.regexpString, regexp, "Regular expression of " + text);
146 } 382 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text);
147 } 383 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text);
148 384 filter.delete();
149 function compareFilter(test, text, expected, postInit) 385 }
150 { 386
151 addDefaults(expected); 387 test.done();
152 388 };
153 let filter = Filter.fromText(text);
154 if (postInit)
155 postInit(filter)
156 let result = serializeFilter(filter);
157 test.equal(result.sort().join("\n"), expected.sort().join("\n"), text);
158
159 // Test round-trip
160 let filter2;
161 let buffer = [];
162 filter.serialize(buffer);
163 if (buffer.length)
164 {
165 let map = {__proto__: null};
166 for (let line of buffer.slice(1))
167 {
168 if (/(.*?)=(.*)/.test(line))
169 map[RegExp.$1] = RegExp.$2;
170 }
171 filter2 = Filter.fromObject(map);
172 }
173 else
174 {
175 filter2 = Filter.fromText(filter.text);
176 }
177
178 test.equal(serializeFilter(filter).join("\n"), serializeFilter(filter2).join(" \n"), text + " deserialization");
179 }
180
181 exports.testFilterClassDefinitions = function(test)
182 {
183 test.equal(typeof Filter, "function", "typeof Filter");
184 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
185 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
186 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
187 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
188 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
189 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
190 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
191 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
192 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
193 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter");
194
195 test.done();
196 };
197
198 exports.testComments = function(test)
199 {
200 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
201 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
202 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
203
204 test.done();
205 };
206
207 exports.testInvalidFilters = function(test)
208 {
209 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
210 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]);
211 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]);
212 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
213
214 function compareCSSRule(domains)
215 {
216 let filterText = domains + "##[-abp-properties='abc']";
217 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]);
218 }
219 compareCSSRule("");
220 compareCSSRule("~foo.com");
221 compareCSSRule("~foo.com,~bar.com");
222 compareCSSRule("foo");
223 compareCSSRule("~foo.com,bar");
224
225 test.done();
226 };
227
228 exports.testFiltersWithState = function(test)
229 {
230 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]);
231 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter)
232 {
233 filter.disabled = false;
234 filter.hitCount = 0;
235 filter.lastHit = 0;
236 });
237 compareFilter(test, "blabla_non_default", ["type=filterlist", "text=blabla_non _default", "regexp=blabla_non_default", "disabled=true", "hitCount=12", "lastHit =20"], function(filter)
238 {
239 filter.disabled = true;
240 filter.hitCount = 12;
241 filter.lastHit = 20;
242 });
243
244 test.done();
245 };
246
247 let t = RegExpFilter.typeMap;
248 let defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | t.GENERICH IDE | t.GENERICBLOCK);
249
250 exports.testSpecialCharacters = function(test)
251 {
252 compareFilter(test, "/ddd|f?a[s]d/", ["type=filterlist", "text=/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d"]);
253 compareFilter(test, "*asdf*d**dd*", ["type=filterlist", "text=*asdf*d**dd*", " regexp=asdf.*d.*dd"]);
254 compareFilter(test, "|*asd|f*d**dd*|", ["type=filterlist", "text=|*asd|f*d**dd *|", "regexp=^.*asd\\|f.*d.*dd.*$"]);
255 compareFilter(test, "dd[]{}$%<>&()d", ["type=filterlist", "text=dd[]{}$%<>&()d ", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d"]);
256
257 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
258 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
259 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
260 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]);
261
262 test.done();
263 };
264
265 exports.testFilterOptions = function(test)
266 {
267 compareFilter(test, "bla$match-case,script,other,third-party,domain=foo.com,si tekey=foo", ["type=filterlist", "text=bla$match-case,script,other,third-party,do main=foo.com,sitekey=foo", "regexp=bla", "matchCase=true", "contentType=" + (t.S CRIPT | t.OTHER), "thirdParty=true", "domains=FOO.COM", "sitekeys=FOO"]);
268 compareFilter(test, "bla$~match-case,~script,~other,~third-party,domain=~bar.c om", ["type=filterlist", "text=bla$~match-case,~script,~other,~third-party,domai n=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER )), "thirdParty=false", "domains=~BAR.COM"]);
269 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com| bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B AR.COM", "sitekeys=BAR|FOO"]);
270
271 // background and image should be the same for backwards compatibility
272 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]);
273 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]);
274 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
275 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
276
277 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]);
278 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
279 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
280 compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text= @@bla$~script,~other,document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.DOCUMENT)]);
281 compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text =@@bla$~script,~other,~document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
282 compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document" , "regexp=bla", "contentType=" + t.DOCUMENT]);
283 compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text= @@bla$~script,~other,elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.ELEMHIDE)]);
284 compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text =@@bla$~script,~other,~elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
285 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide" , "regexp=bla", "contentType=" + t.ELEMHIDE]);
286
287 compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text= @@bla$~script,~other,donottrack", "reason=filter_unknown_option"]);
288 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text =@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]);
289 compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrac k", "reason=filter_unknown_option"]);
290 compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "rea son=filter_unknown_option"]);
291 compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,f oobar", "reason=filter_unknown_option"]);
292 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar, image", "reason=filter_unknown_option"]);
293
294 test.done();
295 };
296
297 exports.testElementHidingRules = function(test)
298 {
299 compareFilter(test, "#ddd", ["type=elemhide", "text=#ddd", "selector=ddd"]);
300 compareFilter(test, "#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector =ddd.fff,ddd#fff"]);
301 compareFilter(test, "#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["typ e=elemhide", "text=#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 'selector =ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
302 compareFilter(test, "#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(fo o=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
303 compareFilter(test, "#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.ff f,#fff"]);
304 compareFilter(test, "#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'sele ctor=[foo="bar"]']);
305 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
306 compareFilter(test, "foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDoma in=foo", "selector=ddd", "domains=FOO"]);
307 compareFilter(test, "foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "sele ctorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
308 compareFilter(test, "foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "se lectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
309 compareFilter(test, "foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#d dd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
310
311 test.done();
312 };
313
314 exports.testElementHidingExceptions = function(test)
315 {
316 compareFilter(test, "#@ddd", ["type=elemhideexception", "text=#@ddd", "selecto r=ddd"]);
317 compareFilter(test, "#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)" , "selector=ddd.fff,ddd#fff"]);
318 compareFilter(test, "#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["ty pe=elemhideexception", "text=#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)" , 'selector=ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
319 compareFilter(test, "#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ ddd(fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
320 compareFilter(test, "#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "s elector=.fff,#fff"]);
321 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']);
322 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
323 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]);
324 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
325 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
326 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
327
328 test.done();
329 };
330
331 exports.testCSSPropertyFilters = function(test)
332 {
333 // Check valid domain combinations
334 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=cssrule", "text =foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[-abp-pr operties='abc']", "domains=FOO.COM", "regexp=abc"]);
335 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=cssrul e", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM", "regexp=abc"]);
336 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=cssrule", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM|~BAR", "regexp=abc"]);
337 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=cssrul e", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain=bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM", "regexp=abc"]);
338
339 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]);
340 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"]);
341 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=cssrule ", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=foo.com", "s elector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM", "prefix=aaa ", "reg exp=abc", "suffix= bbb"]);
342 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=cssrule", "text=foo.com##[-abp-properties='|background-image: url(da ta:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|background-imag e: url(data:*)']", "domains=FOO.COM", "regexp=^background\\-image\\:\\ url\\(dat a\\:.*\\)"]);
343
344 test.done();
345 };
OLDNEW
« no previous file with comments | « test/domainRestrictions.js ('k') | test/regexpFilters_matching.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld