Index: test/cssRules.js |
diff --git a/test/cssRules.js b/test/cssRules.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..bfe8aee94ec55721ea5918b9e4ef3229cf156418 |
--- /dev/null |
+++ b/test/cssRules.js |
@@ -0,0 +1,136 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+"use strict"; |
+ |
+let {createSandbox} = require("./common"); |
+ |
+let CSSPropertyFilter = null; |
+let CSSRules = null; |
+let ElemHide = null; |
+let Filter = null; |
+ |
+exports.setUp = function(callback) |
+{ |
+ let sandboxedRequire = createSandbox(); |
+ ( |
+ {Filter, CSSPropertyFilter} = sandboxedRequire("filterClasses"), |
+ {CSSRules} = sandboxedRequire("cssRules"), |
+ {ElemHide} = sandboxedRequire("elemHide") |
+ ); |
+ |
+ callback(); |
+}; |
+ |
+exports.testDomainRestrictions = function(test) |
+{ |
+ function testSelectorMatches(description, filters, domain, expectedMatches) |
+ { |
+ for (let filter of filters) |
+ { |
+ filter = Filter.fromText(filter); |
+ if (filter instanceof CSSPropertyFilter) |
+ CSSRules.add(filter); |
+ else |
+ ElemHide.add(filter); |
+ } |
+ |
+ let matches = CSSRules.getRulesForDomain(domain).map(filter => filter.text); |
+ test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
+ |
+ CSSRules.clear(); |
+ ElemHide.clear(); |
+ } |
+ |
+ testSelectorMatches( |
+ "Ignore generic filters", |
+ ["##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", |
+ "~example.com##[-abp-properties='foo']"], |
+ "example.com", |
+ ["example.com##[-abp-properties='foo']"] |
+ ); |
+ testSelectorMatches( |
+ "Ignore selectors with exceptions", |
+ ["example.com##[-abp-properties='foo']", |
+ "example.com##[-abp-properties='bar']", |
+ "example.com#@#[-abp-properties='foo']"], |
+ "example.com", |
+ ["example.com##[-abp-properties='bar']"] |
+ ); |
+ testSelectorMatches( |
+ "Ignore filters that include parent domain but exclude subdomain", |
+ ["~www.example.com,example.com##[-abp-properties='foo']"], |
+ "www.example.com", |
+ [] |
+ ); |
+ testSelectorMatches( |
+ "Ignore filters with parent domain if exception matches subdomain", |
+ ["www.example.com#@#[-abp-properties='foo']", |
+ "example.com##[-abp-properties='foo']"], |
+ "www.example.com", |
+ [] |
+ ); |
+ testSelectorMatches( |
+ "Ignore filters for other subdomain", |
+ ["www.example.com##[-abp-properties='foo']", |
+ "other.example.com##[-abp-properties='foo']"], |
+ "other.example.com", |
+ ["other.example.com##[-abp-properties='foo']"] |
+ ); |
+ |
+ test.done(); |
+}; |
+ |
+exports.testCSSPropertyFiltersContainer = function(test) |
+{ |
+ function compareRules(description, domain, expectedMatches) |
+ { |
+ let result = CSSRules.getRulesForDomain(domain) |
+ .map((filter) => filter.text); |
+ expectedMatches = expectedMatches.map(filter => filter.text); |
+ test.deepEqual(result.sort(), expectedMatches.sort(), description); |
+ } |
+ |
+ let domainFilter = Filter.fromText("example.com##filter1"); |
+ let subdomainFilter = Filter.fromText("www.example.com##filter2"); |
+ let otherDomainFilter = Filter.fromText("other.example.com##filter3"); |
+ |
+ CSSRules.add(domainFilter); |
+ CSSRules.add(subdomainFilter); |
+ CSSRules.add(otherDomainFilter); |
+ compareRules( |
+ "Return all matching filters", |
+ "www.example.com", |
+ [domainFilter, subdomainFilter] |
+ ); |
+ |
+ CSSRules.remove(domainFilter); |
+ compareRules( |
+ "Return all matching filters after removing one", |
+ "www.example.com", |
+ [subdomainFilter] |
+ ); |
+ |
+ CSSRules.clear(); |
+ compareRules( |
+ "Return no filters after clearing", |
+ "www.example.com", |
+ [] |
+ ); |
+ |
+ test.done(); |
+}; |