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 Snippets = null; |
| 23 let Filter = null; |
| 24 |
| 25 exports.setUp = function(callback) |
| 26 { |
| 27 let sandboxedRequire = createSandbox(); |
| 28 ( |
| 29 {Filter} = sandboxedRequire("../lib/filterClasses"), |
| 30 {Snippets} = sandboxedRequire("../lib/snippets") |
| 31 ); |
| 32 |
| 33 callback(); |
| 34 }; |
| 35 |
| 36 exports.testDomainRestrictions = function(test) |
| 37 { |
| 38 function testScriptMatches(description, filters, domain, expectedMatches) |
| 39 { |
| 40 for (let filter of filters) |
| 41 Snippets.add(Filter.fromText(filter)); |
| 42 |
| 43 let matches = Snippets.getScriptsForDomain(domain); |
| 44 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
| 45 |
| 46 Snippets.clear(); |
| 47 } |
| 48 |
| 49 testScriptMatches( |
| 50 "Ignore filters that include parent domain but exclude subdomain", |
| 51 [ |
| 52 "~www.example.com,example.com#$#foo" |
| 53 ], |
| 54 "www.example.com", |
| 55 [] |
| 56 ); |
| 57 testScriptMatches( |
| 58 "Ignore filters for other subdomain", |
| 59 [ |
| 60 "www.example.com#$#foo", |
| 61 "other.example.com#$#foo" |
| 62 ], |
| 63 "other.example.com", |
| 64 ["foo"] |
| 65 ); |
| 66 |
| 67 test.done(); |
| 68 }; |
| 69 |
| 70 exports.testSnippetFiltersContainer = function(test) |
| 71 { |
| 72 function compareRules(description, domain, expectedMatches) |
| 73 { |
| 74 let result = Snippets.getScriptsForDomain(domain); |
| 75 expectedMatches = expectedMatches.map(filter => filter.script); |
| 76 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
| 77 } |
| 78 |
| 79 let domainFilter = Filter.fromText("example.com#$#filter1"); |
| 80 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); |
| 81 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); |
| 82 |
| 83 Snippets.add(domainFilter); |
| 84 Snippets.add(subdomainFilter); |
| 85 Snippets.add(otherDomainFilter); |
| 86 compareRules( |
| 87 "Return all matching filters", |
| 88 "www.example.com", |
| 89 [domainFilter, subdomainFilter] |
| 90 ); |
| 91 |
| 92 Snippets.remove(domainFilter); |
| 93 compareRules( |
| 94 "Return all matching filters after removing one", |
| 95 "www.example.com", |
| 96 [subdomainFilter] |
| 97 ); |
| 98 |
| 99 Snippets.clear(); |
| 100 compareRules( |
| 101 "Return no filters after clearing", |
| 102 "www.example.com", |
| 103 [] |
| 104 ); |
| 105 |
| 106 test.done(); |
| 107 }; |
OLD | NEW |