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

Side by Side Diff: test/filterStorage.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created March 7, 2019, 1:14 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
« no previous file with comments | « test/filterNotifier.js ('k') | test/filterStorage_readwrite.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 Filter = null; 23 let Filter = null;
23 let filterNotifier = null; 24 let filterNotifier = null;
24 let filterStorage = null; 25 let filterStorage = null;
25 let Subscription = null; 26 let Subscription = null;
26 27
27 exports.setUp = function(callback) 28 describe("Filter Storage", () =>
28 { 29 {
29 let sandboxedRequire = createSandbox(); 30 beforeEach(() =>
30 31 {
31 sandboxedRequire("../lib/filterListener"); 32 let sandboxedRequire = createSandbox();
32 ( 33
33 {Filter} = sandboxedRequire("../lib/filterClasses"), 34 sandboxedRequire("../lib/filterListener");
34 {filterNotifier} = sandboxedRequire("../lib/filterNotifier"), 35 (
35 {filterStorage} = sandboxedRequire("../lib/filterStorage"), 36 {Filter} = sandboxedRequire("../lib/filterClasses"),
36 {Subscription} = sandboxedRequire("../lib/subscriptionClasses") 37 {filterNotifier} = sandboxedRequire("../lib/filterNotifier"),
37 ); 38 {filterStorage} = sandboxedRequire("../lib/filterStorage"),
38 39 {Subscription} = sandboxedRequire("../lib/subscriptionClasses")
39 callback(); 40 );
40 }; 41 });
41 42
42 function addListener(listener) 43 function addListener(listener)
43 { 44 {
44 let makeWrapper = name => (...args) => listener(name, ...args); 45 let makeWrapper = name => (...args) => listener(name, ...args);
45 46
46 filterNotifier.on("subscription.added", makeWrapper("subscription.added")); 47 filterNotifier.on("subscription.added", makeWrapper("subscription.added"));
47 filterNotifier.on("subscription.removed", 48 filterNotifier.on("subscription.removed",
48 makeWrapper("subscription.removed")); 49 makeWrapper("subscription.removed"));
49 50
50 filterNotifier.on("filter.added", makeWrapper("filter.added")); 51 filterNotifier.on("filter.added", makeWrapper("filter.added"));
51 filterNotifier.on("filter.removed", makeWrapper("filter.removed")); 52 filterNotifier.on("filter.removed", makeWrapper("filter.removed"));
52 filterNotifier.on("filter.moved", makeWrapper("filter.moved")); 53 filterNotifier.on("filter.moved", makeWrapper("filter.moved"));
53 54
54 filterNotifier.on("filter.hitCount", makeWrapper("filter.hitCount")); 55 filterNotifier.on("filter.hitCount", makeWrapper("filter.hitCount"));
55 filterNotifier.on("filter.lastHit", makeWrapper("filter.lastHit")); 56 filterNotifier.on("filter.lastHit", makeWrapper("filter.lastHit"));
56 }
57
58 function compareSubscriptionList(test, testMessage, list,
59 knownSubscriptions = null)
60 {
61 let result = [...filterStorage.knownSubscriptions.keys()];
62 let expected = list.map(subscription => subscription.url);
63 test.deepEqual(result, expected, testMessage);
64
65 if (knownSubscriptions)
66 {
67 test.deepEqual([...Subscription.knownSubscriptions.values()],
68 knownSubscriptions, testMessage);
69 } 57 }
70 } 58
71 59 function compareSubscriptionList(testMessage, list,
72 function compareFiltersList(test, testMessage, list) 60 knownSubscriptions = null)
73 { 61 {
74 let result = [...filterStorage.subscriptions()].map( 62 let result = [...filterStorage.knownSubscriptions.keys()];
75 subscription => [...subscription.filterText()]); 63 let expected = list.map(subscription => subscription.url);
76 test.deepEqual(result, list, testMessage); 64 assert.deepEqual(result, expected, testMessage);
77 } 65
78 66 if (knownSubscriptions)
79 function compareFilterSubscriptions(test, testMessage, filter, list) 67 {
80 { 68 assert.deepEqual([...Subscription.knownSubscriptions.values()],
81 let result = [...filter.subscriptions()].map(subscription => subscription.url) ; 69 knownSubscriptions, testMessage);
82 let expected = list.map(subscription => subscription.url); 70 }
83 test.deepEqual(result, expected, testMessage);
84 }
85
86 exports.testAddingSubscriptions = function(test)
87 {
88 let subscription1 = Subscription.fromURL("http://test1/");
89 let subscription2 = Subscription.fromURL("http://test2/");
90
91 let changes = [];
92 function listener(action, subscription)
93 {
94 if (action.indexOf("subscription.") == 0)
95 changes.push(action + " " + subscription.url);
96 } 71 }
97 addListener(listener); 72
98 73 function compareFiltersList(testMessage, list)
99 compareSubscriptionList(test, "Initial state", []); 74 {
100 test.deepEqual(changes, [], "Received changes"); 75 let result = [...filterStorage.subscriptions()].map(
101 76 subscription => [...subscription.filterText()]);
102 changes = []; 77 assert.deepEqual(result, list, testMessage);
103 filterStorage.addSubscription(subscription1);
104 compareSubscriptionList(test, "Adding first subscription", [subscription1]);
105 test.deepEqual(changes, ["subscription.added http://test1/"], "Received change s");
106
107 changes = [];
108 filterStorage.addSubscription(subscription1);
109 compareSubscriptionList(test, "Adding already added subscription", [subscripti on1]);
110 test.deepEqual(changes, [], "Received changes");
111
112 changes = [];
113 filterStorage.addSubscription(subscription2);
114 compareSubscriptionList(test, "Adding second", [subscription1, subscription2]) ;
115 test.deepEqual(changes, ["subscription.added http://test2/"], "Received change s");
116
117 filterStorage.removeSubscription(subscription1);
118 compareSubscriptionList(test, "Remove", [subscription2]);
119
120 changes = [];
121 filterStorage.addSubscription(subscription1);
122 compareSubscriptionList(test, "Re-adding previously removed subscription", [su bscription2, subscription1]);
123 test.deepEqual(changes, ["subscription.added http://test1/"], "Received change s");
124
125 test.done();
126 };
127
128 exports.testRemovingSubscriptions = function(test)
129 {
130 let subscription1 = Subscription.fromURL("http://test1/");
131 let subscription2 = Subscription.fromURL("http://test2/");
132
133 test.equal(Subscription.fromURL(subscription1.url), subscription1,
134 "Subscription known before addition");
135
136 filterStorage.addSubscription(subscription1);
137 filterStorage.addSubscription(subscription2);
138
139 let changes = [];
140 function listener(action, subscription)
141 {
142 if (action.indexOf("subscription.") == 0)
143 changes.push(action + " " + subscription.url);
144 } 78 }
145 addListener(listener); 79
146 80 function compareFilterSubscriptions(testMessage, filter, list)
147 compareSubscriptionList(test, "Initial state", [subscription1, subscription2], 81 {
148 [subscription1, subscription2]); 82 let result = [...filter.subscriptions()].map(subscription => subscription.ur l);
149 test.deepEqual(changes, [], "Received changes"); 83 let expected = list.map(subscription => subscription.url);
150 84 assert.deepEqual(result, expected, testMessage);
151 test.equal(Subscription.fromURL(subscription1.url), subscription1,
152 "Subscription known after addition");
153
154 changes = [];
155 filterStorage.removeSubscription(subscription1);
156 compareSubscriptionList(test, "Removing first subscription", [subscription2],
157 [subscription2]);
158 test.deepEqual(changes, ["subscription.removed http://test1/"], "Received chan ges");
159
160 // Once a subscription has been removed, it is forgotten; a new object is
161 // created for the previously known subscription URL.
162 test.notEqual(Subscription.fromURL(subscription1.url), subscription1,
163 "Subscription forgotten upon removal");
164 Subscription.knownSubscriptions.delete(subscription1.url);
165
166 changes = [];
167 filterStorage.removeSubscription(subscription1);
168 compareSubscriptionList(test, "Removing already removed subscription", [subscr iption2],
169 [subscription2]);
170 test.deepEqual(changes, [], "Received changes");
171
172 changes = [];
173 filterStorage.removeSubscription(subscription2);
174 compareSubscriptionList(test, "Removing remaining subscription", [], []);
175 test.deepEqual(changes, ["subscription.removed http://test2/"], "Received chan ges");
176
177 filterStorage.addSubscription(subscription1);
178 compareSubscriptionList(test, "Add", [subscription1], []);
179
180 changes = [];
181 filterStorage.removeSubscription(subscription1);
182 compareSubscriptionList(test, "Re-removing previously added subscription", [], []);
183 test.deepEqual(changes, ["subscription.removed http://test1/"], "Received chan ges");
184
185 test.done();
186 };
187
188 exports.testMovingSubscriptions = function(test)
189 {
190 let subscription1 = Subscription.fromURL("http://test1/");
191 let subscription2 = Subscription.fromURL("http://test2/");
192 let subscription3 = Subscription.fromURL("http://test3/");
193
194 filterStorage.addSubscription(subscription1);
195 filterStorage.addSubscription(subscription2);
196 filterStorage.addSubscription(subscription3);
197
198 let changes = [];
199 function listener(action, subscription)
200 {
201 if (action.indexOf("subscription.") == 0)
202 changes.push(action + " " + subscription.url);
203 } 85 }
204 addListener(listener); 86
205 87 it("Adding Subscriptions", () =>
206 compareSubscriptionList(test, "Initial state", [subscription1, subscription2, subscription3]); 88 {
207 test.deepEqual(changes, [], "Received changes"); 89 let subscription1 = Subscription.fromURL("http://test1/");
208 90 let subscription2 = Subscription.fromURL("http://test2/");
209 filterStorage.removeSubscription(subscription2); 91
210 compareSubscriptionList(test, "Remove", [subscription1, subscription3]); 92 let changes = [];
211 93 function listener(action, subscription)
212 test.done(); 94 {
213 }; 95 if (action.indexOf("subscription.") == 0)
214 96 changes.push(action + " " + subscription.url);
215 exports.testAddingFilters = function(test) 97 }
216 { 98 addListener(listener);
217 let subscription1 = Subscription.fromURL("~blocking"); 99
218 subscription1.defaults = ["blocking"]; 100 compareSubscriptionList("Initial state", []);
219 101 assert.deepEqual(changes, [], "Received changes");
220 let subscription2 = Subscription.fromURL("~exceptions"); 102
221 subscription2.defaults = ["whitelist", "elemhide"]; 103 changes = [];
222 104 filterStorage.addSubscription(subscription1);
223 let subscription3 = Subscription.fromURL("~other"); 105 compareSubscriptionList("Adding first subscription", [subscription1]);
224 106 assert.deepEqual(changes, ["subscription.added http://test1/"], "Received ch anges");
225 filterStorage.addSubscription(subscription1); 107
226 filterStorage.addSubscription(subscription2); 108 changes = [];
227 filterStorage.addSubscription(subscription3); 109 filterStorage.addSubscription(subscription1);
228 110 compareSubscriptionList("Adding already added subscription", [subscription1] );
229 let changes = []; 111 assert.deepEqual(changes, [], "Received changes");
230 function listener(action, filter) 112
231 { 113 changes = [];
232 if (action.indexOf("filter.") == 0) 114 filterStorage.addSubscription(subscription2);
233 changes.push(action + " " + filter.text); 115 compareSubscriptionList("Adding second", [subscription1, subscription2]);
234 } 116 assert.deepEqual(changes, ["subscription.added http://test2/"], "Received ch anges");
235 addListener(listener); 117
236 118 filterStorage.removeSubscription(subscription1);
237 compareFiltersList(test, "Initial state", [[], [], []]); 119 compareSubscriptionList("Remove", [subscription2]);
238 test.deepEqual(changes, [], "Received changes"); 120
239 121 changes = [];
240 changes = []; 122 filterStorage.addSubscription(subscription1);
241 filterStorage.addFilter(Filter.fromText("foo")); 123 compareSubscriptionList("Re-adding previously removed subscription", [subscr iption2, subscription1]);
242 compareFiltersList(test, "Adding blocking filter", [["foo"], [], []]); 124 assert.deepEqual(changes, ["subscription.added http://test1/"], "Received ch anges");
243 test.deepEqual(changes, ["filter.added foo"], "Received changes"); 125 });
244 126
245 changes = []; 127 it("Removing Subscriptions", () =>
246 filterStorage.addFilter(Filter.fromText("@@bar")); 128 {
247 compareFiltersList(test, "Adding exception rule", [["foo"], ["@@bar"], []]); 129 let subscription1 = Subscription.fromURL("http://test1/");
248 test.deepEqual(changes, ["filter.added @@bar"], "Received changes"); 130 let subscription2 = Subscription.fromURL("http://test2/");
249 131
250 changes = []; 132 assert.equal(Subscription.fromURL(subscription1.url), subscription1,
251 filterStorage.addFilter(Filter.fromText("foo##bar")); 133 "Subscription known before addition");
252 compareFiltersList(test, "Adding hiding rule", [["foo"], ["@@bar", "foo##bar"] , []]); 134
253 test.deepEqual(changes, ["filter.added foo##bar"], "Received changes"); 135 filterStorage.addSubscription(subscription1);
254 136 filterStorage.addSubscription(subscription2);
255 changes = []; 137
256 filterStorage.addFilter(Filter.fromText("foo#@#bar")); 138 let changes = [];
257 compareFiltersList(test, "Adding hiding exception", [["foo"], ["@@bar", "foo## bar", "foo#@#bar"], []]); 139 function listener(action, subscription)
258 test.deepEqual(changes, ["filter.added foo#@#bar"], "Received changes"); 140 {
259 141 if (action.indexOf("subscription.") == 0)
260 changes = []; 142 changes.push(action + " " + subscription.url);
261 filterStorage.addFilter(Filter.fromText("example.com#$#foobar")); 143 }
262 compareFiltersList(test, "Adding snippet filter", [["foo"], ["@@bar", "foo##ba r", "foo#@#bar"], ["example.com#$#foobar"]]); 144 addListener(listener);
263 test.deepEqual(changes, ["filter.added example.com#$#foobar"], "Received chang es"); 145
264 146 compareSubscriptionList("Initial state", [subscription1, subscription2],
265 changes = []; 147 [subscription1, subscription2]);
266 filterStorage.addFilter(Filter.fromText("!foobar")); 148 assert.deepEqual(changes, [], "Received changes");
267 compareFiltersList(test, "Adding comment", [["foo", "!foobar"], ["@@bar", "foo ##bar", "foo#@#bar"], ["example.com#$#foobar"]]); 149
268 test.deepEqual(changes, ["filter.added !foobar"], "Received changes"); 150 assert.equal(Subscription.fromURL(subscription1.url), subscription1,
269 151 "Subscription known after addition");
270 changes = []; 152
271 filterStorage.addFilter(Filter.fromText("foo")); 153 changes = [];
272 compareFiltersList(test, "Adding already added filter", [["foo", "!foobar"], [ "@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar"]]); 154 filterStorage.removeSubscription(subscription1);
273 test.deepEqual(changes, [], "Received changes"); 155 compareSubscriptionList("Removing first subscription", [subscription2],
274 156 [subscription2]);
275 subscription1.disabled = true; 157 assert.deepEqual(changes, ["subscription.removed http://test1/"], "Received changes");
276 158
277 changes = []; 159 // Once a subscription has been removed, it is forgotten; a new object is
278 filterStorage.addFilter(Filter.fromText("foo")); 160 // created for the previously known subscription URL.
279 compareFiltersList(test, "Adding filter already in a disabled subscription", [ ["foo", "!foobar"], ["@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar", "foo"]]); 161 assert.notEqual(Subscription.fromURL(subscription1.url), subscription1,
280 test.deepEqual(changes, ["filter.added foo"], "Received changes"); 162 "Subscription forgotten upon removal");
281 163 Subscription.knownSubscriptions.delete(subscription1.url);
282 changes = []; 164
283 filterStorage.addFilter(Filter.fromText("foo"), subscription1); 165 changes = [];
284 compareFiltersList(test, "Adding filter to an explicit subscription", [["foo", "!foobar", "foo"], ["@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar", "foo"]]); 166 filterStorage.removeSubscription(subscription1);
285 test.deepEqual(changes, ["filter.added foo"], "Received changes"); 167 compareSubscriptionList("Removing already removed subscription", [subscripti on2],
286 168 [subscription2]);
287 changes = []; 169 assert.deepEqual(changes, [], "Received changes");
288 filterStorage.addFilter(Filter.fromText("example.com#$#foobar"), subscription2 , 0); 170
289 compareFiltersList(test, "Adding filter to an explicit subscription with posit ion", [["foo", "!foobar", "foo"], ["example.com#$#foobar", "@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar", "foo"]]); 171 changes = [];
290 test.deepEqual(changes, ["filter.added example.com#$#foobar"], "Received chang es"); 172 filterStorage.removeSubscription(subscription2);
291 173 compareSubscriptionList("remaining subscription", [], []);
292 test.done(); 174 assert.deepEqual(changes, ["subscription.removed http://test2/"], "Received changes");
293 }; 175
294 176 filterStorage.addSubscription(subscription1);
295 exports.testRemovingFilters = function(test) 177 compareSubscriptionList("Add", [subscription1], []);
296 { 178
297 let subscription1 = Subscription.fromURL("~foo"); 179 changes = [];
298 subscription1.addFilter(Filter.fromText("foo")); 180 filterStorage.removeSubscription(subscription1);
299 subscription1.addFilter(Filter.fromText("foo")); 181 compareSubscriptionList("Re-removing previously added subscription", [], []) ;
300 subscription1.addFilter(Filter.fromText("bar")); 182 assert.deepEqual(changes, ["subscription.removed http://test1/"], "Received changes");
301 183 });
302 let subscription2 = Subscription.fromURL("~bar"); 184
303 subscription2.addFilter(Filter.fromText("foo")); 185 it("Moving Subscriptions", () =>
304 subscription2.addFilter(Filter.fromText("bar")); 186 {
305 subscription2.addFilter(Filter.fromText("foo")); 187 let subscription1 = Subscription.fromURL("http://test1/");
306 188 let subscription2 = Subscription.fromURL("http://test2/");
307 let subscription3 = Subscription.fromURL("http://test/"); 189 let subscription3 = Subscription.fromURL("http://test3/");
308 subscription3.addFilter(Filter.fromText("foo")); 190
309 subscription3.addFilter(Filter.fromText("bar")); 191 filterStorage.addSubscription(subscription1);
310 192 filterStorage.addSubscription(subscription2);
311 filterStorage.addSubscription(subscription1); 193 filterStorage.addSubscription(subscription3);
312 filterStorage.addSubscription(subscription2); 194
313 filterStorage.addSubscription(subscription3); 195 let changes = [];
314 196 function listener(action, subscription)
315 let changes = []; 197 {
316 function listener(action, filter) 198 if (action.indexOf("subscription.") == 0)
317 { 199 changes.push(action + " " + subscription.url);
318 if (action.indexOf("filter.") == 0) 200 }
319 changes.push(action + " " + filter.text); 201 addListener(listener);
320 } 202
321 addListener(listener); 203 compareSubscriptionList("Initial state", [subscription1, subscription2, subs cription3]);
322 204 assert.deepEqual(changes, [], "Received changes");
323 compareFiltersList(test, "Initial state", [["foo", "foo", "bar"], ["foo", "bar ", "foo"], ["foo", "bar"]]); 205
324 test.deepEqual(changes, [], "Received changes"); 206 filterStorage.removeSubscription(subscription2);
325 207 compareSubscriptionList("Remove", [subscription1, subscription3]);
326 changes = []; 208 });
327 filterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0); 209
328 compareFiltersList(test, "Remove with explicit subscription and position", [[" foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]); 210 it("Adding Filters", () =>
329 test.deepEqual(changes, ["filter.removed foo"], "Received changes"); 211 {
330 212 let subscription1 = Subscription.fromURL("~blocking");
331 changes = []; 213 subscription1.defaults = ["blocking"];
332 filterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0); 214
333 compareFiltersList(test, "Remove with explicit subscription and wrong position ", [["foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]); 215 let subscription2 = Subscription.fromURL("~exceptions");
334 test.deepEqual(changes, [], "Received changes"); 216 subscription2.defaults = ["whitelist", "elemhide"];
335 217
336 changes = []; 218 let subscription3 = Subscription.fromURL("~other");
337 filterStorage.removeFilter(Filter.fromText("foo"), subscription1); 219
338 compareFiltersList(test, "Remove with explicit subscription", [["bar"], ["bar" , "foo"], ["foo", "bar"]]); 220 filterStorage.addSubscription(subscription1);
339 test.deepEqual(changes, ["filter.removed foo", "filter.removed foo"], "Receive d changes"); 221 filterStorage.addSubscription(subscription2);
340 222 filterStorage.addSubscription(subscription3);
341 changes = []; 223
342 filterStorage.removeFilter(Filter.fromText("foo"), subscription1); 224 let changes = [];
343 compareFiltersList(test, "Remove from subscription not having the filter", [[" bar"], ["bar", "foo"], ["foo", "bar"]]); 225 function listener(action, filter)
344 test.deepEqual(changes, [], "Received changes"); 226 {
345 227 if (action.indexOf("filter.") == 0)
346 changes = []; 228 changes.push(action + " " + filter.text);
347 filterStorage.removeFilter(Filter.fromText("bar")); 229 }
348 compareFiltersList(test, "Remove everywhere", [[], ["foo"], ["foo", "bar"]]); 230 addListener(listener);
349 test.deepEqual(changes, ["filter.removed bar", "filter.removed bar"], "Receive d changes"); 231
350 232 compareFiltersList("Initial state", [[], [], []]);
351 changes = []; 233 assert.deepEqual(changes, [], "Received changes");
352 filterStorage.removeFilter(Filter.fromText("bar")); 234
353 compareFiltersList(test, "Remove of unknown filter", [[], ["foo"], ["foo", "ba r"]]); 235 changes = [];
354 test.deepEqual(changes, [], "Received changes"); 236 filterStorage.addFilter(Filter.fromText("foo"));
355 237 compareFiltersList("Adding blocking filter", [["foo"], [], []]);
356 test.done(); 238 assert.deepEqual(changes, ["filter.added foo"], "Received changes");
357 }; 239
358 240 changes = [];
359 exports.testMovingFilters = function(test) 241 filterStorage.addFilter(Filter.fromText("@@bar"));
360 { 242 compareFiltersList("Adding exception rule", [["foo"], ["@@bar"], []]);
361 let subscription1 = Subscription.fromURL("~foo"); 243 assert.deepEqual(changes, ["filter.added @@bar"], "Received changes");
362 subscription1.addFilter(Filter.fromText("foo")); 244
363 subscription1.addFilter(Filter.fromText("bar")); 245 changes = [];
364 subscription1.addFilter(Filter.fromText("bas")); 246 filterStorage.addFilter(Filter.fromText("foo##bar"));
365 subscription1.addFilter(Filter.fromText("foo")); 247 compareFiltersList("Adding hiding rule", [["foo"], ["@@bar", "foo##bar"], [] ]);
366 248 assert.deepEqual(changes, ["filter.added foo##bar"], "Received changes");
367 let subscription2 = Subscription.fromURL("http://test/"); 249
368 subscription2.addFilter(Filter.fromText("foo")); 250 changes = [];
369 subscription2.addFilter(Filter.fromText("bar")); 251 filterStorage.addFilter(Filter.fromText("foo#@#bar"));
370 252 compareFiltersList("Adding hiding exception", [["foo"], ["@@bar", "foo##bar" , "foo#@#bar"], []]);
371 filterStorage.addSubscription(subscription1); 253 assert.deepEqual(changes, ["filter.added foo#@#bar"], "Received changes");
372 filterStorage.addSubscription(subscription2); 254
373 255 changes = [];
374 let changes = []; 256 filterStorage.addFilter(Filter.fromText("example.com#$#foobar"));
375 function listener(action, filter) 257 compareFiltersList("Adding snippet filter", [["foo"], ["@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar"]]);
376 { 258 assert.deepEqual(changes, ["filter.added example.com#$#foobar"], "Received c hanges");
377 if (action.indexOf("filter.") == 0) 259
378 changes.push(action + " " + filter.text); 260 changes = [];
379 } 261 filterStorage.addFilter(Filter.fromText("!foobar"));
380 addListener(listener); 262 compareFiltersList("Adding comment", [["foo", "!foobar"], ["@@bar", "foo##ba r", "foo#@#bar"], ["example.com#$#foobar"]]);
381 263 assert.deepEqual(changes, ["filter.added !foobar"], "Received changes");
382 compareFiltersList(test, "Initial state", [["foo", "bar", "bas", "foo"], ["foo ", "bar"]]); 264
383 test.deepEqual(changes, [], "Received changes"); 265 changes = [];
384 266 filterStorage.addFilter(Filter.fromText("foo"));
385 changes = []; 267 compareFiltersList("Adding already added filter", [["foo", "!foobar"], ["@@b ar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar"]]);
386 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 1); 268 assert.deepEqual(changes, [], "Received changes");
387 compareFiltersList(test, "Regular move", [["bar", "foo", "bas", "foo"], ["foo" , "bar"]]); 269
388 test.deepEqual(changes, ["filter.moved foo"], "Received changes"); 270 subscription1.disabled = true;
389 271
390 changes = []; 272 changes = [];
391 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 3); 273 filterStorage.addFilter(Filter.fromText("foo"));
392 compareFiltersList(test, "Invalid move", [["bar", "foo", "bas", "foo"], ["foo" , "bar"]]); 274 compareFiltersList("Adding filter already in a disabled subscription", [["fo o", "!foobar"], ["@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar", "fo o"]]);
393 test.deepEqual(changes, [], "Received changes"); 275 assert.deepEqual(changes, ["filter.added foo"], "Received changes");
394 276
395 changes = []; 277 changes = [];
396 filterStorage.moveFilter(Filter.fromText("foo"), subscription2, 0, 1); 278 filterStorage.addFilter(Filter.fromText("foo"), subscription1);
397 compareFiltersList(test, "Invalid subscription", [["bar", "foo", "bas", "foo"] , ["foo", "bar"]]); 279 compareFiltersList("Adding filter to an explicit subscription", [["foo", "!f oobar", "foo"], ["@@bar", "foo##bar", "foo#@#bar"], ["example.com#$#foobar", "fo o"]]);
398 test.deepEqual(changes, [], "Received changes"); 280 assert.deepEqual(changes, ["filter.added foo"], "Received changes");
399 281
400 changes = []; 282 changes = [];
401 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 1, 1); 283 filterStorage.addFilter(Filter.fromText("example.com#$#foobar"), subscriptio n2, 0);
402 compareFiltersList(test, "Move to current position", [["bar", "foo", "bas", "f oo"], ["foo", "bar"]]); 284 compareFiltersList("Adding filter to an explicit subscription with position" , [["foo", "!foobar", "foo"], ["example.com#$#foobar", "@@bar", "foo##bar", "foo #@#bar"], ["example.com#$#foobar", "foo"]]);
403 test.deepEqual(changes, [], "Received changes"); 285 assert.deepEqual(changes, ["filter.added example.com#$#foobar"], "Received c hanges");
404 286 });
405 changes = []; 287
406 filterStorage.moveFilter(Filter.fromText("bar"), subscription1, 0, 1); 288 it("Removing Filters", () =>
407 compareFiltersList(test, "Regular move", [["foo", "bar", "bas", "foo"], ["foo" , "bar"]]); 289 {
408 test.deepEqual(changes, ["filter.moved bar"], "Received changes"); 290 let subscription1 = Subscription.fromURL("~foo");
409 291 subscription1.addFilter(Filter.fromText("foo"));
410 test.done(); 292 subscription1.addFilter(Filter.fromText("foo"));
411 }; 293 subscription1.addFilter(Filter.fromText("bar"));
412 294
413 exports.testHitCounts = function(test) 295 let subscription2 = Subscription.fromURL("~bar");
414 { 296 subscription2.addFilter(Filter.fromText("foo"));
415 let changes = []; 297 subscription2.addFilter(Filter.fromText("bar"));
416 function listener(action, filter) 298 subscription2.addFilter(Filter.fromText("foo"));
417 { 299
418 if (action.indexOf("filter.") == 0) 300 let subscription3 = Subscription.fromURL("http://test/");
419 changes.push(action + " " + filter.text); 301 subscription3.addFilter(Filter.fromText("foo"));
420 } 302 subscription3.addFilter(Filter.fromText("bar"));
421 addListener(listener); 303
422 304 filterStorage.addSubscription(subscription1);
423 let filter1 = Filter.fromText("filter1"); 305 filterStorage.addSubscription(subscription2);
424 let filter2 = Filter.fromText("filter2"); 306 filterStorage.addSubscription(subscription3);
425 307
426 filterStorage.addFilter(filter1); 308 let changes = [];
427 309 function listener(action, filter)
428 test.equal(filter1.hitCount, 0, "filter1 initial hit count"); 310 {
429 test.equal(filter2.hitCount, 0, "filter2 initial hit count"); 311 if (action.indexOf("filter.") == 0)
430 test.equal(filter1.lastHit, 0, "filter1 initial last hit"); 312 changes.push(action + " " + filter.text);
431 test.equal(filter2.lastHit, 0, "filter2 initial last hit"); 313 }
432 314 addListener(listener);
433 changes = []; 315
434 filterStorage.increaseHitCount(filter1); 316 compareFiltersList("Initial state", [["foo", "foo", "bar"], ["foo", "bar", " foo"], ["foo", "bar"]]);
435 test.equal(filter1.hitCount, 1, "Hit count after increase (filter in list)"); 317 assert.deepEqual(changes, [], "Received changes");
436 test.ok(filter1.lastHit > 0, "Last hit changed after increase"); 318
437 test.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "Received changes"); 319 changes = [];
438 320 filterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0);
439 changes = []; 321 compareFiltersList("Remove with explicit subscription and position", [["foo" , "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]);
440 filterStorage.increaseHitCount(filter2); 322 assert.deepEqual(changes, ["filter.removed foo"], "Received changes");
441 test.equal(filter2.hitCount, 1, "Hit count after increase (filter not in list) "); 323
442 test.ok(filter2.lastHit > 0, "Last hit changed after increase"); 324 changes = [];
443 test.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "Received changes"); 325 filterStorage.removeFilter(Filter.fromText("foo"), subscription2, 0);
444 326 compareFiltersList("Remove with explicit subscription and wrong position", [ ["foo", "foo", "bar"], ["bar", "foo"], ["foo", "bar"]]);
445 changes = []; 327 assert.deepEqual(changes, [], "Received changes");
446 filterStorage.resetHitCounts([filter1]); 328
447 test.equal(filter1.hitCount, 0, "Hit count after reset"); 329 changes = [];
448 test.equal(filter1.lastHit, 0, "Last hit after reset"); 330 filterStorage.removeFilter(Filter.fromText("foo"), subscription1);
449 test.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter1"], "Received changes"); 331 compareFiltersList("Remove with explicit subscription", [["bar"], ["bar", "f oo"], ["foo", "bar"]]);
450 332 assert.deepEqual(changes, ["filter.removed foo", "filter.removed foo"], "Rec eived changes");
451 changes = []; 333
452 filterStorage.resetHitCounts(null); 334 changes = [];
453 test.equal(filter2.hitCount, 0, "Hit count after complete reset"); 335 filterStorage.removeFilter(Filter.fromText("foo"), subscription1);
454 test.equal(filter2.lastHit, 0, "Last hit after complete reset"); 336 compareFiltersList("Remove from subscription not having the filter", [["bar" ], ["bar", "foo"], ["foo", "bar"]]);
455 test.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter2"], "Received changes"); 337 assert.deepEqual(changes, [], "Received changes");
456 338
457 test.done(); 339 changes = [];
458 }; 340 filterStorage.removeFilter(Filter.fromText("bar"));
459 341 compareFiltersList("Remove everywhere", [[], ["foo"], ["foo", "bar"]]);
460 exports.testFilterSubscriptionRelationship = function(test) 342 assert.deepEqual(changes, ["filter.removed bar", "filter.removed bar"], "Rec eived changes");
461 { 343
462 let filter1 = Filter.fromText("filter1"); 344 changes = [];
463 let filter2 = Filter.fromText("filter2"); 345 filterStorage.removeFilter(Filter.fromText("bar"));
464 let filter3 = Filter.fromText("filter3"); 346 compareFiltersList("Remove of unknown filter", [[], ["foo"], ["foo", "bar"]] );
465 347 assert.deepEqual(changes, [], "Received changes");
466 let subscription1 = Subscription.fromURL("http://test1/"); 348 });
467 subscription1.addFilter(filter1); 349
468 subscription1.addFilter(filter2); 350 it("Moving Filters", () =>
469 351 {
470 let subscription2 = Subscription.fromURL("http://test2/"); 352 let subscription1 = Subscription.fromURL("~foo");
471 subscription2.addFilter(filter2); 353 subscription1.addFilter(Filter.fromText("foo"));
472 subscription2.addFilter(filter3); 354 subscription1.addFilter(Filter.fromText("bar"));
473 355 subscription1.addFilter(Filter.fromText("bas"));
474 let subscription3 = Subscription.fromURL("http://test3/"); 356 subscription1.addFilter(Filter.fromText("foo"));
475 subscription3.addFilter(filter1); 357
476 subscription3.addFilter(filter2); 358 let subscription2 = Subscription.fromURL("http://test/");
477 subscription3.addFilter(filter3); 359 subscription2.addFilter(Filter.fromText("foo"));
478 360 subscription2.addFilter(Filter.fromText("bar"));
479 compareFilterSubscriptions(test, "Initial filter1 subscriptions", filter1, []) ; 361
480 compareFilterSubscriptions(test, "Initial filter2 subscriptions", filter2, []) ; 362 filterStorage.addSubscription(subscription1);
481 compareFilterSubscriptions(test, "Initial filter3 subscriptions", filter3, []) ; 363 filterStorage.addSubscription(subscription2);
482 364
483 filterStorage.addSubscription(subscription1); 365 let changes = [];
484 366 function listener(action, filter)
485 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st1/", filter1, [subscription1]); 367 {
486 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st1/", filter2, [subscription1]); 368 if (action.indexOf("filter.") == 0)
487 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st1/", filter3, []); 369 changes.push(action + " " + filter.text);
488 370 }
489 filterStorage.addSubscription(subscription2); 371 addListener(listener);
490 372
491 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st2/", filter1, [subscription1]); 373 compareFiltersList("Initial state", [["foo", "bar", "bas", "foo"], ["foo", " bar"]]);
492 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st2/", filter2, [subscription1, subscription2]); 374 assert.deepEqual(changes, [], "Received changes");
493 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st2/", filter3, [subscription2]); 375
494 376 changes = [];
495 filterStorage.removeSubscription(subscription1); 377 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 1);
496 378 compareFiltersList("Regular move", [["bar", "foo", "bas", "foo"], ["foo", "b ar"]]);
497 compareFilterSubscriptions(test, "filter1 subscriptions after removing http:// test1/", filter1, []); 379 assert.deepEqual(changes, ["filter.moved foo"], "Received changes");
498 compareFilterSubscriptions(test, "filter2 subscriptions after removing http:// test1/", filter2, [subscription2]); 380
499 compareFilterSubscriptions(test, "filter3 subscriptions after removing http:// test1/", filter3, [subscription2]); 381 changes = [];
500 382 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 0, 3);
501 filterStorage.updateSubscriptionFilters(subscription3, [filter3.text]); 383 compareFiltersList("Invalid move", [["bar", "foo", "bas", "foo"], ["foo", "b ar"]]);
502 384 assert.deepEqual(changes, [], "Received changes");
503 compareFilterSubscriptions(test, "filter1 subscriptions after updating http:// test3/ filters", filter1, []); 385
504 compareFilterSubscriptions(test, "filter2 subscriptions after updating http:// test3/ filters", filter2, [subscription2]); 386 changes = [];
505 compareFilterSubscriptions(test, "filter3 subscriptions after updating http:// test3/ filters", filter3, [subscription2]); 387 filterStorage.moveFilter(Filter.fromText("foo"), subscription2, 0, 1);
506 388 compareFiltersList("Invalid subscription", [["bar", "foo", "bas", "foo"], [" foo", "bar"]]);
507 filterStorage.addSubscription(subscription3); 389 assert.deepEqual(changes, [], "Received changes");
508 390
509 compareFilterSubscriptions(test, "filter1 subscriptions after adding http://te st3/", filter1, []); 391 changes = [];
510 compareFilterSubscriptions(test, "filter2 subscriptions after adding http://te st3/", filter2, [subscription2]); 392 filterStorage.moveFilter(Filter.fromText("foo"), subscription1, 1, 1);
511 compareFilterSubscriptions(test, "filter3 subscriptions after adding http://te st3/", filter3, [subscription2, subscription3]); 393 compareFiltersList("Move to current position", [["bar", "foo", "bas", "foo"] , ["foo", "bar"]]);
512 394 assert.deepEqual(changes, [], "Received changes");
513 filterStorage.updateSubscriptionFilters(subscription3, [filter1.text, filter2. text]); 395
514 396 changes = [];
515 compareFilterSubscriptions(test, "filter1 subscriptions after updating http:// test3/ filters", filter1, [subscription3]); 397 filterStorage.moveFilter(Filter.fromText("bar"), subscription1, 0, 1);
516 compareFilterSubscriptions(test, "filter2 subscriptions after updating http:// test3/ filters", filter2, [subscription2, subscription3]); 398 compareFiltersList("Regular move", [["foo", "bar", "bas", "foo"], ["foo", "b ar"]]);
517 compareFilterSubscriptions(test, "filter3 subscriptions after updating http:// test3/ filters", filter3, [subscription2]); 399 assert.deepEqual(changes, ["filter.moved bar"], "Received changes");
518 400 });
519 filterStorage.removeSubscription(subscription3); 401
520 402 it("Hit Counts", () =>
521 compareFilterSubscriptions(test, "filter1 subscriptions after removing http:// test3/", filter1, []); 403 {
522 compareFilterSubscriptions(test, "filter2 subscriptions after removing http:// test3/", filter2, [subscription2]); 404 let changes = [];
523 compareFilterSubscriptions(test, "filter3 subscriptions after removing http:// test3/", filter3, [subscription2]); 405 function listener(action, filter)
524 406 {
525 test.done(); 407 if (action.indexOf("filter.") == 0)
526 }; 408 changes.push(action + " " + filter.text);
409 }
410 addListener(listener);
411
412 let filter1 = Filter.fromText("filter1");
413 let filter2 = Filter.fromText("filter2");
414
415 filterStorage.addFilter(filter1);
416
417 assert.equal(filter1.hitCount, 0, "filter1 initial hit count");
418 assert.equal(filter2.hitCount, 0, "filter2 initial hit count");
419 assert.equal(filter1.lastHit, 0, "filter1 initial last hit");
420 assert.equal(filter2.lastHit, 0, "filter2 initial last hit");
421
422 changes = [];
423 filterStorage.increaseHitCount(filter1);
424 assert.equal(filter1.hitCount, 1, "Hit count after increase (filter in list) ");
425 assert.ok(filter1.lastHit > 0, "Last hit changed after increase");
426 assert.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter 1"], "Received changes");
427
428 changes = [];
429 filterStorage.increaseHitCount(filter2);
430 assert.equal(filter2.hitCount, 1, "Hit count after increase (filter not in l ist)");
431 assert.ok(filter2.lastHit > 0, "Last hit changed after increase");
432 assert.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter 2"], "Received changes");
433
434 changes = [];
435 filterStorage.resetHitCounts([filter1]);
436 assert.equal(filter1.hitCount, 0, "Hit count after reset");
437 assert.equal(filter1.lastHit, 0, "Last hit after reset");
438 assert.deepEqual(changes, ["filter.hitCount filter1", "filter.lastHit filter 1"], "Received changes");
439
440 changes = [];
441 filterStorage.resetHitCounts(null);
442 assert.equal(filter2.hitCount, 0, "Hit count after complete reset");
443 assert.equal(filter2.lastHit, 0, "Last hit after complete reset");
444 assert.deepEqual(changes, ["filter.hitCount filter2", "filter.lastHit filter 2"], "Received changes");
445 });
446
447 it("Subscription Relationship", () =>
448 {
449 let filter1 = Filter.fromText("filter1");
450 let filter2 = Filter.fromText("filter2");
451 let filter3 = Filter.fromText("filter3");
452
453 let subscription1 = Subscription.fromURL("http://test1/");
454 subscription1.addFilter(filter1);
455 subscription1.addFilter(filter2);
456
457 let subscription2 = Subscription.fromURL("http://test2/");
458 subscription2.addFilter(filter2);
459 subscription2.addFilter(filter3);
460
461 let subscription3 = Subscription.fromURL("http://test3/");
462 subscription3.addFilter(filter1);
463 subscription3.addFilter(filter2);
464 subscription3.addFilter(filter3);
465
466 compareFilterSubscriptions("Initial filter1 subscriptions", filter1, []);
467 compareFilterSubscriptions("Initial filter2 subscriptions", filter2, []);
468 compareFilterSubscriptions("Initial filter3 subscriptions", filter3, []);
469
470 filterStorage.addSubscription(subscription1);
471
472 compareFilterSubscriptions("filter1 subscriptions after adding http://test1/ ", filter1, [subscription1]);
473 compareFilterSubscriptions("filter2 subscriptions after adding http://test1/ ", filter2, [subscription1]);
474 compareFilterSubscriptions("filter3 subscriptions after adding http://test1/ ", filter3, []);
475
476 filterStorage.addSubscription(subscription2);
477
478 compareFilterSubscriptions("filter1 subscriptions after adding http://test2/ ", filter1, [subscription1]);
479 compareFilterSubscriptions("filter2 subscriptions after adding http://test2/ ", filter2, [subscription1, subscription2]);
480 compareFilterSubscriptions("filter3 subscriptions after adding http://test2/ ", filter3, [subscription2]);
481
482 filterStorage.removeSubscription(subscription1);
483
484 compareFilterSubscriptions("filter1 subscriptions after removing http://test 1/", filter1, []);
485 compareFilterSubscriptions("filter2 subscriptions after removing http://test 1/", filter2, [subscription2]);
486 compareFilterSubscriptions("filter3 subscriptions after removing http://test 1/", filter3, [subscription2]);
487
488 filterStorage.updateSubscriptionFilters(subscription3, [filter3.text]);
489
490 compareFilterSubscriptions("filter1 subscriptions after updating http://test 3/ filters", filter1, []);
491 compareFilterSubscriptions("filter2 subscriptions after updating http://test 3/ filters", filter2, [subscription2]);
492 compareFilterSubscriptions("filter3 subscriptions after updating http://test 3/ filters", filter3, [subscription2]);
493
494 filterStorage.addSubscription(subscription3);
495
496 compareFilterSubscriptions("filter1 subscriptions after adding http://test3/ ", filter1, []);
497 compareFilterSubscriptions("filter2 subscriptions after adding http://test3/ ", filter2, [subscription2]);
498 compareFilterSubscriptions("filter3 subscriptions after adding http://test3/ ", filter3, [subscription2, subscription3]);
499
500 filterStorage.updateSubscriptionFilters(subscription3, [filter1.text, filter 2.text]);
501
502 compareFilterSubscriptions("filter1 subscriptions after updating http://test 3/ filters", filter1, [subscription3]);
503 compareFilterSubscriptions("filter2 subscriptions after updating http://test 3/ filters", filter2, [subscription2, subscription3]);
504 compareFilterSubscriptions("filter3 subscriptions after updating http://test 3/ filters", filter3, [subscription2]);
505
506 filterStorage.removeSubscription(subscription3);
507
508 compareFilterSubscriptions("filter1 subscriptions after removing http://test 3/", filter1, []);
509 compareFilterSubscriptions("filter2 subscriptions after removing http://test 3/", filter2, [subscription2]);
510 compareFilterSubscriptions("filter3 subscriptions after removing http://test 3/", filter3, [subscription2]);
511 });
512 });
OLDNEW
« no previous file with comments | « test/filterNotifier.js ('k') | test/filterStorage_readwrite.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld