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