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 Dec. 3, 2015, 12: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 & ~( 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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
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"]']); 295 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"]']); 296 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"]); 297 compareFilter("#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "selec tor=.fff,#fff"]);
271 compareFilter("#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=bar)" , 'selector=[foo="bar"]']); 298 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"]); 299 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"]); 300 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"]); 301 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"]); 302 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"]); 303 compareFilter("foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo,~baz ,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
277 }); 304 });
305
306 test("CSS property filters", function()
307 {
308
309 // Check valid domain combinations
310 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", "regexp=abc"]);
311 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", "regexp=abc"]);
312 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", "regexp=abc"]);
313 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", "regexp=abc"]);
314
315 compareFilter("##[-abp-properties='']", ["type=elemhide", "text=##[-abp-prop erties='']", "selector=[-abp-properties='']"]);
316 compareFilter("foo.com#@#[-abp-properties='abc']", ["type=elemhideexception" , "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selector= [-abp-properties='abc']", "domains=FOO.COM"]);
317 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"]);
318 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", "regexp=^background\\-image\\:\\ url\\(data\\: .*\\)"]);
319 });
278 })(); 320 })();
OLDNEW

Powered by Google App Engine
This is Rietveld