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 assert = require("assert"); |
20 const {createSandbox} = require("./_common"); | 21 const {createSandbox} = require("./_common"); |
21 | 22 |
22 let f$ = null; | 23 let f$ = null; |
23 | 24 |
24 let Subscription = null; | 25 let Subscription = null; |
25 let SpecialSubscription = null; | 26 let SpecialSubscription = null; |
26 let DownloadableSubscription = null; | 27 let DownloadableSubscription = null; |
27 let RegularSubscription = null; | 28 let RegularSubscription = null; |
28 let ExternalSubscription = null; | 29 let ExternalSubscription = null; |
29 let Filter = null; | 30 let Filter = null; |
30 | 31 |
31 exports.setUp = function(callback) | 32 describe("Subscription Classes", () => |
32 { | 33 { |
33 let sandboxedRequire = createSandbox(); | 34 beforeEach(() => |
34 ( | 35 { |
35 {Subscription, SpecialSubscription, | 36 let sandboxedRequire = createSandbox(); |
36 DownloadableSubscription, RegularSubscription, | 37 ( |
37 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses"), | 38 {Subscription, SpecialSubscription, |
38 {Filter} = sandboxedRequire("../lib/filterClasses") | 39 DownloadableSubscription, RegularSubscription, |
39 ); | 40 ExternalSubscription} = sandboxedRequire("../lib/subscriptionClasses"), |
40 | 41 {Filter} = sandboxedRequire("../lib/filterClasses") |
41 f$ = Filter.fromText; | 42 ); |
42 | 43 |
43 callback(); | 44 f$ = Filter.fromText; |
44 }; | 45 }); |
45 | 46 |
46 function compareSubscription(test, url, expected, postInit) | 47 function compareSubscription(url, expected, postInit) |
47 { | 48 { |
48 expected.push("[Subscription]"); | 49 expected.push("[Subscription]"); |
49 let subscription = Subscription.fromURL(url); | 50 let subscription = Subscription.fromURL(url); |
50 if (postInit) | 51 if (postInit) |
51 postInit(subscription); | 52 postInit(subscription); |
52 let result = [...subscription.serialize()]; | 53 let result = [...subscription.serialize()]; |
53 test.equal(result.sort().join("\n"), expected.sort().join("\n"), url); | 54 assert.equal(result.sort().join("\n"), expected.sort().join("\n"), url); |
54 | 55 |
55 let map = Object.create(null); | 56 let map = Object.create(null); |
56 for (let line of result.slice(1)) | 57 for (let line of result.slice(1)) |
57 { | 58 { |
58 if (/(.*?)=(.*)/.test(line)) | 59 if (/(.*?)=(.*)/.test(line)) |
59 map[RegExp.$1] = RegExp.$2; | 60 map[RegExp.$1] = RegExp.$2; |
| 61 } |
| 62 let subscription2 = Subscription.fromObject(map); |
| 63 assert.equal(subscription.toString(), subscription2.toString(), url + " dese
rialization"); |
60 } | 64 } |
61 let subscription2 = Subscription.fromObject(map); | 65 |
62 test.equal(subscription.toString(), subscription2.toString(), url + " deserial
ization"); | 66 function compareSubscriptionFilters(subscription, expected) |
63 } | 67 { |
64 | 68 assert.deepEqual([...subscription.filterText()], expected); |
65 function compareSubscriptionFilters(test, subscription, expected) | 69 |
66 { | 70 assert.equal(subscription.filterCount, expected.length); |
67 test.deepEqual([...subscription.filterText()], expected); | 71 |
68 | 72 for (let i = 0; i < subscription.filterCount; i++) |
69 test.equal(subscription.filterCount, expected.length); | 73 assert.equal(subscription.filterTextAt(i), expected[i]); |
70 | 74 |
71 for (let i = 0; i < subscription.filterCount; i++) | 75 assert.ok(!subscription.filterTextAt(subscription.filterCount)); |
72 test.equal(subscription.filterTextAt(i), expected[i]); | 76 assert.ok(!subscription.filterTextAt(-1)); |
73 | 77 } |
74 test.ok(!subscription.filterTextAt(subscription.filterCount)); | 78 |
75 test.ok(!subscription.filterTextAt(-1)); | 79 it("Definitions", () => |
76 } | 80 { |
77 | 81 assert.equal(typeof Subscription, "function", "typeof Subscription"); |
78 exports.testSubscriptionClassDefinitions = function(test) | 82 assert.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscrip
tion"); |
79 { | 83 assert.equal(typeof RegularSubscription, "function", "typeof RegularSubscrip
tion"); |
80 test.equal(typeof Subscription, "function", "typeof Subscription"); | 84 assert.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscr
iption"); |
81 test.equal(typeof SpecialSubscription, "function", "typeof SpecialSubscription
"); | 85 assert.equal(typeof DownloadableSubscription, "function", "typeof Downloadab
leSubscription"); |
82 test.equal(typeof RegularSubscription, "function", "typeof RegularSubscription
"); | 86 }); |
83 test.equal(typeof ExternalSubscription, "function", "typeof ExternalSubscripti
on"); | 87 |
84 test.equal(typeof DownloadableSubscription, "function", "typeof DownloadableSu
bscription"); | 88 it("Subscription with state", () => |
85 | 89 { |
86 test.done(); | 90 compareSubscription("~fl~", ["url=~fl~"]); |
87 }; | 91 compareSubscription("http://test/default", ["url=http://test/default", "titl
e=http://test/default"]); |
88 | 92 compareSubscription( |
89 exports.testSubscriptionsWithState = function(test) | 93 "http://test/default_titled", ["url=http://test/default_titled", "title=te
st"], |
90 { | 94 subscription => |
91 compareSubscription(test, "~fl~", ["url=~fl~"]); | 95 { |
92 compareSubscription(test, "http://test/default", ["url=http://test/default", "
title=http://test/default"]); | 96 subscription.title = "test"; |
93 compareSubscription( | 97 } |
94 test, "http://test/default_titled", ["url=http://test/default_titled", "titl
e=test"], | 98 ); |
95 subscription => | 99 compareSubscription( |
96 { | 100 "http://test/non_default", |
97 subscription.title = "test"; | 101 [ |
98 } | 102 "url=http://test/non_default", "type=ads", "title=test", "disabled=true"
, |
99 ); | 103 "lastSuccess=8", "lastDownload=12", "lastCheck=16", "softExpiration=18", |
100 compareSubscription( | 104 "expires=20", "downloadStatus=foo", "errors=3", "version=24", |
101 test, "http://test/non_default", | 105 "requiredVersion=0.6" |
102 [ | 106 ], |
103 "url=http://test/non_default", "type=ads", "title=test", "disabled=true", | 107 subscription => |
104 "lastSuccess=8", "lastDownload=12", "lastCheck=16", "softExpiration=18", | 108 { |
105 "expires=20", "downloadStatus=foo", "errors=3", "version=24", | 109 subscription.type = "ads"; |
106 "requiredVersion=0.6" | 110 subscription.title = "test"; |
107 ], | 111 subscription.disabled = true; |
108 subscription => | 112 subscription.lastSuccess = 8; |
109 { | 113 subscription.lastDownload = 12; |
110 subscription.type = "ads"; | 114 subscription.lastCheck = 16; |
111 subscription.title = "test"; | 115 subscription.softExpiration = 18; |
112 subscription.disabled = true; | 116 subscription.expires = 20; |
113 subscription.lastSuccess = 8; | 117 subscription.downloadStatus = "foo"; |
114 subscription.lastDownload = 12; | 118 subscription.errors = 3; |
115 subscription.lastCheck = 16; | 119 subscription.version = 24; |
116 subscription.softExpiration = 18; | 120 subscription.requiredVersion = "0.6"; |
117 subscription.expires = 20; | 121 } |
118 subscription.downloadStatus = "foo"; | 122 ); |
119 subscription.errors = 3; | 123 compareSubscription( |
120 subscription.version = 24; | 124 "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], |
121 subscription.requiredVersion = "0.6"; | 125 subscription => |
122 } | 126 { |
123 ); | 127 subscription.title = "Test group"; |
124 compareSubscription( | 128 subscription.disabled = true; |
125 test, "~wl~", ["url=~wl~", "disabled=true", "title=Test group"], | 129 } |
126 subscription => | 130 ); |
127 { | 131 }); |
128 subscription.title = "Test group"; | 132 |
129 subscription.disabled = true; | 133 it("Filter Management", () => |
130 } | 134 { |
131 ); | 135 let subscription = Subscription.fromURL("https://example.com/"); |
132 | 136 |
133 test.done(); | 137 compareSubscriptionFilters(subscription, []); |
134 }; | 138 |
135 | 139 subscription.addFilter(f$("##.foo")); |
136 exports.testFilterManagement = function(test) | 140 compareSubscriptionFilters(subscription, ["##.foo"]); |
137 { | 141 assert.equal(subscription.findFilterIndex(f$("##.foo")), 0); |
138 let subscription = Subscription.fromURL("https://example.com/"); | 142 |
139 | 143 subscription.addFilter(f$("##.bar")); |
140 compareSubscriptionFilters(test, subscription, []); | 144 compareSubscriptionFilters(subscription, ["##.foo", "##.bar"]); |
141 | 145 assert.equal(subscription.findFilterIndex(f$("##.bar")), 1); |
142 subscription.addFilter(f$("##.foo")); | 146 |
143 compareSubscriptionFilters(test, subscription, ["##.foo"]); | 147 // Repeat filter. |
144 test.equal(subscription.findFilterIndex(f$("##.foo")), 0); | 148 subscription.addFilter(f$("##.bar")); |
145 | 149 compareSubscriptionFilters(subscription, ["##.foo", "##.bar", |
146 subscription.addFilter(f$("##.bar")); | 150 "##.bar"]); |
147 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); | 151 |
148 test.equal(subscription.findFilterIndex(f$("##.bar")), 1); | 152 // The first occurrence is found. |
149 | 153 assert.equal(subscription.findFilterIndex(f$("##.bar")), 1); |
150 // Repeat filter. | 154 |
151 subscription.addFilter(f$("##.bar")); | 155 subscription.deleteFilterAt(0); |
152 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar", | 156 compareSubscriptionFilters(subscription, ["##.bar", "##.bar"]); |
153 "##.bar"]); | 157 assert.equal(subscription.findFilterIndex(f$("##.bar")), 0); |
154 | 158 |
155 // The first occurrence is found. | 159 subscription.insertFilterAt(f$("##.foo"), 0); |
156 test.equal(subscription.findFilterIndex(f$("##.bar")), 1); | 160 compareSubscriptionFilters(subscription, ["##.foo", "##.bar", |
157 | 161 "##.bar"]); |
158 subscription.deleteFilterAt(0); | 162 assert.equal(subscription.findFilterIndex(f$("##.bar")), 1); |
159 compareSubscriptionFilters(test, subscription, ["##.bar", "##.bar"]); | 163 |
160 test.equal(subscription.findFilterIndex(f$("##.bar")), 0); | 164 subscription.deleteFilterAt(1); |
161 | 165 compareSubscriptionFilters(subscription, ["##.foo", "##.bar"]); |
162 subscription.insertFilterAt(f$("##.foo"), 0); | 166 assert.equal(subscription.findFilterIndex(f$("##.bar")), 1); |
163 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar", | 167 |
164 "##.bar"]); | 168 subscription.deleteFilterAt(1); |
165 test.equal(subscription.findFilterIndex(f$("##.bar")), 1); | 169 compareSubscriptionFilters(subscription, ["##.foo"]); |
166 | 170 assert.equal(subscription.findFilterIndex(f$("##.bar")), -1); |
167 subscription.deleteFilterAt(1); | 171 |
168 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); | 172 subscription.addFilter(f$("##.bar")); |
169 test.equal(subscription.findFilterIndex(f$("##.bar")), 1); | 173 compareSubscriptionFilters(subscription, ["##.foo", "##.bar"]); |
170 | 174 assert.equal(subscription.findFilterIndex(f$("##.bar")), 1); |
171 subscription.deleteFilterAt(1); | 175 |
172 compareSubscriptionFilters(test, subscription, ["##.foo"]); | 176 subscription.clearFilters(); |
173 test.equal(subscription.findFilterIndex(f$("##.bar")), -1); | 177 compareSubscriptionFilters(subscription, []); |
174 | 178 assert.equal(subscription.findFilterIndex(f$("##.foo")), -1); |
175 subscription.addFilter(f$("##.bar")); | 179 assert.equal(subscription.findFilterIndex(f$("##.bar")), -1); |
176 compareSubscriptionFilters(test, subscription, ["##.foo", "##.bar"]); | 180 |
177 test.equal(subscription.findFilterIndex(f$("##.bar")), 1); | 181 subscription.addFilter(f$("##.bar")); |
178 | 182 compareSubscriptionFilters(subscription, ["##.bar"]); |
179 subscription.clearFilters(); | 183 |
180 compareSubscriptionFilters(test, subscription, []); | 184 subscription.addFilter(f$("##.foo")); |
181 test.equal(subscription.findFilterIndex(f$("##.foo")), -1); | 185 compareSubscriptionFilters(subscription, ["##.bar", "##.foo"]); |
182 test.equal(subscription.findFilterIndex(f$("##.bar")), -1); | 186 assert.equal(subscription.findFilterIndex(f$("##.bar")), 0); |
183 | 187 assert.equal(subscription.findFilterIndex(f$("##.foo")), 1); |
184 subscription.addFilter(f$("##.bar")); | 188 |
185 compareSubscriptionFilters(test, subscription, ["##.bar"]); | 189 // Insert outside of bounds. |
186 | 190 subscription.insertFilterAt(f$("##.lambda"), 1000); |
187 subscription.addFilter(f$("##.foo")); | 191 compareSubscriptionFilters(subscription, ["##.bar", "##.foo", |
188 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo"]); | 192 "##.lambda"]); |
189 test.equal(subscription.findFilterIndex(f$("##.bar")), 0); | 193 assert.equal(subscription.findFilterIndex(f$("##.lambda")), 2); |
190 test.equal(subscription.findFilterIndex(f$("##.foo")), 1); | 194 |
191 | 195 // Delete outside of bounds. |
192 // Insert outside of bounds. | 196 subscription.deleteFilterAt(1000); |
193 subscription.insertFilterAt(f$("##.lambda"), 1000); | 197 compareSubscriptionFilters(subscription, ["##.bar", "##.foo", |
194 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo", | 198 "##.lambda"]); |
195 "##.lambda"]); | 199 assert.equal(subscription.findFilterIndex(f$("##.lambda")), 2); |
196 test.equal(subscription.findFilterIndex(f$("##.lambda")), 2); | 200 |
197 | 201 // Insert outside of bounds (negative). |
198 // Delete outside of bounds. | 202 subscription.insertFilterAt(f$("##.lambda"), -1000); |
199 subscription.deleteFilterAt(1000); | 203 compareSubscriptionFilters(subscription, ["##.lambda", "##.bar", |
200 compareSubscriptionFilters(test, subscription, ["##.bar", "##.foo", | 204 "##.foo", "##.lambda"]); |
201 "##.lambda"]); | 205 assert.equal(subscription.findFilterIndex(f$("##.lambda")), 0); |
202 test.equal(subscription.findFilterIndex(f$("##.lambda")), 2); | 206 |
203 | 207 // Delete outside of bounds (negative). |
204 // Insert outside of bounds (negative). | 208 subscription.deleteFilterAt(-1000); |
205 subscription.insertFilterAt(f$("##.lambda"), -1000); | 209 compareSubscriptionFilters(subscription, ["##.lambda", "##.bar", |
206 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar", | 210 "##.foo", "##.lambda"]); |
207 "##.foo", "##.lambda"]); | 211 assert.equal(subscription.findFilterIndex(f$("##.lambda")), 0); |
208 test.equal(subscription.findFilterIndex(f$("##.lambda")), 0); | 212 }); |
209 | 213 }); |
210 // Delete outside of bounds (negative). | |
211 subscription.deleteFilterAt(-1000); | |
212 compareSubscriptionFilters(test, subscription, ["##.lambda", "##.bar", | |
213 "##.foo", "##.lambda"]); | |
214 test.equal(subscription.findFilterIndex(f$("##.lambda")), 0); | |
215 | |
216 test.done(); | |
217 }; | |
OLD | NEW |