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

Unified Diff: test/snippets.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Add filter to known filters Created May 7, 2018, 7:02 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
« no previous file with comments | « test/filterClasses.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/snippets.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/test/snippets.js
@@ -0,0 +1,136 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-present 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";
+
+const {createSandbox} = require("./_common");
+
+let SnippetFilter = null;
+let Snippets = null;
+let ElemHide = null;
+let Filter = null;
+
+exports.setUp = function(callback)
+{
+ let sandboxedRequire = createSandbox();
+ (
+ {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"),
+ {ElemHide} = sandboxedRequire("../lib/elemHide"),
+ {Snippets} = sandboxedRequire("../lib/snippets")
+ );
+
+ callback();
+};
+
+exports.testDomainRestrictions = function(test)
+{
+ function testScriptMatches(description, filters, domain, expectedMatches)
+ {
+ for (let filter of filters)
+ {
+ filter = Filter.fromText(filter);
+ if (filter instanceof SnippetFilter)
+ Snippets.add(filter);
+ else
+ ElemHide.add(filter);
+ }
+
+ let matches = Snippets.getScriptsForDomain(domain);
+ test.deepEqual(matches.sort(), expectedMatches.sort(), description);
+
+ Snippets.clear();
+ ElemHide.clear();
+ }
+
+ testScriptMatches(
+ "Ignore scripts with exceptions",
+ [
+ "example.com#$#foo",
+ "example.com#$#bar",
+ "example.com#@#foo"
+ ],
+ "example.com",
+ ["bar"]
+ );
+ testScriptMatches(
+ "Ignore filters that include parent domain but exclude subdomain",
+ [
+ "~www.example.com,example.com#$#foo"
+ ],
+ "www.example.com",
+ []
+ );
+ testScriptMatches(
+ "Ignore filters with parent domain if exception matches subdomain",
+ [
+ "www.example.com#@#foo",
+ "example.com#$#foo"
+ ],
+ "www.example.com",
+ []
+ );
+ testScriptMatches(
+ "Ignore filters for other subdomain",
+ [
+ "www.example.com#$#foo",
+ "other.example.com#$#foo"
+ ],
+ "other.example.com",
+ ["foo"]
+ );
+
+ test.done();
+};
+
+exports.testSnippetFiltersContainer = function(test)
+{
+ function compareRules(description, domain, expectedMatches)
+ {
+ let result = Snippets.getScriptsForDomain(domain);
+ expectedMatches = expectedMatches.map(filter => filter.code);
+ 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");
+
+ Snippets.add(domainFilter);
+ Snippets.add(subdomainFilter);
+ Snippets.add(otherDomainFilter);
+ compareRules(
+ "Return all matching filters",
+ "www.example.com",
+ [domainFilter, subdomainFilter]
+ );
+
+ Snippets.remove(domainFilter);
+ compareRules(
+ "Return all matching filters after removing one",
+ "www.example.com",
+ [subdomainFilter]
+ );
+
+ Snippets.clear();
+ compareRules(
+ "Return no filters after clearing",
+ "www.example.com",
+ []
+ );
+
+ test.done();
+};
« no previous file with comments | « test/filterClasses.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld