| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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 noUnconditional, provideFilterKeys) | |
| 46 { | 45 { |
| 46 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); | |
| 47 | |
| 48 // Test without filter keys | |
| 47 test.deepEqual( | 49 test.deepEqual( |
| 48 normalizeSelectors(ElemHide.getSelectorsForDomain( | 50 normalizeSelectors(ElemHide.getSelectorsForDomain(domain, criteria)), |
| 49 domain, specificOnly, noUnconditional, provideFilterKeys | 51 normalizedExpectedSelectors |
| 50 )), | |
| 51 normalizeSelectors(expectedSelectors) | |
| 52 ); | 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); | |
| 53 } | 60 } |
| 54 | 61 |
| 55 selectorsEqual("", []); | 62 testResult("", []); |
| 56 | 63 |
| 57 addFilter("~foo.example.com,example.com##foo"); | 64 addFilter("~foo.example.com,example.com##foo"); |
| 58 selectorsEqual("barfoo.example.com", ["foo"]); | 65 testResult("barfoo.example.com", ["foo"]); |
| 59 selectorsEqual("bar.foo.example.com", []); | 66 testResult("bar.foo.example.com", []); |
| 60 selectorsEqual("foo.example.com", []); | 67 testResult("foo.example.com", []); |
| 61 selectorsEqual("example.com", ["foo"]); | 68 testResult("example.com", ["foo"]); |
| 62 selectorsEqual("com", []); | 69 testResult("com", []); |
| 63 selectorsEqual("", []); | 70 testResult("", []); |
| 64 | 71 |
| 65 addFilter("foo.example.com##turnip"); | 72 addFilter("foo.example.com##turnip"); |
| 66 selectorsEqual("foo.example.com", ["turnip"]); | 73 testResult("foo.example.com", ["turnip"]); |
| 67 selectorsEqual("example.com", ["foo"]); | 74 testResult("example.com", ["foo"]); |
| 68 selectorsEqual("com", []); | 75 testResult("com", []); |
| 69 selectorsEqual("", []); | 76 testResult("", []); |
| 70 | 77 |
| 71 addFilter("example.com#@#foo"); | 78 addFilter("example.com#@#foo"); |
| 72 selectorsEqual("foo.example.com", ["turnip"]); | 79 testResult("foo.example.com", ["turnip"]); |
| 73 selectorsEqual("example.com", []); | 80 testResult("example.com", []); |
| 74 selectorsEqual("com", []); | 81 testResult("com", []); |
| 75 selectorsEqual("", []); | 82 testResult("", []); |
| 76 | 83 |
| 77 addFilter("com##bar"); | 84 addFilter("com##bar"); |
| 78 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 85 testResult("foo.example.com", ["turnip", "bar"]); |
| 79 selectorsEqual("example.com", ["bar"]); | 86 testResult("example.com", ["bar"]); |
| 80 selectorsEqual("com", ["bar"]); | 87 testResult("com", ["bar"]); |
| 81 selectorsEqual("", []); | 88 testResult("", []); |
| 82 | 89 |
| 83 addFilter("example.com#@#bar"); | 90 addFilter("example.com#@#bar"); |
| 84 selectorsEqual("foo.example.com", ["turnip"]); | 91 testResult("foo.example.com", ["turnip"]); |
| 85 selectorsEqual("example.com", []); | 92 testResult("example.com", []); |
| 86 selectorsEqual("com", ["bar"]); | 93 testResult("com", ["bar"]); |
| 87 selectorsEqual("", []); | 94 testResult("", []); |
| 88 | 95 |
| 89 removeFilter("example.com#@#foo"); | 96 removeFilter("example.com#@#foo"); |
| 90 selectorsEqual("foo.example.com", ["turnip"]); | 97 testResult("foo.example.com", ["turnip"]); |
| 91 selectorsEqual("example.com", ["foo"]); | 98 testResult("example.com", ["foo"]); |
| 92 selectorsEqual("com", ["bar"]); | 99 testResult("com", ["bar"]); |
| 93 selectorsEqual("", []); | 100 testResult("", []); |
| 94 | 101 |
| 95 removeFilter("example.com#@#bar"); | 102 removeFilter("example.com#@#bar"); |
| 96 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 103 testResult("foo.example.com", ["turnip", "bar"]); |
| 97 selectorsEqual("example.com", ["foo", "bar"]); | 104 testResult("example.com", ["foo", "bar"]); |
| 98 selectorsEqual("com", ["bar"]); | 105 testResult("com", ["bar"]); |
| 99 selectorsEqual("", []); | 106 testResult("", []); |
| 100 | 107 |
| 101 addFilter("##generic"); | 108 addFilter("##generic"); |
| 102 selectorsEqual("foo.example.com", ["turnip", "bar", "generic"]); | 109 testResult("foo.example.com", ["turnip", "bar", "generic"]); |
| 103 selectorsEqual("example.com", ["foo", "bar", "generic"]); | 110 testResult("example.com", ["foo", "bar", "generic"]); |
| 104 selectorsEqual("com", ["bar", "generic"]); | 111 testResult("com", ["bar", "generic"]); |
| 105 selectorsEqual("", ["generic"]); | 112 testResult("", ["generic"]); |
| 106 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 113 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY); |
| 107 selectorsEqual("example.com", ["foo", "bar"], true); | 114 testResult("example.com", ["foo", "bar"], ElemHide.SPECIFIC_ONLY); |
| 108 selectorsEqual("com", ["bar"], true); | 115 testResult("com", ["bar"], ElemHide.SPECIFIC_ONLY); |
| 109 selectorsEqual("", [], true); | 116 testResult("", [], ElemHide.SPECIFIC_ONLY); |
| 110 removeFilter("##generic"); | 117 removeFilter("##generic"); |
| 111 | 118 |
| 112 addFilter("~adblockplus.org##example"); | 119 addFilter("~adblockplus.org##example"); |
| 113 selectorsEqual("adblockplus.org", []); | 120 testResult("adblockplus.org", []); |
| 114 selectorsEqual("", ["example"]); | 121 testResult("", ["example"]); |
| 115 selectorsEqual("foo.example.com", ["turnip", "bar", "example"]); | 122 testResult("foo.example.com", ["turnip", "bar", "example"]); |
| 116 selectorsEqual("foo.example.com", ["turnip", "bar"], true); | 123 testResult("foo.example.com", ["turnip", "bar"], ElemHide.SPECIFIC_ONLY); |
| 117 removeFilter("~adblockplus.org##example"); | 124 removeFilter("~adblockplus.org##example"); |
| 118 | 125 |
| 119 removeFilter("~foo.example.com,example.com##foo"); | 126 removeFilter("~foo.example.com,example.com##foo"); |
| 120 selectorsEqual("foo.example.com", ["turnip", "bar"]); | 127 testResult("foo.example.com", ["turnip", "bar"]); |
| 121 selectorsEqual("example.com", ["bar"]); | 128 testResult("example.com", ["bar"]); |
| 122 selectorsEqual("com", ["bar"]); | 129 testResult("com", ["bar"]); |
| 123 selectorsEqual("", []); | 130 testResult("", []); |
| 124 | 131 |
| 125 removeFilter("com##bar"); | 132 removeFilter("com##bar"); |
| 126 selectorsEqual("foo.example.com", ["turnip"]); | 133 testResult("foo.example.com", ["turnip"]); |
| 127 selectorsEqual("example.com", []); | 134 testResult("example.com", []); |
| 128 selectorsEqual("com", []); | 135 testResult("com", []); |
| 129 selectorsEqual("", []); | 136 testResult("", []); |
| 130 | 137 |
| 131 removeFilter("foo.example.com##turnip"); | 138 removeFilter("foo.example.com##turnip"); |
| 132 selectorsEqual("foo.example.com", []); | 139 testResult("foo.example.com", []); |
| 133 selectorsEqual("example.com", []); | 140 testResult("example.com", []); |
| 134 selectorsEqual("com", []); | 141 testResult("com", []); |
| 135 selectorsEqual("", []); | 142 testResult("", []); |
| 136 | 143 |
| 137 addFilter("example.com##dupe"); | 144 addFilter("example.com##dupe"); |
| 138 addFilter("example.com##dupe"); | 145 addFilter("example.com##dupe"); |
| 139 selectorsEqual("example.com", ["dupe"]); | 146 testResult("example.com", ["dupe"]); |
| 140 removeFilter("example.com##dupe"); | 147 removeFilter("example.com##dupe"); |
| 141 selectorsEqual("example.com", []); | 148 testResult("example.com", []); |
| 142 removeFilter("example.com##dupe"); | 149 removeFilter("example.com##dupe"); |
| 143 | 150 |
| 144 addFilter("~foo.example.com,example.com##foo"); | 151 addFilter("~foo.example.com,example.com##foo"); |
| 145 | 152 |
| 146 addFilter("##foo"); | 153 addFilter("##foo"); |
| 147 selectorsEqual("foo.example.com", ["foo"]); | 154 testResult("foo.example.com", ["foo"]); |
| 148 selectorsEqual("example.com", ["foo"]); | 155 testResult("example.com", ["foo"]); |
| 149 selectorsEqual("com", ["foo"]); | 156 testResult("com", ["foo"]); |
| 150 selectorsEqual("", ["foo"]); | 157 testResult("", ["foo"]); |
| 151 removeFilter("##foo"); | 158 removeFilter("##foo"); |
| 152 | 159 |
| 153 addFilter("example.org##foo"); | 160 addFilter("example.org##foo"); |
| 154 selectorsEqual("foo.example.com", []); | 161 testResult("foo.example.com", []); |
| 155 selectorsEqual("example.com", ["foo"]); | 162 testResult("example.com", ["foo"]); |
| 156 selectorsEqual("com", []); | 163 testResult("com", []); |
| 157 selectorsEqual("", []); | 164 testResult("", []); |
| 158 removeFilter("example.org##foo"); | 165 removeFilter("example.org##foo"); |
| 159 | 166 |
| 160 addFilter("~example.com##foo"); | 167 addFilter("~example.com##foo"); |
| 161 selectorsEqual("foo.example.com", []); | 168 testResult("foo.example.com", []); |
| 162 selectorsEqual("example.com", ["foo"]); | 169 testResult("example.com", ["foo"]); |
| 163 selectorsEqual("com", ["foo"]); | 170 testResult("com", ["foo"]); |
| 164 selectorsEqual("", ["foo"]); | 171 testResult("", ["foo"]); |
| 165 removeFilter("~example.org##foo"); | 172 removeFilter("~example.com##foo"); |
| 166 | 173 |
| 167 // Test specificOnly and noUnconditional | 174 removeFilter("~foo.example.com,example.com##foo"); |
| 168 addFilter("~example.com##foo"); | 175 |
|
Wladimir Palant
2016/09/20 10:36:07
Please add at least one unconditional filter, othe
kzar
2016/09/20 14:23:48
Done.
| |
| 169 selectorsEqual("foo.com", [], true, false); | 176 // Test criteria |
| 170 selectorsEqual("foo.com", ["foo"], false, true); | 177 addFilter("##hello"); |
|
Wladimir Palant
2016/09/20 10:36:08
You are not testing the scenario where both specif
kzar
2016/09/20 14:23:49
Done. (I use slightly different logic for the chec
| |
| 171 addFilter("foo.com##foo"); | 178 addFilter("~example.com##world"); |
| 172 selectorsEqual("foo.com", ["foo"], true, false); | 179 addFilter("foo.com##specific"); |
| 173 selectorsEqual("foo.com", ["foo"], false, true); | 180 testResult("foo.com", ["specific"], ElemHide.SPECIFIC_ONLY); |
| 174 removeFilter("foo.com##foo"); | 181 testResult("foo.com", ["specific", "world"], ElemHide.NO_UNCONDITIONAL); |
| 175 removeFilter("~example.org##foo"); | 182 testResult("foo.com", ["hello", "specific", "world"], ElemHide.ALL_MATCHING); |
| 176 | 183 testResult("foo.com", ["hello", "specific", "world"]); |
| 177 // Test provideFilterKeys | 184 removeFilter("foo.com##specific"); |
| 178 addFilter("~foo.com##nope"); | 185 removeFilter("~example.com##world"); |
| 179 addFilter("##foo"); | 186 removeFilter("##hello"); |
| 180 addFilter("##bar"); | 187 testResult("foo.com", []); |
| 181 addFilter("##hello"); | 188 |
| 182 addFilter("##world"); | 189 addFilter("##hello"); |
| 183 let selectorsWithKeys = ElemHide.getSelectorsForDomain("foo.com", false, | 190 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); |
|
Wladimir Palant
2016/09/20 10:36:08
How about:
let [selectors, filterKeys] = ...
kzar
2016/09/20 14:23:48
Done.
| |
| 184 false, true); | 191 testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 185 let selectors = selectorsWithKeys[0]; | 192 testResult("foo.com", ["hello"], ElemHide.ALL_MATCHING); |
| 186 let filterKeys = selectorsWithKeys[1]; | 193 testResult("foo.com", ["hello"]); |
| 187 test.deepEqual(filterKeys.map(k => ElemHide.getFilterByKey(k).selector), | 194 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); |
| 188 selectors); | 195 testResult("bar.com", [], ElemHide.NO_UNCONDITIONAL); |
| 189 test.deepEqual(normalizeSelectors(selectors), | 196 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); |
| 190 normalizeSelectors(["bar", "foo", "hello", "world"])); | 197 testResult("bar.com", ["hello"]); |
|
Wladimir Palant
2016/09/20 10:36:08
One-time correctness test is definitely not suffic
kzar
2016/09/20 14:23:48
I've made it so the filter keys are tested for all
| |
| 191 removeFilter("##world"); | 198 addFilter("foo.com#@#hello"); |
| 192 removeFilter("##hello"); | 199 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); |
| 193 removeFilter("##bar"); | 200 testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); |
| 194 removeFilter("##foo"); | 201 testResult("foo.com", [], ElemHide.ALL_MATCHING); |
| 195 removeFilter("~foo.com##nope"); | 202 testResult("foo.com", []); |
| 203 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); | |
| 204 testResult("bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); | |
| 205 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); | |
| 206 testResult("bar.com", ["hello"]); | |
| 207 removeFilter("foo.com#@#hello"); | |
| 208 testResult("foo.com", [], ElemHide.SPECIFIC_ONLY); | |
| 209 // Note: We don't take care to track conditional selectors which became | |
| 210 // unconditional when a filter was removed. This was too expensive. | |
| 211 //testResult("foo.com", [], ElemHide.NO_UNCONDITIONAL); | |
| 212 testResult("foo.com", ["hello"], ElemHide.ALL_MATCHING); | |
| 213 testResult("foo.com", ["hello"]); | |
| 214 testResult("bar.com", [], ElemHide.SPECIFIC_ONLY); | |
| 215 testResult("bar.com", ["hello"], ElemHide.NO_UNCONDITIONAL); | |
| 216 testResult("bar.com", ["hello"], ElemHide.ALL_MATCHING); | |
| 217 testResult("bar.com", ["hello"]); | |
| 218 removeFilter("##hello"); | |
| 219 testResult("foo.com", []); | |
| 220 testResult("bar.com", []); | |
| 221 | |
| 222 addFilter("##hello"); | |
| 223 addFilter("foo.com##hello"); | |
| 224 testResult("foo.com", ["hello"]); | |
| 225 removeFilter("foo.com##hello"); | |
| 226 testResult("foo.com", ["hello"]); | |
| 227 removeFilter("##hello"); | |
| 228 testResult("foo.com", []); | |
| 229 | |
| 230 addFilter("##hello"); | |
| 231 addFilter("foo.com##hello"); | |
| 232 testResult("foo.com", ["hello"]); | |
| 233 removeFilter("##hello"); | |
| 234 testResult("foo.com", ["hello"]); | |
| 235 removeFilter("foo.com##hello"); | |
| 236 testResult("foo.com", []); | |
| 237 | |
| 238 // Advanced filter keys test | |
| 239 testResult("", []); | |
| 240 addFilter("##dupe"); | |
| 241 addFilter(",,##dupe"); | |
| 242 addFilter(",,,##dupe"); | |
| 243 addFilter("foo.com##dupe"); | |
| 244 testResult("", ["dupe"]); | |
| 245 removeFilter(",,,##dupe"); | |
| 246 testResult("", ["dupe"]); | |
| 247 removeFilter("foo.com##dupe"); | |
| 248 testResult("", ["dupe"]); | |
| 249 removeFilter(",,##dupe"); | |
| 250 testResult("", ["dupe"]); | |
| 251 removeFilter("##dupe"); | |
| 252 testResult("", []); | |
| 196 | 253 |
| 197 test.done(); | 254 test.done(); |
| 198 }; | 255 }; |
| LEFT | RIGHT |