OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.libadblockplus.tests; |
| 19 |
| 20 import org.adblockplus.libadblockplus.Filter; |
| 21 import org.adblockplus.libadblockplus.FilterEngine; |
| 22 import org.adblockplus.libadblockplus.MockFilterChangeCallback; |
| 23 import org.adblockplus.libadblockplus.Subscription; |
| 24 |
| 25 import org.junit.Test; |
| 26 |
| 27 public class FilterEngineTest extends FilterEngineGenericTest { |
| 28 |
| 29 @Test |
| 30 public void testFilterCreation() { |
| 31 Filter filter1 = filterEngine.getFilter("foo"); |
| 32 assertEquals(Filter.Type.BLOCKING, filter1.getType()); |
| 33 Filter filter2 = filterEngine.getFilter("@@foo"); |
| 34 assertEquals(Filter.Type.EXCEPTION, filter2.getType()); |
| 35 Filter filter3 = filterEngine.getFilter("example.com##foo"); |
| 36 assertEquals(Filter.Type.ELEMHIDE, filter3.getType()); |
| 37 Filter filter4 = filterEngine.getFilter("example.com#@#foo"); |
| 38 assertEquals(Filter.Type.ELEMHIDE_EXCEPTION, filter4.getType()); |
| 39 Filter filter5 = filterEngine.getFilter(" foo "); |
| 40 assertEquals(filter1, filter5); |
| 41 } |
| 42 |
| 43 @Test |
| 44 public void testFilterProperties() { |
| 45 Filter filter = filterEngine.getFilter("foo"); |
| 46 |
| 47 assertTrue(filter.getProperty("stringFoo").isUndefined()); |
| 48 assertTrue(filter.getProperty("intFoo").isUndefined()); |
| 49 assertTrue(filter.getProperty("boolFoo").isUndefined()); |
| 50 |
| 51 // TODO : since there is no setProperty for Filter finish the test later |
| 52 } |
| 53 |
| 54 @Test |
| 55 public void testAddRemoveFilters() { |
| 56 while (filterEngine.getListedFilters().size() > 0) |
| 57 filterEngine.getListedFilters().get(0).removeFromList(); |
| 58 |
| 59 assertEquals(0, filterEngine.getListedFilters().size()); |
| 60 Filter filter = filterEngine.getFilter("foo"); |
| 61 assertEquals(0, filterEngine.getListedFilters().size()); |
| 62 assertFalse(filter.isListed()); |
| 63 |
| 64 filter.addToList(); |
| 65 assertEquals(1, filterEngine.getListedFilters().size()); |
| 66 assertEquals(filter, filterEngine.getListedFilters().get(0)); |
| 67 assertTrue(filter.isListed()); |
| 68 |
| 69 filter.addToList(); |
| 70 assertEquals(1, filterEngine.getListedFilters().size()); |
| 71 assertEquals(filter, filterEngine.getListedFilters().get(0)); |
| 72 assertTrue(filter.isListed()); |
| 73 |
| 74 filter.removeFromList(); |
| 75 assertEquals(0, filterEngine.getListedFilters().size()); |
| 76 assertFalse(filter.isListed()); |
| 77 |
| 78 filter.removeFromList(); |
| 79 assertEquals(0, filterEngine.getListedFilters().size()); |
| 80 assertFalse(filter.isListed()); |
| 81 } |
| 82 |
| 83 @Test |
| 84 public void testSubscriptionProperties() { |
| 85 Subscription subscription = filterEngine.getSubscription("foo"); |
| 86 |
| 87 assertTrue(subscription.getProperty("stringFoo").isUndefined()); |
| 88 assertTrue(subscription.getProperty("intFoo").isUndefined()); |
| 89 assertTrue(subscription.getProperty("boolFoo").isUndefined()); |
| 90 |
| 91 // TODO : since there is no setProperty finish the test later |
| 92 } |
| 93 |
| 94 @Test |
| 95 public void testAddRemoveSubscriptions() { |
| 96 while (filterEngine.getListedSubscriptions().size() > 0) |
| 97 filterEngine.getListedSubscriptions().get(0).removeFromList(); |
| 98 |
| 99 assertEquals(0, filterEngine.getListedSubscriptions().size()); |
| 100 Subscription subscription = filterEngine.getSubscription("foo"); |
| 101 assertEquals(0, filterEngine.getListedSubscriptions().size()); |
| 102 assertFalse(subscription.isListed()); |
| 103 subscription.addToList(); |
| 104 assertEquals(1, filterEngine.getListedSubscriptions().size()); |
| 105 assertEquals(subscription, filterEngine.getListedSubscriptions().get(0))
; |
| 106 assertTrue(subscription.isListed()); |
| 107 subscription.addToList(); |
| 108 assertEquals(1, filterEngine.getListedSubscriptions().size()); |
| 109 assertEquals(subscription, filterEngine.getListedSubscriptions().get(0))
; |
| 110 assertTrue(subscription.isListed()); |
| 111 subscription.removeFromList(); |
| 112 assertEquals(0, filterEngine.getListedSubscriptions().size()); |
| 113 assertFalse(subscription.isListed()); |
| 114 subscription.removeFromList(); |
| 115 assertEquals(0, filterEngine.getListedSubscriptions().size()); |
| 116 assertFalse(subscription.isListed()); |
| 117 } |
| 118 |
| 119 @Test |
| 120 public void testSubscriptionUpdates() { |
| 121 Subscription subscription = filterEngine.getSubscription("foo"); |
| 122 assertFalse(subscription.isUpdating()); |
| 123 subscription.updateFilters(); |
| 124 } |
| 125 |
| 126 @Test |
| 127 public void testMatches() { |
| 128 filterEngine.getFilter("adbanner.gif").addToList(); |
| 129 filterEngine.getFilter("@@notbanner.gif").addToList(); |
| 130 filterEngine.getFilter("tpbanner.gif$third-party").addToList(); |
| 131 filterEngine.getFilter("fpbanner.gif$~third-party").addToList(); |
| 132 filterEngine.getFilter("combanner.gif$domain=example.com").addToList(); |
| 133 filterEngine.getFilter("orgbanner.gif$domain=~example.com").addToList(); |
| 134 |
| 135 Filter match1 = filterEngine.matches( |
| 136 "http://example.org/foobar.gif", |
| 137 FilterEngine.ContentType.IMAGE, |
| 138 ""); |
| 139 assertNull(match1); |
| 140 |
| 141 Filter match2 = filterEngine.matches( |
| 142 "http://example.org/adbanner.gif", |
| 143 FilterEngine.ContentType.IMAGE, |
| 144 ""); |
| 145 assertNotNull(match2); |
| 146 assertEquals(Filter.Type.BLOCKING, match2.getType()); |
| 147 |
| 148 Filter match3 = filterEngine.matches( |
| 149 "http://example.org/notbanner.gif", |
| 150 FilterEngine.ContentType.IMAGE, |
| 151 ""); |
| 152 assertNotNull(match3); |
| 153 assertEquals(Filter.Type.EXCEPTION, match3.getType()); |
| 154 |
| 155 Filter match4 = filterEngine.matches( |
| 156 "http://example.org/notbanner.gif", |
| 157 FilterEngine.ContentType.IMAGE, ""); |
| 158 assertNotNull(match4); |
| 159 assertEquals(Filter.Type.EXCEPTION, match3.getType()); |
| 160 |
| 161 Filter match5 = filterEngine.matches( |
| 162 "http://example.org/tpbanner.gif", |
| 163 FilterEngine.ContentType.IMAGE, |
| 164 "http://example.org/"); |
| 165 assertNull(match5); |
| 166 |
| 167 Filter match6 = filterEngine.matches( |
| 168 "http://example.org/fpbanner.gif", |
| 169 FilterEngine.ContentType.IMAGE, |
| 170 "http://example.org/"); |
| 171 assertNotNull(match6); |
| 172 assertEquals(Filter.Type.BLOCKING, match6.getType()); |
| 173 |
| 174 Filter match7 = filterEngine.matches( |
| 175 "http://example.org/tpbanner.gif", |
| 176 FilterEngine.ContentType.IMAGE, |
| 177 "http://example.com/"); |
| 178 assertNotNull(match7); |
| 179 assertEquals(Filter.Type.BLOCKING, match7.getType()); |
| 180 |
| 181 Filter match8 = filterEngine.matches( |
| 182 "http://example.org/fpbanner.gif", |
| 183 FilterEngine.ContentType.IMAGE, |
| 184 "http://example.com/"); |
| 185 assertNull(match8); |
| 186 |
| 187 Filter match9 = filterEngine.matches( |
| 188 "http://example.org/combanner.gif", |
| 189 FilterEngine.ContentType.IMAGE, |
| 190 "http://example.com/"); |
| 191 assertNotNull(match9); |
| 192 assertEquals(Filter.Type.BLOCKING, match9.getType()); |
| 193 |
| 194 Filter match10 = filterEngine.matches( |
| 195 "http://example.org/combanner.gif", |
| 196 FilterEngine.ContentType.IMAGE, |
| 197 "http://example.org/"); |
| 198 assertNull(match10); |
| 199 |
| 200 Filter match11 = filterEngine.matches( |
| 201 "http://example.org/orgbanner.gif", |
| 202 FilterEngine.ContentType.IMAGE, |
| 203 "http://example.com/"); |
| 204 assertNull(match11); |
| 205 |
| 206 Filter match12 = filterEngine.matches( |
| 207 "http://example.org/orgbanner.gif", |
| 208 FilterEngine.ContentType.IMAGE, |
| 209 "http://example.org/"); |
| 210 assertNotNull(match12); |
| 211 assertEquals(Filter.Type.BLOCKING, match12.getType()); |
| 212 } |
| 213 |
| 214 @Test |
| 215 public void testMatchesOnWhitelistedDomain() { |
| 216 filterEngine.getFilter("adbanner.gif").addToList(); |
| 217 filterEngine.getFilter("@@||example.org^$document").addToList(); |
| 218 |
| 219 Filter match1 = filterEngine.matches( |
| 220 "http://ads.com/adbanner.gif", |
| 221 FilterEngine.ContentType.IMAGE, |
| 222 "http://example.com/"); |
| 223 assertNotNull(match1); |
| 224 assertEquals(Filter.Type.BLOCKING, match1.getType()); |
| 225 |
| 226 Filter match2 = filterEngine.matches( |
| 227 "http://ads.com/adbanner.gif", |
| 228 FilterEngine.ContentType.IMAGE, |
| 229 "http://example.org/"); |
| 230 assertNotNull(match2); |
| 231 assertEquals(Filter.Type.EXCEPTION, match2.getType()); |
| 232 } |
| 233 |
| 234 @Test |
| 235 public void testMatchesNestedFrameRequest() { |
| 236 filterEngine.getFilter("adbanner.gif").addToList(); |
| 237 filterEngine.getFilter("@@adbanner.gif$domain=example.org").addToList(); |
| 238 |
| 239 Filter match1 = filterEngine.matches( |
| 240 "http://ads.com/adbanner.gif", |
| 241 FilterEngine.ContentType.IMAGE, |
| 242 new String[] { |
| 243 "http://ads.com/frame/", |
| 244 "http://example.com/" |
| 245 }); |
| 246 assertNotNull(match1); |
| 247 assertEquals(Filter.Type.BLOCKING, match1.getType()); |
| 248 |
| 249 Filter match2 = filterEngine.matches( |
| 250 "http://ads.com/adbanner.gif", |
| 251 FilterEngine.ContentType.IMAGE, |
| 252 new String[] { |
| 253 "http://ads.com/frame/", |
| 254 "http://example.org/" |
| 255 }); |
| 256 assertNotNull(match2); |
| 257 assertEquals(Filter.Type.EXCEPTION, match2.getType()); |
| 258 |
| 259 Filter match3 = filterEngine.matches( |
| 260 "http://ads.com/adbanner.gif", |
| 261 FilterEngine.ContentType.IMAGE, |
| 262 new String[] { |
| 263 "http://example.org/", |
| 264 "http://ads.com/frame/" |
| 265 }); |
| 266 assertNotNull(match3); |
| 267 assertEquals(Filter.Type.BLOCKING, match3.getType()); |
| 268 } |
| 269 |
| 270 @Test |
| 271 public void testMatchesNestedFrameOnWhitelistedDomain() { |
| 272 filterEngine.getFilter("adbanner.gif").addToList(); |
| 273 filterEngine.getFilter("@@||example.org^$document,domain=ads.com").addTo
List(); |
| 274 |
| 275 Filter match1 = filterEngine.matches( |
| 276 "http://ads.com/adbanner.gif", |
| 277 FilterEngine.ContentType.IMAGE, |
| 278 new String[] { |
| 279 "http://ads.com/frame/", |
| 280 "http://example.com/" |
| 281 }); |
| 282 assertNotNull(match1); |
| 283 assertEquals(Filter.Type.BLOCKING, match1.getType()); |
| 284 |
| 285 Filter match2 = filterEngine.matches( |
| 286 "http://ads.com/adbanner.gif", |
| 287 FilterEngine.ContentType.IMAGE, |
| 288 new String[] { |
| 289 "http://ads.com/frame/", |
| 290 "http://example.org/" |
| 291 }); |
| 292 assertNotNull(match2); |
| 293 assertEquals(Filter.Type.EXCEPTION, match2.getType()); |
| 294 |
| 295 Filter match3 = filterEngine.matches( |
| 296 "http://ads.com/adbanner.gif", |
| 297 FilterEngine.ContentType.IMAGE, |
| 298 new String[] { |
| 299 "http://example.org/" |
| 300 }); |
| 301 assertNotNull(match3); |
| 302 assertEquals(Filter.Type.BLOCKING, match3.getType()); |
| 303 |
| 304 Filter match4 = filterEngine.matches( |
| 305 "http://ads.com/adbanner.gif", |
| 306 FilterEngine.ContentType.IMAGE, |
| 307 new String[] { |
| 308 "http://example.org/", |
| 309 "http://ads.com/frame/" |
| 310 }); |
| 311 assertNotNull(match4); |
| 312 assertEquals(Filter.Type.BLOCKING, match4.getType()); |
| 313 |
| 314 Filter match5 = filterEngine.matches( |
| 315 "http://ads.com/adbanner.gif", |
| 316 FilterEngine.ContentType.IMAGE, |
| 317 new String[]{ |
| 318 "http://ads.com/frame/", |
| 319 "http://example.org/", |
| 320 "http://example.com/" |
| 321 }); |
| 322 assertNotNull(match5); |
| 323 assertEquals(Filter.Type.EXCEPTION, match5.getType()); |
| 324 } |
| 325 |
| 326 @Test |
| 327 public void testFirstRunFlag() { |
| 328 assertFalse(filterEngine.isFirstRun()); |
| 329 } |
| 330 |
| 331 @Test |
| 332 public void testSetRemoveFilterChangeCallback() { |
| 333 MockFilterChangeCallback mockFilterChangeCallback = new MockFilterChange
Callback(0); |
| 334 |
| 335 filterEngine.setFilterChangeCallback(mockFilterChangeCallback); |
| 336 filterEngine.getFilter("foo").addToList(); |
| 337 assertEquals(2, mockFilterChangeCallback.getTimesCalled()); |
| 338 |
| 339 filterEngine.removeFilterChangeCallback(); |
| 340 filterEngine.getFilter("foo").removeFromList(); |
| 341 assertEquals(2, mockFilterChangeCallback.getTimesCalled()); |
| 342 } |
| 343 |
| 344 @Test |
| 345 public void testDocumentWhitelisting() { |
| 346 filterEngine.getFilter("@@||example.org^$document").addToList(); |
| 347 filterEngine.getFilter("@@||example.com^$document,domain=example.de").ad
dToList(); |
| 348 |
| 349 assertTrue(filterEngine.isDocumentWhitelisted("http://example.org", new
String[] {})); |
| 350 assertFalse(filterEngine.isDocumentWhitelisted("http://example.co.uk", n
ew String[] {})); |
| 351 assertFalse(filterEngine.isDocumentWhitelisted("http://example.com", new
String[] {})); |
| 352 |
| 353 String[] documentUrls1 = new String[] { "http://example.de" }; |
| 354 assertTrue(filterEngine.isDocumentWhitelisted("http://example.com", docu
mentUrls1)); |
| 355 assertFalse(filterEngine.isDocumentWhitelisted("http://example.co.uk", d
ocumentUrls1)); |
| 356 } |
| 357 |
| 358 @Test |
| 359 public void testElemhideWhitelisting() { |
| 360 filterEngine.getFilter("@@||example.org^$elemhide").addToList(); |
| 361 filterEngine.getFilter("@@||example.com^$elemhide,domain=example.de").ad
dToList(); |
| 362 |
| 363 assertTrue(filterEngine.isElemhideWhitelisted("http://example.org", new
String[] {})); |
| 364 assertFalse(filterEngine.isElemhideWhitelisted("http://example.co.uk", n
ew String[] {})); |
| 365 assertFalse(filterEngine.isElemhideWhitelisted("http://example.com", new
String[] {})); |
| 366 |
| 367 String[] documentUrls1 = new String[] { "http://example.de" }; |
| 368 assertTrue(filterEngine.isElemhideWhitelisted("http://example.com", docu
mentUrls1)); |
| 369 assertFalse(filterEngine.isElemhideWhitelisted("http://example.co.uk", d
ocumentUrls1)); |
| 370 } |
| 371 |
| 372 } |
OLD | NEW |