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

Side by Side Diff: test/elemHide.js

Issue 29349187: Issue 4167 - getSelectorsForDomain criteria + keys (Closed)
Patch Set: Addressed feedback Created Sept. 20, 2016, 6:32 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
« lib/elemHide.js ('K') | « lib/elemHide.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 23 matching lines...) Expand all
34 34
35 function normalizeSelectors(selectors) 35 function normalizeSelectors(selectors)
36 { 36 {
37 // getSelectorsForDomain is currently allowed to return duplicate selectors 37 // getSelectorsForDomain is currently allowed to return duplicate selectors
38 // for performance reasons, so we need to remove duplicates here. 38 // for performance reasons, so we need to remove duplicates here.
39 return selectors.sort().filter((selector, index, selectors) => 39 return selectors.sort().filter((selector, index, selectors) =>
40 { 40 {
41 return index == 0 || selector != selectors[index - 1]; 41 return index == 0 || selector != selectors[index - 1];
42 }); 42 });
43 } 43 }
44 function selectorsEqual(domain, expectedSelectors, specificOnly) 44 function testResult(domain, expectedSelectors, criteria)
45 { 45 {
46 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors);
47
48 // Test without filter keys
46 test.deepEqual( 49 test.deepEqual(
47 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, specificOnly)), 50 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, criteria)),
48 normalizeSelectors(expectedSelectors) 51 normalizedExpectedSelectors
49 ); 52 );
53
54 // With filter keys
55 let [selectors, filterKeys] = ElemHide.getSelectorsForDomain(domain, criteri a,
56 true);
57 test.deepEqual(filterKeys.map(k => ElemHide.getFilterByKey(k).selector),
58 selectors);
59 test.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors);
50 } 60 }
51 61
52 selectorsEqual("", []); 62 testResult("", []);
53 63
54 addFilter("~foo.example.com,example.com##foo"); 64 addFilter("~foo.example.com,example.com##foo");
55 selectorsEqual("barfoo.example.com", ["foo"]); 65 testResult("barfoo.example.com", ["foo"]);
56 selectorsEqual("bar.foo.example.com", []); 66 testResult("bar.foo.example.com", []);
57 selectorsEqual("foo.example.com", []); 67 testResult("foo.example.com", []);
58 selectorsEqual("example.com", ["foo"]); 68 testResult("example.com", ["foo"]);
59 selectorsEqual("com", []); 69 testResult("com", []);
60 selectorsEqual("", []); 70 testResult("", []);
61 71
62 addFilter("foo.example.com##turnip"); 72 addFilter("foo.example.com##turnip");
63 selectorsEqual("foo.example.com", ["turnip"]); 73 testResult("foo.example.com", ["turnip"]);
64 selectorsEqual("example.com", ["foo"]); 74 testResult("example.com", ["foo"]);
65 selectorsEqual("com", []); 75 testResult("com", []);
66 selectorsEqual("", []); 76 testResult("", []);
67 77
68 addFilter("example.com#@#foo"); 78 addFilter("example.com#@#foo");
69 selectorsEqual("foo.example.com", ["turnip"]); 79 testResult("foo.example.com", ["turnip"]);
70 selectorsEqual("example.com", []); 80 testResult("example.com", []);
71 selectorsEqual("com", []); 81 testResult("com", []);
72 selectorsEqual("", []); 82 testResult("", []);
73 83
74 addFilter("com##bar"); 84 addFilter("com##bar");
75 selectorsEqual("foo.example.com", ["turnip", "bar"]); 85 testResult("foo.example.com", ["turnip", "bar"]);
76 selectorsEqual("example.com", ["bar"]); 86 testResult("example.com", ["bar"]);
77 selectorsEqual("com", ["bar"]); 87 testResult("com", ["bar"]);
78 selectorsEqual("", []); 88 testResult("", []);
79 89
80 addFilter("example.com#@#bar"); 90 addFilter("example.com#@#bar");
81 selectorsEqual("foo.example.com", ["turnip"]); 91 testResult("foo.example.com", ["turnip"]);
82 selectorsEqual("example.com", []); 92 testResult("example.com", []);
83 selectorsEqual("com", ["bar"]); 93 testResult("com", ["bar"]);
84 selectorsEqual("", []); 94 testResult("", []);
85 95
86 removeFilter("example.com#@#foo"); 96 removeFilter("example.com#@#foo");
87 selectorsEqual("foo.example.com", ["turnip"]); 97 testResult("foo.example.com", ["turnip"]);
88 selectorsEqual("example.com", ["foo"]); 98 testResult("example.com", ["foo"]);
89 selectorsEqual("com", ["bar"]); 99 testResult("com", ["bar"]);
90 selectorsEqual("", []); 100 testResult("", []);
91 101
92 removeFilter("example.com#@#bar"); 102 removeFilter("example.com#@#bar");
93 selectorsEqual("foo.example.com", ["turnip", "bar"]); 103 testResult("foo.example.com", ["turnip", "bar"]);
94 selectorsEqual("example.com", ["foo", "bar"]); 104 testResult("example.com", ["foo", "bar"]);
95 selectorsEqual("com", ["bar"]); 105 testResult("com", ["bar"]);
96 selectorsEqual("", []); 106 testResult("", []);
97 107
98 addFilter("##generic"); 108 addFilter("##generic");
99 selectorsEqual("foo.example.com", ["turnip", "bar", "generic"]); 109 testResult("foo.example.com", ["turnip", "bar", "generic"]);
100 selectorsEqual("example.com", ["foo", "bar", "generic"]); 110 testResult("example.com", ["foo", "bar", "generic"]);
101 selectorsEqual("com", ["bar", "generic"]); 111 testResult("com", ["bar", "generic"]);
102 selectorsEqual("", ["generic"]); 112 testResult("", ["generic"]);
103 selectorsEqual("foo.example.com", ["turnip", "bar"], true); 113 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY);
104 selectorsEqual("example.com", ["foo", "bar"], true); 114 testResult("example.com", ["foo", "bar"], ElemHide.SPECIFIC_ONLY);
105 selectorsEqual("com", ["bar"], true); 115 testResult("com", ["bar"], ElemHide.SPECIFIC_ONLY);
106 selectorsEqual("", [], true); 116 testResult("", [], ElemHide.SPECIFIC_ONLY);
107 removeFilter("##generic"); 117 removeFilter("##generic");
108 118
109 addFilter("~adblockplus.org##example"); 119 addFilter("~adblockplus.org##example");
110 selectorsEqual("adblockplus.org", []); 120 testResult("adblockplus.org", []);
111 selectorsEqual("", ["example"]); 121 testResult("", ["example"]);
112 selectorsEqual("foo.example.com", ["turnip", "bar", "example"]); 122 testResult("foo.example.com", ["turnip", "bar", "example"]);
113 selectorsEqual("foo.example.com", ["turnip", "bar"], true); 123 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY);
114 removeFilter("~adblockplus.org##example"); 124 removeFilter("~adblockplus.org##example");
115 125
116 removeFilter("~foo.example.com,example.com##foo"); 126 removeFilter("~foo.example.com,example.com##foo");
117 selectorsEqual("foo.example.com", ["turnip", "bar"]); 127 testResult("foo.example.com", ["turnip", "bar"]);
118 selectorsEqual("example.com", ["bar"]); 128 testResult("example.com", ["bar"]);
119 selectorsEqual("com", ["bar"]); 129 testResult("com", ["bar"]);
120 selectorsEqual("", []); 130 testResult("", []);
121 131
122 removeFilter("com##bar"); 132 removeFilter("com##bar");
123 selectorsEqual("foo.example.com", ["turnip"]); 133 testResult("foo.example.com", ["turnip"]);
124 selectorsEqual("example.com", []); 134 testResult("example.com", []);
125 selectorsEqual("com", []); 135 testResult("com", []);
126 selectorsEqual("", []); 136 testResult("", []);
127 137
128 removeFilter("foo.example.com##turnip"); 138 removeFilter("foo.example.com##turnip");
129 selectorsEqual("foo.example.com", []); 139 testResult("foo.example.com", []);
130 selectorsEqual("example.com", []); 140 testResult("example.com", []);
131 selectorsEqual("com", []); 141 testResult("com", []);
132 selectorsEqual("", []); 142 testResult("", []);
133 143
134 addFilter("example.com##dupe"); 144 addFilter("example.com##dupe");
135 addFilter("example.com##dupe"); 145 addFilter("example.com##dupe");
136 selectorsEqual("example.com", ["dupe"]); 146 testResult("example.com", ["dupe"]);
137 removeFilter("example.com##dupe"); 147 removeFilter("example.com##dupe");
138 selectorsEqual("example.com", []); 148 testResult("example.com", []);
139 removeFilter("example.com##dupe"); 149 removeFilter("example.com##dupe");
140 150
141 addFilter("~foo.example.com,example.com##foo"); 151 addFilter("~foo.example.com,example.com##foo");
142 152
143 addFilter("##foo"); 153 addFilter("##foo");
144 selectorsEqual("foo.example.com", ["foo"]); 154 testResult("foo.example.com", ["foo"]);
145 selectorsEqual("example.com", ["foo"]); 155 testResult("example.com", ["foo"]);
146 selectorsEqual("com", ["foo"]); 156 testResult("com", ["foo"]);
147 selectorsEqual("", ["foo"]); 157 testResult("", ["foo"]);
148 removeFilter("##foo"); 158 removeFilter("##foo");
149 159
150 addFilter("example.org##foo"); 160 addFilter("example.org##foo");
151 selectorsEqual("foo.example.com", []); 161 testResult("foo.example.com", []);
152 selectorsEqual("example.com", ["foo"]); 162 testResult("example.com", ["foo"]);
153 selectorsEqual("com", []); 163 testResult("com", []);
154 selectorsEqual("", []); 164 testResult("", []);
155 removeFilter("example.org##foo"); 165 removeFilter("example.org##foo");
156 166
157 addFilter("~example.com##foo"); 167 addFilter("~example.com##foo");
158 selectorsEqual("foo.example.com", []); 168 testResult("foo.example.com", []);
159 selectorsEqual("example.com", ["foo"]); 169 testResult("example.com", ["foo"]);
160 selectorsEqual("com", ["foo"]); 170 testResult("com", ["foo"]);
161 selectorsEqual("", ["foo"]); 171 testResult("", ["foo"]);
162 removeFilter("example.org##foo"); 172 removeFilter("~example.com##foo");
173
174 removeFilter("~foo.example.com,example.com##foo");
175
176 // Test criteria
177 addFilter("##hello");
178 addFilter("~example.com##world");
179 addFilter("foo.com##specific");
180 testResult("foo.com", ["specific"], ElemHide.SPECIFIC_ONLY);
181 testResult("foo.com", ["specific", "world"], ElemHide.NO_UNCONDITIONAL);
182 testResult("foo.com", ["hello", "specific", "world"], ElemHide.ALL_MATCHING);
183 testResult("foo.com", ["hello", "specific", "world"]);
184 removeFilter("foo.com##specific");
185 removeFilter("~example.com##world");
186 removeFilter("##hello");
187
188 addFilter("##hello");
189 addFilter("foo.com##hello");
190 testResult("foo.com", ["hello"]);
191 removeFilter("foo.com##hello");
192 testResult("foo.com", ["hello"]);
193 removeFilter("##hello");
Wladimir Palant 2016/09/26 14:56:54 How about: testResult("foo.com", []); This is
kzar 2016/09/27 12:40:22 Done.
194
195 addFilter("##hello");
196 addFilter("foo.com##hello");
197 testResult("foo.com", ["hello"]);
198 removeFilter("##hello");
199 testResult("foo.com", ["hello"]);
200 removeFilter("foo.com##hello");
Wladimir Palant 2016/09/26 14:56:54 Same here, how about testing the final state?
kzar 2016/09/27 12:40:22 Done.
201
Wladimir Palant 2016/09/26 14:56:54 How about testing the combination of ##hello and f
kzar 2016/09/27 12:40:22 Done.
202 // Advanced filter keys test
203 testResult("", []);
204 addFilter("##dupe");
205 addFilter(",,##dupe");
206 addFilter(",,,##dupe");
207 addFilter("foo.com##dupe");
208 testResult("", ["dupe"]);
209 removeFilter(",,,##dupe");
210 testResult("", ["dupe"]);
211 removeFilter("foo.com##dupe");
212 testResult("", ["dupe"]);
213 removeFilter(",,##dupe");
214 testResult("", ["dupe"]);
215 removeFilter("##dupe");
216 testResult("", []);
163 217
164 test.done(); 218 test.done();
165 }; 219 };
OLDNEW
« lib/elemHide.js ('K') | « lib/elemHide.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld