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

Side by Side Diff: chrome/content/tests/filterClasses.js

Issue 29324604: Issue 2394 - Added unit tests for CSS property filters (Closed)
Patch Set: Created Aug. 25, 2015, 3:51 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 (function() 1 (function()
2 { 2 {
3 module("Filter classes", {setup: prepareFilterComponents, teardown: restoreFil terComponents}); 3 module("Filter classes", {setup: prepareFilterComponents, teardown: restoreFil terComponents});
4 4
5 function serializeFilter(filter) 5 function serializeFilter(filter)
6 { 6 {
7 // Filter serialization only writes out essential properties, need to do a f ull serialization here 7 // Filter serialization only writes out essential properties, need to do a f ull serialization here
8 let result = []; 8 let result = [];
9 result.push("text=" + filter.text); 9 result.push("text=" + filter.text);
10 if (filter instanceof InvalidFilter) 10 if (filter instanceof InvalidFilter)
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 { 51 {
52 result.push("type=whitelist"); 52 result.push("type=whitelist");
53 } 53 }
54 } 54 }
55 else if (filter instanceof ElemHideBase) 55 else if (filter instanceof ElemHideBase)
56 { 56 {
57 if (filter instanceof ElemHideFilter) 57 if (filter instanceof ElemHideFilter)
58 result.push("type=elemhide"); 58 result.push("type=elemhide");
59 else if (filter instanceof ElemHideException) 59 else if (filter instanceof ElemHideException)
60 result.push("type=elemhideexception"); 60 result.push("type=elemhideexception");
61 else if (filter instanceof CSSPropertyFilter)
62 {
63 result.push("type=cssrule");
64 result.push("prefix=" + (filter.selectorPrefix || ""));
65 result.push("regexp=" + filter.regexpString);
66 result.push("suffix=" + (filter.selectorSuffix || ""));
67 }
61 68
62 result.push("selectorDomain=" + (filter.selectorDomain || "")); 69 result.push("selectorDomain=" + (filter.selectorDomain || ""));
63 result.push("selector=" + filter.selector); 70 result.push("selector=" + filter.selector);
64 } 71 }
65 } 72 }
66 return result; 73 return result;
67 } 74 }
68 75
69 function addDefaults(expected) 76 function addDefaults(expected)
70 { 77 {
71 let type = null; 78 let type = null;
72 let hasProperty = {}; 79 let hasProperty = {};
73 for (let entry of expected) 80 for (let entry of expected)
74 { 81 {
75 if (/^type=(.*)/.test(entry)) 82 if (/^type=(.*)/.test(entry))
76 type = RegExp.$1; 83 type = RegExp.$1;
77 else if (/^(\w+)/.test(entry)) 84 else if (/^(\w+)/.test(entry))
78 hasProperty[RegExp.$1] = true; 85 hasProperty[RegExp.$1] = true;
79 } 86 }
80 87
81 function addProperty(prop, value) 88 function addProperty(prop, value)
82 { 89 {
83 if (!(prop in hasProperty)) 90 if (!(prop in hasProperty))
84 expected.push(prop + "=" + value); 91 expected.push(prop + "=" + value);
85 } 92 }
86 93
87 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || typ e == "elemhideexception") 94 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || typ e == "elemhideexception" || type == "cssrule")
88 { 95 {
89 addProperty("disabled", "false"); 96 addProperty("disabled", "false");
90 addProperty("lastHit", "0"); 97 addProperty("lastHit", "0");
91 addProperty("hitCount", "0"); 98 addProperty("hitCount", "0");
92 } 99 }
93 if (type == "whitelist" || type == "filterlist") 100 if (type == "whitelist" || type == "filterlist")
94 { 101 {
95 addProperty("contentType", 0x7FFFFFFF & ~(RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP)); 102 addProperty("contentType", 0x7FFFFFFF & ~(RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | RegExpFilter.typeMap.POPUP));
96 addProperty("matchCase", "false"); 103 addProperty("matchCase", "false");
97 addProperty("thirdParty", "null"); 104 addProperty("thirdParty", "null");
98 addProperty("domains", ""); 105 addProperty("domains", "");
99 addProperty("sitekeys", ""); 106 addProperty("sitekeys", "");
100 } 107 }
101 if (type == "filterlist") 108 if (type == "filterlist")
102 { 109 {
103 addProperty("collapse", "null"); 110 addProperty("collapse", "null");
104 } 111 }
105 if (type == "elemhide" || type == "elemhideexception") 112 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule")
106 { 113 {
107 addProperty("selectorDomain", ""); 114 addProperty("selectorDomain", "");
108 addProperty("domains", ""); 115 addProperty("domains", "");
109 } 116 }
117 if (type == "cssrule")
118 {
119 addProperty("regexp", "");
120 addProperty("prefix", "");
121 addProperty("suffix", "");
122 }
110 } 123 }
111 124
112 function compareFilter(text, expected, postInit) 125 function compareFilter(text, expected, postInit)
113 { 126 {
114 addDefaults(expected); 127 addDefaults(expected);
115 128
116 let filter = Filter.fromText(text); 129 let filter = Filter.fromText(text);
117 if (postInit) 130 if (postInit)
118 postInit(filter) 131 postInit(filter)
119 let result = serializeFilter(filter); 132 let result = serializeFilter(filter);
(...skipping 26 matching lines...) Expand all
146 equal(typeof Filter, "function", "typeof Filter"); 159 equal(typeof Filter, "function", "typeof Filter");
147 equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 160 equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
148 equal(typeof CommentFilter, "function", "typeof CommentFilter"); 161 equal(typeof CommentFilter, "function", "typeof CommentFilter");
149 equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 162 equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
150 equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 163 equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
151 equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 164 equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
152 equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 165 equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
153 equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 166 equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
154 equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 167 equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
155 equal(typeof ElemHideException, "function", "typeof ElemHideException"); 168 equal(typeof ElemHideException, "function", "typeof ElemHideException");
169 equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter");
156 }); 170 });
157 171
158 test("Comments", function() 172 test("Comments", function()
159 { 173 {
160 compareFilter("!asdf", ["type=comment", "text=!asdf"]); 174 compareFilter("!asdf", ["type=comment", "text=!asdf"]);
161 compareFilter("!foo#bar", ["type=comment", "text=!foo#bar"]); 175 compareFilter("!foo#bar", ["type=comment", "text=!foo#bar"]);
162 compareFilter("!foo##bar", ["type=comment", "text=!foo##bar"]); 176 compareFilter("!foo##bar", ["type=comment", "text=!foo##bar"]);
163 }); 177 });
164 178
165 test("Invalid filters", function() 179 test("Invalid filters", function()
166 { 180 {
167 compareFilter("/??/", ["type=invalid", "text=/??/", "hasReason"]); 181 compareFilter("/??/", ["type=invalid", "text=/??/", "hasReason"]);
168 182
169 compareFilter("#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "hasRe ason"]); 183 compareFilter("#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "hasRe ason"]);
170 { 184 {
171 let result = Filter.fromText("#dd(asd)(ddd)").reason; 185 let result = Filter.fromText("#dd(asd)(ddd)").reason;
172 equal(result, Utils.getString("filter_elemhide_duplicate_id"), "#dd(asd)(d dd).reason"); 186 equal(result, Utils.getString("filter_elemhide_duplicate_id"), "#dd(asd)(d dd).reason");
173 } 187 }
174 188
175 compareFilter("#*", ["type=invalid", "text=#*", "hasReason"]); 189 compareFilter("#*", ["type=invalid", "text=#*", "hasReason"]);
176 { 190 {
177 let result = Filter.fromText("#*").reason; 191 let result = Filter.fromText("#*").reason;
178 equal(result, Utils.getString("filter_elemhide_nocriteria"), "#*.reason"); 192 equal(result, Utils.getString("filter_elemhide_nocriteria"), "#*.reason");
179 } 193 }
194
195 function compareCSSRule(domains)
196 {
197 let filterText = domains + "##[-abp-properties='abc']";
198 compareFilter(filterText, ["type=invalid", "text=" + filterText, "hasReaso n"]);
199 let reason = Filter.fromText(filterText).reason;
200 equal(reason, Utils.getString("filter_cssproperty_nodomain"), filterText + ".reason");
201 }
202 compareCSSRule("");
203 compareCSSRule("~foo.com");
204 compareCSSRule("~foo.com,~bar.com");
205 compareCSSRule("foo");
Wladimir Palant 2015/11/10 10:49:03 Why is foo##[-abp-properties='abc'] invalid?
Thomas Greiner 2015/12/03 12:55:20 Considering domains with only one part (e.g. "com"
206 compareCSSRule("~foo.com,bar");
180 }); 207 });
181 208
182 test("Filters with state", function() 209 test("Filters with state", function()
183 { 210 {
184 compareFilter("blabla", ["type=filterlist", "text=blabla", "regexp=blabla"]) ; 211 compareFilter("blabla", ["type=filterlist", "text=blabla", "regexp=blabla"]) ;
185 compareFilter("blabla_default", ["type=filterlist", "text=blabla_default", " regexp=blabla_default"], function(filter) 212 compareFilter("blabla_default", ["type=filterlist", "text=blabla_default", " regexp=blabla_default"], function(filter)
186 { 213 {
187 filter.disabled = false; 214 filter.disabled = false;
188 filter.hitCount = 0; 215 filter.hitCount = 0;
189 filter.lastHit = 0; 216 filter.lastHit = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 compareFilter("#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector=ddd .fff,ddd#fff"]); 275 compareFilter("#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector=ddd .fff,ddd#fff"]);
249 compareFilter("#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["type=el emhide", "text=#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 'selector=ddd [foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']); 276 compareFilter("#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["type=el emhide", "text=#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 'selector=ddd [foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
250 compareFilter("#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(foo=ba r)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); 277 compareFilter("#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(foo=ba r)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
251 compareFilter("#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.fff,#f ff"]); 278 compareFilter("#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.fff,#f ff"]);
252 compareFilter("#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'selector =[foo="bar"]']); 279 compareFilter("#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'selector =[foo="bar"]']);
253 compareFilter("##body > div:first-child", ["type=elemhide", "text=##body > d iv:first-child", "selector=body > div:first-child"]); 280 compareFilter("##body > div:first-child", ["type=elemhide", "text=##body > d iv:first-child", "selector=body > div:first-child"]);
254 compareFilter("foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDomain=f oo", "selector=ddd", "domains=FOO"]); 281 compareFilter("foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDomain=f oo", "selector=ddd", "domains=FOO"]);
255 compareFilter("foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "selector Domain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 282 compareFilter("foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "selector Domain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
256 compareFilter("foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "select orDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 283 compareFilter("foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "select orDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
257 compareFilter("foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); 284 compareFilter("foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
285 compareFilter("##[-abp-properties='']", ["type=elemhide", "text=##[-abp-prop erties='']", "selector=[-abp-properties='']"]);
Wladimir Palant 2015/11/10 10:49:03 Wouldn't it make sense to have this filter in the
Thomas Greiner 2015/12/03 12:55:20 Done.
258 }); 286 });
259 287
260 test("Element hiding exceptions", function() 288 test("Element hiding exceptions", function()
261 { 289 {
262 compareFilter("#@ddd", ["type=elemhideexception", "text=#@ddd", "selector=dd d"]); 290 compareFilter("#@ddd", ["type=elemhideexception", "text=#@ddd", "selector=dd d"]);
263 compareFilter("#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)", "s elector=ddd.fff,ddd#fff"]); 291 compareFilter("#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)", "s elector=ddd.fff,ddd#fff"]);
264 compareFilter("#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["type=e lemhideexception", "text=#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 's elector=ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']); 292 compareFilter("#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["type=e lemhideexception", "text=#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 's elector=ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
265 compareFilter("#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ddd( fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); 293 compareFilter("#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ddd( fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
266 compareFilter("#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "selec tor=.fff,#fff"]); 294 compareFilter("#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "selec tor=.fff,#fff"]);
267 compareFilter("#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=bar)" , 'selector=[foo="bar"]']); 295 compareFilter("#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=bar)" , 'selector=[foo="bar"]']);
268 compareFilter("#@#body > div:first-child", ["type=elemhideexception", "text= #@#body > div:first-child", "selector=body > div:first-child"]); 296 compareFilter("#@#body > div:first-child", ["type=elemhideexception", "text= #@#body > div:first-child", "selector=body > div:first-child"]);
269 compareFilter("foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "selec torDomain=foo", "selector=ddd", "domains=FOO"]); 297 compareFilter("foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "selec torDomain=foo", "selector=ddd", "domains=FOO"]);
270 compareFilter("foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ddd" , "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 298 compareFilter("foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ddd" , "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
271 compareFilter("foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar#@dd d", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 299 compareFilter("foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar#@dd d", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
272 compareFilter("foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo,~baz ,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); 300 compareFilter("foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo,~baz ,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
301 compareFilter("foo.com#@#[-abp-properties='abc']", ["type=elemhideexception" , "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selector= [-abp-properties='abc']", "domains=FOO.COM"]);
Wladimir Palant 2015/11/10 10:49:02 Wouldn't it make sense to have this filter in the
Thomas Greiner 2015/12/03 12:55:21 Done.
302 });
303
304 test("CSS property filters", function()
305 {
306 // Check valid domain combinations
307 compareFilter("foo.com##[-abp-properties='abc']", ["type=cssrule", "text=foo .com##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[-abp-proper ties='abc']", "domains=FOO.COM", "prefix=", "regexp=abc", "suffix="]);
308 compareFilter("foo.com,~bar.com##[-abp-properties='abc']", ["type=cssrule", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain=foo.com", "sel ector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM", "prefix=", "regexp=a bc", "suffix="]);
309 compareFilter("foo.com,~bar##[-abp-properties='abc']", ["type=cssrule", "tex t=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[- abp-properties='abc']", "domains=FOO.COM|~BAR", "prefix=", "regexp=abc", "suffix ="]);
310 compareFilter("~foo.com,bar.com##[-abp-properties='abc']", ["type=cssrule", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain=bar.com", "sel ector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM", "prefix=", "regexp=a bc", "suffix="]);
Wladimir Palant 2015/11/10 10:49:03 You make prefix and suffix default to an empty str
Thomas Greiner 2015/12/03 12:55:21 Done.
311
312 compareFilter("foo.com##aaa [-abp-properties='abc'] bbb", ["type=cssrule", " text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=foo.com", "selec tor=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM", "prefix=aaa ", "regexp= abc", "suffix= bbb"]);
313 compareFilter("foo.com##[-abp-properties='|background-image: url(data:*)']", ["type=cssrule", "text=foo.com##[-abp-properties='|background-image: url(data:* )']", "selectorDomain=foo.com", "selector=[-abp-properties='|background-image: u rl(data:*)']", "domains=FOO.COM", "prefix=", "regexp=^background\\-image\\:\\ ur l\\(data\\:.*\\)", "suffix="]);
273 }); 314 });
274 })(); 315 })();
OLDNEW

Powered by Google App Engine
This is Rietveld