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

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

Issue 29819555: Fix libadblockplus-android tests
Patch Set: Fixed indentation issues. Created Aug. 27, 2018, 8:03 p.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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.tests; 18 package org.adblockplus.libadblockplus.tests;
19 19
20 import android.os.SystemClock; 20 import org.adblockplus.libadblockplus.BaseFilterEngineTest;
21
22 import org.adblockplus.libadblockplus.HeaderEntry; 21 import org.adblockplus.libadblockplus.HeaderEntry;
23 import org.adblockplus.libadblockplus.ServerResponse; 22 import org.adblockplus.libadblockplus.ServerResponse;
24 import org.adblockplus.libadblockplus.Subscription; 23 import org.adblockplus.libadblockplus.ThrowingWebRequest;
25 import org.adblockplus.libadblockplus.WebRequest;
26 import org.adblockplus.libadblockplus.android.AndroidWebRequest;
27 import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper; 24 import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper;
28 import org.adblockplus.libadblockplus.android.Utils; 25 import org.adblockplus.libadblockplus.android.Utils;
29 import org.adblockplus.libadblockplus.tests.test.R; 26 import org.adblockplus.libadblockplus.tests.test.R;
30 import org.junit.Test; 27 import org.junit.Test;
31 28
32 import java.util.Collection; 29 import java.util.Collection;
33 import java.util.HashMap; 30 import java.util.HashMap;
34 import java.util.HashSet; 31 import java.util.HashSet;
35 import java.util.LinkedList; 32 import java.util.LinkedList;
36 import java.util.List; 33 import java.util.List;
37 import java.util.Map; 34 import java.util.Map;
38 import java.util.Set; 35 import java.util.Set;
39 36
40 public class AndroidWebRequestResourceWrapperTest extends BaseFilterEngineTest 37 public class AndroidWebRequestResourceWrapperTest extends BaseFilterEngineTest
41 { 38 {
42 private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s 39 private static final class TestRequest extends ThrowingWebRequest
43
44 private static final class TestRequest extends AndroidWebRequest
45 { 40 {
46 private List<String> urls = new LinkedList<String>(); 41 private List<String> urls = new LinkedList<String>();
47 42
48 public List<String> getUrls() 43 public List<String> getUrls()
49 { 44 {
50 return urls; 45 return urls;
51 } 46 }
52 47
53 @Override 48 @Override
54 public ServerResponse httpGET(String urlStr, List<HeaderEntry> headers) 49 public ServerResponse httpGET(String urlStr, List<HeaderEntry> headers)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 protected void setUp() throws Exception 102 protected void setUp() throws Exception
108 { 103 {
109 request = new TestRequest(); 104 request = new TestRequest();
110 preloadMap = new HashMap<String, Integer>(); 105 preloadMap = new HashMap<String, Integer>();
111 storage = new TestStorage(); 106 storage = new TestStorage();
112 wrapper = new AndroidWebRequestResourceWrapper( 107 wrapper = new AndroidWebRequestResourceWrapper(
113 getInstrumentation().getContext(), request, preloadMap, storage); 108 getInstrumentation().getContext(), request, preloadMap, storage);
114 wrapperListener = new TestWrapperListener(); 109 wrapperListener = new TestWrapperListener();
115 wrapper.setListener(wrapperListener); 110 wrapper.setListener(wrapperListener);
116 111
112 setWebRequest(wrapper);
113
117 super.setUp(); 114 super.setUp();
118 } 115 }
119 116
120 @Override
121 protected WebRequest createWebRequest()
122 {
123 return wrapper;
124 }
125
126 private void updateSubscriptions()
127 {
128 for (final Subscription s : this.filterEngine.getListedSubscriptions())
129 {
130 try
131 {
132 s.updateFilters();
133 }
134 finally
135 {
136 s.dispose();
137 }
138 }
139 }
140
141 private List<String> getUrlsListWithoutParams(Collection<String> urlWithParams ) 117 private List<String> getUrlsListWithoutParams(Collection<String> urlWithParams )
142 { 118 {
143 List<String> list = new LinkedList<String>(); 119 List<String> list = new LinkedList<String>();
144 for (String eachUrl : urlWithParams) 120 for (String eachUrl : urlWithParams)
145 { 121 {
146 list.add(Utils.getUrlWithoutParams(eachUrl)); 122 list.add(Utils.getUrlWithoutParams(eachUrl));
147 } 123 }
148 return list; 124 return list;
149 } 125 }
150 126
127 private void reset()
128 {
129 preloadMap.clear();
130 request.getUrls().clear();
131 storage.getInterceptedUrls().clear();
132 wrapperListener.getUrlsToResourceId().clear();
133 }
134
135 @Override
136 protected int getUpdateRequestCount()
137 {
138 return request.getUrls().size() + storage.getInterceptedUrls().size();
139 }
140
151 private void testIntercepted(final String preloadUrl, final int resourceId) 141 private void testIntercepted(final String preloadUrl, final int resourceId)
152 { 142 {
153 preloadMap.clear(); 143 reset();
154 preloadMap.put(preloadUrl, resourceId); 144 preloadMap.put(preloadUrl, resourceId);
155
156 assertEquals(0, request.getUrls().size()); 145 assertEquals(0, request.getUrls().size());
157
158 assertEquals(0, storage.getInterceptedUrls().size()); 146 assertEquals(0, storage.getInterceptedUrls().size());
159
160 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); 147 assertEquals(0, wrapperListener.getUrlsToResourceId().size());
161 148
162 updateSubscriptions(); 149 updateSubscriptions();
163 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
164 150
165 if (request.getUrls().size() > 0) 151 if (request.getUrls().size() > 0)
166 { 152 {
167 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU rls()); 153 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU rls());
168 assertFalse(requestsWithoutParams.contains(preloadUrl)); 154 assertFalse(requestsWithoutParams.contains(preloadUrl));
169 } 155 }
170 156
171 assertEquals(1, storage.getInterceptedUrls().size()); 157 assertEquals(1, storage.getInterceptedUrls().size());
172 assertTrue(storage.getInterceptedUrls().contains(preloadUrl)); 158 assertTrue(storage.getInterceptedUrls().contains(preloadUrl));
173 159
(...skipping 24 matching lines...) Expand all
198 { 184 {
199 testIntercepted( 185 testIntercepted(
200 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules); 186 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules);
201 } 187 }
202 188
203 @Test 189 @Test
204 public void testIntercepted_OnceOnly() 190 public void testIntercepted_OnceOnly()
205 { 191 {
206 final String preloadUrl = AndroidWebRequestResourceWrapper.EASYLIST; 192 final String preloadUrl = AndroidWebRequestResourceWrapper.EASYLIST;
207 193
208 preloadMap.clear(); 194 reset();
209 preloadMap.put(preloadUrl, R.raw.easylist); 195 preloadMap.put(preloadUrl, R.raw.easylist);
210
211 assertEquals(0, request.getUrls().size()); 196 assertEquals(0, request.getUrls().size());
212
213 assertEquals(0, storage.getInterceptedUrls().size()); 197 assertEquals(0, storage.getInterceptedUrls().size());
214
215 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); 198 assertEquals(0, wrapperListener.getUrlsToResourceId().size());
216 199
217 // update #1 - should be intercepted 200 // update #1 - should be intercepted
218 updateSubscriptions(); 201 updateSubscriptions();
219 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
220 202
221 int requestsCount = request.getUrls().size(); 203 int requestsCount = request.getUrls().size();
222 if (requestsCount > 0) 204 if (requestsCount > 0)
223 { 205 {
224 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU rls()); 206 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU rls());
225 assertFalse(requestsWithoutParams.contains(preloadUrl)); 207 assertFalse(requestsWithoutParams.contains(preloadUrl));
226 } 208 }
227 209
228 assertEquals(1, storage.getInterceptedUrls().size()); 210 assertEquals(1, storage.getInterceptedUrls().size());
229 assertTrue(storage.getInterceptedUrls().contains(preloadUrl)); 211 assertTrue(storage.getInterceptedUrls().contains(preloadUrl));
230 212
231 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0); 213 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0);
232 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams( 214 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams(
233 wrapperListener.getUrlsToResourceId().keySet()); 215 wrapperListener.getUrlsToResourceId().keySet());
234 assertTrue(notifiedInterceptedUrls.contains(preloadUrl)); 216 assertTrue(notifiedInterceptedUrls.contains(preloadUrl));
235 217
236 // update #2 - should NOT be intercepted but actually requested from the we b 218 // update #2 - should NOT be intercepted but actually requested from the we b
237 wrapperListener.getUrlsToResourceId().clear(); 219 wrapperListener.getUrlsToResourceId().clear();
238 220
239 updateSubscriptions(); 221 updateSubscriptions();
240 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
241 222
242 assertTrue(request.getUrls().size() > requestsCount); 223 assertTrue(request.getUrls().size() > requestsCount);
243 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrl s()); 224 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrl s());
244 assertTrue(requestsWithoutParams.contains(preloadUrl)); 225 assertTrue(requestsWithoutParams.contains(preloadUrl));
245 226
246 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); 227 assertEquals(0, wrapperListener.getUrlsToResourceId().size());
247 } 228 }
248 229
249 private void testNotIntercepted(final String interceptedUrl, final int resourc eId, 230 private void testNotIntercepted(final String interceptedUrl, final int resourc eId,
250 final String notInterceptedUrl) 231 final String notInterceptedUrl)
251 { 232 {
252 preloadMap.clear(); 233 reset();
253 preloadMap.put(interceptedUrl, resourceId); 234 preloadMap.put(interceptedUrl, resourceId);
254
255 assertEquals(0, request.getUrls().size()); 235 assertEquals(0, request.getUrls().size());
256 assertEquals(0, storage.getInterceptedUrls().size()); 236 assertEquals(0, storage.getInterceptedUrls().size());
257 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); 237 assertEquals(0, wrapperListener.getUrlsToResourceId().size());
258 238
259 updateSubscriptions(); 239 updateSubscriptions();
260 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
261 240
262 assertEquals(1, request.getUrls().size()); 241 assertEquals(1, request.getUrls().size());
263 List<String> requestUrlsWithoutParams = getUrlsListWithoutParams(request.get Urls()); 242 List<String> requestUrlsWithoutParams = getUrlsListWithoutParams(request.get Urls());
264 assertFalse(requestUrlsWithoutParams.contains(interceptedUrl)); 243 assertFalse(requestUrlsWithoutParams.contains(interceptedUrl));
265 assertTrue(requestUrlsWithoutParams.contains(notInterceptedUrl)); 244 assertTrue(requestUrlsWithoutParams.contains(notInterceptedUrl));
266 assertEquals(1, storage.getInterceptedUrls().size()); 245 assertEquals(1, storage.getInterceptedUrls().size());
267 assertTrue(storage.getInterceptedUrls().contains(interceptedUrl)); 246 assertTrue(storage.getInterceptedUrls().contains(interceptedUrl));
268 assertFalse(storage.getInterceptedUrls().contains(notInterceptedUrl)); 247 assertFalse(storage.getInterceptedUrls().contains(notInterceptedUrl));
269 assertTrue(wrapperListener.getUrlsToResourceId().size() > 0); 248 assertTrue(wrapperListener.getUrlsToResourceId().size() > 0);
270 249
271 for (String eachString : wrapperListener.getUrlsToResourceId().keySet()) 250 for (String eachString : wrapperListener.getUrlsToResourceId().keySet())
272 { 251 {
273 if (Utils.getUrlWithoutParams(eachString).equals(notInterceptedUrl)) 252 if (Utils.getUrlWithoutParams(eachString).equals(notInterceptedUrl))
274 { 253 {
275 fail(); 254 fail();
276 } 255 }
277 } 256 }
278 } 257 }
279 258
280 @Test 259 @Test
281 public void testInterceptedAll() 260 public void testInterceptedAll()
282 { 261 {
283 preloadMap.clear(); 262 reset();
284 preloadMap.put(AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist); 263 preloadMap.put(AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist);
285 preloadMap.put(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.except ionrules); 264 preloadMap.put(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.except ionrules);
286
287 assertEquals(0, request.getUrls().size()); 265 assertEquals(0, request.getUrls().size());
288
289 assertEquals(0, storage.getInterceptedUrls().size()); 266 assertEquals(0, storage.getInterceptedUrls().size());
290
291 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); 267 assertEquals(0, wrapperListener.getUrlsToResourceId().size());
292 268
293 updateSubscriptions(); 269 updateSubscriptions();
294 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
295 270
296 assertEquals(0, request.getUrls().size()); 271 assertEquals(0, request.getUrls().size());
297 assertEquals(2, storage.getInterceptedUrls().size()); 272 assertEquals(2, storage.getInterceptedUrls().size());
298 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr apper.EASYLIST)); 273 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr apper.EASYLIST));
299 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr apper.ACCEPTABLE_ADS)); 274 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr apper.ACCEPTABLE_ADS));
300 275
301 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0); 276 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0);
302 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams( 277 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams(
303 wrapperListener.getUrlsToResourceId().keySet()); 278 wrapperListener.getUrlsToResourceId().keySet());
304 assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper .EASYLIST)); 279 assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper .EASYLIST));
(...skipping 23 matching lines...) Expand all
328 } 303 }
329 304
330 @Test 305 @Test
331 public void testNotIntercepted_AcceptableAds() 306 public void testNotIntercepted_AcceptableAds()
332 { 307 {
333 testNotIntercepted( 308 testNotIntercepted(
334 AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist, 309 AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist,
335 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS); 310 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS);
336 } 311 }
337 } 312 }
OLDNEW

Powered by Google App Engine
This is Rietveld