Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-2017 eyeo GmbH | |
4 * | |
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 | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
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/>. | |
16 */ | |
17 | |
18 "use strict"; | |
19 | |
20 const {createSandbox} = require("./_common"); | |
21 | |
22 let Filter = null; | |
23 let FilterNotifier = null; | |
24 let FilterStorage = null; | |
25 let Subscription = null; | |
26 | |
27 exports.setUp = function(callback) | |
28 { | |
29 let sandboxedRequire = createSandbox(); | |
30 | |
31 ( | |
32 {Filter} = sandboxedRequire("../lib/filterClasses"), | |
33 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"), | |
34 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | |
35 {Subscription} = sandboxedRequire("../lib/subscriptionClasses") | |
36 ); | |
37 | |
38 callback(); | |
39 }; | |
40 | |
41 function compareSubscriptionList(test, testMessage, list) | |
42 { | |
43 let result = Array.from(FilterStorage.subscriptions, subscription => subscript ion.url); | |
44 let expected = list.map(subscription => subscription.url); | |
45 test.deepEqual(result, expected, testMessage); | |
46 } | |
47 | |
48 function compareFiltersList(test, testMessage, list) | |
49 { | |
50 let result = Array.from(FilterStorage.subscriptions, subscription => | |
51 { | |
52 return Array.from(subscription.filters, filter => filter.text); | |
53 }); | |
54 test.deepEqual(result, list, testMessage); | |
55 } | |
56 | |
57 exports.testAddingRemovingSubscriptions = function(test) | |
58 { | |
59 let subscription1 = Subscription.fromURL("http://test1/"); | |
60 let subscription2 = Subscription.fromURL("http://test2/"); | |
61 test.ok(!subscription1.listed, "First subscription not listed"); | |
62 test.ok(!subscription2.listed, "Second subscription not listed"); | |
63 | |
64 let changes = []; | |
65 function listener(action, subscription) | |
66 { | |
67 if (action.indexOf("subscription.") == 0) | |
68 changes.push(action + " " + subscription.url); | |
sergei
2017/05/08 10:54:39
Is there any reason to not use arrays, changes.pus
Wladimir Palant
2017/05/08 12:55:41
This is code from the master branch adapted. But t
| |
69 } | |
70 FilterNotifier.addListener(listener); | |
71 | |
72 compareSubscriptionList(test, "Initial state", []); | |
73 test.deepEqual(changes, [], "Received changes"); | |
74 | |
75 changes = []; | |
76 FilterStorage.addSubscription(subscription1); | |
77 compareSubscriptionList(test, "Adding first subscription", [subscription1]); | |
78 test.deepEqual(changes, ["subscription.added http://test1/"], "Received change s"); | |
79 test.ok(subscription1.listed, "First subscription listed"); | |
80 test.ok(!subscription2.listed, "Second subscription not listed"); | |
81 | |
82 changes = []; | |
83 FilterStorage.addSubscription(subscription1); | |
84 compareSubscriptionList(test, "Adding already added subscription", [subscripti on1]); | |
85 test.deepEqual(changes, [], "Received changes"); | |
86 test.ok(subscription1.listed, "First subscription listed"); | |
87 test.ok(!subscription2.listed, "Second subscription not listed"); | |
88 | |
89 changes = []; | |
90 FilterStorage.addSubscription(subscription2); | |
91 compareSubscriptionList(test, "Adding second", [subscription1, subscription2]) ; | |
92 test.deepEqual(changes, ["subscription.added http://test2/"], "Received change s"); | |
93 test.ok(subscription1.listed, "First subscription listed"); | |
94 test.ok(subscription2.listed, "Second subscription listed"); | |
95 | |
96 changes = []; | |
97 FilterStorage.removeSubscription(subscription1); | |
98 compareSubscriptionList(test, "Remove", [subscription2]); | |
99 test.deepEqual(changes, ["subscription.removed http://test1/"], "Received chan ges"); | |
100 test.ok(!subscription1.listed, "First subscription not listed"); | |
101 test.ok(subscription2.listed, "Second subscription listed"); | |
102 | |
103 changes = []; | |
104 FilterStorage.removeSubscription(subscription1); | |
105 compareSubscriptionList(test, "Removing already removed subscription", [subscr iption2]); | |
106 test.deepEqual(changes, [], "Received changes"); | |
107 test.ok(!subscription1.listed, "First subscription not listed"); | |
108 test.ok(subscription2.listed, "Second subscription listed"); | |
109 | |
110 changes = []; | |
111 FilterStorage.removeSubscription(subscription2); | |
112 compareSubscriptionList(test, "Removing remaining subscription", []); | |
113 test.deepEqual(changes, ["subscription.removed http://test2/"], "Received chan ges"); | |
114 test.ok(!subscription1.listed, "First subscription not listed"); | |
115 test.ok(!subscription2.listed, "Second subscription not listed"); | |
116 | |
117 changes = []; | |
118 FilterStorage.addSubscription(subscription2); | |
119 compareSubscriptionList(test, "Re-adding previously removed subscription", [su bscription2]); | |
120 test.deepEqual(changes, ["subscription.added http://test2/"], "Received change s"); | |
121 test.ok(!subscription1.listed, "First subscription not listed"); | |
122 test.ok(subscription2.listed, "Second subscription listed"); | |
123 | |
124 changes = []; | |
125 FilterStorage.removeSubscription(subscription2); | |
126 compareSubscriptionList(test, "Re-removing previously added subscription", []) ; | |
127 test.deepEqual(changes, ["subscription.removed http://test2/"], "Received chan ges"); | |
128 test.ok(!subscription1.listed, "First subscription not listed"); | |
129 test.ok(!subscription2.listed, "Second subscription not listed"); | |
130 | |
131 subscription1.delete(); | |
132 subscription2.delete(); | |
133 | |
134 test.done(); | |
135 }; | |
136 | |
137 exports.testMovingSubscriptions = function(test) | |
138 { | |
139 let subscription1 = Subscription.fromURL("http://test1/"); | |
140 let subscription2 = Subscription.fromURL("http://test2/"); | |
141 let subscription3 = Subscription.fromURL("http://test3/"); | |
142 | |
143 FilterStorage.addSubscription(subscription1); | |
144 FilterStorage.addSubscription(subscription2); | |
145 FilterStorage.addSubscription(subscription3); | |
146 | |
147 let changes = []; | |
148 function listener(action, subscription) | |
149 { | |
150 if (action.indexOf("subscription.") == 0) | |
151 changes.push(action + " " + subscription.url); | |
152 } | |
153 FilterNotifier.addListener(listener); | |
154 | |
155 compareSubscriptionList(test, "Initial state", [subscription1, subscription2, subscription3]); | |
156 test.deepEqual(changes, [], "Received changes"); | |
157 | |
158 changes = []; | |
159 test.ok(FilterStorage.moveSubscription(subscription1), "Move without explicit position succeeded"); | |
160 compareSubscriptionList(test, "Move without explicit position", [subscription2 , subscription3, subscription1]); | |
161 test.deepEqual(changes, ["subscription.moved http://test1/"], "Received change s"); | |
162 | |
163 changes = []; | |
164 test.ok(!FilterStorage.moveSubscription(subscription1), "Move without explicit position failed (subscription already last)"); | |
165 compareSubscriptionList(test, "Move without explicit position (subscription al ready last)", [subscription2, subscription3, subscription1]); | |
166 test.deepEqual(changes, [], "Received changes"); | |
167 | |
168 changes = []; | |
169 test.ok(FilterStorage.moveSubscription(subscription2, subscription1), "Move wi th explicit position succeeded"); | |
170 compareSubscriptionList(test, "Move with explicit position", [subscription3, s ubscription2, subscription1]); | |
171 test.deepEqual(changes, ["subscription.moved http://test2/"], "Received change s"); | |
172 | |
173 changes = []; | |
174 test.ok(!FilterStorage.moveSubscription(subscription3, subscription2), "Move w ithout explicit position failed (subscription already at position)"); | |
175 compareSubscriptionList(test, "Move without explicit position (subscription al ready at position)", [subscription3, subscription2, subscription1]); | |
176 test.deepEqual(changes, [], "Received changes"); | |
177 | |
178 FilterStorage.removeSubscription(subscription2); | |
179 compareSubscriptionList(test, "Remove", [subscription3, subscription1]); | |
180 | |
181 changes = []; | |
182 test.ok(FilterStorage.moveSubscription(subscription3, subscription2), "Move be fore removed subscription succeeded"); | |
183 compareSubscriptionList(test, "Move before removed subscription", [subscriptio n1, subscription3]); | |
184 test.deepEqual(changes, ["subscription.moved http://test3/"], "Received change s"); | |
185 | |
186 changes = []; | |
187 test.ok(!FilterStorage.moveSubscription(subscription2), "Move of removed subsc ription failed"); | |
188 compareSubscriptionList(test, "Move of removed subscription", [subscription1, subscription3]); | |
189 test.deepEqual(changes, [], "Received changes"); | |
190 | |
191 subscription1.delete(); | |
192 subscription2.delete(); | |
193 subscription3.delete(); | |
194 | |
195 test.done(); | |
196 }; | |
197 | |
198 exports.testAddingRemovingFilters = function(test) | |
199 { | |
200 function addFilter(text) | |
201 { | |
202 let filter = Filter.fromText(text); | |
203 FilterStorage.addFilter(filter); | |
204 filter.delete(); | |
205 } | |
206 | |
207 function removeFilter(text) | |
208 { | |
209 let filter = Filter.fromText(text); | |
210 FilterStorage.removeFilter(filter); | |
211 filter.delete(); | |
212 } | |
213 | |
214 | |
215 let changes = []; | |
216 function listener(action, filter, subscription, position) | |
217 { | |
218 if (action.indexOf("filter.") == 0) | |
219 { | |
220 changes.push([ | |
221 action, filter.text, FilterStorage.indexOfSubscription(subscription), | |
222 position | |
223 ].join(" ")); | |
224 } | |
225 } | |
226 FilterNotifier.addListener(listener); | |
227 | |
228 compareFiltersList(test, "Initial state", []); | |
229 test.deepEqual(changes, [], "Received changes"); | |
230 | |
231 changes = []; | |
232 addFilter("foo"); | |
233 compareFiltersList(test, "Adding blocking filter", [["foo"]]); | |
234 test.deepEqual(changes, ["filter.added foo 0 0"], "Received changes"); | |
235 | |
236 changes = []; | |
237 addFilter("@@bar"); | |
238 compareFiltersList(test, "Adding exception rule", [["foo"], ["@@bar"]]); | |
239 test.deepEqual(changes, ["filter.added @@bar 1 0"], "Received changes"); | |
240 | |
241 { | |
242 let subscription = FilterStorage.subscriptionAt(1); | |
243 let filter = Filter.fromText("##elemhide"); | |
244 subscription.makeDefaultFor(filter); | |
245 filter.delete(); | |
246 subscription.delete(); | |
247 } | |
248 | |
249 { | |
250 let subscription = Subscription.fromURL("~other"); | |
251 FilterStorage.addSubscription(subscription); | |
252 subscription.delete(); | |
253 } | |
254 | |
255 changes = []; | |
256 addFilter("foo##bar"); | |
257 compareFiltersList(test, "Adding hiding rule", [["foo"], ["@@bar", "foo##bar"] , []]); | |
258 test.deepEqual(changes, ["filter.added foo##bar 1 1"], "Received changes"); | |
259 | |
260 changes = []; | |
261 addFilter("foo#@#bar"); | |
262 compareFiltersList(test, "Adding hiding exception", [["foo"], ["@@bar", "foo## bar", "foo#@#bar"], []]); | |
263 test.deepEqual(changes, ["filter.added foo#@#bar 1 2"], "Received changes"); | |
264 | |
265 changes = []; | |
266 addFilter("!foobar"); | |
267 compareFiltersList(test, "Adding comment", [["foo"], ["@@bar", "foo##bar", "fo o#@#bar"], ["!foobar"]]); | |
268 test.deepEqual(changes, ["filter.added !foobar 2 0"], "Received changes"); | |
269 | |
270 changes = []; | |
271 addFilter("foo"); | |
272 compareFiltersList(test, "Adding already added filter", [["foo"], ["@@bar", "f oo##bar", "foo#@#bar"], ["!foobar"]]); | |
273 test.deepEqual(changes, [], "Received changes"); | |
274 | |
275 { | |
276 let subscription = FilterStorage.subscriptionAt(0); | |
277 subscription.disabled = true; | |
278 subscription.delete(); | |
279 } | |
280 | |
281 changes = []; | |
282 addFilter("foo"); | |
283 compareFiltersList(test, "Adding filter already in a disabled subscription", [ ["foo"], ["@@bar", "foo##bar", "foo#@#bar"], ["!foobar", "foo"]]); | |
284 test.deepEqual(changes, ["filter.added foo 2 1"], "Received changes"); | |
285 | |
286 changes = []; | |
287 removeFilter("foo"); | |
288 compareFiltersList(test, "Removing filter", [[], ["@@bar", "foo##bar", "foo#@# bar"], ["!foobar"]]); | |
289 test.deepEqual(changes, [ | |
290 "filter.removed foo 0 0", | |
291 "filter.removed foo 2 1", | |
292 ], "Received changes"); | |
293 | |
294 changes = []; | |
295 removeFilter("foo"); | |
296 compareFiltersList(test, "Removing unknown filter", [[], ["@@bar", "foo##bar", "foo#@#bar"], ["!foobar"]]); | |
297 test.deepEqual(changes, [], "Received changes"); | |
298 | |
299 test.done(); | |
300 }; | |
OLD | NEW |