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

Unified Diff: test/cssRules.js

Issue 29354864: Issue 4223 - Migrate some more of adblockplustests (Closed)
Patch Set: Migrate filterStorage tests Created Sept. 29, 2016, 7:24 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/cssRules.js
diff --git a/test/cssRules.js b/test/cssRules.js
new file mode 100644
index 0000000000000000000000000000000000000000..46f8221711adc60c2f3d38a8f491c21e88ac3eeb
--- /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 sandboxedRequire = null;
Wladimir Palant 2016/09/30 09:37:53 This doesn't need to be a global variable.
kzar 2016/10/03 13:46:14 Done.
+
+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").CSSRules;
+ ElemHide = sandboxedRequire("elemHide").ElemHide;
Wladimir Palant 2016/09/30 09:37:53 How about: ( {Filter, CSSPropertyFilter} =
kzar 2016/10/03 13:46:14 Done.
+
+ 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();
+};

Powered by Google App Engine
This is Rietveld