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

Side by Side Diff: test/filterClasses.js

Issue 29336430: Issue 3659 - Remove locale dependency from filter classes (Closed)
Patch Set: Simplified test setup Created Feb. 17, 2016, 3:52 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 | « package.json ('k') | test_wrapper.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 "use strict";
19
20 let {
21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
23 ElemHideException, CSSPropertyFilter
24 } = require("../lib/filterClasses");
25
26 function serializeFilter(filter)
27 {
28 // Filter serialization only writes out essential properties, need to do a ful l serialization here
29 let result = [];
30 result.push("text=" + filter.text);
31 if (filter instanceof InvalidFilter)
32 {
33 result.push("type=invalid");
34 result.push("reason=" + filter.reason);
35 }
36 else if (filter instanceof CommentFilter)
37 {
38 result.push("type=comment");
39 }
40 else if (filter instanceof ActiveFilter)
41 {
42 result.push("disabled=" + filter.disabled);
43 result.push("lastHit=" + filter.lastHit);
44 result.push("hitCount=" + filter.hitCount);
45
46 let domains = [];
47 if (filter.domains)
48 {
49 for (let domain in filter.domains)
50 if (domain != "")
51 domains.push(filter.domains[domain] ? domain : "~" + domain);
52 }
53 result.push("domains=" + domains.sort().join("|"));
54
55 if (filter instanceof RegExpFilter)
56 {
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 {
67 result.push("type=filterlist");
68 result.push("collapse=" + filter.collapse);
69 }
70 else if (filter instanceof WhitelistFilter)
71 {
72 result.push("type=whitelist");
73 }
74 }
75 else if (filter instanceof ElemHideBase)
76 {
77 if (filter instanceof ElemHideFilter)
78 result.push("type=elemhide");
79 else if (filter instanceof ElemHideException)
80 result.push("type=elemhideexception");
81 else if (filter instanceof CSSPropertyFilter)
82 {
83 result.push("type=cssrule");
84 result.push("prefix=" + (filter.selectorPrefix || ""));
85 result.push("regexp=" + filter.regexpString);
86 result.push("suffix=" + (filter.selectorSuffix || ""));
87 }
88
89 result.push("selectorDomain=" + (filter.selectorDomain || ""));
90 result.push("selector=" + filter.selector);
91 }
92 }
93 return result;
94 }
95
96 function addDefaults(expected)
97 {
98 let type = null;
99 let hasProperty = {};
100 for (let entry of expected)
101 {
102 if (/^type=(.*)/.test(entry))
103 type = RegExp.$1;
104 else if (/^(\w+)/.test(entry))
105 hasProperty[RegExp.$1] = true;
106 }
107
108 function addProperty(prop, value)
109 {
110 if (!(prop in hasProperty))
111 expected.push(prop + "=" + value);
112 }
113
114 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule")
115 {
116 addProperty("disabled", "false");
117 addProperty("lastHit", "0");
118 addProperty("hitCount", "0");
119 }
120 if (type == "whitelist" || type == "filterlist")
121 {
122 addProperty("contentType", 0x7FFFFFFF & ~(
123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
125 RegExpFilter.typeMap.GENERICBLOCK
126 ));
127 addProperty("matchCase", "false");
128 addProperty("thirdParty", "null");
129 addProperty("domains", "");
130 addProperty("sitekeys", "");
131 }
132 if (type == "filterlist")
133 {
134 addProperty("collapse", "null");
135 }
136 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule")
137 {
138 addProperty("selectorDomain", "");
139 addProperty("domains", "");
140 }
141 if (type == "cssrule")
142 {
143 addProperty("regexp", "");
144 addProperty("prefix", "");
145 addProperty("suffix", "");
146 }
147 }
148
149 function compareFilter(test, text, expected, postInit)
150 {
151 addDefaults(expected);
152
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 | « package.json ('k') | test_wrapper.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld