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

Side by Side Diff: test/snippets.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Make snippet filters similar to element hiding filters Created April 25, 2018, 5: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
« lib/snippets.js ('K') | « test/filterClasses.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
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
Manish Jethani 2018/04/26 13:19:17 This is also almost exactly like test/elemHideEmul
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 "use strict";
19
20 const {createSandbox} = require("./_common");
21
22 let SnippetFilter = null;
23 let Snippets = null;
24 let ElemHide = null;
25 let Filter = null;
26
27 exports.setUp = function(callback)
28 {
29 let sandboxedRequire = createSandbox();
30 (
31 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"),
32 {ElemHide} = sandboxedRequire("../lib/elemHide"),
33 {Snippets} = sandboxedRequire("../lib/snippets")
34 );
35
36 callback();
37 };
38
39 exports.testDomainRestrictions = function(test)
40 {
41 function testSelectorMatches(description, filters, domain, expectedMatches)
42 {
43 for (let filter of filters)
44 {
45 filter = Filter.fromText(filter);
46 if (filter instanceof SnippetFilter)
47 Snippets.add(filter);
48 else
49 ElemHide.add(filter);
50 }
51
52 let matches = Snippets.getScriptsForDomain(domain)
53 .map(filter => filter.text);
54 test.deepEqual(matches.sort(), expectedMatches.sort(), description);
55
56 Snippets.clear();
57 ElemHide.clear();
58 }
59
60 testSelectorMatches(
61 "Ignore selectors with exceptions",
62 [
63 "example.com#$#foo",
64 "example.com#$#bar",
65 "example.com#@#foo"
66 ],
67 "example.com",
68 ["example.com#$#bar"]
69 );
70 testSelectorMatches(
71 "Ignore filters that include parent domain but exclude subdomain",
72 [
73 "~www.example.com,example.com#$#foo"
74 ],
75 "www.example.com",
76 []
77 );
78 testSelectorMatches(
79 "Ignore filters with parent domain if exception matches subdomain",
80 [
81 "www.example.com#@#foo",
82 "example.com#$#foo"
83 ],
84 "www.example.com",
85 []
86 );
87 testSelectorMatches(
88 "Ignore filters for other subdomain",
89 [
90 "www.example.com#$#foo",
91 "other.example.com#$#foo"
92 ],
93 "other.example.com",
94 ["other.example.com#$#foo"]
95 );
96
97 test.done();
98 };
99
100 exports.testSnippetFiltersContainer = function(test)
101 {
102 function compareRules(description, domain, expectedMatches)
103 {
104 let result = Snippets.getScriptsForDomain(domain)
105 .map(filter => filter.text);
106 expectedMatches = expectedMatches.map(filter => filter.text);
107 test.deepEqual(result.sort(), expectedMatches.sort(), description);
108 }
109
110 let domainFilter = Filter.fromText("example.com#$#filter1");
111 let subdomainFilter = Filter.fromText("www.example.com#$#filter2");
112 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3");
113
114 Snippets.add(domainFilter);
115 Snippets.add(subdomainFilter);
116 Snippets.add(otherDomainFilter);
117 compareRules(
118 "Return all matching filters",
119 "www.example.com",
120 [domainFilter, subdomainFilter]
121 );
122
123 Snippets.remove(domainFilter);
124 compareRules(
125 "Return all matching filters after removing one",
126 "www.example.com",
127 [subdomainFilter]
128 );
129
130 Snippets.clear();
131 compareRules(
132 "Return no filters after clearing",
133 "www.example.com",
134 []
135 );
136
137 test.done();
138 };
OLDNEW
« lib/snippets.js ('K') | « test/filterClasses.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld