OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 let { |
| 4 Filter, InvalidFilter, CommentFilter, RegExpFilter, WhitelistFilter, |
| 5 ElemHideFilter, ElemHideException, CSSPropertyFilter |
| 6 } = require("../lib/filterClasses"); |
| 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 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], |
| 38 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], |
| 39 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css
property"], |
| 40 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], |
| 41 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], |
| 42 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], |
| 43 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF
ilter, "invalid"], |
| 44 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid
eException, "elemhideexception"], |
| 45 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], |
| 46 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], |
| 47 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], |
| 48 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], |
| 49 ["example.com##[-abp-properties='something']", CSSPropertyFilter, "cssproper
ty"], |
| 50 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide
exception"], |
| 51 ["example.com##[-abp-properties=\"something\"]", CSSPropertyFilter, "cssprop
erty"], |
| 52 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi
deexception"], |
| 53 ["example.com##[-abp-properties=(something)]", ElemHideFilter, "elemhide"], |
| 54 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide
exception"], |
| 55 ]; |
| 56 for (let [text, type, typeName, location] of tests) |
| 57 { |
| 58 let filter = Filter.fromText(text); |
| 59 try |
| 60 { |
| 61 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 62 test.equal(filter.text, text, "Correct filter text for " + text); |
| 63 test.ok(filter instanceof type, "Correct filter type for " + text); |
| 64 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeN
ame); |
| 65 if (type == InvalidFilter) |
| 66 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 67 } |
| 68 finally |
| 69 { |
| 70 filter.delete(); |
| 71 } |
| 72 } |
| 73 test.done(); |
| 74 }; |
| 75 |
| 76 exports.testNormalize = function(test) |
| 77 { |
| 78 let tests = [ |
| 79 [" foo bar ", "foobar"], |
| 80 ["foobar", "foobar"], |
| 81 [" ! comment something ", "! comment something"], |
| 82 [" ! \n comment something ", "! comment something"], |
| 83 [" foo , bar ## foo > bar ", "foo,bar##foo > bar"], |
| 84 [" foo , bar #@# foo > bar ", "foo,bar#@#foo > bar"], |
| 85 ]; |
| 86 for (let [text, expected] of tests) |
| 87 test.equal(Filter.normalize(text), expected); |
| 88 test.done(); |
| 89 }; |
| 90 |
| 91 exports.testSerialize = function(test) |
| 92 { |
| 93 // Comment |
| 94 let filter = Filter.fromText("! serialize"); |
| 95 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 96 filter.delete(); |
| 97 |
| 98 // Blocking filter |
| 99 filter = Filter.fromText("serialize"); |
| 100 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 101 filter.disabled = true; |
| 102 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 103 filter.disabled = false; |
| 104 filter.hitCount = 10; |
| 105 filter.lastHit = 12; |
| 106 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 107 filter.delete(); |
| 108 |
| 109 // Invalid filter |
| 110 filter = Filter.fromText("serialize$foobar"); |
| 111 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 112 filter.delete(); |
| 113 |
| 114 // Element hiding filter |
| 115 filter = Filter.fromText("example.com##serialize"); |
| 116 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| 117 filter.disabled = true; |
| 118 filter.lastHit = 5; |
| 119 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
| 120 filter.delete(); |
| 121 |
| 122 test.done(); |
| 123 } |
| 124 |
| 125 exports.testActiveFilter = function(test) |
| 126 { |
| 127 let filter1 = Filter.fromText("asdf"); |
| 128 let filter1copy = Filter.fromText("asdf"); |
| 129 let filter2 = Filter.fromText("##foobar"); |
| 130 |
| 131 try |
| 132 { |
| 133 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Fi
lters are initially enabled"); |
| 134 filter1.disabled = true; |
| 135 test.ok(filter1.disabled, "Disabling filter works"); |
| 136 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 137 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 138 |
| 139 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitC
ount === 0, "Filters have no hit initially"); |
| 140 filter1.hitCount = 5; |
| 141 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 142 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also chan
ged"); |
| 143 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected")
; |
| 144 |
| 145 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHi
t === 0, "Filters have no last hit time initially"); |
| 146 filter1.lastHit = 10; |
| 147 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 148 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also
changed"); |
| 149 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affecte
d"); |
| 150 } |
| 151 finally |
| 152 { |
| 153 filter1.delete(); |
| 154 filter1copy.delete(); |
| 155 filter2.delete(); |
| 156 } |
| 157 |
| 158 test.done(); |
| 159 }; |
| 160 |
| 161 exports.testIsGeneric = function(test) |
| 162 { |
| 163 let tests = [ |
| 164 ["asfd", true], |
| 165 ["|http://example.com/asdf", true], |
| 166 ["||example.com/asdf", true], |
| 167 ["asfd$third-party", true], |
| 168 ["asdf$domain=com", false], |
| 169 ["asdf$domain=example.com", false], |
| 170 ["asdf$image,domain=example.com", false], |
| 171 ["asdf$~image,domain=example.com", false], |
| 172 ["asdf$third-party,domain=example.com", false], |
| 173 ["||example.com/asdf$~coLLapse,domain=example.com", false], |
| 174 ["||example.com/asdf$domain=~example.com", true], |
| 175 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 176 ["asdf$domain=foo.example.com|~example.com", false], |
| 177 ["asdf$domain=foo.com|~example.com", false], |
| 178 ["asdf$domain=~foo.com|~example.com", true], |
| 179 ]; |
| 180 |
| 181 for (let [text, generic] of tests) |
| 182 { |
| 183 let filter = Filter.fromText(text); |
| 184 try |
| 185 { |
| 186 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 187 } |
| 188 finally |
| 189 { |
| 190 filter.delete(); |
| 191 } |
| 192 } |
| 193 |
| 194 test.done(); |
| 195 } |
| 196 |
| 197 exports.testElemHideSelector = function(test) |
| 198 { |
| 199 function doTest(text, selector, selectorDomain) |
| 200 { |
| 201 let filter = Filter.fromText(text); |
| 202 try |
| 203 { |
| 204 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 205 |
| 206 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 207 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 208 test.equal(actualDomains, expectedDomains, "Correct domains list for " + t
ext); |
| 209 } |
| 210 finally |
| 211 { |
| 212 filter.delete(); |
| 213 } |
| 214 } |
| 215 |
| 216 let tests = [ |
| 217 ["##foobar", "foobar", ""], |
| 218 ["~example.com##foobar", "foobar", ""], |
| 219 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
| 220 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 221 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 222 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 223 |
| 224 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 225 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 226 ]; |
| 227 |
| 228 for (let [text, selector, selectorDomain] of tests) |
| 229 { |
| 230 doTest(text, selector, selectorDomain); |
| 231 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 232 } |
| 233 |
| 234 test.done(); |
| 235 }; |
| 236 |
| 237 exports.textCSSRules = function(test) |
| 238 { |
| 239 let tests = [ |
| 240 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
| 241 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
| 242 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
| 243 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
| 244 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
| 245 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
| 246 ]; |
| 247 |
| 248 for (let [text, regexp, prefix, suffix] of tests) |
| 249 { |
| 250 let filter = Filter.fromText(text); |
| 251 try |
| 252 { |
| 253 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
| 254 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
| 255 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
| 256 } |
| 257 finally |
| 258 { |
| 259 filter.delete(); |
| 260 } |
| 261 } |
| 262 |
| 263 test.done(); |
| 264 }; |
OLD | NEW |