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

Delta Between Two Patch Sets: chrome/content/tests/cssRules.js

Issue 29324604: Issue 2394 - Added unit tests for CSS property filters (Closed)
Left Patch Set: Created Aug. 25, 2015, 3:51 p.m.
Right Patch Set: Created Dec. 7, 2015, 5:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/common.js ('k') | chrome/content/tests/filterClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 (function() 1 (function()
2 { 2 {
3 module("CSS property filter", { 3 module("CSS property filter", {
4 setup: function() 4 setup: function()
5 { 5 {
6 prepareFilterComponents.call(this); 6 prepareFilterComponents.call(this);
7 preparePrefs.call(this); 7 preparePrefs.call(this);
8 }, 8 },
9 teardown: function() 9 teardown: function()
10 { 10 {
11 restoreFilterComponents.call(this); 11 restoreFilterComponents.call(this);
12 restorePrefs.call(this); 12 restorePrefs.call(this);
13 } 13 }
14 }); 14 });
15 15
16 function runSelectorTest([text, domain, filters, expected]) 16 function runSelectorTest([text, domain, filters, expected])
17 { 17 {
18 for (let filter of filters) 18 for (let filter of filters)
19 { 19 {
20 filter = Filter.fromText(filter); 20 filter = Filter.fromText(filter);
21 if (filter instanceof CSSPropertyFilter) 21 if (filter instanceof CSSPropertyFilter)
22 CSSRules.add(filter); 22 CSSRules.add(filter);
23 else 23 else
24 ElemHide.add(filter); 24 ElemHide.add(filter);
Wladimir Palant 2015/11/10 10:49:02 Why add to ElemHide here if you are only testing C
Thomas Greiner 2015/12/03 12:55:19 Because exception rules are still handled by `Elem
25 } 25 }
26 26
27 let selectors = CSSRules.getRulesForDomain(domain); 27 let result = CSSRules.getRulesForDomain(domain)
Wladimir Palant 2015/11/10 10:49:02 Note that getRulesForDomain() should return filter
Thomas Greiner 2015/12/03 12:55:19 Done.
28 equal(selectors.sort().join("\n"), expected.sort().join("\n"), text); 28 .map((filter) => filter.text);
29 deepEqual(result.sort(), expected.sort(), text);
29 30
30 CSSRules.clear(); 31 CSSRules.clear();
31 ElemHide.clear(); 32 ElemHide.clear();
Wladimir Palant 2015/11/10 10:49:02 You should add a CSSRules.clear() call to prepareF
Thomas Greiner 2015/12/03 12:55:19 I already did.
32 } 33 }
33 34
34 let selectorTests = [ 35 let selectorTests = [
35 ["Return unique selectors from single filter", "www.example.com", ["www.exam ple.com,example.com##[-abp-properties='foo']"], ["[-abp-properties='foo']"]], 36 ["Ignore generic filters", "example.com", ["##[-abp-properties='foo']", "exa mple.com##[-abp-properties='foo']", "~example.com##[-abp-properties='foo']"], [" example.com##[-abp-properties='foo']"]],
36 // ["Return unique selectors from multiple filters", "www.example.com", ["ww w.example.com##[-abp-properties='foo']", "other.example.org,www.example.com##[-a bp-properties='foo']"], ["[-abp-properties='foo']"]], 37 ["Ignore selectors with exceptions", "example.com", ["example.com##[-abp-pro perties='foo']", "example.com##[-abp-properties='bar']", "example.com#@#[-abp-pr operties='foo']"], ["example.com##[-abp-properties='bar']"]],
Thomas Greiner 2015/08/25 16:11:47 I commented this one out since our current impleme
37 ["Ignore selectors with exceptions", "example.com", ["example.com##[-abp-pro perties='foo']", "example.com##[-abp-properties='bar']", "example.com#@#[-abp-pr operties='foo']"], ["[-abp-properties='bar']"]],
38 ["Ignore filters that include parent domain but exclude subdomain", "www.exa mple.com", ["~www.example.com,example.com##[-abp-properties='foo']"], []], 38 ["Ignore filters that include parent domain but exclude subdomain", "www.exa mple.com", ["~www.example.com,example.com##[-abp-properties='foo']"], []],
39 ["Ignore filters with parent domain if exception matches subdomain", "www.ex ample.com", ["www.example.com#@#[-abp-properties='foo']", "example.com##[-abp-pr operties='foo']"], []] 39 ["Ignore filters with parent domain if exception matches subdomain", "www.ex ample.com", ["www.example.com#@#[-abp-properties='foo']", "example.com##[-abp-pr operties='foo']"], []],
Wladimir Palant 2015/11/10 10:49:02 I'd expect a test here verifying that generic rule
Thomas Greiner 2015/12/03 12:55:20 Done. Added a test for ignoring generic filters.
40 ["Ignore filters for other subdomain", "other.example.com", ["www.example.co m##[-abp-properties='foo']", "other.example.com##[-abp-properties='foo']"], ["ot her.example.com##[-abp-properties='foo']"]]
40 ]; 41 ];
41 42
42 test("Domain restrictions", function() 43 test("Domain restrictions", function()
43 { 44 {
44 for (let selectorTest of selectorTests) 45 selectorTests.forEach(runSelectorTest);
45 runSelectorTest(selectorTest);
Wladimir Palant 2015/11/10 10:49:02 selectorTests.forEach(runSelectorTest)?
Thomas Greiner 2015/12/03 12:55:20 Done.
46 }); 46 });
47
48 let testObjectCount = 0;
49 function constructTestObject(domain)
50 {
51 let selector = "filter_" + (++testObjectCount);
52 return {
53 filter: Filter.fromText(domain + "##" + selector),
54 selector: selector
55 };
56 }
57 47
58 function compareRules(text, domain, expected) 48 function compareRules(text, domain, expected)
59 { 49 {
60 let result = CSSRules.getRulesForDomain(domain); 50 let result = CSSRules.getRulesForDomain(domain)
51 .map((filter) => filter.text);
52 expected = expected.map((filter) => filter.text);
61 deepEqual(result.sort(), expected.sort(), text); 53 deepEqual(result.sort(), expected.sort(), text);
62 } 54 }
63 55
64 test("CSS property filters container", function() 56 test("CSS property filters container", function()
65 { 57 {
66 let generic = constructTestObject(""); 58 let domainFilter = Filter.fromText("example.com##filter1");
67 let domain = constructTestObject("example.com"); 59 let subdomainFilter = Filter.fromText("www.example.com##filter2");
68 let subdomain = constructTestObject("www.example.com"); 60 let otherDomainFilter = Filter.fromText("other.example.com##filter3");
69 let otherDomain = constructTestObject("other.example.com");
70 61
71 CSSRules.add(generic.filter); 62 CSSRules.add(domainFilter);
72 CSSRules.add(domain.filter); 63 CSSRules.add(subdomainFilter);
73 CSSRules.add(subdomain.filter); 64 CSSRules.add(otherDomainFilter);
74 CSSRules.add(otherDomain.filter);
75 compareRules("Return all matching filters", "www.example.com", 65 compareRules("Return all matching filters", "www.example.com",
76 [generic.selector, domain.selector, subdomain.selector]); 66 [domainFilter, subdomainFilter]);
67
68 CSSRules.remove(domainFilter);
69 compareRules("Return all matching filters after removing one",
70 "www.example.com", [subdomainFilter]);
77 71
78 CSSRules.clear(); 72 CSSRules.clear();
79 CSSRules.add(subdomain.filter); 73 compareRules("Return no filters after clearing", "www.example.com", []);
80 CSSRules.add(otherDomain.filter);
81 compareRules("Don't match other subdomain", "www.example.com",
82 [subdomain.selector]);
Wladimir Palant 2015/11/10 10:49:02 This check makes little sense IMHO, it's already c
Thomas Greiner 2015/12/03 12:55:20 Done. Moved this to `selectorTests`. Generally, t
83 }); 74 });
84 })(); 75 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld