Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/FilterEngineTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Patch Set: fix typo ".. file[s]" Created Sept. 15, 2016, 10:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(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 {
33 Filter filter1 = filterEngine.getFilter("foo");
34 assertEquals(Filter.Type.BLOCKING, filter1.getType());
35 Filter filter2 = filterEngine.getFilter("@@foo");
36 assertEquals(Filter.Type.EXCEPTION, filter2.getType());
37 Filter filter3 = filterEngine.getFilter("example.com##foo");
38 assertEquals(Filter.Type.ELEMHIDE, filter3.getType());
39 Filter filter4 = filterEngine.getFilter("example.com#@#foo");
40 assertEquals(Filter.Type.ELEMHIDE_EXCEPTION, filter4.getType());
41 Filter filter5 = filterEngine.getFilter(" foo ");
42 assertEquals(filter1, filter5);
43 }
44
45 @Test
46 public void testAddRemoveFilters()
47 {
48 while (filterEngine.getListedFilters().size() > 0)
49 {
50 filterEngine.getListedFilters().get(0).removeFromList();
51 }
52
53 assertEquals(0, filterEngine.getListedFilters().size());
54 Filter filter = filterEngine.getFilter("foo");
55 assertEquals(0, filterEngine.getListedFilters().size());
56 assertFalse(filter.isListed());
57
58 filter.addToList();
59 assertEquals(1, filterEngine.getListedFilters().size());
60 assertEquals(filter, filterEngine.getListedFilters().get(0));
61 assertTrue(filter.isListed());
62
63 filter.addToList();
64 assertEquals(1, filterEngine.getListedFilters().size());
65 assertEquals(filter, filterEngine.getListedFilters().get(0));
66 assertTrue(filter.isListed());
67
68 filter.removeFromList();
69 assertEquals(0, filterEngine.getListedFilters().size());
70 assertFalse(filter.isListed());
71
72 filter.removeFromList();
73 assertEquals(0, filterEngine.getListedFilters().size());
74 assertFalse(filter.isListed());
75 }
76
77 @Test
78 public void testAddRemoveSubscriptions()
79 {
80 while (filterEngine.getListedSubscriptions().size() > 0)
81 {
82 filterEngine.getListedSubscriptions().get(0).removeFromList();
83 }
84
85 assertEquals(0, filterEngine.getListedSubscriptions().size());
86 Subscription subscription = filterEngine.getSubscription("foo");
87 assertEquals(0, filterEngine.getListedSubscriptions().size());
88 assertFalse(subscription.isListed());
89 subscription.addToList();
90 assertEquals(1, filterEngine.getListedSubscriptions().size());
91 assertEquals(subscription, filterEngine.getListedSubscriptions().get(0));
92 assertTrue(subscription.isListed());
93 subscription.addToList();
94 assertEquals(1, filterEngine.getListedSubscriptions().size());
95 assertEquals(subscription, filterEngine.getListedSubscriptions().get(0));
96 assertTrue(subscription.isListed());
97 subscription.removeFromList();
98 assertEquals(0, filterEngine.getListedSubscriptions().size());
99 assertFalse(subscription.isListed());
100 subscription.removeFromList();
101 assertEquals(0, filterEngine.getListedSubscriptions().size());
102 assertFalse(subscription.isListed());
103 }
104
105 @Test
106 public void testSubscriptionUpdates()
107 {
108 Subscription subscription = filterEngine.getSubscription("foo");
109 assertFalse(subscription.isUpdating());
110 subscription.updateFilters();
111 }
112
113 @Test
114 public void testMatches()
115 {
116 filterEngine.getFilter("adbanner.gif").addToList();
117 filterEngine.getFilter("@@notbanner.gif").addToList();
118 filterEngine.getFilter("tpbanner.gif$third-party").addToList();
119 filterEngine.getFilter("fpbanner.gif$~third-party").addToList();
120 filterEngine.getFilter("combanner.gif$domain=example.com").addToList();
121 filterEngine.getFilter("orgbanner.gif$domain=~example.com").addToList();
122
123 Filter match1 = filterEngine.matches(
124 "http://example.org/foobar.gif",
125 FilterEngine.ContentType.IMAGE,
126 "");
127 assertNull(match1);
128
129 Filter match2 = filterEngine.matches(
130 "http://example.org/adbanner.gif",
131 FilterEngine.ContentType.IMAGE,
132 "");
133 assertNotNull(match2);
134 assertEquals(Filter.Type.BLOCKING, match2.getType());
135
136 Filter match3 = filterEngine.matches(
137 "http://example.org/notbanner.gif",
138 FilterEngine.ContentType.IMAGE,
139 "");
140 assertNotNull(match3);
141 assertEquals(Filter.Type.EXCEPTION, match3.getType());
142
143 Filter match4 = filterEngine.matches(
144 "http://example.org/notbanner.gif",
145 FilterEngine.ContentType.IMAGE, "");
146 assertNotNull(match4);
147 assertEquals(Filter.Type.EXCEPTION, match4.getType());
148
149 Filter match5 = filterEngine.matches(
150 "http://example.org/tpbanner.gif",
151 FilterEngine.ContentType.IMAGE,
152 "http://example.org/");
153 assertNull(match5);
154
155 Filter match6 = filterEngine.matches(
156 "http://example.org/fpbanner.gif",
157 FilterEngine.ContentType.IMAGE,
158 "http://example.org/");
159 assertNotNull(match6);
160 assertEquals(Filter.Type.BLOCKING, match6.getType());
161
162 Filter match7 = filterEngine.matches(
163 "http://example.org/tpbanner.gif",
164 FilterEngine.ContentType.IMAGE,
165 "http://example.com/");
166 assertNotNull(match7);
167 assertEquals(Filter.Type.BLOCKING, match7.getType());
168
169 Filter match8 = filterEngine.matches(
170 "http://example.org/fpbanner.gif",
171 FilterEngine.ContentType.IMAGE,
172 "http://example.com/");
173 assertNull(match8);
174
175 Filter match9 = filterEngine.matches(
176 "http://example.org/combanner.gif",
177 FilterEngine.ContentType.IMAGE,
178 "http://example.com/");
179 assertNotNull(match9);
180 assertEquals(Filter.Type.BLOCKING, match9.getType());
181
182 Filter match10 = filterEngine.matches(
183 "http://example.org/combanner.gif",
184 FilterEngine.ContentType.IMAGE,
185 "http://example.org/");
186 assertNull(match10);
187
188 Filter match11 = filterEngine.matches(
189 "http://example.org/orgbanner.gif",
190 FilterEngine.ContentType.IMAGE,
191 "http://example.com/");
192 assertNull(match11);
193
194 Filter match12 = filterEngine.matches(
195 "http://example.org/orgbanner.gif",
196 FilterEngine.ContentType.IMAGE,
197 "http://example.org/");
198 assertNotNull(match12);
199 assertEquals(Filter.Type.BLOCKING, match12.getType());
200 }
201
202 @Test
203 public void testMatchesOnWhitelistedDomain()
204 {
205 filterEngine.getFilter("adbanner.gif").addToList();
206 filterEngine.getFilter("@@||example.org^$document").addToList();
207
208 Filter match1 = filterEngine.matches(
209 "http://ads.com/adbanner.gif",
210 FilterEngine.ContentType.IMAGE,
211 "http://example.com/");
212 assertNotNull(match1);
213 assertEquals(Filter.Type.BLOCKING, match1.getType());
214
215 Filter match2 = filterEngine.matches(
216 "http://ads.com/adbanner.gif",
217 FilterEngine.ContentType.IMAGE,
218 "http://example.org/");
219 assertNotNull(match2);
220 assertEquals(Filter.Type.EXCEPTION, match2.getType());
221 }
222
223 @Test
224 public void testMatchesNestedFrameRequest()
225 {
226 filterEngine.getFilter("adbanner.gif").addToList();
227 filterEngine.getFilter("@@adbanner.gif$domain=example.org").addToList();
228
229 Filter match1 = filterEngine.matches(
230 "http://ads.com/adbanner.gif",
231 FilterEngine.ContentType.IMAGE,
232 new String[]
233 {
234 "http://ads.com/frame/",
235 "http://example.com/"
236 });
237 assertNotNull(match1);
238 assertEquals(Filter.Type.BLOCKING, match1.getType());
239
240 Filter match2 = filterEngine.matches(
241 "http://ads.com/adbanner.gif",
242 FilterEngine.ContentType.IMAGE,
243 new String[]
244 {
245 "http://ads.com/frame/",
246 "http://example.org/"
247 });
248 assertNotNull(match2);
249 assertEquals(Filter.Type.EXCEPTION, match2.getType());
250
251 Filter match3 = filterEngine.matches(
252 "http://ads.com/adbanner.gif",
253 FilterEngine.ContentType.IMAGE,
254 new String[]
255 {
256 "http://example.org/",
257 "http://ads.com/frame/"
258 });
259 assertNotNull(match3);
260 assertEquals(Filter.Type.BLOCKING, match3.getType());
261 }
262
263 @Test
264 public void testMatchesNestedFrameOnWhitelistedDomain()
265 {
266 filterEngine.getFilter("adbanner.gif").addToList();
267 filterEngine.getFilter("@@||example.org^$document,domain=ads.com").addToList ();
268
269 Filter match1 = filterEngine.matches(
270 "http://ads.com/adbanner.gif",
271 FilterEngine.ContentType.IMAGE,
272 new String[]
273 {
274 "http://ads.com/frame/",
275 "http://example.com/"
276 });
277 assertNotNull(match1);
278 assertEquals(Filter.Type.BLOCKING, match1.getType());
279
280 Filter match2 = filterEngine.matches(
281 "http://ads.com/adbanner.gif",
282 FilterEngine.ContentType.IMAGE,
283 new String[]
284 {
285 "http://ads.com/frame/",
286 "http://example.org/"
287 });
288 assertNotNull(match2);
289 assertEquals(Filter.Type.EXCEPTION, match2.getType());
290
291 Filter match3 = filterEngine.matches(
292 "http://ads.com/adbanner.gif",
293 FilterEngine.ContentType.IMAGE,
294 new String[]
295 {
296 "http://example.org/"
297 });
298 assertNotNull(match3);
299 assertEquals(Filter.Type.BLOCKING, match3.getType());
300
301 Filter match4 = filterEngine.matches(
302 "http://ads.com/adbanner.gif",
303 FilterEngine.ContentType.IMAGE,
304 new String[]
305 {
306 "http://example.org/",
307 "http://ads.com/frame/"
308 });
309 assertNotNull(match4);
310 assertEquals(Filter.Type.BLOCKING, match4.getType());
311
312 Filter match5 = filterEngine.matches(
313 "http://ads.com/adbanner.gif",
314 FilterEngine.ContentType.IMAGE,
315 new String[]
316 {
317 "http://ads.com/frame/",
318 "http://example.org/",
319 "http://example.com/"
320 });
321 assertNotNull(match5);
322 assertEquals(Filter.Type.EXCEPTION, match5.getType());
323 }
324
325 @Test
326 public void testFirstRunFlag()
327 {
328 assertFalse(filterEngine.isFirstRun());
329 }
330
331 @Test
332 public void testSetRemoveFilterChangeCallback()
333 {
334 MockFilterChangeCallback mockFilterChangeCallback = new MockFilterChangeCall back(0);
335
336 filterEngine.setFilterChangeCallback(mockFilterChangeCallback);
337 filterEngine.getFilter("foo").addToList();
338 assertEquals(1, mockFilterChangeCallback.getTimesCalled());
339
340 filterEngine.removeFilterChangeCallback();
341 filterEngine.getFilter("foo").removeFromList();
342 assertEquals(1, mockFilterChangeCallback.getTimesCalled());
343 }
344
345 @Test
346 public void testDocumentWhitelisting()
347 {
348 filterEngine.getFilter("@@||example.org^$document").addToList();
349 filterEngine.getFilter("@@||example.com^$document,domain=example.de").addToL ist();
350
351 String[] emptyArray = new String[]
352 {
353 };
354
355 assertTrue(filterEngine.isDocumentWhitelisted("http://example.org", emptyArr ay));
356 assertFalse(filterEngine.isDocumentWhitelisted("http://example.co.uk", empty Array));
357 assertFalse(filterEngine.isDocumentWhitelisted("http://example.com", emptyAr ray));
358
359 String[] documentUrls1 = new String[]
360 {
361 "http://example.de"
362 };
363 assertTrue(filterEngine.isDocumentWhitelisted("http://example.com", document Urls1));
364 assertFalse(filterEngine.isDocumentWhitelisted("http://example.co.uk", docum entUrls1));
365 }
366
367 @Test
368 public void testElemhideWhitelisting()
369 {
370 filterEngine.getFilter("@@||example.org^$elemhide").addToList();
371 filterEngine.getFilter("@@||example.com^$elemhide,domain=example.de").addToL ist();
372
373 String[] emptyArray = new String[]
374 {
375 };
376
377 assertTrue(filterEngine.isElemhideWhitelisted("http://example.org", emptyArr ay));
378 assertFalse(filterEngine.isElemhideWhitelisted("http://example.co.uk", empty Array));
379 assertFalse(filterEngine.isElemhideWhitelisted("http://example.com", emptyAr ray));
380
381 String[] documentUrls1 = new String[]
382 {
383 "http://example.de"
384 };
385 assertTrue(filterEngine.isElemhideWhitelisted("http://example.com", document Urls1));
386 assertFalse(filterEngine.isElemhideWhitelisted("http://example.co.uk", docum entUrls1));
387 }
388 }
OLDNEW

Powered by Google App Engine
This is Rietveld