OLD | NEW |
(Empty) | |
| 1 "use strict"; |
| 2 |
| 3 let { |
| 4 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
| 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", 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 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.testActiveFilter = function(test) |
| 177 { |
| 178 let filter1 = Filter.fromText("asdf"); |
| 179 let filter1copy = Filter.fromText("asdf"); |
| 180 let filter2 = Filter.fromText("##foobar"); |
| 181 |
| 182 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); |
| 183 filter1.disabled = true; |
| 184 test.ok(filter1.disabled, "Disabling filter works"); |
| 185 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 186 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 187 |
| 188 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou
nt === 0, "Filters have no hit initially"); |
| 189 filter1.hitCount = 5; |
| 190 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 191 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change
d"); |
| 192 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); |
| 193 |
| 194 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit
=== 0, "Filters have no last hit time initially"); |
| 195 filter1.lastHit = 10; |
| 196 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 197 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch
anged"); |
| 198 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected"
); |
| 199 |
| 200 filter1.delete(); |
| 201 filter1copy.delete(); |
| 202 filter2.delete(); |
| 203 |
| 204 test.done(); |
| 205 }; |
| 206 |
| 207 exports.testIsGeneric = function(test) |
| 208 { |
| 209 let tests = [ |
| 210 ["asfd", true], |
| 211 ["|http://example.com/asdf", true], |
| 212 ["||example.com/asdf", true], |
| 213 ["asfd$third-party", true], |
| 214 ["asdf$domain=com", false], |
| 215 ["asdf$domain=example.com", false], |
| 216 ["asdf$image,domain=example.com", false], |
| 217 ["asdf$~image,domain=example.com", false], |
| 218 ["asdf$third-party,domain=example.com", false], |
| 219 ["||example.com/asdf$~coLLapse,domain=example.com", false], |
| 220 ["||example.com/asdf$domain=~example.com", true], |
| 221 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 222 ["asdf$domain=foo.example.com|~example.com", false], |
| 223 ["asdf$domain=foo.com|~example.com", false], |
| 224 ["asdf$domain=~foo.com|~example.com", true], |
| 225 ]; |
| 226 |
| 227 for (let [text, generic] of tests) |
| 228 { |
| 229 let filter = Filter.fromText(text); |
| 230 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 231 filter.delete(); |
| 232 } |
| 233 |
| 234 test.done(); |
| 235 } |
| 236 |
| 237 exports.testElemHideSelector = function(test) |
| 238 { |
| 239 function doTest(text, selector, selectorDomain) |
| 240 { |
| 241 let filter = Filter.fromText(text); |
| 242 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 243 |
| 244 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 245 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 246 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex
t); |
| 247 |
| 248 filter.delete(); |
| 249 } |
| 250 |
| 251 let tests = [ |
| 252 ["##foobar", "foobar", ""], |
| 253 ["~example.com##foobar", "foobar", ""], |
| 254 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
| 255 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 256 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 257 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 258 |
| 259 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 260 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 261 ]; |
| 262 |
| 263 for (let [text, selector, selectorDomain] of tests) |
| 264 { |
| 265 doTest(text, selector, selectorDomain); |
| 266 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 267 } |
| 268 |
| 269 test.done(); |
| 270 }; |
| 271 |
| 272 exports.textCSSRules = function(test) |
| 273 { |
| 274 let tests = [ |
| 275 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
| 276 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
| 277 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
| 278 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
| 279 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
| 280 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
| 281 ]; |
| 282 |
| 283 for (let [text, regexp, prefix, suffix] of tests) |
| 284 { |
| 285 let filter = Filter.fromText(text); |
| 286 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
| 287 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
| 288 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
| 289 filter.delete(); |
| 290 } |
| 291 |
| 292 test.done(); |
| 293 }; |
OLD | NEW |