OLD | NEW |
| (Empty) |
1 (function() | |
2 { | |
3 module("Filter storage", { | |
4 setup: function() | |
5 { | |
6 prepareFilterComponents.call(this); | |
7 preparePrefs.call(this); | |
8 Prefs.savestats = true; | |
9 }, | |
10 teardown: function() | |
11 { | |
12 restoreFilterComponents.call(this); | |
13 restorePrefs.call(this); | |
14 } | |
15 }); | |
16 | |
17 function compareSubscriptionList(test, list) | |
18 { | |
19 let result = FilterStorage.subscriptions.map(function(subscription) {return
subscription.url}); | |
20 let expected = list.map(function(subscription) {return subscription.url}); | |
21 deepEqual(result, expected, test); | |
22 } | |
23 | |
24 function compareFiltersList(test, list) | |
25 { | |
26 let result = FilterStorage.subscriptions.map(function(subscription) {return
subscription.filters.map(function(filter) {return filter.text})}); | |
27 deepEqual(result, list, test); | |
28 } | |
29 | |
30 function compareFilterSubscriptions(test, filter, list) | |
31 { | |
32 let result = filter.subscriptions.map(function(subscription) {return subscri
ption.url}); | |
33 let expected = list.map(function(subscription) {return subscription.url}); | |
34 deepEqual(result, expected, test); | |
35 } | |
36 | |
37 test("Adding subscriptions", function() | |
38 { | |
39 let subscription1 = Subscription.fromURL("http://test1/"); | |
40 let subscription2 = Subscription.fromURL("http://test2/"); | |
41 | |
42 let changes = []; | |
43 function listener(action, subscription) | |
44 { | |
45 if (action.indexOf("subscription.") == 0) | |
46 changes.push(action + " " + subscription.url); | |
47 } | |
48 FilterNotifier.addListener(listener); | |
49 | |
50 compareSubscriptionList("Initial state", []); | |
51 deepEqual(changes, [], "Received changes"); | |
52 | |
53 changes = []; | |
54 FilterStorage.addSubscription(subscription1); | |
55 compareSubscriptionList("Regular add", [subscription1]); | |
56 deepEqual(changes, ["subscription.added http://test1/"], "Received changes")
; | |
57 | |
58 changes = []; | |
59 FilterStorage.addSubscription(subscription1); | |
60 compareSubscriptionList("Adding already added subscription", [subscription1]
); | |
61 deepEqual(changes, [], "Received changes"); | |
62 | |
63 changes = []; | |
64 FilterStorage.addSubscription(subscription2, true); | |
65 compareSubscriptionList("Silent add", [subscription1, subscription2]); | |
66 deepEqual(changes, [], "Received changes"); | |
67 | |
68 FilterStorage.removeSubscription(subscription1); | |
69 compareSubscriptionList("Remove", [subscription2]); | |
70 | |
71 changes = []; | |
72 FilterStorage.addSubscription(subscription1); | |
73 compareSubscriptionList("Re-adding previously removed subscription", [subscr
iption2, subscription1]); | |
74 deepEqual(changes, ["subscription.added http://test1/"], "Received changes")
; | |
75 }); | |
76 | |
77 test("Removing subscriptions", function() | |
78 { | |
79 let subscription1 = Subscription.fromURL("http://test1/"); | |
80 let subscription2 = Subscription.fromURL("http://test2/"); | |
81 FilterStorage.addSubscription(subscription1); | |
82 FilterStorage.addSubscription(subscription2); | |
83 | |
84 let changes = []; | |
85 function listener(action, subscription) | |
86 { | |
87 if (action.indexOf("subscription.") == 0) | |
88 changes.push(action + " " + subscription.url); | |
89 } | |
90 FilterNotifier.addListener(listener); | |
91 | |
92 compareSubscriptionList("Initial state", [subscription1, subscription2]); | |
93 deepEqual(changes, [], "Received changes"); | |
94 | |
95 changes = []; | |
96 FilterStorage.removeSubscription(subscription1); | |
97 compareSubscriptionList("Regular remove", [subscription2]); | |
98 deepEqual(changes, ["subscription.removed http://test1/"], "Received changes
"); | |
99 | |
100 changes = []; | |
101 FilterStorage.removeSubscription(subscription1); | |
102 compareSubscriptionList("Removing already removed subscription", [subscripti
on2]); | |
103 deepEqual(changes, [], "Received changes"); | |
104 | |
105 changes = []; | |
106 FilterStorage.removeSubscription(subscription2, true); | |
107 compareSubscriptionList("Silent remove", []); | |
108 deepEqual(changes, [], "Received changes"); | |
109 | |
110 FilterStorage.addSubscription(subscription1); | |
111 compareSubscriptionList("Add", [subscription1]); | |
112 | |
113 changes = []; | |
114 FilterStorage.removeSubscription(subscription1); | |
115 compareSubscriptionList("Re-removing previously added subscription", []); | |
116 deepEqual(changes, ["subscription.removed http://test1/"], "Received changes
"); | |
117 }); | |
118 | |
119 test("Moving subscriptions", function() | |
120 { | |
121 let subscription1 = Subscription.fromURL("http://test1/"); | |
122 let subscription2 = Subscription.fromURL("http://test2/"); | |
123 let subscription3 = Subscription.fromURL("http://test3/"); | |
124 | |
125 FilterStorage.addSubscription(subscription1); | |
126 FilterStorage.addSubscription(subscription2); | |
127 FilterStorage.addSubscription(subscription3); | |
128 | |
129 let changes = []; | |
130 function listener(action, subscription) | |
131 { | |
132 if (action.indexOf("subscription.") == 0) | |
133 changes.push(action + " " + subscription.url); | |
134 } | |
135 FilterNotifier.addListener(listener); | |
136 | |
137 compareSubscriptionList("Initial state", [subscription1, subscription2, subs
cription3]); | |
138 deepEqual(changes, [], "Received changes"); | |
139 | |
140 changes = []; | |
141 FilterStorage.moveSubscription(subscription1); | |
142 compareSubscriptionList("Move without explicit position", [subscription2, su
bscription3, subscription1]); | |
143 deepEqual(changes, ["subscription.moved http://test1/"], "Received changes")
; | |
144 | |
145 changes = []; | |
146 FilterStorage.moveSubscription(subscription1); | |
147 compareSubscriptionList("Move without explicit position (subscription alread
y last)", [subscription2, subscription3, subscription1]); | |
148 deepEqual(changes, [], "Received changes"); | |
149 | |
150 changes = []; | |
151 FilterStorage.moveSubscription(subscription2, subscription1); | |
152 compareSubscriptionList("Move with explicit position", [subscription3, subsc
ription2, subscription1]); | |
153 deepEqual(changes, ["subscription.moved http://test2/"], "Received changes")
; | |
154 | |
155 changes = []; | |
156 FilterStorage.moveSubscription(subscription3, subscription2); | |
157 compareSubscriptionList("Move without explicit position (subscription alread
y at position)", [subscription3, subscription2, subscription1]); | |
158 deepEqual(changes, [], "Received changes"); | |
159 | |
160 FilterStorage.removeSubscription(subscription2); | |
161 compareSubscriptionList("Remove", [subscription3, subscription1]); | |
162 | |
163 changes = []; | |
164 FilterStorage.moveSubscription(subscription3, subscription2); | |
165 compareSubscriptionList("Move before removed subscription", [subscription1,
subscription3]); | |
166 deepEqual(changes, ["subscription.moved http://test3/"], "Received changes")
; | |
167 | |
168 changes = []; | |
169 FilterStorage.moveSubscription(subscription2); | |
170 compareSubscriptionList("Move of removed subscription", [subscription1, subs
cription3]); | |
171 deepEqual(changes, [], "Received changes"); | |
172 }); | |
173 | |
174 test("Adding filters", function() | |
175 { | |
176 let subscription1 = Subscription.fromURL("~blocking"); | |
177 subscription1.defaults = ["blocking"]; | |
178 | |
179 let subscription2 = Subscription.fromURL("~exceptions"); | |
180 subscription2.defaults = ["whitelist", "elemhide"]; | |
181 | |
182 let subscription3 = Subscription.fromURL("~other"); | |
183 | |
184 FilterStorage.addSubscription(subscription1); | |
185 FilterStorage.addSubscription(subscription2); | |
186 FilterStorage.addSubscription(subscription3); | |
187 | |
188 let changes = []; | |
189 function listener(action, filter) | |
190 { | |
191 if (action.indexOf("filter.") == 0) | |
192 changes.push(action + " " + filter.text); | |
193 } | |
194 FilterNotifier.addListener(listener); | |
195 | |
196 compareFiltersList("Initial state", [[], [], []]); | |
197 deepEqual(changes, [], "Received changes"); | |
198 | |
199 changes = []; | |
200 FilterStorage.addFilter(Filter.fromText("foo")); | |
201 compareFiltersList("Adding blocking filter", [["foo"], [], []]); | |
202 deepEqual(changes, ["filter.added foo"], "Received changes"); | |
203 | |
204 changes = []; | |
205 FilterStorage.addFilter(Filter.fromText("@@bar")); | |
206 compareFiltersList("Adding exception rule", [["foo"], ["@@bar"], []]); | |
207 deepEqual(changes, ["filter.added @@bar"], "Received changes"); | |
208 | |
209 changes = []; | |
210 FilterStorage.addFilter(Filter.fromText("foo#bar")); | |
211 compareFiltersList("Adding hiding rule", [["foo"], ["@@bar", "foo#bar"], []]
); | |
212 deepEqual(changes, ["filter.added foo#bar"], "Received changes"); | |
213 | |
214 changes = []; | |
215 FilterStorage.addFilter(Filter.fromText("foo#@#bar")); | |
216 compareFiltersList("Adding hiding exception", [["foo"], ["@@bar", "foo#bar",
"foo#@#bar"], []]); | |
217 deepEqual(changes, ["filter.added foo#@#bar"], "Received changes"); | |
218 | |
219 changes = []; | |
220 FilterStorage.addFilter(Filter.fromText("!foobar"), undefined, undefined, tr
ue); | |
221 compareFiltersList("Adding comment silent", [["foo"], ["@@bar", "foo#bar", "
foo#@#bar"], ["!foobar"]]); | |
222 deepEqual(changes, [], "Received changes"); | |
223 | |
224 changes = []; | |
225 FilterStorage.addFilter(Filter.fromText("foo")); | |
226 compareFiltersList("Adding already added filter", [["foo"], ["@@bar", "foo#b
ar", "foo#@#bar"], ["!foobar"]]); | |
227 deepEqual(changes, [], "Received changes"); | |
228 | |
229 subscription1.disabled = true; | |
230 | |
231 changes = []; | |
232 FilterStorage.addFilter(Filter.fromText("foo")); | |
233 compareFiltersList("Adding filter already in a disabled subscription", [["fo
o"], ["@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "foo"]]); | |
234 deepEqual(changes, ["filter.added foo"], "Received changes"); | |
235 | |
236 changes = []; | |
237 FilterStorage.addFilter(Filter.fromText("foo"), subscription1); | |
238 compareFiltersList("Adding filter to an explicit subscription", [["foo", "fo
o"], ["@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "foo"]]); | |
239 deepEqual(changes, ["filter.added foo"], "Received changes"); | |
240 | |
241 changes = []; | |
242 FilterStorage.addFilter(Filter.fromText("!foobar"), subscription2, 0); | |
243 compareFiltersList("Adding filter to an explicit subscription with position"
, [["foo", "foo"], ["!foobar", "@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "fo
o"]]); | |
244 deepEqual(changes, ["filter.added !foobar"], "Received changes"); | |
245 }); | |
246 | |
247 test("Removing filters", function() | |
248 { | |
249 let subscription1 = Subscription.fromURL("~foo"); | |
250 subscription1.filters = [Filter.fromText("foo"), Filter.fromText("foo"), Fil
ter.fromText("bar")]; | |
251 | |
252 let subscription2 = Subscription.fromURL("~bar"); | |
253 subscription2.filters = [Filter.fromText("foo"), Filter.fromText("bar"), Fil
ter.fromText("foo")]; | |
254 | |
255 let subscription3 = Subscription.fromURL("http://test/"); | |
256 subscription3.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; | |
257 | |
258 FilterStorage.addSubscription(subscription1); | |
259 FilterStorage.addSubscription(subscription2); | |
260 FilterStorage.addSubscription(subscription3); | |
261 | |
262 let changes = []; | |
263 function listener(action, filter) | |
264 { | |
265 if (action.indexOf("filter.") == 0) | |
266 changes.push(action + " " + filter.text); | |
267 } | |
268 FilterNotifier.addListener(listener); | |
269 | |
270 compareFiltersList("Initial state", [["foo", "foo", "bar"], ["foo", "bar", "
foo"], ["foo", "bar"]]); | |
271 deepEqual(changes, [], "Received changes"); | |
272 | |
273 changes = []; | |
274 FilterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0); | |
275 compareFiltersList("Remove with explicit subscription and position", [["foo"
, "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]); | |
276 deepEqual(changes, ["filter.removed foo"], "Received changes"); | |
277 | |
278 changes = []; | |
279 FilterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0); | |
280 compareFiltersList("Remove with explicit subscription and wrong position", [
["foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]); | |
281 deepEqual(changes, [], "Received changes"); | |
282 | |
283 changes = []; | |
284 FilterStorage.removeFilter(Filter.fromText("foo"), subscription1); | |
285 compareFiltersList("Remove with explicit subscription", [["bar"], ["bar", "f
oo"], ["foo", "bar"]]); | |
286 deepEqual(changes, ["filter.removed foo", "filter.removed foo"], "Received c
hanges"); | |
287 | |
288 changes = []; | |
289 FilterStorage.removeFilter(Filter.fromText("foo"), subscription1); | |
290 compareFiltersList("Remove from subscription not having the filter", [["bar"
], ["bar", "foo"], ["foo", "bar"]]); | |
291 deepEqual(changes, [], "Received changes"); | |
292 | |
293 changes = []; | |
294 FilterStorage.removeFilter(Filter.fromText("bar")); | |
295 compareFiltersList("Remove everywhere", [[], ["foo"], ["foo", "bar"]]); | |
296 deepEqual(changes, ["filter.removed bar", "filter.removed bar"], "Received c
hanges"); | |
297 | |
298 changes = []; | |
299 FilterStorage.removeFilter(Filter.fromText("bar")); | |
300 compareFiltersList("Remove of unknown filter", [[], ["foo"], ["foo", "bar"]]
); | |
301 deepEqual(changes, [], "Received changes"); | |
302 }); | |
303 | |
304 test("Moving filters", function() | |
305 { | |
306 let subscription1 = Subscription.fromURL("~foo"); | |
307 subscription1.filters = [Filter.fromText("foo"), Filter.fromText("bar"), Fil
ter.fromText("bas"), Filter.fromText("foo")]; | |
308 | |
309 let subscription2 = Subscription.fromURL("http://test/"); | |
310 subscription2.filters = [Filter.fromText("foo"), Filter.fromText("bar")]; | |
311 | |
312 FilterStorage.addSubscription(subscription1); | |
313 FilterStorage.addSubscription(subscription2); | |
314 | |
315 let changes = []; | |
316 function listener(action, filter) | |
317 { | |
318 if (action.indexOf("filter.") == 0) | |
319 changes.push(action + " " + filter.text); | |
320 } | |
321 FilterNotifier.addListener(listener); | |
322 | |
323 compareFiltersList("Initial state", [["foo", "bar", "bas", "foo"], ["foo", "
bar"]]); | |
324 deepEqual(changes, [], "Received changes"); | |
325 | |
326 changes = []; | |
327 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 1); | |
328 compareFiltersList("Regular move", [["bar", "foo", "bas", "foo"], ["foo", "b
ar"]]); | |
329 deepEqual(changes, ["filter.moved foo"], "Received changes"); | |
330 | |
331 changes = []; | |
332 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 3); | |
333 compareFiltersList("Invalid move", [["bar", "foo", "bas", "foo"], ["foo", "b
ar"]]); | |
334 deepEqual(changes, [], "Received changes"); | |
335 | |
336 changes = []; | |
337 FilterStorage.moveFilter(Filter.fromText("foo"), subscription2, 0, 1); | |
338 compareFiltersList("Invalid subscription", [["bar", "foo", "bas", "foo"], ["
foo", "bar"]]); | |
339 deepEqual(changes, [], "Received changes"); | |
340 | |
341 changes = []; | |
342 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 1, 1); | |
343 compareFiltersList("Move to current position", [["bar", "foo", "bas", "foo"]
, ["foo", "bar"]]); | |
344 deepEqual(changes, [], "Received changes"); | |
345 | |
346 changes = []; | |
347 FilterStorage.moveFilter(Filter.fromText("bar"), subscription1, 0, 1); | |
348 compareFiltersList("Regular move", [["foo", "bar", "bas", "foo"], ["foo", "b
ar"]]); | |
349 deepEqual(changes, ["filter.moved bar"], "Received changes"); | |
350 }); | |
351 | |
352 test("Hit counts", function() | |
353 { | |
354 let changes = []; | |
355 function listener(action, filter) | |
356 { | |
357 if (action.indexOf("filter.") == 0) | |
358 changes.push(action + " " + filter.text); | |
359 } | |
360 FilterNotifier.addListener(listener); | |
361 | |
362 let filter1 = Filter.fromText("filter1"); | |
363 let filter2 = Filter.fromText("filter2"); | |
364 | |
365 FilterStorage.addFilter(filter1); | |
366 | |
367 equal(filter1.hitCount, 0, "filter1 initial hit count"); | |
368 equal(filter2.hitCount, 0, "filter2 initial hit count"); | |
369 equal(filter1.lastHit, 0, "filter1 initial last hit"); | |
370 equal(filter2.lastHit, 0, "filter2 initial last hit"); | |
371 | |
372 changes = []; | |
373 FilterStorage.increaseHitCount(filter1); | |
374 equal(filter1.hitCount, 1, "Hit count after increase (filter in list)"); | |
375 ok(filter1.lastHit > 0, "Last hit changed after increase"); | |
376 deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "R
eceived changes"); | |
377 | |
378 changes = []; | |
379 FilterStorage.increaseHitCount(filter2); | |
380 equal(filter2.hitCount, 1, "Hit count after increase (filter not in list)"); | |
381 ok(filter2.lastHit > 0, "Last hit changed after increase"); | |
382 deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "R
eceived changes"); | |
383 | |
384 changes = []; | |
385 FilterStorage.resetHitCounts([filter1]); | |
386 equal(filter1.hitCount, 0, "Hit count after reset"); | |
387 equal(filter1.lastHit, 0, "Last hit after reset"); | |
388 deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "R
eceived changes"); | |
389 | |
390 changes = []; | |
391 FilterStorage.resetHitCounts(null); | |
392 equal(filter2.hitCount, 0, "Hit count after complete reset"); | |
393 equal(filter2.lastHit, 0, "Last hit after complete reset"); | |
394 deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "R
eceived changes"); | |
395 }); | |
396 | |
397 test("Filter/subscription relationship", function() | |
398 { | |
399 let filter1 = Filter.fromText("filter1"); | |
400 let filter2 = Filter.fromText("filter2"); | |
401 let filter3 = Filter.fromText("filter3"); | |
402 | |
403 let subscription1 = Subscription.fromURL("http://test1/"); | |
404 subscription1.filters = [filter1, filter2]; | |
405 | |
406 let subscription2 = Subscription.fromURL("http://test2/"); | |
407 subscription2.filters = [filter2, filter3]; | |
408 | |
409 let subscription3 = Subscription.fromURL("http://test3/"); | |
410 subscription3.filters = [filter1, filter2, filter3]; | |
411 | |
412 compareFilterSubscriptions("Initial filter1 subscriptions", filter1, []); | |
413 compareFilterSubscriptions("Initial filter2 subscriptions", filter2, []); | |
414 compareFilterSubscriptions("Initial filter3 subscriptions", filter3, []); | |
415 | |
416 FilterStorage.addSubscription(subscription1); | |
417 | |
418 compareFilterSubscriptions("filter1 subscriptions after adding http://test1/
", filter1, [subscription1]); | |
419 compareFilterSubscriptions("filter2 subscriptions after adding http://test1/
", filter2, [subscription1]); | |
420 compareFilterSubscriptions("filter3 subscriptions after adding http://test1/
", filter3, []); | |
421 | |
422 FilterStorage.addSubscription(subscription2); | |
423 | |
424 compareFilterSubscriptions("filter1 subscriptions after adding http://test2/
", filter1, [subscription1]); | |
425 compareFilterSubscriptions("filter2 subscriptions after adding http://test2/
", filter2, [subscription1, subscription2]); | |
426 compareFilterSubscriptions("filter3 subscriptions after adding http://test2/
", filter3, [subscription2]); | |
427 | |
428 FilterStorage.removeSubscription(subscription1); | |
429 | |
430 compareFilterSubscriptions("filter1 subscriptions after removing http://test
1/", filter1, []); | |
431 compareFilterSubscriptions("filter2 subscriptions after removing http://test
1/", filter2, [subscription2]); | |
432 compareFilterSubscriptions("filter3 subscriptions after removing http://test
1/", filter3, [subscription2]); | |
433 | |
434 FilterStorage.updateSubscriptionFilters(subscription3, [filter3]); | |
435 | |
436 compareFilterSubscriptions("filter1 subscriptions after updating http://test
3/ filters", filter1, []); | |
437 compareFilterSubscriptions("filter2 subscriptions after updating http://test
3/ filters", filter2, [subscription2]); | |
438 compareFilterSubscriptions("filter3 subscriptions after updating http://test
3/ filters", filter3, [subscription2]); | |
439 | |
440 FilterStorage.addSubscription(subscription3); | |
441 | |
442 compareFilterSubscriptions("filter1 subscriptions after adding http://test3/
", filter1, []); | |
443 compareFilterSubscriptions("filter2 subscriptions after adding http://test3/
", filter2, [subscription2]); | |
444 compareFilterSubscriptions("filter3 subscriptions after adding http://test3/
", filter3, [subscription2, subscription3]); | |
445 | |
446 FilterStorage.updateSubscriptionFilters(subscription3, [filter1, filter2]); | |
447 | |
448 compareFilterSubscriptions("filter1 subscriptions after updating http://test
3/ filters", filter1, [subscription3]); | |
449 compareFilterSubscriptions("filter2 subscriptions after updating http://test
3/ filters", filter2, [subscription2, subscription3]); | |
450 compareFilterSubscriptions("filter3 subscriptions after updating http://test
3/ filters", filter3, [subscription2]); | |
451 | |
452 FilterStorage.removeSubscription(subscription3); | |
453 | |
454 compareFilterSubscriptions("filter1 subscriptions after removing http://test
3/", filter1, []); | |
455 compareFilterSubscriptions("filter2 subscriptions after removing http://test
3/", filter2, [subscription2]); | |
456 compareFilterSubscriptions("filter3 subscriptions after removing http://test
3/", filter3, [subscription2]); | |
457 }); | |
458 })(); | |
OLD | NEW |