OLD | NEW |
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 Loading... |
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 let allSelectors = 0; |
| 63 let noUnconditional = 1; |
| 64 let specificOnly = 2; |
| 65 |
| 66 testResult("", []); |
53 | 67 |
54 addFilter("~foo.example.com,example.com##foo"); | 68 addFilter("~foo.example.com,example.com##foo"); |
55 selectorsEqual("barfoo.example.com", ["foo"]); | 69 testResult("barfoo.example.com", ["foo"]); |
56 selectorsEqual("bar.foo.example.com", []); | 70 testResult("bar.foo.example.com", []); |
57 selectorsEqual("foo.example.com", []); | 71 testResult("foo.example.com", []); |
58 selectorsEqual("example.com", ["foo"]); | 72 testResult("example.com", ["foo"]); |
59 selectorsEqual("com", []); | 73 testResult("com", []); |
60 selectorsEqual("", []); | 74 testResult("", []); |
61 | 75 |
62 addFilter("foo.example.com##turnip"); | 76 addFilter("foo.example.com##turnip"); |
63 selectorsEqual("foo.example.com", ["turnip"]); | 77 testResult("foo.example.com", ["turnip"]); |
64 selectorsEqual("example.com", ["foo"]); | 78 testResult("example.com", ["foo"]); |
65 selectorsEqual("com", []); | 79 testResult("com", []); |
66 selectorsEqual("", []); | 80 testResult("", []); |
67 | 81 |
68 addFilter("example.com#@#foo"); | 82 addFilter("example.com#@#foo"); |
69 selectorsEqual("foo.example.com", ["turnip"]); | 83 testResult("foo.example.com", ["turnip"]); |
70 selectorsEqual("example.com", []); | 84 testResult("example.com", []); |
71 selectorsEqual("com", []); | 85 testResult("com", []); |
72 selectorsEqual("", []); | 86 testResult("", []); |
73 | 87 |
74 addFilter("com##bar"); | 88 addFilter("com##bar"); |
75 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 89 testResult("foo.example.com", ["turnip", "bar"]); |
76 selectorsEqual("example.com", ["bar"]); | 90 testResult("example.com", ["bar"]); |
77 selectorsEqual("com", ["bar"]); | 91 testResult("com", ["bar"]); |
78 selectorsEqual("", []); | 92 testResult("", []); |
79 | 93 |
80 addFilter("example.com#@#bar"); | 94 addFilter("example.com#@#bar"); |
81 selectorsEqual("foo.example.com", ["turnip"]); | 95 testResult("foo.example.com", ["turnip"]); |
82 selectorsEqual("example.com", []); | 96 testResult("example.com", []); |
83 selectorsEqual("com", ["bar"]); | 97 testResult("com", ["bar"]); |
84 selectorsEqual("", []); | 98 testResult("", []); |
85 | 99 |
86 removeFilter("example.com#@#foo"); | 100 removeFilter("example.com#@#foo"); |
87 selectorsEqual("foo.example.com", ["turnip"]); | 101 testResult("foo.example.com", ["turnip"]); |
88 selectorsEqual("example.com", ["foo"]); | 102 testResult("example.com", ["foo"]); |
89 selectorsEqual("com", ["bar"]); | 103 testResult("com", ["bar"]); |
90 selectorsEqual("", []); | 104 testResult("", []); |
91 | 105 |
92 removeFilter("example.com#@#bar"); | 106 removeFilter("example.com#@#bar"); |
93 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 107 testResult("foo.example.com", ["turnip", "bar"]); |
94 selectorsEqual("example.com", ["foo", "bar"]); | 108 testResult("example.com", ["foo", "bar"]); |
95 selectorsEqual("com", ["bar"]); | 109 testResult("com", ["bar"]); |
96 selectorsEqual("", []); | 110 testResult("", []); |
97 | 111 |
98 addFilter("##generic"); | 112 addFilter("##generic"); |
99 selectorsEqual("foo.example.com", ["turnip", "bar", "generic"]); | 113 testResult("foo.example.com", ["turnip", "bar", "generic"]); |
100 selectorsEqual("example.com", ["foo", "bar", "generic"]); | 114 testResult("example.com", ["foo", "bar", "generic"]); |
101 selectorsEqual("com", ["bar", "generic"]); | 115 testResult("com", ["bar", "generic"]); |
102 selectorsEqual("", ["generic"]); | 116 testResult("", ["generic"]); |
103 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 117 testResult("foo.example.com", ["turnip", "bar"], specificOnly); |
104 selectorsEqual("example.com", ["foo", "bar"], true); | 118 testResult("example.com", ["foo", "bar"], specificOnly); |
105 selectorsEqual("com", ["bar"], true); | 119 testResult("com", ["bar"], specificOnly); |
106 selectorsEqual("", [], true); | 120 testResult("", [], specificOnly); |
107 removeFilter("##generic"); | 121 removeFilter("##generic"); |
108 | 122 |
109 addFilter("~adblockplus.org##example"); | 123 addFilter("~adblockplus.org##example"); |
110 selectorsEqual("adblockplus.org", []); | 124 testResult("adblockplus.org", []); |
111 selectorsEqual("", ["example"]); | 125 testResult("", ["example"]); |
112 selectorsEqual("foo.example.com", ["turnip", "bar", "example"]); | 126 testResult("foo.example.com", ["turnip", "bar", "example"]); |
113 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 127 testResult("foo.example.com", ["turnip", "bar"], specificOnly); |
114 removeFilter("~adblockplus.org##example"); | 128 removeFilter("~adblockplus.org##example"); |
115 | 129 |
116 removeFilter("~foo.example.com,example.com##foo"); | 130 removeFilter("~foo.example.com,example.com##foo"); |
117 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 131 testResult("foo.example.com", ["turnip", "bar"]); |
118 selectorsEqual("example.com", ["bar"]); | 132 testResult("example.com", ["bar"]); |
119 selectorsEqual("com", ["bar"]); | 133 testResult("com", ["bar"]); |
120 selectorsEqual("", []); | 134 testResult("", []); |
121 | 135 |
122 removeFilter("com##bar"); | 136 removeFilter("com##bar"); |
123 selectorsEqual("foo.example.com", ["turnip"]); | 137 testResult("foo.example.com", ["turnip"]); |
124 selectorsEqual("example.com", []); | 138 testResult("example.com", []); |
125 selectorsEqual("com", []); | 139 testResult("com", []); |
126 selectorsEqual("", []); | 140 testResult("", []); |
127 | 141 |
128 removeFilter("foo.example.com##turnip"); | 142 removeFilter("foo.example.com##turnip"); |
129 selectorsEqual("foo.example.com", []); | 143 testResult("foo.example.com", []); |
130 selectorsEqual("example.com", []); | 144 testResult("example.com", []); |
131 selectorsEqual("com", []); | 145 testResult("com", []); |
132 selectorsEqual("", []); | 146 testResult("", []); |
133 | 147 |
134 addFilter("example.com##dupe"); | 148 addFilter("example.com##dupe"); |
135 addFilter("example.com##dupe"); | 149 addFilter("example.com##dupe"); |
136 selectorsEqual("example.com", ["dupe"]); | 150 testResult("example.com", ["dupe"]); |
137 removeFilter("example.com##dupe"); | 151 removeFilter("example.com##dupe"); |
138 selectorsEqual("example.com", []); | 152 testResult("example.com", []); |
139 removeFilter("example.com##dupe"); | 153 removeFilter("example.com##dupe"); |
140 | 154 |
141 addFilter("~foo.example.com,example.com##foo"); | 155 addFilter("~foo.example.com,example.com##foo"); |
142 | 156 |
143 addFilter("##foo"); | 157 addFilter("##foo"); |
144 selectorsEqual("foo.example.com", ["foo"]); | 158 testResult("foo.example.com", ["foo"]); |
145 selectorsEqual("example.com", ["foo"]); | 159 testResult("example.com", ["foo"]); |
146 selectorsEqual("com", ["foo"]); | 160 testResult("com", ["foo"]); |
147 selectorsEqual("", ["foo"]); | 161 testResult("", ["foo"]); |
148 removeFilter("##foo"); | 162 removeFilter("##foo"); |
149 | 163 |
150 addFilter("example.org##foo"); | 164 addFilter("example.org##foo"); |
151 selectorsEqual("foo.example.com", []); | 165 testResult("foo.example.com", []); |
152 selectorsEqual("example.com", ["foo"]); | 166 testResult("example.com", ["foo"]); |
153 selectorsEqual("com", []); | 167 testResult("com", []); |
154 selectorsEqual("", []); | 168 testResult("", []); |
155 removeFilter("example.org##foo"); | 169 removeFilter("example.org##foo"); |
156 | 170 |
157 addFilter("~example.com##foo"); | 171 addFilter("~example.com##foo"); |
158 selectorsEqual("foo.example.com", []); | 172 testResult("foo.example.com", []); |
159 selectorsEqual("example.com", ["foo"]); | 173 testResult("example.com", ["foo"]); |
160 selectorsEqual("com", ["foo"]); | 174 testResult("com", ["foo"]); |
161 selectorsEqual("", ["foo"]); | 175 testResult("", ["foo"]); |
162 removeFilter("example.org##foo"); | 176 removeFilter("~example.com##foo"); |
| 177 |
| 178 removeFilter("~foo.example.com,example.com##foo"); |
| 179 |
| 180 // Test criteria |
| 181 addFilter("##hello"); |
| 182 addFilter("~example.com##world"); |
| 183 addFilter("foo.com##specific"); |
| 184 testResult("foo.com", ["specific"], specificOnly); |
| 185 testResult("foo.com", ["specific", "world"], noUnconditional); |
| 186 testResult("foo.com", ["hello", "specific", "world"], allSelectors); |
| 187 testResult("foo.com", ["hello", "specific", "world"]); |
| 188 removeFilter("foo.com##specific"); |
| 189 removeFilter("~example.com##world"); |
| 190 removeFilter("##hello"); |
| 191 |
| 192 // Advanced criteria test |
| 193 addFilter("##hello"); |
| 194 addFilter("foo.com##hello"); |
| 195 testResult("foo.com", ["hello"]); |
| 196 removeFilter("foo.com##hello"); |
| 197 testResult("foo.com", ["hello"]); |
| 198 removeFilter("##hello"); |
| 199 |
| 200 // Advanced filter keys test |
| 201 testResult("", []); |
| 202 addFilter("##dupe"); |
| 203 addFilter(",,##dupe"); |
| 204 addFilter(",,,##dupe"); |
| 205 testResult("", ["dupe"]); |
| 206 removeFilter(",,,##dupe"); |
| 207 testResult("", ["dupe"]); |
| 208 removeFilter(",,##dupe"); |
| 209 testResult("", ["dupe"]); |
| 210 removeFilter("##dupe"); |
| 211 testResult("", []); |
163 | 212 |
164 test.done(); | 213 test.done(); |
165 }; | 214 }; |
OLD | NEW |