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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 "use strict"; | 18 "use strict"; |
19 | 19 |
20 const {createSandbox} = require("./_common"); | 20 const {createSandbox} = require("./_common"); |
21 | 21 |
| 22 let f$ = null; |
| 23 |
22 let Subscription = null; | 24 let Subscription = null; |
23 let SpecialSubscription = null; | 25 let SpecialSubscription = null; |
24 let DownloadableSubscription = null; | 26 let DownloadableSubscription = null; |
25 let RegularSubscription = null; | 27 let RegularSubscription = null; |
26 let ExternalSubscription = null; | 28 let ExternalSubscription = null; |
| 29 let Filter = null; |
27 | 30 |
28 exports.setUp = function(callback) | 31 exports.setUp = function(callback) |
29 { | 32 { |
30 let sandboxedRequire = createSandbox(); | 33 let sandboxedRequire = createSandbox(); |
31 ( | 34 ( |
32 {Subscription, SpecialSubscription, | 35 {Subscription, SpecialSubscription, |
33 DownloadableSubscription, RegularSubscription, | 36 DownloadableSubscription, RegularSubscription, |
34 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses") | 37 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses"), |
| 38 {Filter} = sandboxedRequire("../lib/filterClasses") |
35 ); | 39 ); |
36 | 40 |
| 41 f$ = Filter.fromText; |
| 42 |
37 callback(); | 43 callback(); |
38 }; | 44 }; |
39 | 45 |
40 function compareSubscription(test, url, expected, postInit) | 46 function compareSubscription(test, url, expected, postInit) |
41 { | 47 { |
42 expected.push("[Subscription]"); | 48 expected.push("[Subscription]"); |
43 let subscription = Subscription.fromURL(url); | 49 let subscription = Subscription.fromURL(url); |
44 if (postInit) | 50 if (postInit) |
45 postInit(subscription); | 51 postInit(subscription); |
46 let result = [...subscription.serialize()]; | 52 let result = [...subscription.serialize()]; |
47 test.equal(result.sort().join("\n"), expected.sort().join("\n"), url); | 53 test.equal(result.sort().join("\n"), expected.sort().join("\n"), url); |
48 | 54 |
49 let map = Object.create(null); | 55 let map = Object.create(null); |
50 for (let line of result.slice(1)) | 56 for (let line of result.slice(1)) |
51 { | 57 { |
52 if (/(.*?)=(.*)/.test(line)) | 58 if (/(.*?)=(.*)/.test(line)) |
53 map[RegExp.$1] = RegExp.$2; | 59 map[RegExp.$1] = RegExp.$2; |
54 } | 60 } |
55 let subscription2 = Subscription.fromObject(map); | 61 let subscription2 = Subscription.fromObject(map); |
56 test.equal(subscription.toString(), subscription2.toString(), url + " deserial
ization"); | 62 test.equal(subscription.toString(), subscription2.toString(), url + " deserial
ization"); |
57 } | 63 } |
58 | 64 |
| 65 function compareSubscriptionFilters(test, subscription, expected) |
| 66 { |
| 67 test.deepEqual([...subscription.filterText()], expected); |
| 68 test.deepEqual([...subscription.filters()], expected.map(f$)); |
| 69 |
| 70 test.equal(subscription.filterCount, expected.length); |
| 71 |
| 72 for (let i = 0; i < subscription.filterCount; i++) |
| 73 { |
| 74 test.equal(subscription.filterAt(i).text, expected[i]); |
| 75 test.ok(subscription.hasFilter(f$(expected[i]))); |
| 76 } |
| 77 |
| 78 test.ok(!subscription.filterAt(subscription.filterCount)); |
| 79 test.ok(!subscription.filterAt(-1)); |
| 80 |
| 81 subscription.clearCaches(); |
| 82 |
| 83 for (let i = 0; i < subscription.filterCount; i++) |
| 84 test.ok(subscription.hasFilter(f$(expected[i]))); |
| 85 } |
| 86 |
59 exports.testSubscriptionClassDefinitions = function(test) | 87 exports.testSubscriptionClassDefinitions = function(test) |
60 { | 88 { |
61 test.equal(typeof Subscription, "function", "typeof Subscription"); | 89 test.equal(typeof Subscription, "function", "typeof Subscription"); |
62 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription
"); | 90 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription
"); |
63 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription
"); | 91 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription
"); |
64 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti
on"); | 92 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti
on"); |
65 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu
bscription"); | 93 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu
bscription"); |
66 | 94 |
67 test.done(); | 95 test.done(); |
68 }; | 96 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], | 134 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], |
107 subscription => | 135 subscription => |
108 { | 136 { |
109 subscription.title = "Test group"; | 137 subscription.title = "Test group"; |
110 subscription.disabled = true; | 138 subscription.disabled = true; |
111 } | 139 } |
112 ); | 140 ); |
113 | 141 |
114 test.done(); | 142 test.done(); |
115 }; | 143 }; |
| 144 |
| 145 exports.testFilterManagement = function(test) |
| 146 { |
| 147 let subscription = Subscription.fromURL("https://example.com/"); |
| 148 |
| 149 compareSubscriptionFilters(test, subscription, []); |
| 150 |
| 151 subscription.addFilter(f$("##.foo")); |
| 152 compareSubscriptionFilters(test, subscription, ["##.foo"]); |
| 153 test.equal(subscription.searchFilter(f$("##.foo")), 0); |
| 154 |
| 155 subscription.addFilter(f$("##.bar")); |
| 156 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); |
| 157 test.equal(subscription.searchFilter(f$("##.bar")), 1); |
| 158 |
| 159 // Repeat filter. |
| 160 subscription.addFilter(f$("##.bar")); |
| 161 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar", |
| 162 "##.bar"]); |
| 163 |
| 164 // The first occurrence is found. |
| 165 test.equal(subscription.searchFilter(f$("##.bar")), 1); |
| 166 |
| 167 subscription.deleteFilterAt(0); |
| 168 compareSubscriptionFilters(test, subscription, ["##.bar", "##.bar"]); |
| 169 test.equal(subscription.searchFilter(f$("##.bar")), 0); |
| 170 test.ok(!subscription.hasFilter(f$("##.foo"))); |
| 171 |
| 172 subscription.insertFilterAt(f$("##.foo"), 0); |
| 173 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar", |
| 174 "##.bar"]); |
| 175 test.equal(subscription.searchFilter(f$("##.bar")), 1); |
| 176 |
| 177 subscription.deleteFilterAt(1); |
| 178 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); |
| 179 test.equal(subscription.searchFilter(f$("##.bar")), 1); |
| 180 |
| 181 subscription.deleteFilterAt(1); |
| 182 compareSubscriptionFilters(test, subscription, ["##.foo"]); |
| 183 test.equal(subscription.searchFilter(f$("##.bar")), -1); |
| 184 test.ok(!subscription.hasFilter(f$("##.bar"))); |
| 185 |
| 186 subscription.addFilter(f$("##.bar")); |
| 187 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); |
| 188 test.equal(subscription.searchFilter(f$("##.bar")), 1); |
| 189 |
| 190 subscription.clearFilters(); |
| 191 compareSubscriptionFilters(test, subscription, []); |
| 192 test.ok(!subscription.hasFilter(f$("##.foo"))); |
| 193 test.ok(!subscription.hasFilter(f$("##.bar"))); |
| 194 test.equal(subscription.searchFilter(f$("##.foo")), -1); |
| 195 test.equal(subscription.searchFilter(f$("##.bar")), -1); |
| 196 |
| 197 subscription.addFilter(f$("##.bar")); |
| 198 compareSubscriptionFilters(test, subscription, ["##.bar"]); |
| 199 |
| 200 subscription.addFilter(f$("##.foo")); |
| 201 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo"]); |
| 202 test.equal(subscription.searchFilter(f$("##.bar")), 0); |
| 203 test.equal(subscription.searchFilter(f$("##.foo")), 1); |
| 204 |
| 205 // Insert outside of bounds. |
| 206 subscription.insertFilterAt(f$("##.lambda"), 1000); |
| 207 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo", |
| 208 "##.lambda"]); |
| 209 test.equal(subscription.searchFilter(f$("##.lambda")), 2); |
| 210 |
| 211 // Delete outside of bounds. |
| 212 subscription.deleteFilterAt(1000); |
| 213 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo", |
| 214 "##.lambda"]); |
| 215 test.equal(subscription.searchFilter(f$("##.lambda")), 2); |
| 216 |
| 217 // Insert outside of bounds (negative). |
| 218 subscription.insertFilterAt(f$("##.lambda"), -1000); |
| 219 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar", |
| 220 "##.foo", "##.lambda"]); |
| 221 test.equal(subscription.searchFilter(f$("##.lambda")), 0); |
| 222 |
| 223 // Delete outside of bounds (negative). |
| 224 subscription.deleteFilterAt(-1000); |
| 225 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar", |
| 226 "##.foo", "##.lambda"]); |
| 227 test.equal(subscription.searchFilter(f$("##.lambda")), 0); |
| 228 |
| 229 test.done(); |
| 230 }; |
OLD | NEW |