OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 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 testScriptMatches(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 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
| 54 |
| 55 Snippets.clear(); |
| 56 ElemHide.clear(); |
| 57 } |
| 58 |
| 59 testScriptMatches( |
| 60 "Ignore scripts with exceptions", |
| 61 [ |
| 62 "example.com#$#foo", |
| 63 "example.com#$#bar", |
| 64 "example.com#@#foo" |
| 65 ], |
| 66 "example.com", |
| 67 ["bar"] |
| 68 ); |
| 69 testScriptMatches( |
| 70 "Ignore filters that include parent domain but exclude subdomain", |
| 71 [ |
| 72 "~www.example.com,example.com#$#foo" |
| 73 ], |
| 74 "www.example.com", |
| 75 [] |
| 76 ); |
| 77 testScriptMatches( |
| 78 "Ignore filters with parent domain if exception matches subdomain", |
| 79 [ |
| 80 "www.example.com#@#foo", |
| 81 "example.com#$#foo" |
| 82 ], |
| 83 "www.example.com", |
| 84 [] |
| 85 ); |
| 86 testScriptMatches( |
| 87 "Ignore filters for other subdomain", |
| 88 [ |
| 89 "www.example.com#$#foo", |
| 90 "other.example.com#$#foo" |
| 91 ], |
| 92 "other.example.com", |
| 93 ["foo"] |
| 94 ); |
| 95 |
| 96 test.done(); |
| 97 }; |
| 98 |
| 99 exports.testSnippetFiltersContainer = function(test) |
| 100 { |
| 101 function compareRules(description, domain, expectedMatches) |
| 102 { |
| 103 let result = Snippets.getScriptsForDomain(domain); |
| 104 expectedMatches = expectedMatches.map(filter => filter.code); |
| 105 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
| 106 } |
| 107 |
| 108 let domainFilter = Filter.fromText("example.com#$#filter1"); |
| 109 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); |
| 110 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); |
| 111 |
| 112 Snippets.add(domainFilter); |
| 113 Snippets.add(subdomainFilter); |
| 114 Snippets.add(otherDomainFilter); |
| 115 compareRules( |
| 116 "Return all matching filters", |
| 117 "www.example.com", |
| 118 [domainFilter, subdomainFilter] |
| 119 ); |
| 120 |
| 121 Snippets.remove(domainFilter); |
| 122 compareRules( |
| 123 "Return all matching filters after removing one", |
| 124 "www.example.com", |
| 125 [subdomainFilter] |
| 126 ); |
| 127 |
| 128 Snippets.clear(); |
| 129 compareRules( |
| 130 "Return no filters after clearing", |
| 131 "www.example.com", |
| 132 [] |
| 133 ); |
| 134 |
| 135 test.done(); |
| 136 }; |
OLD | NEW |