Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/filterStorage.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Addressed feedback Created Oct. 4, 2016, 12:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 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 let {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 sandboxedRequire("../lib/filterListener");
32 (
33 {Filter} = sandboxedRequire("../lib/filterClasses"),
34 {FilterNotifier} = sandboxedRequire("../lib/filterNotifier"),
35 {FilterStorage} = sandboxedRequire("../lib/filterStorage"),
36 {Subscription} = sandboxedRequire("../lib/subscriptionClasses")
37 );
38
39 callback();
40 };
41
42 function compareSubscriptionList(test, testMessage, list)
43 {
44 let result = FilterStorage.subscriptions.map(subscription => subscription.url) ;
45 let expected = list.map(subscription => subscription.url);
46 test.deepEqual(result, expected, testMessage);
47 }
48
49 function compareFiltersList(test, testMessage, list)
50 {
51 let result = FilterStorage.subscriptions.map(
52 subscription => subscription.filters.map(
53 filter => filter.text));
54 test.deepEqual(result, list, testMessage);
55 }
56
57 function compareFilterSubscriptions(test, testMessage, filter, list)
58 {
59 let result = filter.subscriptions.map(subscription => subscription.url);
60 let expected = list.map(subscription => subscription.url);
61 test.deepEqual(result, expected, testMessage);
62 }
63
64 exports.testAddingSubscriptions = function(test)
65 {
66 let subscription1 = Subscription.fromURL("http://test1/");
67 let subscription2 = Subscription.fromURL("http://test2/");
68
69 let changes = [];
70 function listener(action, subscription)
71 {
72 if (action.indexOf("subscription.") == 0)
73 changes.push(action + " " + subscription.url);
74 }
75 FilterNotifier.addListener(listener);
76
77 compareSubscriptionList(test, "Initial state", []);
78 test.deepEqual(changes, [], "Received changes");
79
80 changes = [];
81 FilterStorage.addSubscription(subscription1);
82 compareSubscriptionList(test, "Regular add", [subscription1]);
83 test.deepEqual(changes, ["subscription.added http://test1/"], "Received change s");
84
85 changes = [];
86 FilterStorage.addSubscription(subscription1);
87 compareSubscriptionList(test, "Adding already added subscription", [subscripti on1]);
88 test.deepEqual(changes, [], "Received changes");
89
90 changes = [];
91 FilterStorage.addSubscription(subscription2, true);
92 compareSubscriptionList(test, "Silent add", [subscription1, subscription2]);
93 test.deepEqual(changes, [], "Received changes");
94
95 FilterStorage.removeSubscription(subscription1);
96 compareSubscriptionList(test, "Remove", [subscription2]);
97
98 changes = [];
99 FilterStorage.addSubscription(subscription1);
100 compareSubscriptionList(test, "Re-adding previously removed subscription", [su bscription2, subscription1]);
101 test.deepEqual(changes, ["subscription.added http://test1/"], "Received change s");
102
103 test.done();
104 };
105
106 exports.testRemovingSubscriptions = function(test)
107 {
108 let subscription1 = Subscription.fromURL("http://test1/");
109 let subscription2 = Subscription.fromURL("http://test2/");
110 FilterStorage.addSubscription(subscription1);
111 FilterStorage.addSubscription(subscription2);
112
113 let changes = [];
114 function listener(action, subscription)
115 {
116 if (action.indexOf("subscription.") == 0)
117 changes.push(action + " " + subscription.url);
118 }
119 FilterNotifier.addListener(listener);
120
121 compareSubscriptionList(test, "Initial state", [subscription1, subscription2]) ;
122 test.deepEqual(changes, [], "Received changes");
123
124 changes = [];
125 FilterStorage.removeSubscription(subscription1);
126 compareSubscriptionList(test, "Regular remove", [subscription2]);
127 test.deepEqual(changes, ["subscription.removed http://test1/"], "Received chan ges");
128
129 changes = [];
130 FilterStorage.removeSubscription(subscription1);
131 compareSubscriptionList(test, "Removing already removed subscription", [subscr iption2]);
132 test.deepEqual(changes, [], "Received changes");
133
134 changes = [];
135 FilterStorage.removeSubscription(subscription2, true);
136 compareSubscriptionList(test, "Silent remove", []);
137 test.deepEqual(changes, [], "Received changes");
138
139 FilterStorage.addSubscription(subscription1);
140 compareSubscriptionList(test, "Add", [subscription1]);
141
142 changes = [];
143 FilterStorage.removeSubscription(subscription1);
144 compareSubscriptionList(test, "Re-removing previously added subscription", []) ;
145 test.deepEqual(changes, ["subscription.removed http://test1/"], "Received chan ges");
146
147 test.done();
148 };
149
150 exports.testMovingSubscriptions = function(test)
151 {
152 let subscription1 = Subscription.fromURL("http://test1/");
153 let subscription2 = Subscription.fromURL("http://test2/");
154 let subscription3 = Subscription.fromURL("http://test3/");
155
156 FilterStorage.addSubscription(subscription1);
157 FilterStorage.addSubscription(subscription2);
158 FilterStorage.addSubscription(subscription3);
159
160 let changes = [];
161 function listener(action, subscription)
162 {
163 if (action.indexOf("subscription.") == 0)
164 changes.push(action + " " + subscription.url);
165 }
166 FilterNotifier.addListener(listener);
167
168 compareSubscriptionList(test, "Initial state", [subscription1, subscription2, subscription3]);
169 test.deepEqual(changes, [], "Received changes");
170
171 changes = [];
172 FilterStorage.moveSubscription(subscription1);
173 compareSubscriptionList(test, "Move without explicit position", [subscription2 , subscription3, subscription1]);
174 test.deepEqual(changes, ["subscription.moved http://test1/"], "Received change s");
175
176 changes = [];
177 FilterStorage.moveSubscription(subscription1);
178 compareSubscriptionList(test, "Move without explicit position (subscription al ready last)", [subscription2, subscription3, subscription1]);
179 test.deepEqual(changes, [], "Received changes");
180
181 changes = [];
182 FilterStorage.moveSubscription(subscription2, subscription1);
183 compareSubscriptionList(test, "Move with explicit position", [subscription3, s ubscription2, subscription1]);
184 test.deepEqual(changes, ["subscription.moved http://test2/"], "Received change s");
185
186 changes = [];
187 FilterStorage.moveSubscription(subscription3, subscription2);
188 compareSubscriptionList(test, "Move without explicit position (subscription al ready at position)", [subscription3, subscription2, subscription1]);
189 test.deepEqual(changes, [], "Received changes");
190
191 FilterStorage.removeSubscription(subscription2);
192 compareSubscriptionList(test, "Remove", [subscription3, subscription1]);
193
194 changes = [];
195 FilterStorage.moveSubscription(subscription3, subscription2);
196 compareSubscriptionList(test, "Move before removed subscription", [subscriptio n1, subscription3]);
197 test.deepEqual(changes, ["subscription.moved http://test3/"], "Received change s");
198
199 changes = [];
200 FilterStorage.moveSubscription(subscription2);
201 compareSubscriptionList(test, "Move of removed subscription", [subscription1, subscription3]);
202 test.deepEqual(changes, [], "Received changes");
203
204 test.done();
205 };
206
207 exports.testAddingFilters = function(test)
208 {
209 let subscription1 = Subscription.fromURL("~blocking");
210 subscription1.defaults = ["blocking"];
211
212 let subscription2 = Subscription.fromURL("~exceptions");
213 subscription2.defaults = ["whitelist", "elemhide"];
214
215 let subscription3 = Subscription.fromURL("~other");
216
217 FilterStorage.addSubscription(subscription1);
218 FilterStorage.addSubscription(subscription2);
219 FilterStorage.addSubscription(subscription3);
220
221 let changes = [];
222 function listener(action, filter)
223 {
224 if (action.indexOf("filter.") == 0)
225 changes.push(action + " " + filter.text);
226 }
227 FilterNotifier.addListener(listener);
228
229 compareFiltersList(test, "Initial state", [[], [], []]);
230 test.deepEqual(changes, [], "Received changes");
231
232 changes = [];
233 FilterStorage.addFilter(Filter.fromText("foo"));
234 compareFiltersList(test, "Adding blocking filter", [["foo"], [], []]);
235 test.deepEqual(changes, ["filter.added foo"], "Received changes");
236
237 changes = [];
238 FilterStorage.addFilter(Filter.fromText("@@bar"));
239 compareFiltersList(test, "Adding exception rule", [["foo"], ["@@bar"], []]);
240 test.deepEqual(changes, ["filter.added @@bar"], "Received changes");
241
242 changes = [];
243 FilterStorage.addFilter(Filter.fromText("foo#bar"));
244 compareFiltersList(test, "Adding hiding rule", [["foo"], ["@@bar", "foo#bar"], []]);
245 test.deepEqual(changes, ["filter.added foo#bar"], "Received changes");
246
247 changes = [];
248 FilterStorage.addFilter(Filter.fromText("foo#@#bar"));
249 compareFiltersList(test, "Adding hiding exception", [["foo"], ["@@bar", "foo#b ar", "foo#@#bar"], []]);
250 test.deepEqual(changes, ["filter.added foo#@#bar"], "Received changes");
251
252 changes = [];
253 FilterStorage.addFilter(Filter.fromText("!foobar"), undefined, undefined, true );
254 compareFiltersList(test, "Adding comment silent", [["foo"], ["@@bar", "foo#bar ", "foo#@#bar"], ["!foobar"]]);
255 test.deepEqual(changes, [], "Received changes");
256
257 changes = [];
258 FilterStorage.addFilter(Filter.fromText("foo"));
259 compareFiltersList(test, "Adding already added filter", [["foo"], ["@@bar", "f oo#bar", "foo#@#bar"], ["!foobar"]]);
260 test.deepEqual(changes, [], "Received changes");
261
262 subscription1.disabled = true;
263
264 changes = [];
265 FilterStorage.addFilter(Filter.fromText("foo"));
266 compareFiltersList(test, "Adding filter already in a disabled subscription", [ ["foo"], ["@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "foo"]]);
267 test.deepEqual(changes, ["filter.added foo"], "Received changes");
268
269 changes = [];
270 FilterStorage.addFilter(Filter.fromText("foo"), subscription1);
271 compareFiltersList(test, "Adding filter to an explicit subscription", [["foo", "foo"], ["@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "foo"]]);
272 test.deepEqual(changes, ["filter.added foo"], "Received changes");
273
274 changes = [];
275 FilterStorage.addFilter(Filter.fromText("!foobar"), subscription2, 0);
276 compareFiltersList(test, "Adding filter to an explicit subscription with posit ion", [["foo", "foo"], ["!foobar", "@@bar", "foo#bar", "foo#@#bar"], ["!foobar", "foo"]]);
277 test.deepEqual(changes, ["filter.added !foobar"], "Received changes");
278
279 test.done();
280 };
281
282 exports.testRemovingFilters = function(test)
283 {
284 let subscription1 = Subscription.fromURL("~foo");
285 subscription1.filters = [Filter.fromText("foo"), Filter.fromText("foo"), Filte r.fromText("bar")];
286
287 let subscription2 = Subscription.fromURL("~bar");
288 subscription2.filters = [Filter.fromText("foo"), Filter.fromText("bar"), Filte r.fromText("foo")];
289
290 let subscription3 = Subscription.fromURL("http://test/");
291 subscription3.filters = [Filter.fromText("foo"), Filter.fromText("bar")];
292
293 FilterStorage.addSubscription(subscription1);
294 FilterStorage.addSubscription(subscription2);
295 FilterStorage.addSubscription(subscription3);
296
297 let changes = [];
298 function listener(action, filter)
299 {
300 if (action.indexOf("filter.") == 0)
301 changes.push(action + " " + filter.text);
302 }
303 FilterNotifier.addListener(listener);
304
305 compareFiltersList(test, "Initial state", [["foo", "foo", "bar"], ["foo", "bar ", "foo"], ["foo", "bar"]]);
306 test.deepEqual(changes, [], "Received changes");
307
308 changes = [];
309 FilterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0);
310 compareFiltersList(test, "Remove with explicit subscription and position", [[" foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]);
311 test.deepEqual(changes, ["filter.removed foo"], "Received changes");
312
313 changes = [];
314 FilterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0);
315 compareFiltersList(test, "Remove with explicit subscription and wrong position ", [["foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]);
316 test.deepEqual(changes, [], "Received changes");
317
318 changes = [];
319 FilterStorage.removeFilter(Filter.fromText("foo"), subscription1);
320 compareFiltersList(test, "Remove with explicit subscription", [["bar"], ["bar" , "foo"], ["foo", "bar"]]);
321 test.deepEqual(changes, ["filter.removed foo", "filter.removed foo"], "Receive d changes");
322
323 changes = [];
324 FilterStorage.removeFilter(Filter.fromText("foo"), subscription1);
325 compareFiltersList(test, "Remove from subscription not having the filter", [[" bar"], ["bar", "foo"], ["foo", "bar"]]);
326 test.deepEqual(changes, [], "Received changes");
327
328 changes = [];
329 FilterStorage.removeFilter(Filter.fromText("bar"));
330 compareFiltersList(test, "Remove everywhere", [[], ["foo"], ["foo", "bar"]]);
331 test.deepEqual(changes, ["filter.removed bar", "filter.removed bar"], "Receive d changes");
332
333 changes = [];
334 FilterStorage.removeFilter(Filter.fromText("bar"));
335 compareFiltersList(test, "Remove of unknown filter", [[], ["foo"], ["foo", "ba r"]]);
336 test.deepEqual(changes, [], "Received changes");
337
338 test.done();
339 };
340
341 exports.testMovingFilters = function(test)
342 {
343 let subscription1 = Subscription.fromURL("~foo");
344 subscription1.filters = [Filter.fromText("foo"), Filter.fromText("bar"), Filte r.fromText("bas"), Filter.fromText("foo")];
345
346 let subscription2 = Subscription.fromURL("http://test/");
347 subscription2.filters = [Filter.fromText("foo"), Filter.fromText("bar")];
348
349 FilterStorage.addSubscription(subscription1);
350 FilterStorage.addSubscription(subscription2);
351
352 let changes = [];
353 function listener(action, filter)
354 {
355 if (action.indexOf("filter.") == 0)
356 changes.push(action + " " + filter.text);
357 }
358 FilterNotifier.addListener(listener);
359
360 compareFiltersList(test, "Initial state", [["foo", "bar", "bas", "foo"], ["foo ", "bar"]]);
361 test.deepEqual(changes, [], "Received changes");
362
363 changes = [];
364 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 1);
365 compareFiltersList(test, "Regular move", [["bar", "foo", "bas", "foo"], ["foo" , "bar"]]);
366 test.deepEqual(changes, ["filter.moved foo"], "Received changes");
367
368 changes = [];
369 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 3);
370 compareFiltersList(test, "Invalid move", [["bar", "foo", "bas", "foo"], ["foo" , "bar"]]);
371 test.deepEqual(changes, [], "Received changes");
372
373 changes = [];
374 FilterStorage.moveFilter(Filter.fromText("foo"), subscription2, 0, 1);
375 compareFiltersList(test, "Invalid subscription", [["bar", "foo", "bas", "foo"] , ["foo", "bar"]]);
376 test.deepEqual(changes, [], "Received changes");
377
378 changes = [];
379 FilterStorage.moveFilter(Filter.fromText("foo"), subscription1, 1, 1);
380 compareFiltersList(test, "Move to current position", [["bar", "foo", "bas", "f oo"], ["foo", "bar"]]);
381 test.deepEqual(changes, [], "Received changes");
382
383 changes = [];
384 FilterStorage.moveFilter(Filter.fromText("bar"), subscription1, 0, 1);
385 compareFiltersList(test, "Regular move", [["foo", "bar", "bas", "foo"], ["foo" , "bar"]]);
386 test.deepEqual(changes, ["filter.moved bar"], "Received changes");
387
388 test.done();
389 };
390
391 exports.testHitCounts = function(test)
392 {
393 let changes = [];
394 function listener(action, filter)
395 {
396 if (action.indexOf("filter.") == 0)
397 changes.push(action + " " + filter.text);
398 }
399 FilterNotifier.addListener(listener);
400
401 let filter1 = Filter.fromText("filter1");
402 let filter2 = Filter.fromText("filter2");
403
404 FilterStorage.addFilter(filter1);
405
406 test.equal(filter1.hitCount, 0, "filter1 initial hit count");
407 test.equal(filter2.hitCount, 0, "filter2 initial hit count");
408 test.equal(filter1.lastHit, 0, "filter1 initial last hit");
409 test.equal(filter2.lastHit, 0, "filter2 initial last hit");
410
411 changes = [];
412 FilterStorage.increaseHitCount(filter1);
413 test.equal(filter1.hitCount, 1, "Hit count after increase (filter in list)");
414 test.ok(filter1.lastHit > 0, "Last hit changed after increase");
415 test.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "Received changes");
416
417 changes = [];
418 FilterStorage.increaseHitCount(filter2);
419 test.equal(filter2.hitCount, 1, "Hit count after increase (filter not in list) ");
420 test.ok(filter2.lastHit > 0, "Last hit changed after increase");
421 test.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "Received changes");
422
423 changes = [];
424 FilterStorage.resetHitCounts([filter1]);
425 test.equal(filter1.hitCount, 0, "Hit count after reset");
426 test.equal(filter1.lastHit, 0, "Last hit after reset");
427 test.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "Received changes");
428
429 changes = [];
430 FilterStorage.resetHitCounts(null);
431 test.equal(filter2.hitCount, 0, "Hit count after complete reset");
432 test.equal(filter2.lastHit, 0, "Last hit after complete reset");
433 test.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "Received changes");
434
435 test.done();
436 };
437
438 exports.testFilterSubscriptionRelationship = function(test)
439 {
440 let filter1 = Filter.fromText("filter1");
441 let filter2 = Filter.fromText("filter2");
442 let filter3 = Filter.fromText("filter3");
443
444 let subscription1 = Subscription.fromURL("http://test1/");
445 subscription1.filters = [filter1, filter2];
446
447 let subscription2 = Subscription.fromURL("http://test2/");
448 subscription2.filters = [filter2, filter3];
449
450 let subscription3 = Subscription.fromURL("http://test3/");
451 subscription3.filters = [filter1, filter2, filter3];
452
453 compareFilterSubscriptions(test, "Initial filter1 subscriptions", filter1, []) ;
454 compareFilterSubscriptions(test, "Initial filter2 subscriptions", filter2, []) ;
455 compareFilterSubscriptions(test, "Initial filter3 subscriptions", filter3, []) ;
456
457 FilterStorage.addSubscription(subscription1);
458
459 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st1/", filter1, [subscription1]);
460 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st1/", filter2, [subscription1]);
461 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st1/", filter3, []);
462
463 FilterStorage.addSubscription(subscription2);
464
465 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st2/", filter1, [subscription1]);
466 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st2/", filter2, [subscription1, subscription2]);
467 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st2/", filter3, [subscription2]);
468
469 FilterStorage.removeSubscription(subscription1);
470
471 compareFilterSubscriptions(test, "filter1 subscriptions after removing http:// test1/", filter1, []);
472 compareFilterSubscriptions(test, "filter2 subscriptions after removing http:// test1/", filter2, [subscription2]);
473 compareFilterSubscriptions(test, "filter3 subscriptions after removing http:// test1/", filter3, [subscription2]);
474
475 FilterStorage.updateSubscriptionFilters(subscription3, [filter3]);
476
477 compareFilterSubscriptions(test, "filter1 subscriptions after updating http:// test3/ filters", filter1, []);
478 compareFilterSubscriptions(test, "filter2 subscriptions after updating http:// test3/ filters", filter2, [subscription2]);
479 compareFilterSubscriptions(test, "filter3 subscriptions after updating http:// test3/ filters", filter3, [subscription2]);
480
481 FilterStorage.addSubscription(subscription3);
482
483 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st3/", filter1, []);
484 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st3/", filter2, [subscription2]);
485 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st3/", filter3, [subscription2, subscription3]);
486
487 FilterStorage.updateSubscriptionFilters(subscription3, [filter1, filter2]);
488
489 compareFilterSubscriptions(test, "filter1 subscriptions after updating http:// test3/ filters", filter1, [subscription3]);
490 compareFilterSubscriptions(test, "filter2 subscriptions after updating http:// test3/ filters", filter2, [subscription2, subscription3]);
491 compareFilterSubscriptions(test, "filter3 subscriptions after updating http:// test3/ filters", filter3, [subscription2]);
492
493 FilterStorage.removeSubscription(subscription3);
494
495 compareFilterSubscriptions(test, "filter1 subscriptions after removing http:// test3/", filter1, []);
496 compareFilterSubscriptions(test, "filter2 subscriptions after removing http:// test3/", filter2, [subscription2]);
497 compareFilterSubscriptions(test, "filter3 subscriptions after removing http:// test3/", filter3, [subscription2]);
498
499 test.done();
500 };
OLDNEW
« no previous file with comments | « test/filterNotifier.js ('k') | test/matcher.js » ('j') | test_runner.js » ('J')

Powered by Google App Engine
This is Rietveld