OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 let { |
| 4 Filter, InvalidFilter, CommentFilter, ActiveFilter, BlockingFilter, |
| 5 WhitelistFilter, ElemHideFilter, ElemHideException, CSSPropertyFilter |
| 6 } = require("../lib/filterClasses"); |
| 7 |
| 8 exports.testFromText = function(test) |
| 9 { |
| 10 let tests = [ |
| 11 ["!asdf", CommentFilter, "comment"], |
| 12 ["asdf", BlockingFilter, "blocking"], |
| 13 ["asdf$image,~collapse", BlockingFilter, "blocking"], |
| 14 ["/asdf/", BlockingFilter, "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", BlockingFilter, "blocking"], |
| 28 ["foobar|foobas##asdf", BlockingFilter, "blocking"], |
| 29 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], |
| 30 ["foobar##", BlockingFilter, "blocking"], |
| 31 ["foobar#@#", BlockingFilter, "blocking"], |
| 32 ["asdf$foobar", InvalidFilter, "invalid"], |
| 33 ["asdf$image,foobar", InvalidFilter, "invalid"], |
| 34 ["asdf$image=foobar", BlockingFilter, "blocking"], |
| 35 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "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 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 60 test.equal(filter.text, text, "Correct filter text for " + text); |
| 61 test.ok(filter instanceof type, "Correct filter type for " + text); |
| 62 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); |
| 63 if (type == InvalidFilter) |
| 64 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 65 filter.delete(); |
| 66 } |
| 67 test.done(); |
| 68 }; |
| 69 |
| 70 exports.getKnownFilter = function(test) |
| 71 { |
| 72 let filter1 = Filter.getKnownFilter("someknownfilter"); |
| 73 test.ok(!filter1, "Unknown filter returns null"); |
| 74 |
| 75 let filter2 = Filter.fromText("someknownfilter"); |
| 76 filter1 = Filter.getKnownFilter("someknownfilter"); |
| 77 test.ok(filter1, "Known filter returned"); |
| 78 |
| 79 filter2.hitCount = 432; |
| 80 test.equal(filter1.hitCount, 432, "Changes on previous instance are reflected
on new instance"); |
| 81 |
| 82 filter1.delete(); |
| 83 filter2.delete(); |
| 84 |
| 85 test.done(); |
| 86 }; |
| 87 |
| 88 exports.testNormalize = function(test) |
| 89 { |
| 90 let tests = [ |
| 91 [" ! comment something ", "! comment something"], |
| 92 [" ! \n comment something ", "! comment something"], |
| 93 [" foo bar ", "foobar"], |
| 94 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"]
, |
| 95 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b
ar"], |
| 96 ["foOBar"], |
| 97 ["foOBar#xyz"], |
| 98 ["foOBar$iMaGe,object_subrequest,~coLLapse", "foOBar$image,object-subrequest
,~collapse"], |
| 99 ["foOBar$doMain=EXample.COM|~exAMPLE.РФ", "foOBar$domain=example.com|~exampl
e.рф"], |
| 100 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], |
| 101 ["exampLE.com##fooBAr", "example.com##fooBAr"], |
| 102 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], |
| 103 ["exampLE.РФ#@#fooBAr", "example.рф#@#fooBAr"], |
| 104 ]; |
| 105 |
| 106 for (let [text, expected, selectorDomain, selector] of tests) |
| 107 { |
| 108 if (!expected) |
| 109 expected = text; |
| 110 |
| 111 let filter1 = Filter.fromText(text); |
| 112 let filter2 = Filter.fromText(expected); |
| 113 |
| 114 test.equal(filter1.text, expected, "Filter text " + text + " got normalized"
); |
| 115 test.equal(filter2.text, expected, "Already normalized text " + expected + "
didn't change"); |
| 116 |
| 117 if (filter1 instanceof ActiveFilter) |
| 118 { |
| 119 filter1.hitCount = 567; |
| 120 test.equal(filter1.hitCount, filter2.hitCount, "Property changes on filter
" + text + " get reflected on filter " + expected); |
| 121 } |
| 122 |
| 123 if (selectorDomain) |
| 124 { |
| 125 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 126 let actualDomains1 = filter1.selectorDomain.split(",").sort().join(","); |
| 127 let actualDomains2 = filter2.selectorDomain.split(",").sort().join(","); |
| 128 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + text); |
| 129 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + expected); |
| 130 |
| 131 test.equal(filter1.selector, selector, "Correct selector for filter " + te
xt); |
| 132 test.equal(filter2.selector, selector, "Correct selector for filter " + ex
pected); |
| 133 } |
| 134 |
| 135 filter1.delete(); |
| 136 filter2.delete(); |
| 137 } |
| 138 |
| 139 test.done(); |
| 140 }; |
| 141 |
| 142 exports.testSerialize = function(test) |
| 143 { |
| 144 // Comment |
| 145 let filter = Filter.fromText("! serialize"); |
| 146 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 147 filter.delete(); |
| 148 |
| 149 // Blocking filter |
| 150 filter = Filter.fromText("serialize"); |
| 151 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 152 filter.disabled = true; |
| 153 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 154 filter.disabled = false; |
| 155 filter.hitCount = 10; |
| 156 filter.lastHit = 12; |
| 157 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 158 filter.delete(); |
| 159 |
| 160 // Invalid filter |
| 161 filter = Filter.fromText("serialize$foobar"); |
| 162 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 163 filter.delete(); |
| 164 |
| 165 // Element hiding filter |
| 166 filter = Filter.fromText("example.com##serialize"); |
| 167 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| 168 filter.disabled = true; |
| 169 filter.lastHit = 5; |
| 170 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
| 171 filter.delete(); |
| 172 |
| 173 test.done(); |
| 174 }; |
| 175 |
| 176 exports.testInvalidReasons = function(test) |
| 177 { |
| 178 let tests = [ |
| 179 ["/??/", "filter_invalid_regexp"], |
| 180 ["asd$foobar", "filter_unknown_option"], |
| 181 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"], |
| 182 ]; |
| 183 |
| 184 for (let [text, reason] of tests) |
| 185 { |
| 186 let filter = Filter.fromText(text); |
| 187 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali
d"); |
| 188 filter.delete(); |
| 189 } |
| 190 |
| 191 test.done(); |
| 192 }; |
| 193 |
| 194 exports.testActiveFilter = function(test) |
| 195 { |
| 196 let filter1 = Filter.fromText("asdf"); |
| 197 let filter1copy = Filter.fromText("asdf"); |
| 198 let filter2 = Filter.fromText("##foobar"); |
| 199 |
| 200 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); |
| 201 filter1.disabled = true; |
| 202 test.ok(filter1.disabled, "Disabling filter works"); |
| 203 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 204 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 205 |
| 206 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou
nt === 0, "Filters have no hit initially"); |
| 207 filter1.hitCount = 5; |
| 208 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 209 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change
d"); |
| 210 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); |
| 211 |
| 212 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit
=== 0, "Filters have no last hit time initially"); |
| 213 filter1.lastHit = 10; |
| 214 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 215 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch
anged"); |
| 216 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected"
); |
| 217 |
| 218 filter1.delete(); |
| 219 filter1copy.delete(); |
| 220 filter2.delete(); |
| 221 |
| 222 test.done(); |
| 223 }; |
| 224 |
| 225 exports.testIsGeneric = function(test) |
| 226 { |
| 227 let tests = [ |
| 228 ["asfd", true], |
| 229 ["|http://example.com/asdf", true], |
| 230 ["||example.com/asdf", true], |
| 231 ["asfd$third-party", true], |
| 232 ["asdf$domain=com", false], |
| 233 ["asdf$domain=example.com", false], |
| 234 ["asdf$image,domain=example.com", false], |
| 235 ["asdf$~image,domain=example.com", false], |
| 236 ["asdf$third-party,domain=example.com", false], |
| 237 ["||example.com/asdf$~coLLapse,domain=example.com", false], |
| 238 ["||example.com/asdf$domain=~example.com", true], |
| 239 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 240 ["asdf$domain=foo.example.com|~example.com", false], |
| 241 ["asdf$domain=foo.com|~example.com", false], |
| 242 ["asdf$domain=~foo.com|~example.com", true], |
| 243 ]; |
| 244 |
| 245 for (let [text, generic] of tests) |
| 246 { |
| 247 let filter = Filter.fromText(text); |
| 248 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 249 filter.delete(); |
| 250 } |
| 251 |
| 252 test.done(); |
| 253 } |
| 254 |
| 255 exports.testElemHideSelector = function(test) |
| 256 { |
| 257 function doTest(text, selector, selectorDomain) |
| 258 { |
| 259 let filter = Filter.fromText(text); |
| 260 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 261 |
| 262 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 263 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 264 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex
t); |
| 265 |
| 266 filter.delete(); |
| 267 } |
| 268 |
| 269 let tests = [ |
| 270 ["##foobar", "foobar", ""], |
| 271 ["~example.com##foobar", "foobar", ""], |
| 272 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
| 273 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 274 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 275 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 276 |
| 277 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 278 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 279 ]; |
| 280 |
| 281 for (let [text, selector, selectorDomain] of tests) |
| 282 { |
| 283 doTest(text, selector, selectorDomain); |
| 284 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 285 } |
| 286 |
| 287 test.done(); |
| 288 }; |
| 289 |
| 290 exports.testCSSRules = function(test) |
| 291 { |
| 292 let tests = [ |
| 293 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
| 294 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
| 295 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
| 296 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
| 297 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
| 298 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
| 299 ]; |
| 300 |
| 301 for (let [text, regexp, prefix, suffix] of tests) |
| 302 { |
| 303 let filter = Filter.fromText(text); |
| 304 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
| 305 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
| 306 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
| 307 filter.delete(); |
| 308 } |
| 309 |
| 310 test.done(); |
| 311 }; |
OLD | NEW |