Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 let common = require("common"); | 20 let {createSandbox} = require("./_common"); |
21 | 21 let sandboxedRequire = null; |
22 let {CSSRules} = require("cssRules"); | 22 |
23 let {ElemHide} = require("elemHide"); | 23 let FilterStorage = null; |
24 let {FilterListener} = require("filterListener"); | 24 let Subscription = null; |
25 let {FilterStorage} = require("filterStorage"); | 25 let Filter = null; |
26 let {Subscription} = require("subscriptionClasses"); | 26 let defaultMatcher = null; |
27 let {Filter} = require("filterClasses"); | 27 let SpecialSubscription = null; |
28 let {defaultMatcher} = require("matcher"); | |
29 let {SpecialSubscription} = require("subscriptionClasses"); | |
30 | |
31 | 28 |
32 exports.setUp = function(callback) | 29 exports.setUp = function(callback) |
33 { | 30 { |
34 common.prepareFilterComponents.call(this, true); | 31 sandboxedRequire = createSandbox({ |
35 common.preparePrefs.call(this); | 32 elemHide: ["filterByKey", "exceptions"], |
33 cssRules: ["filters"] | |
34 }); | |
35 | |
36 // We need to require the filterListener module so that filter changes will be | |
37 // noticed, even though we don't directly use the module here. | |
38 sandboxedRequire("../lib/filterListener"); | |
39 | |
40 ( | |
41 {FilterStorage} = sandboxedRequire("../lib/filterStorage"), | |
42 {Subscription, SpecialSubscription} = sandboxedRequire("../lib/subscriptionC lasses"), | |
43 {Filter} = sandboxedRequire("../lib/filterClasses"), | |
44 {defaultMatcher} = sandboxedRequire("../lib/matcher") | |
45 ); | |
36 | 46 |
37 FilterStorage.addSubscription(Subscription.fromURL("~fl~")); | 47 FilterStorage.addSubscription(Subscription.fromURL("~fl~")); |
38 FilterStorage.addSubscription(Subscription.fromURL("~wl~")); | 48 FilterStorage.addSubscription(Subscription.fromURL("~wl~")); |
39 FilterStorage.addSubscription(Subscription.fromURL("~eh~")); | 49 FilterStorage.addSubscription(Subscription.fromURL("~eh~")); |
40 | 50 |
41 Subscription.fromURL("~fl~").defaults = ["blocking"]; | 51 Subscription.fromURL("~fl~").defaults = ["blocking"]; |
42 Subscription.fromURL("~wl~").defaults = ["whitelist"]; | 52 Subscription.fromURL("~wl~").defaults = ["whitelist"]; |
43 Subscription.fromURL("~eh~").defaults = ["elemhide"]; | 53 Subscription.fromURL("~eh~").defaults = ["elemhide"]; |
44 | |
45 callback(); | |
46 }; | |
47 | |
48 exports.tearDown = function(callback) | |
49 { | |
50 common.restoreFilterComponents.call(this); | |
51 common.restorePrefs.call(this); | |
52 | 54 |
53 callback(); | 55 callback(); |
54 }; | 56 }; |
55 | 57 |
56 function checkKnownFilters(test, text, expected) | 58 function checkKnownFilters(test, text, expected) |
57 { | 59 { |
58 let result = {}; | 60 let result = {}; |
59 for (let type of ["blacklist", "whitelist"]) | 61 for (let type of ["blacklist", "whitelist"]) |
60 { | 62 { |
61 let matcher = defaultMatcher[type]; | 63 let matcher = defaultMatcher[type]; |
62 let filters = []; | 64 let filters = []; |
63 for (let keyword in matcher.filterByKeyword) | 65 for (let keyword in matcher.filterByKeyword) |
64 { | 66 { |
65 let list = matcher.filterByKeyword[keyword]; | 67 let list = matcher.filterByKeyword[keyword]; |
66 for (let i = 0; i < list.length; i++) | 68 for (let i = 0; i < list.length; i++) |
67 { | 69 { |
68 let filter = list[i]; | 70 let filter = list[i]; |
69 test.equal(matcher.getKeywordForFilter(filter), keyword, | 71 test.equal(matcher.getKeywordForFilter(filter), keyword, |
70 "Keyword of filter " + filter.text); | 72 "Keyword of filter " + filter.text); |
71 filters.push(filter.text); | 73 filters.push(filter.text); |
72 } | 74 } |
73 } | 75 } |
74 result[type] = filters; | 76 result[type] = filters; |
75 } | 77 } |
76 | 78 |
77 let ElemHideFilterByKey = ElemHide._getFilterByKey(); | 79 let elemHide = sandboxedRequire("../lib/elemHide"); |
78 let ElemHideExceptions = ElemHide._getExceptions(); | |
79 result.elemhide = []; | 80 result.elemhide = []; |
80 for (let key in ElemHideFilterByKey) | 81 for (let key in elemHide.filterByKey) |
81 result.elemhide.push(ElemHideFilterByKey[key].text); | 82 result.elemhide.push(elemHide.filterByKey[key].text); |
82 | 83 |
83 result.elemhideexception = []; | 84 result.elemhideexception = []; |
84 for (let selector in ElemHideExceptions) | 85 for (let selector in elemHide.exceptions) |
85 { | 86 { |
86 let list = ElemHideExceptions[selector]; | 87 let list = elemHide.exceptions[selector]; |
87 for (let exception of list) | 88 for (let exception of list) |
88 result.elemhideexception.push(exception.text); | 89 result.elemhideexception.push(exception.text); |
89 } | 90 } |
90 | 91 |
91 let allCSSRules = CSSRules._getAllRules(); | 92 let cssRules = sandboxedRequire("../lib/cssRules"); |
92 result.cssrule = []; | 93 result.cssrule = []; |
93 for (let filterText in allCSSRules) | 94 for (let filterText in cssRules.filters) |
94 result.cssrule.push(filterText); | 95 result.cssrule.push(filterText); |
95 | 96 |
96 let types = ["blacklist", "whitelist", "elemhide", "elemhideexception", | 97 let types = ["blacklist", "whitelist", "elemhide", "elemhideexception", |
97 "cssrule"]; | 98 "cssrule"]; |
98 for (let type of types) | 99 for (let type of types) |
100 { | |
99 if (!(type in expected)) | 101 if (!(type in expected)) |
100 expected[type] = []; | 102 expected[type] = []; |
103 else | |
104 expected[type].sort(); | |
105 result[type].sort(); | |
106 } | |
101 | 107 |
102 test.deepEqual(result, expected, text); | 108 test.deepEqual(result, expected, text); |
103 } | 109 } |
104 | 110 |
105 exports.testAddingAndRemovingFilters = function(test) | 111 exports.testAddingAndRemovingFilters = function(test) |
106 { | 112 { |
107 let filter1 = Filter.fromText("filter1"); | 113 let filter1 = Filter.fromText("filter1"); |
108 let filter2 = Filter.fromText("@@filter2"); | 114 let filter2 = Filter.fromText("@@filter2"); |
109 let filter3 = Filter.fromText("#filter3"); | 115 let filter3 = Filter.fromText("#filter3"); |
110 let filter4 = Filter.fromText("!filter4"); | 116 let filter4 = Filter.fromText("!filter4"); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 let filter3 = Filter.fromText("#filter3"); | 195 let filter3 = Filter.fromText("#filter3"); |
190 let filter4 = Filter.fromText("!filter4"); | 196 let filter4 = Filter.fromText("!filter4"); |
191 let filter5 = Filter.fromText("#@#filter5"); | 197 let filter5 = Filter.fromText("#@#filter5"); |
192 let filter6 = Filter.fromText("example.com##[-abp-properties='filter6']"); | 198 let filter6 = Filter.fromText("example.com##[-abp-properties='filter6']"); |
193 let filter7 = Filter.fromText("example.com#@#[-abp-properties='filter7']"); | 199 let filter7 = Filter.fromText("example.com#@#[-abp-properties='filter7']"); |
194 | 200 |
195 let subscription = Subscription.fromURL("http://test1/"); | 201 let subscription = Subscription.fromURL("http://test1/"); |
196 subscription.filters = [filter1, filter2, filter3, filter4, filter5, filter6, filter7]; | 202 subscription.filters = [filter1, filter2, filter3, filter4, filter5, filter6, filter7]; |
197 | 203 |
198 FilterStorage.addSubscription(subscription); | 204 FilterStorage.addSubscription(subscription); |
199 checkKnownFilters(test, "add subscription with filter1, @@filter2, #filter3, ! filter4, #@#filter5, example.com##[-abp-properties='filter6'], example.com#@#[-a bp-properties='filter7']", {blacklist: [filter1.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filter7.text], cssrule: [filter6.text]}); | 205 checkKnownFilters(test, "add subscription with filter1, @@filter2, #filter3, ! filter4, #@#filter5, example.com##[-abp-properties='filter6'], example.com#@#[-a bp-properties='filter7']", {blacklist: [filter1.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filter7.text], cssrule: [filter6.text]}); |
kzar
2016/09/25 13:49:39
This test and the two below it fail occasionally.
kzar
2016/09/29 15:53:09
Still not got to the bottom of this.
kzar
2016/09/29 15:58:56
Ah, was simply that the order varied! Fixed.
| |
200 | 206 |
201 filter2.disabled = false; | 207 filter2.disabled = false; |
202 checkKnownFilters(test, "enable @@filter2", {blacklist: [filter1.text], whitel ist: [filter2.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filter7.text], cssrule: [filter6.text]}); | 208 checkKnownFilters(test, "enable @@filter2", {blacklist: [filter1.text], whitel ist: [filter2.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filter7.text], cssrule: [filter6.text]}); |
203 | 209 |
204 FilterStorage.addFilter(filter1); | 210 FilterStorage.addFilter(filter1); |
205 checkKnownFilters(test, "add filter1", {blacklist: [filter1.text], whitelist: [filter2.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filt er7.text], cssrule: [filter6.text]}); | 211 checkKnownFilters(test, "add filter1", {blacklist: [filter1.text], whitelist: [filter2.text], elemhide: [filter3.text], elemhideexception: [filter5.text, filt er7.text], cssrule: [filter6.text]}); |
206 | 212 |
207 FilterStorage.updateSubscriptionFilters(subscription, [filter4]); | 213 FilterStorage.updateSubscriptionFilters(subscription, [filter4]); |
208 checkKnownFilters(test, "change subscription filters to filter4", {blacklist: [filter1.text]}); | 214 checkKnownFilters(test, "change subscription filters to filter4", {blacklist: [filter1.text]}); |
209 | 215 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
312 checkKnownFilters(test, "disable exception rules", {blacklist: [filter1.text, filter3.text], whitelist: [filter2.text]}); | 318 checkKnownFilters(test, "disable exception rules", {blacklist: [filter1.text, filter3.text], whitelist: [filter2.text]}); |
313 | 319 |
314 FilterStorage.removeSubscription(subscription4); | 320 FilterStorage.removeSubscription(subscription4); |
315 checkKnownFilters(test, "remove subscription from the list", {blacklist: [filt er1.text]}); | 321 checkKnownFilters(test, "remove subscription from the list", {blacklist: [filt er1.text]}); |
316 | 322 |
317 subscription3.disabled = false; | 323 subscription3.disabled = false; |
318 checkKnownFilters(test, "enable exception rules", {blacklist: [filter1.text], whitelist: [filter2.text]}); | 324 checkKnownFilters(test, "enable exception rules", {blacklist: [filter1.text], whitelist: [filter2.text]}); |
319 | 325 |
320 test.done(); | 326 test.done(); |
321 }; | 327 }; |
LEFT | RIGHT |