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

Delta Between Two Patch Sets: chrome/content/tests/matcher.js

Issue 6439460933730304: Issue 616 - Add tests for $generichide and $genericblock (Closed)
Left Patch Set: Created March 19, 2015, 10:58 a.m.
Right Patch Set: Couple more tweaks Created Oct. 2, 2015, 10:37 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/tests/filterClasses.js ('k') | chrome/content/tests/policy.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 (function() 1 (function()
2 { 2 {
3 module("Filter matcher", {setup: prepareFilterComponents, teardown: restoreFil terComponents}); 3 module("Filter matcher", {setup: prepareFilterComponents, teardown: restoreFil terComponents});
4 4
5 function compareKeywords(text, expected) 5 function compareKeywords(text, expected)
6 { 6 {
7 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)]) 7 for (let filter of [Filter.fromText(text), Filter.fromText("@@" + text)])
8 { 8 {
9 let matcher = new Matcher(); 9 let matcher = new Matcher();
10 let result = []; 10 let result = [];
(...skipping 12 matching lines...) Expand all
23 equal(result.join(", "), expected.join(", "), "Keyword candidates for " + filter.text); 23 equal(result.join(", "), expected.join(", "), "Keyword candidates for " + filter.text);
24 } 24 }
25 } 25 }
26 26
27 function checkMatch(filters, location, contentType, docDomain, thirdParty, sit ekey, specificOnly, expected) 27 function checkMatch(filters, location, contentType, docDomain, thirdParty, sit ekey, specificOnly, expected)
28 { 28 {
29 let matcher = new Matcher(); 29 let matcher = new Matcher();
30 for (let filter of filters) 30 for (let filter of filters)
31 matcher.add(Filter.fromText(filter)); 31 matcher.add(Filter.fromText(filter));
32 32
33 let result = matcher.matchesAny(location, contentType, docDomain, thirdParty , sitekey, specificOnly); 33 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], docDomain, thirdParty, sitekey, specificOnly);
34 if (result) 34 if (result)
35 result = result.text; 35 result = result.text;
36 36
37 equal(result, expected, "match(" + location + ", " + contentType + ", " + do cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n")); 37 equal(result, expected, "match(" + location + ", " + contentType + ", " + do cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specificOnly") + ") with:\n" + filters.join("\n"));
38 38
39 let combinedMatcher = new CombinedMatcher(); 39 let combinedMatcher = new CombinedMatcher();
40 for (let i = 0; i < 2; i++) 40 for (let i = 0; i < 2; i++)
41 { 41 {
42 for (let filter of filters) 42 for (let filter of filters)
43 combinedMatcher.add(Filter.fromText(filter)); 43 combinedMatcher.add(Filter.fromText(filter));
44 44
45 let result = combinedMatcher.matchesAny(location, contentType, docDomain, thirdParty, sitekey, specificOnly); 45 let result = combinedMatcher.matchesAny(location, RegExpFilter.typeMap[con tentType], docDomain, thirdParty, sitekey, specificOnly);
46 if (result) 46 if (result)
47 result = result.text; 47 result = result.text;
48 48
49 equal(result, expected, "combinedMatch(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specif icOnly") + ") with:\n" + filters.join("\n")); 49 equal(result, expected, "combinedMatch(" + location + ", " + contentType + ", " + docDomain + ", " + (thirdParty ? "third-party" : "first-party") + ", " + (sitekey || "no-sitekey") + ", " + (specificOnly ? "specificOnly" : "not-specif icOnly") + ") with:\n" + filters.join("\n"));
50 50
51 // Generic whitelisting rules can match for specificOnly searches, so we
52 // can't easily know which rule will match for these whitelisting tests
53 if (specificOnly)
54 continue;
55
51 // For next run: add whitelisting filters for filters that aren't already 56 // For next run: add whitelisting filters for filters that aren't already
57 filters = filters.map((text) => text.substr(0, 2) == "@@" ? text : "@@" + text);
52 if (expected && expected.substr(0, 2) != "@@") 58 if (expected && expected.substr(0, 2) != "@@")
53 { 59 expected = "@@" + expected;
54 filters = filters.filter((text) => text.substr(0, 2) != "@@").map((text) => "@@" + text);
55 if (expected)
56 expected = "@@" + expected;
57 }
58 } 60 }
59 } 61 }
60 62
61 function cacheCheck(matcher, location, contentType, docDomain, thirdParty, exp ected) 63 function cacheCheck(matcher, location, contentType, docDomain, thirdParty, exp ected)
62 { 64 {
63 let result = matcher.matchesAny(location, contentType, docDomain, thirdParty ); 65 let result = matcher.matchesAny(location, RegExpFilter.typeMap[contentType], docDomain, thirdParty);
64 if (result) 66 if (result)
65 result = result.text; 67 result = result.text;
66 68
67 equal(result, expected, "match(" + location + ", " + contentType + ", " + do cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with static f ilters"); 69 equal(result, expected, "match(" + location + ", " + contentType + ", " + do cDomain + ", " + (thirdParty ? "third-party" : "first-party") + ") with static f ilters");
68 } 70 }
69 71
70 test("Matcher class definitions", function() 72 test("Matcher class definitions", function()
71 { 73 {
72 equal(typeof Matcher, "function", "typeof Matcher"); 74 equal(typeof Matcher, "function", "typeof Matcher");
73 equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher"); 75 equal(typeof CombinedMatcher, "function", "typeof CombinedMatcher");
(...skipping 17 matching lines...) Expand all
91 compareKeywords("&asdf=1234|", ["asdf", "1234"]); 93 compareKeywords("&asdf=1234|", ["asdf", "1234"]);
92 compareKeywords("^foo%2Ebar^", ["foo%2ebar"]); 94 compareKeywords("^foo%2Ebar^", ["foo%2ebar"]);
93 compareKeywords("^aSdF^1234", ["asdf"]); 95 compareKeywords("^aSdF^1234", ["asdf"]);
94 compareKeywords("_asdf_1234_", ["asdf", "1234"]); 96 compareKeywords("_asdf_1234_", ["asdf", "1234"]);
95 compareKeywords("+asdf-1234=", ["asdf", "1234"]); 97 compareKeywords("+asdf-1234=", ["asdf", "1234"]);
96 compareKeywords("/123^ad2&ad&", ["123", "ad2"]); 98 compareKeywords("/123^ad2&ad&", ["123", "ad2"]);
97 compareKeywords("/123^ad2&ad$script,domain=example.com", ["123", "ad2"]); 99 compareKeywords("/123^ad2&ad$script,domain=example.com", ["123", "ad2"]);
98 }); 100 });
99 101
100 test("Filter matching", function() 102 test("Filter matching", function()
101 { 103 {
kzar 2015/03/19 11:03:22 Most of this change is down to the addition of the
102 checkMatch([], "http://abc/def", "IMAGE", null, false, null, false, null); 104 checkMatch([], "http://abc/def", "IMAGE", null, false, null, false, null);
103 checkMatch(["abc"], "http://abc/def", "IMAGE", null, false, null, false, "ab c"); 105 checkMatch(["abc"], "http://abc/def", "IMAGE", null, false, null, false, "ab c");
104 checkMatch(["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, fal se, "abc"); 106 checkMatch(["abc", "ddd"], "http://abc/def", "IMAGE", null, false, null, fal se, "abc");
105 checkMatch(["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, fal se, "abc"); 107 checkMatch(["ddd", "abc"], "http://abc/def", "IMAGE", null, false, null, fal se, "abc");
106 checkMatch(["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, fal se, null); 108 checkMatch(["ddd", "abd"], "http://abc/def", "IMAGE", null, false, null, fal se, null);
107 checkMatch(["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null , false, "://abc/d"); 109 checkMatch(["abc", "://abc/d"], "http://abc/def", "IMAGE", null, false, null , false, "://abc/d");
108 checkMatch(["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null , false, "://abc/d"); 110 checkMatch(["://abc/d", "abc"], "http://abc/def", "IMAGE", null, false, null , false, "://abc/d");
109 checkMatch(["|http://"], "http://abc/def", "IMAGE", null, false, null, false , "|http://"); 111 checkMatch(["|http://"], "http://abc/def", "IMAGE", null, false, null, false , "|http://");
110 checkMatch(["|http://abc"], "http://abc/def", "IMAGE", null, false, null, fa lse, "|http://abc"); 112 checkMatch(["|http://abc"], "http://abc/def", "IMAGE", null, false, null, fa lse, "|http://abc");
111 checkMatch(["|abc"], "http://abc/def", "IMAGE", null, false, null, false, nu ll); 113 checkMatch(["|abc"], "http://abc/def", "IMAGE", null, false, null, false, nu ll);
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null); 157 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "bar.com", false, null, false, null);
156 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null); 158 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://abc/def", "IMAGE", "baz.com", false, null, false, null);
157 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$doma in=~foo.com|~bar.com"); 159 checkMatch(["abc$domain=foo.com", "cba$domain=bar.com", "ccc$domain=~foo.com |~bar.com"], "http://ccc/def", "IMAGE", "baz.com", false, null, false, "ccc$doma in=~foo.com|~bar.com");
158 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo -publickey"); 160 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "foo.com", false, "foo-publickey", false, "abc$sitekey=foo -publickey");
159 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar -publickey"); 161 checkMatch(["abc$sitekey=foo-publickey", "abc$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, "abc$sitekey=bar -publickey");
160 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null); 162 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "bar.com", false, "bar-publickey", false, null);
161 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "baz.com", false, null, false, null); 163 checkMatch(["abc$sitekey=foo-publickey", "cba$sitekey=bar-publickey"], "http ://abc/def", "IMAGE", "baz.com", false, null, false, null);
162 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publi ckey", false, "abc$sitekey=foo-publickey,domain=foo.com"); 164 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "foo-publi ckey", false, "abc$sitekey=foo-publickey,domain=foo.com");
163 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-publi ckey", false, null); 165 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "foo.com", false, "bar-publi ckey", false, null);
164 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-publi ckey", false, null); 166 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "foo-publi ckey", false, null);
165 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publi ckey", false, "abc$sitekey=bar-publickey,domain=bar.com"); 167 checkMatch(["abc$sitekey=foo-publickey,domain=foo.com", "abc$sitekey=bar-pub lickey,domain=bar.com"], "http://abc/def", "IMAGE", "bar.com", false, "bar-publi ckey", false, "abc$sitekey=bar-publickey,domain=bar.com");
kzar 2015/03/19 11:03:22 The new tests are below.
166 checkMatch(["@@kzar.co.uk$generichide"], "http://kzar.co.uk/dave", "GENERICH IDE", "kzar.co.uk", false, null, false, "@@kzar.co.uk$generichide"); 168 checkMatch(["@@foo.com$generichide"], "http://foo.com/bar", "GENERICHIDE", " foo.com", false, null, false, "@@foo.com$generichide");
167 checkMatch(["@@kzar.co.uk$genericblock"], "http://kzar.co.uk/dave", "GENERIC BLOCK", "kzar.co.uk", false, null, false, "@@kzar.co.uk$genericblock"); 169 checkMatch(["@@foo.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", false, null, false, "@@foo.com$genericblock");
168 checkMatch(["@@foo.com$generichide"], "http://kzar.co.uk/dave", "GENERICHIDE ", "kzar.co.uk", false, null, false, null); 170 checkMatch(["@@bar.com$generichide"], "http://foo.com/bar", "GENERICHIDE", " foo.com", false, null, false, null);
169 checkMatch(["@@foo.com$genericblock"], "http://kzar.co.uk/dave", "GENERICBLO CK", "kzar.co.uk", false, null, false, null); 171 checkMatch(["@@bar.com$genericblock"], "http://foo.com/bar", "GENERICBLOCK", "foo.com", false, null, false, null);
170 checkMatch(["###dave"], "http://kzar.co.uk/dave", "IMAGE", "kzar.co.uk", fal se, null, true, null); 172 checkMatch(["/bar"], "http://foo.com/bar", "IMAGE", "foo.com", false, null, true, null);
171 checkMatch(["kzar.co.uk###dave"], "http://kzar.co.uk/dave", "IMAGE", "kzar.c o.uk", false, null, true, null); 173 checkMatch(["/bar$domain=foo.com"], "http://foo.com/bar", "IMAGE", "foo.com" , false, null, true, "/bar$domain=foo.com");
172 checkMatch(["/dave$domain=kzar.co.uk"], "http://kzar.co.uk/dave", "IMAGE", " kzar.co.uk", false, null, true, "/dave$domain=kzar.co.uk");
173 checkMatch(["/dave"], "http://kzar.co.uk/dave", "IMAGE", "kzar.co.uk", false , null, true, null);
174 }); 174 });
175 175
176 test("Result cache checks", function() 176 test("Result cache checks", function()
177 { 177 {
178 let matcher = new CombinedMatcher(); 178 let matcher = new CombinedMatcher();
179 matcher.add(Filter.fromText("abc$image")); 179 matcher.add(Filter.fromText("abc$image"));
180 matcher.add(Filter.fromText("abc$script")); 180 matcher.add(Filter.fromText("abc$script"));
181 matcher.add(Filter.fromText("abc$~image,~script,~document")); 181 matcher.add(Filter.fromText("abc$~image,~script,~media"));
182 matcher.add(Filter.fromText("cba$third-party")); 182 matcher.add(Filter.fromText("cba$third-party"));
183 matcher.add(Filter.fromText("cba$~third-party,~script")); 183 matcher.add(Filter.fromText("cba$~third-party,~script"));
184 matcher.add(Filter.fromText("http://def$image")); 184 matcher.add(Filter.fromText("http://def$image"));
185 matcher.add(Filter.fromText("http://def$script")); 185 matcher.add(Filter.fromText("http://def$script"));
186 matcher.add(Filter.fromText("http://def$~image,~script,~document")); 186 matcher.add(Filter.fromText("http://def$~image,~script,~media"));
187 matcher.add(Filter.fromText("http://fed$third-party")); 187 matcher.add(Filter.fromText("http://fed$third-party"));
188 matcher.add(Filter.fromText("http://fed$~third-party,~script")); 188 matcher.add(Filter.fromText("http://fed$~third-party,~script"));
189 189
190 cacheCheck(matcher, "http://abc", "IMAGE", null, false, "abc$image"); 190 cacheCheck(matcher, "http://abc", "IMAGE", null, false, "abc$image");
191 cacheCheck(matcher, "http://abc", "SCRIPT", null, false, "abc$script"); 191 cacheCheck(matcher, "http://abc", "SCRIPT", null, false, "abc$script");
192 cacheCheck(matcher, "http://abc", "OTHER", null, false, "abc$~image,~script, ~document"); 192 cacheCheck(matcher, "http://abc", "OTHER", null, false, "abc$~image,~script, ~media");
193 cacheCheck(matcher, "http://cba", "IMAGE", null, false, "cba$~third-party,~s cript"); 193 cacheCheck(matcher, "http://cba", "IMAGE", null, false, "cba$~third-party,~s cript");
194 cacheCheck(matcher, "http://cba", "IMAGE", null, true, "cba$third-party"); 194 cacheCheck(matcher, "http://cba", "IMAGE", null, true, "cba$third-party");
195 cacheCheck(matcher, "http://def", "IMAGE", null, false, "http://def$image"); 195 cacheCheck(matcher, "http://def", "IMAGE", null, false, "http://def$image");
196 cacheCheck(matcher, "http://def", "SCRIPT", null, false, "http://def$script" ); 196 cacheCheck(matcher, "http://def", "SCRIPT", null, false, "http://def$script" );
197 cacheCheck(matcher, "http://def", "OTHER", null, false, "http://def$~image,~ script,~document"); 197 cacheCheck(matcher, "http://def", "OTHER", null, false, "http://def$~image,~ script,~media");
198 cacheCheck(matcher, "http://fed", "IMAGE", null, false, "http://fed$~third-p arty,~script"); 198 cacheCheck(matcher, "http://fed", "IMAGE", null, false, "http://fed$~third-p arty,~script");
199 cacheCheck(matcher, "http://fed", "IMAGE", null, true, "http://fed$third-par ty"); 199 cacheCheck(matcher, "http://fed", "IMAGE", null, true, "http://fed$third-par ty");
200 cacheCheck(matcher, "http://abc_cba", "DOCUMENT", null, false, "cba$~third-p arty,~script"); 200 cacheCheck(matcher, "http://abc_cba", "MEDIA", null, false, "cba$~third-part y,~script");
201 cacheCheck(matcher, "http://abc_cba", "DOCUMENT", null, true, "cba$third-par ty"); 201 cacheCheck(matcher, "http://abc_cba", "MEDIA", null, true, "cba$third-party" );
202 cacheCheck(matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script"); 202 cacheCheck(matcher, "http://abc_cba", "SCRIPT", null, false, "abc$script");
203 cacheCheck(matcher, "http://def?http://fed", "DOCUMENT", null, false, "http: //fed$~third-party,~script"); 203 cacheCheck(matcher, "http://def?http://fed", "MEDIA", null, false, "http://f ed$~third-party,~script");
204 cacheCheck(matcher, "http://def?http://fed", "DOCUMENT", null, true, "http:/ /fed$third-party"); 204 cacheCheck(matcher, "http://def?http://fed", "MEDIA", null, true, "http://fe d$third-party");
205 cacheCheck(matcher, "http://def?http://fed", "SCRIPT", null, false, "http:// def$script"); 205 cacheCheck(matcher, "http://def?http://fed", "SCRIPT", null, false, "http:// def$script");
206 }); 206 });
207 })(); 207 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld