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,~coLLapse", "foOBar$image,~collapse"], |
| 99 ["foOBar$doMain=EXample.COM|~exAMPLE.info", "foOBar$domain=example.com|~exam
ple.info"], |
| 100 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], |
| 101 ["exampLE.com##fooBAr", "example.com##fooBAr"], |
| 102 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], |
| 103 ]; |
| 104 |
| 105 for (let [text, expected, selectorDomain, selector] of tests) |
| 106 { |
| 107 if (!expected) |
| 108 expected = text; |
| 109 |
| 110 let filter1 = Filter.fromText(text); |
| 111 let filter2 = Filter.fromText(expected); |
| 112 |
| 113 test.equal(filter1.text, expected, "Filter text " + text + " got normalized"
); |
| 114 test.equal(filter2.text, expected, "Already normalized text " + expected + "
didn't change"); |
| 115 |
| 116 if (filter1 instanceof ActiveFilter) |
| 117 { |
| 118 filter1.hitCount = 567; |
| 119 test.equal(filter1.hitCount, filter2.hitCount, "Property changes on filter
" + text + " get reflected on filter " + expected); |
| 120 } |
| 121 |
| 122 if (selectorDomain) |
| 123 { |
| 124 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 125 let actualDomains1 = filter1.selectorDomain.split(",").sort().join(","); |
| 126 let actualDomains2 = filter2.selectorDomain.split(",").sort().join(","); |
| 127 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + text); |
| 128 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + expected); |
| 129 |
| 130 test.equal(filter1.selector, selector, "Correct selector for filter " + te
xt); |
| 131 test.equal(filter2.selector, selector, "Correct selector for filter " + ex
pected); |
| 132 } |
| 133 |
| 134 filter1.delete(); |
| 135 filter2.delete(); |
| 136 } |
| 137 |
| 138 test.done(); |
| 139 }; |
| 140 |
| 141 exports.testSerialize = function(test) |
| 142 { |
| 143 // Comment |
| 144 let filter = Filter.fromText("! serialize"); |
| 145 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 146 filter.delete(); |
| 147 |
| 148 // Blocking filter |
| 149 filter = Filter.fromText("serialize"); |
| 150 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 151 filter.disabled = true; |
| 152 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 153 filter.disabled = false; |
| 154 filter.hitCount = 10; |
| 155 filter.lastHit = 12; |
| 156 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 157 filter.delete(); |
| 158 |
| 159 // Invalid filter |
| 160 filter = Filter.fromText("serialize$foobar"); |
| 161 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 162 filter.delete(); |
| 163 |
| 164 // Element hiding filter |
| 165 filter = Filter.fromText("example.com##serialize"); |
| 166 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| 167 filter.disabled = true; |
| 168 filter.lastHit = 5; |
| 169 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
| 170 filter.delete(); |
| 171 |
| 172 test.done(); |
| 173 } |
| 174 |
| 175 exports.testActiveFilter = function(test) |
| 176 { |
| 177 let filter1 = Filter.fromText("asdf"); |
| 178 let filter1copy = Filter.fromText("asdf"); |
| 179 let filter2 = Filter.fromText("##foobar"); |
| 180 |
| 181 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); |
| 182 filter1.disabled = true; |
| 183 test.ok(filter1.disabled, "Disabling filter works"); |
| 184 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 185 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 186 |
| 187 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou
nt === 0, "Filters have no hit initially"); |
| 188 filter1.hitCount = 5; |
| 189 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 190 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change
d"); |
| 191 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); |
| 192 |
| 193 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit
=== 0, "Filters have no last hit time initially"); |
| 194 filter1.lastHit = 10; |
| 195 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 196 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch
anged"); |
| 197 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected"
); |
| 198 |
| 199 filter1.delete(); |
| 200 filter1copy.delete(); |
| 201 filter2.delete(); |
| 202 |
| 203 test.done(); |
| 204 }; |
| 205 |
| 206 exports.testIsGeneric = function(test) |
| 207 { |
| 208 let tests = [ |
| 209 ["asfd", true], |
| 210 ["|http://example.com/asdf", true], |
| 211 ["||example.com/asdf", true], |
| 212 ["asfd$third-party", true], |
| 213 ["asdf$domain=com", false], |
| 214 ["asdf$domain=example.com", false], |
| 215 ["asdf$image,domain=example.com", false], |
| 216 ["asdf$~image,domain=example.com", false], |
| 217 ["asdf$third-party,domain=example.com", false], |
| 218 ["||example.com/asdf$~coLLapse,domain=example.com", false], |
| 219 ["||example.com/asdf$domain=~example.com", true], |
| 220 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 221 ["asdf$domain=foo.example.com|~example.com", false], |
| 222 ["asdf$domain=foo.com|~example.com", false], |
| 223 ["asdf$domain=~foo.com|~example.com", true], |
| 224 ]; |
| 225 |
| 226 for (let [text, generic] of tests) |
| 227 { |
| 228 let filter = Filter.fromText(text); |
| 229 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 230 filter.delete(); |
| 231 } |
| 232 |
| 233 test.done(); |
| 234 } |
| 235 |
| 236 exports.testElemHideSelector = function(test) |
| 237 { |
| 238 function doTest(text, selector, selectorDomain) |
| 239 { |
| 240 let filter = Filter.fromText(text); |
| 241 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 242 |
| 243 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 244 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 245 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex
t); |
| 246 |
| 247 filter.delete(); |
| 248 } |
| 249 |
| 250 let tests = [ |
| 251 ["##foobar", "foobar", ""], |
| 252 ["~example.com##foobar", "foobar", ""], |
| 253 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
| 254 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 255 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 256 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 257 |
| 258 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 259 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 260 ]; |
| 261 |
| 262 for (let [text, selector, selectorDomain] of tests) |
| 263 { |
| 264 doTest(text, selector, selectorDomain); |
| 265 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 266 } |
| 267 |
| 268 test.done(); |
| 269 }; |
| 270 |
| 271 exports.textCSSRules = function(test) |
| 272 { |
| 273 let tests = [ |
| 274 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
| 275 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
| 276 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
| 277 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
| 278 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
| 279 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
| 280 ]; |
| 281 |
| 282 for (let [text, regexp, prefix, suffix] of tests) |
| 283 { |
| 284 let filter = Filter.fromText(text); |
| 285 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
| 286 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
| 287 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
| 288 filter.delete(); |
| 289 } |
| 290 |
| 291 test.done(); |
| 292 }; |
OLD | NEW |