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

Side by Side Diff: test/filterStorage.js

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

Powered by Google App Engine
This is Rietveld