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 |
59 exports.testSubscriptionClassDefinitions = function(test) | 82 exports.testSubscriptionClassDefinitions = function(test) |
60 { | 83 { |
61 test.equal(typeof Subscription, "function", "typeof Subscription"); | 84 test.equal(typeof Subscription, "function", "typeof Subscription"); |
62 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription
"); | 85 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription
"); |
63 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription
"); | 86 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription
"); |
64 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti
on"); | 87 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti
on"); |
65 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu
bscription"); | 88 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu
bscription"); |
66 | 89 |
67 test.done(); | 90 test.done(); |
68 }; | 91 }; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], | 129 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], |
107 subscription => | 130 subscription => |
108 { | 131 { |
109 subscription.title = "Test group"; | 132 subscription.title = "Test group"; |
110 subscription.disabled = true; | 133 subscription.disabled = true; |
111 } | 134 } |
112 ); | 135 ); |
113 | 136 |
114 test.done(); | 137 test.done(); |
115 }; | 138 }; |
| 139 |
| 140 exports.testFilterManagement = function(test) |
| 141 { |
| 142 let subscription = Subscription.fromURL("https://example.com/"); |
| 143 |
| 144 compareSubscriptionFilters(test, subscription, []); |
| 145 |
| 146 subscription.addFilter(f$("foo")); |
| 147 compareSubscriptionFilters(test, subscription, ["foo"]); |
| 148 test.equal(subscription.searchFilter(f$("foo")), 0); |
| 149 |
| 150 subscription.addFilter(f$("bar")); |
| 151 compareSubscriptionFilters(test, subscription, ["foo", "bar"]); |
| 152 test.equal(subscription.searchFilter(f$("bar")), 1); |
| 153 |
| 154 // Repeat filter. |
| 155 subscription.addFilter(f$("bar")); |
| 156 compareSubscriptionFilters(test, subscription, ["foo", "bar", "bar"]); |
| 157 |
| 158 // The first occurrence is found. |
| 159 test.equal(subscription.searchFilter(f$("bar")), 1); |
| 160 |
| 161 subscription.deleteFilterAt(0); |
| 162 compareSubscriptionFilters(test, subscription, ["bar", "bar"]); |
| 163 test.equal(subscription.searchFilter(f$("bar")), 0); |
| 164 test.ok(!subscription.hasFilter(f$("foo"))); |
| 165 |
| 166 subscription.insertFilterAt(f$("foo"), 0); |
| 167 compareSubscriptionFilters(test, subscription, ["foo", "bar", "bar"]); |
| 168 test.equal(subscription.searchFilter(f$("bar")), 1); |
| 169 |
| 170 subscription.deleteFilterAt(1); |
| 171 compareSubscriptionFilters(test, subscription, ["foo", "bar"]); |
| 172 test.equal(subscription.searchFilter(f$("bar")), 1); |
| 173 |
| 174 subscription.deleteFilterAt(1); |
| 175 compareSubscriptionFilters(test, subscription, ["foo"]); |
| 176 test.equal(subscription.searchFilter(f$("bar")), -1); |
| 177 test.ok(!subscription.hasFilter(f$("bar"))); |
| 178 |
| 179 subscription.addFilter(f$("bar")); |
| 180 compareSubscriptionFilters(test, subscription, ["foo", "bar"]); |
| 181 test.equal(subscription.searchFilter(f$("bar")), 1); |
| 182 |
| 183 subscription.clearFilters(); |
| 184 compareSubscriptionFilters(test, subscription, []); |
| 185 test.ok(!subscription.hasFilter(f$("foo"))); |
| 186 test.ok(!subscription.hasFilter(f$("bar"))); |
| 187 test.equal(subscription.searchFilter(f$("foo")), -1); |
| 188 test.equal(subscription.searchFilter(f$("bar")), -1); |
| 189 |
| 190 subscription.addFilter(f$("bar")); |
| 191 compareSubscriptionFilters(test, subscription, ["bar"]); |
| 192 |
| 193 subscription.addFilter(f$("foo")); |
| 194 compareSubscriptionFilters(test, subscription, ["bar", "foo"]); |
| 195 test.equal(subscription.searchFilter(f$("bar")), 0); |
| 196 test.equal(subscription.searchFilter(f$("foo")), 1); |
| 197 |
| 198 // Insert outside of bounds. |
| 199 subscription.insertFilterAt(f$("lambda"), 1000); |
| 200 compareSubscriptionFilters(test, subscription, ["bar", "foo", "lambda"]); |
| 201 test.equal(subscription.searchFilter(f$("lambda")), 2); |
| 202 |
| 203 // Delete outside of bounds. |
| 204 subscription.deleteFilterAt(1000); |
| 205 compareSubscriptionFilters(test, subscription, ["bar", "foo", "lambda"]); |
| 206 test.equal(subscription.searchFilter(f$("lambda")), 2); |
| 207 |
| 208 // Insert outside of bounds (negative). |
| 209 subscription.insertFilterAt(f$("lambda"), -1000); |
| 210 compareSubscriptionFilters(test, subscription, ["lambda", "bar", "foo", |
| 211 "lambda"]); |
| 212 test.equal(subscription.searchFilter(f$("lambda")), 0); |
| 213 |
| 214 // Delete outside of bounds (negative). |
| 215 subscription.deleteFilterAt(-1000); |
| 216 compareSubscriptionFilters(test, subscription, ["lambda", "bar", "foo", |
| 217 "lambda"]); |
| 218 test.equal(subscription.searchFilter(f$("lambda")), 0); |
| 219 |
| 220 test.done(); |
| 221 }; |
OLD | NEW |