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

Side by Side Diff: test/elemHide.js

Issue 29882558: Issue 6955 - Avoid making copies of common selector list (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Keep track of exceptions per domain Created Sept. 17, 2018, 12:15 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 | « lib/elemHideExceptions.js ('k') | no next file » | 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
(...skipping 26 matching lines...) Expand all
37 {Filter} = sandboxedRequire("../lib/filterClasses") 37 {Filter} = sandboxedRequire("../lib/filterClasses")
38 ); 38 );
39 39
40 callback(); 40 callback();
41 }; 41 };
42 42
43 function normalizeSelectors(selectors) 43 function normalizeSelectors(selectors)
44 { 44 {
45 // getSelectorsForDomain is currently allowed to return duplicate selectors 45 // getSelectorsForDomain is currently allowed to return duplicate selectors
46 // for performance reasons, so we need to remove duplicates here. 46 // for performance reasons, so we need to remove duplicates here.
47 return selectors.sort().filter((selector, index, sortedSelectors) => 47 return selectors.slice().sort().filter((selector, index, sortedSelectors) =>
48 { 48 {
49 return index == 0 || selector != sortedSelectors[index - 1]; 49 return index == 0 || selector != sortedSelectors[index - 1];
50 }); 50 });
51 } 51 }
52 52
53 function testResult(test, domain, expectedSelectors, specificOnly) 53 function testResult(test, domain, expectedSelectors, specificOnly)
54 { 54 {
55 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); 55 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors);
56 56
57 test.deepEqual( 57 test.deepEqual(
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 test.equal(filtersByDomain.size, 2); 266 test.equal(filtersByDomain.size, 2);
267 267
268 ElemHide.remove(Filter.fromText("example.com##test")); 268 ElemHide.remove(Filter.fromText("example.com##test"));
269 test.equal(filtersByDomain.size, 2); 269 test.equal(filtersByDomain.size, 2);
270 270
271 ElemHide.remove(Filter.fromText("example.com,~www.example.com##test")); 271 ElemHide.remove(Filter.fromText("example.com,~www.example.com##test"));
272 test.equal(filtersByDomain.size, 0); 272 test.equal(filtersByDomain.size, 0);
273 273
274 test.done(); 274 test.done();
275 }; 275 };
276
277 exports.testCommonSelectorsDedup = function(test)
278 {
279 // Here we compare references to verify that the selector lookup function
280 // returns the same object for the common case.
281
282 ElemHide.add(Filter.fromText("##test1"));
283 ElemHide.add(Filter.fromText("##test2"));
284 ElemHide.add(Filter.fromText("##test3"));
285
286 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
287 ElemHide.getSelectorsForDomain("bar.example.com"));
288
289 // Domain-specific filter.
290 ElemHide.add(Filter.fromText("foo.example.com##test4"));
291
292 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
293 ElemHide.getSelectorsForDomain("bar.example.com"));
294
295 ElemHide.remove(Filter.fromText("foo.example.com##test4"));
296
297 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
298 ElemHide.getSelectorsForDomain("bar.example.com"));
299
300 // Generic filter with domain excluded.
301 ElemHide.add(Filter.fromText("~foo.example.com##test4"));
302
303 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
304 ElemHide.getSelectorsForDomain("bar.example.com"));
305
306 ElemHide.remove(Filter.fromText("~foo.example.com##test4"));
307
308 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
309 ElemHide.getSelectorsForDomain("bar.example.com"));
310
311 // Domain-specific exception.
312 ElemHideExceptions.add(Filter.fromText("foo.example.com#@#test4"));
313
314 // Even though the exception does not apply to any filters yet, the objects
315 // returned for foo.example.com and bar.example.com are not one and the same.
316 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
317 ElemHide.getSelectorsForDomain("bar.example.com"));
318
319 ElemHide.add(Filter.fromText("##test4"));
320
321 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
322 ElemHide.getSelectorsForDomain("bar.example.com"));
323
324 ElemHide.remove(Filter.fromText("##test4"));
325 ElemHideExceptions.remove(Filter.fromText("foo.example.com#@#test4"));
326
327 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
328 ElemHide.getSelectorsForDomain("bar.example.com"));
329
330 // Generic exception with domain excluded.
331 ElemHideExceptions.add(Filter.fromText("~foo.example.com#@#test4"));
332
333 // Even though the exception does not apply to any filters yet, the objects
334 // returned for foo.example.com and bar.example.com are not one and the same.
335 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
336 ElemHide.getSelectorsForDomain("bar.example.com"));
337
338 ElemHide.add(Filter.fromText("##test4"));
339
340 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
341 ElemHide.getSelectorsForDomain("bar.example.com"));
342
343 ElemHide.remove(Filter.fromText("##test4"));
344 ElemHideExceptions.remove(Filter.fromText("~foo.example.com#@#test4"));
345
346 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
347 ElemHide.getSelectorsForDomain("bar.example.com"));
348
349 // Generic exception.
350 ElemHide.add(Filter.fromText("##test4"));
351 ElemHideExceptions.add(Filter.fromText("#@#test4"));
352
353 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
354 ElemHide.getSelectorsForDomain("bar.example.com"));
355
356 ElemHideExceptions.add(Filter.fromText("example.com#@#test4"));
357
358 // In this case, even though the exception applies to both domains, we get
359 // different objects.
360 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
361 ElemHide.getSelectorsForDomain("bar.example.com"));
362 test.deepEqual(ElemHide.getSelectorsForDomain("foo.example.com"),
363 ElemHide.getSelectorsForDomain("bar.example.com"));
364
365 ElemHideExceptions.remove(Filter.fromText("example.com#@#test4"));
366
367 // Generic exception with unrelated domain excluded.
Manish Jethani 2018/09/17 12:21:15 This particular test would not pass with Patch Set
368 ElemHideExceptions.add(Filter.fromText("~x4aabd7c0ddcd.com#@#test4"));
369
370 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
371 ElemHide.getSelectorsForDomain("bar.example.com"));
372
373 ElemHideExceptions.remove(Filter.fromText("~x4aabd7c0ddcd.com#@#test4"));
374 ElemHideExceptions.remove(Filter.fromText("#@#test4"));
375 ElemHide.remove(Filter.fromText("##test4"));
376
377 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
378 ElemHide.getSelectorsForDomain("bar.example.com"));
379
380 ElemHide.remove(Filter.fromText("##test1"));
381 ElemHide.remove(Filter.fromText("##test2"));
382 ElemHide.remove(Filter.fromText("##test3"));
383
384 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"),
385 ElemHide.getSelectorsForDomain("bar.example.com"));
386
387 test.done();
388 };
OLDNEW
« no previous file with comments | « lib/elemHideExceptions.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld