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: Rebased and adapted to review changes from #2392 and #2393 Created Nov. 25, 2015, 6:53 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 & ~( 102 addProperty("contentType", 0x7FFFFFFF & ~(
96 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 103 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
97 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 104 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
98 RegExpFilter.typeMap.GENERICBLOCK 105 RegExpFilter.typeMap.GENERICBLOCK
99 )); 106 ));
100 addProperty("matchCase", "false"); 107 addProperty("matchCase", "false");
101 addProperty("thirdParty", "null"); 108 addProperty("thirdParty", "null");
102 addProperty("domains", ""); 109 addProperty("domains", "");
103 addProperty("sitekeys", ""); 110 addProperty("sitekeys", "");
104 } 111 }
105 if (type == "filterlist") 112 if (type == "filterlist")
106 { 113 {
107 addProperty("collapse", "null"); 114 addProperty("collapse", "null");
108 } 115 }
109 if (type == "elemhide" || type == "elemhideexception") 116 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule")
110 { 117 {
111 addProperty("selectorDomain", ""); 118 addProperty("selectorDomain", "");
112 addProperty("domains", ""); 119 addProperty("domains", "");
113 } 120 }
121 if (type == "cssrule")
122 {
123 addProperty("regexp", "");
124 addProperty("prefix", "");
125 addProperty("suffix", "");
126 }
114 } 127 }
115 128
116 function compareFilter(text, expected, postInit) 129 function compareFilter(text, expected, postInit)
117 { 130 {
118 addDefaults(expected); 131 addDefaults(expected);
119 132
120 let filter = Filter.fromText(text); 133 let filter = Filter.fromText(text);
121 if (postInit) 134 if (postInit)
122 postInit(filter) 135 postInit(filter)
123 let result = serializeFilter(filter); 136 let result = serializeFilter(filter);
(...skipping 26 matching lines...) Expand all
150 equal(typeof Filter, "function", "typeof Filter"); 163 equal(typeof Filter, "function", "typeof Filter");
151 equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 164 equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
152 equal(typeof CommentFilter, "function", "typeof CommentFilter"); 165 equal(typeof CommentFilter, "function", "typeof CommentFilter");
153 equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 166 equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
154 equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 167 equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
155 equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 168 equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
156 equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 169 equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
157 equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 170 equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
158 equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 171 equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
159 equal(typeof ElemHideException, "function", "typeof ElemHideException"); 172 equal(typeof ElemHideException, "function", "typeof ElemHideException");
173 equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter");
160 }); 174 });
161 175
162 test("Comments", function() 176 test("Comments", function()
163 { 177 {
164 compareFilter("!asdf", ["type=comment", "text=!asdf"]); 178 compareFilter("!asdf", ["type=comment", "text=!asdf"]);
165 compareFilter("!foo#bar", ["type=comment", "text=!foo#bar"]); 179 compareFilter("!foo#bar", ["type=comment", "text=!foo#bar"]);
166 compareFilter("!foo##bar", ["type=comment", "text=!foo##bar"]); 180 compareFilter("!foo##bar", ["type=comment", "text=!foo##bar"]);
167 }); 181 });
168 182
169 test("Invalid filters", function() 183 test("Invalid filters", function()
170 { 184 {
171 compareFilter("/??/", ["type=invalid", "text=/??/", "hasReason"]); 185 compareFilter("/??/", ["type=invalid", "text=/??/", "hasReason"]);
172 186
173 compareFilter("#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "hasRe ason"]); 187 compareFilter("#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "hasRe ason"]);
174 { 188 {
175 let result = Filter.fromText("#dd(asd)(ddd)").reason; 189 let result = Filter.fromText("#dd(asd)(ddd)").reason;
176 equal(result, Utils.getString("filter_elemhide_duplicate_id"), "#dd(asd)(d dd).reason"); 190 equal(result, Utils.getString("filter_elemhide_duplicate_id"), "#dd(asd)(d dd).reason");
177 } 191 }
178 192
179 compareFilter("#*", ["type=invalid", "text=#*", "hasReason"]); 193 compareFilter("#*", ["type=invalid", "text=#*", "hasReason"]);
180 { 194 {
181 let result = Filter.fromText("#*").reason; 195 let result = Filter.fromText("#*").reason;
182 equal(result, Utils.getString("filter_elemhide_nocriteria"), "#*.reason"); 196 equal(result, Utils.getString("filter_elemhide_nocriteria"), "#*.reason");
183 } 197 }
198
199 function compareCSSRule(domains)
200 {
201 let filterText = domains + "##[-abp-properties='abc']";
202 compareFilter(filterText, ["type=invalid", "text=" + filterText, "hasReaso n"]);
203 let reason = Filter.fromText(filterText).reason;
204 equal(reason, Utils.getString("filter_cssproperty_nodomain"), filterText + ".reason");
205 }
206 compareCSSRule("");
207 compareCSSRule("~foo.com");
208 compareCSSRule("~foo.com,~bar.com");
209 compareCSSRule("foo");
210 compareCSSRule("~foo.com,bar");
184 }); 211 });
185 212
186 test("Filters with state", function() 213 test("Filters with state", function()
187 { 214 {
188 compareFilter("blabla", ["type=filterlist", "text=blabla", "regexp=blabla"]) ; 215 compareFilter("blabla", ["type=filterlist", "text=blabla", "regexp=blabla"]) ;
189 compareFilter("blabla_default", ["type=filterlist", "text=blabla_default", " regexp=blabla_default"], function(filter) 216 compareFilter("blabla_default", ["type=filterlist", "text=blabla_default", " regexp=blabla_default"], function(filter)
190 { 217 {
191 filter.disabled = false; 218 filter.disabled = false;
192 filter.hitCount = 0; 219 filter.hitCount = 0;
193 filter.lastHit = 0; 220 filter.lastHit = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 compareFilter("#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector=ddd .fff,ddd#fff"]); 279 compareFilter("#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector=ddd .fff,ddd#fff"]);
253 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"]']); 280 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"]']);
254 compareFilter("#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(foo=ba r)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); 281 compareFilter("#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(foo=ba r)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
255 compareFilter("#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.fff,#f ff"]); 282 compareFilter("#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.fff,#f ff"]);
256 compareFilter("#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'selector =[foo="bar"]']); 283 compareFilter("#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'selector =[foo="bar"]']);
257 compareFilter("##body > div:first-child", ["type=elemhide", "text=##body > d iv:first-child", "selector=body > div:first-child"]); 284 compareFilter("##body > div:first-child", ["type=elemhide", "text=##body > d iv:first-child", "selector=body > div:first-child"]);
258 compareFilter("foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDomain=f oo", "selector=ddd", "domains=FOO"]); 285 compareFilter("foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDomain=f oo", "selector=ddd", "domains=FOO"]);
259 compareFilter("foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "selector Domain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 286 compareFilter("foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "selector Domain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
260 compareFilter("foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "select orDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 287 compareFilter("foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "select orDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
261 compareFilter("foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); 288 compareFilter("foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
289 compareFilter("##[-abp-properties='']", ["type=elemhide", "text=##[-abp-prop erties='']", "selector=[-abp-properties='']"]);
262 }); 290 });
263 291
264 test("Element hiding exceptions", function() 292 test("Element hiding exceptions", function()
265 { 293 {
266 compareFilter("#@ddd", ["type=elemhideexception", "text=#@ddd", "selector=dd d"]); 294 compareFilter("#@ddd", ["type=elemhideexception", "text=#@ddd", "selector=dd d"]);
267 compareFilter("#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)", "s elector=ddd.fff,ddd#fff"]); 295 compareFilter("#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)", "s elector=ddd.fff,ddd#fff"]);
268 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"]']); 296 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"]']);
269 compareFilter("#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ddd( fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); 297 compareFilter("#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ddd( fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
270 compareFilter("#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "selec tor=.fff,#fff"]); 298 compareFilter("#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "selec tor=.fff,#fff"]);
271 compareFilter("#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=bar)" , 'selector=[foo="bar"]']); 299 compareFilter("#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=bar)" , 'selector=[foo="bar"]']);
272 compareFilter("#@#body > div:first-child", ["type=elemhideexception", "text= #@#body > div:first-child", "selector=body > div:first-child"]); 300 compareFilter("#@#body > div:first-child", ["type=elemhideexception", "text= #@#body > div:first-child", "selector=body > div:first-child"]);
273 compareFilter("foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "selec torDomain=foo", "selector=ddd", "domains=FOO"]); 301 compareFilter("foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "selec torDomain=foo", "selector=ddd", "domains=FOO"]);
274 compareFilter("foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ddd" , "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 302 compareFilter("foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ddd" , "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
275 compareFilter("foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar#@dd d", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 303 compareFilter("foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar#@dd d", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
276 compareFilter("foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo,~baz ,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); 304 compareFilter("foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo,~baz ,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
305 compareFilter("foo.com#@#[-abp-properties='abc']", ["type=elemhideexception" , "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selector= [-abp-properties='abc']", "domains=FOO.COM"]);
306 });
307
308 test("CSS property filters", function()
309 {
310 // Check valid domain combinations
311 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="]);
312 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="]);
313 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 ="]);
314 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="]);
315
316 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"]);
317 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="]);
277 }); 318 });
278 })(); 319 })();
OLDNEW

Powered by Google App Engine
This is Rietveld