OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 let { |
| 4 Filter, InvalidFilter, CommentFilter, RegExpFilter, WhiteListFilter, |
| 5 ElemHideFilter, ElemHideException |
| 6 } = require("../lib/filterClassesNew"); |
| 7 |
| 8 exports.testFromText = function(test) |
| 9 { |
| 10 let tests = [ |
| 11 ["!asdf", CommentFilter, "comment"], |
| 12 ["asdf", RegExpFilter, "blocking"], |
| 13 ["asdf$image,~collapse", RegExpFilter, "blocking"], |
| 14 ["/asdf/", RegExpFilter, "blocking"], |
| 15 ["/asdf??+/", InvalidFilter, "invalid"], |
| 16 ["@@asdf", WhiteListFilter, "whitelist"], |
| 17 ["@@asdf$image,~collapse", WhiteListFilter, "whitelist"], |
| 18 ["@@/asdf/", WhiteListFilter, "whitelist"], |
| 19 ["@@/asdf??+/", InvalidFilter, "invalid"], |
| 20 ["##asdf", ElemHideFilter, "elemhide"], |
| 21 ["#@#asdf", ElemHideException, "elemhideexception"], |
| 22 ["foobar##asdf", ElemHideFilter, "elemhide"], |
| 23 ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
| 24 ["foobar##a", ElemHideFilter, "elemhide"], |
| 25 ["foobar#@#a", ElemHideException, "elemhideexception"], |
| 26 |
| 27 ["foobar#asdf", RegExpFilter, "blocking"], |
| 28 ["foobar|foobas##asdf", RegExpFilter, "blocking"], |
| 29 ["foobar##asdf{asdf}", RegExpFilter, "blocking"], |
| 30 ["foobar##", RegExpFilter, "blocking"], |
| 31 ["foobar#@#", RegExpFilter, "blocking"], |
| 32 ["asdf$foobar", InvalidFilter, "invalid"], |
| 33 ["asdf$image,foobar", InvalidFilter, "invalid"], |
| 34 ["asdf$image=foobar", RegExpFilter, "blocking"], |
| 35 ["asdf$image=foobar=xyz,~coLLapse", RegExpFilter, "blocking"], |
| 36 ]; |
| 37 for (let [text, type, typeName, location] of tests) |
| 38 { |
| 39 let filter = Filter.fromText(text); |
| 40 try |
| 41 { |
| 42 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 43 test.equal(filter.text, text, "Correct filter text for " + text); |
| 44 test.ok(filter instanceof type, "Correct filter type for " + text); |
| 45 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeN
ame); |
| 46 if (type == InvalidFilter) |
| 47 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 48 } |
| 49 finally |
| 50 { |
| 51 filter.delete(); |
| 52 } |
| 53 } |
| 54 test.done(); |
| 55 }; |
| 56 |
| 57 exports.testNormalize = function(test) |
| 58 { |
| 59 let tests = [ |
| 60 [" foo bar ", "foobar"], |
| 61 ["foobar", "foobar"], |
| 62 [" ! comment something ", "! comment something"], |
| 63 [" ! \n comment something ", "! comment something"], |
| 64 [" foo , bar ## foo > bar ", "foo,bar##foo > bar"], |
| 65 [" foo , bar #@# foo > bar ", "foo,bar#@#foo > bar"], |
| 66 ]; |
| 67 for (let [text, expected] of tests) |
| 68 test.equal(Filter.normalize(text), expected); |
| 69 test.done(); |
| 70 }; |
| 71 |
| 72 exports.testSerialize = function(test) |
| 73 { |
| 74 // Comment |
| 75 let filter = Filter.fromText("! serialize"); |
| 76 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 77 filter.delete(); |
| 78 |
| 79 // Blocking filter |
| 80 filter = Filter.fromText("serialize"); |
| 81 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 82 filter.disabled = true; |
| 83 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 84 filter.disabled = false; |
| 85 filter.hitCount = 10; |
| 86 filter.lastHit = 12; |
| 87 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 88 filter.delete(); |
| 89 |
| 90 // Invalid filter |
| 91 filter = Filter.fromText("serialize$foobar"); |
| 92 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 93 filter.delete(); |
| 94 |
| 95 // Element hiding filter |
| 96 filter = Filter.fromText("example.com##serialize"); |
| 97 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| 98 filter.disabled = true; |
| 99 filter.lastHit = 5; |
| 100 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
| 101 filter.delete(); |
| 102 |
| 103 test.done(); |
| 104 } |
| 105 |
| 106 exports.testActiveFilter = function(test) |
| 107 { |
| 108 let filter1 = Filter.fromText("asdf"); |
| 109 let filter1copy = Filter.fromText("asdf"); |
| 110 let filter2 = Filter.fromText("##foobar"); |
| 111 |
| 112 try |
| 113 { |
| 114 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Fi
lters are initially enabled"); |
| 115 filter1.disabled = true; |
| 116 test.ok(filter1.disabled, "Disabling filter works"); |
| 117 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 118 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 119 |
| 120 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitC
ount === 0, "Filters have no hit initially"); |
| 121 filter1.hitCount = 5; |
| 122 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 123 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also chan
ged"); |
| 124 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected")
; |
| 125 |
| 126 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHi
t === 0, "Filters have no last hit time initially"); |
| 127 filter1.lastHit = 10; |
| 128 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 129 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also
changed"); |
| 130 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affecte
d"); |
| 131 } |
| 132 finally |
| 133 { |
| 134 filter1.delete(); |
| 135 filter1copy.delete(); |
| 136 filter2.delete(); |
| 137 } |
| 138 |
| 139 test.done(); |
| 140 }; |
| 141 |
| 142 exports.testIsGeneric = function(test) |
| 143 { |
| 144 let tests = [ |
| 145 ["asfd", true], |
| 146 ["|http://example.com/asdf", true], |
| 147 ["||example.com/asdf", true], |
| 148 ["asfd$third-party", true], |
| 149 ["asdf$domain=com", false], |
| 150 ["asdf$domain=example.com", false], |
| 151 ["asdf$image,domain=example.com", false], |
| 152 ["asdf$~image,domain=example.com", false], |
| 153 ["asdf$third-party,domain=example.com", false], |
| 154 ["||example.com/asdf$~collapse,domain=example.com", false], |
| 155 ["||example.com/asdf$domain=~example.com", true], |
| 156 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 157 ["asdf$domain=foo.example.com|~example.com", false], |
| 158 ["asdf$domain=foo.com|~example.com", false], |
| 159 ["asdf$domain=~foo.com|~example.com", true], |
| 160 ]; |
| 161 |
| 162 for (let [text, generic] of tests) |
| 163 { |
| 164 let filter = Filter.fromText(text); |
| 165 try |
| 166 { |
| 167 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 168 } |
| 169 finally |
| 170 { |
| 171 filter.delete(); |
| 172 } |
| 173 } |
| 174 |
| 175 test.done(); |
| 176 } |
| 177 |
| 178 exports.testElemHideSelector = function(test) |
| 179 { |
| 180 function doTest(text, selector, selectorDomain) |
| 181 { |
| 182 let filter = Filter.fromText(text); |
| 183 try |
| 184 { |
| 185 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 186 |
| 187 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 188 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 189 test.equal(actualDomains, expectedDomains, "Correct domains list for " + t
ext); |
| 190 } |
| 191 finally |
| 192 { |
| 193 filter.delete(); |
| 194 } |
| 195 } |
| 196 |
| 197 let tests = [ |
| 198 ["##foobar", "foobar", ""], |
| 199 ["~example.com##foobar", "foobar", ""], |
| 200 ["example.com##body > div:first-child", "body > div:first-child", "EXAMPLE.C
OM"], |
| 201 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","XYZ"], |
| 202 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "COM,EXAMPLE.INFO"], |
| 203 ["foo,bar,bas,bam##foobar", "foobar", "FOO,BAR,BAS,BAM"], |
| 204 |
| 205 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 206 ["foo,bar,bas,~bar##foobar", "foobar", "FOO,BAS"], |
| 207 ]; |
| 208 |
| 209 for (let [text, selector, selectorDomain] of tests) |
| 210 { |
| 211 doTest(text, selector, selectorDomain); |
| 212 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 213 } |
| 214 |
| 215 test.done(); |
| 216 }; |
OLD | NEW |