| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present 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.os.SystemClock; | |
| 21 | |
| 22 import org.adblockplus.libadblockplus.HeaderEntry; | |
| 23 import org.adblockplus.libadblockplus.ServerResponse; | |
| 24 import org.adblockplus.libadblockplus.Subscription; | |
| 25 import org.adblockplus.libadblockplus.WebRequest; | |
| 26 import org.adblockplus.libadblockplus.android.AndroidWebRequest; | |
| 27 import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper; | |
| 28 import org.adblockplus.libadblockplus.android.Utils; | |
| 29 import org.adblockplus.libadblockplus.tests.test.R; | |
| 30 import org.junit.Test; | |
| 31 | |
| 32 import java.util.Collection; | |
| 33 import java.util.HashMap; | |
| 34 import java.util.HashSet; | |
| 35 import java.util.LinkedList; | |
| 36 import java.util.List; | |
| 37 import java.util.Map; | |
| 38 import java.util.Set; | |
| 39 | |
| 40 public class AndroidWebRequestResourceWrapperTest extends BaseFilterEngineTest | |
| 41 { | |
| 42 private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s | |
| 43 | |
| 44 private static final class TestRequest extends AndroidWebRequest | |
| 45 { | |
| 46 private List<String> urls = new LinkedList<String>(); | |
| 47 | |
| 48 public List<String> getUrls() | |
| 49 { | |
| 50 return urls; | |
| 51 } | |
| 52 | |
| 53 @Override | |
| 54 public ServerResponse httpGET(String urlStr, List<HeaderEntry> headers) | |
| 55 { | |
| 56 urls.add(urlStr); | |
| 57 return super.httpGET(urlStr, headers); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 // in-memory storage for testing only | |
| 62 private static final class TestStorage implements AndroidWebRequestResourceWra
pper.Storage | |
| 63 { | |
| 64 private Set<String> interceptedUrls = new HashSet<String>(); | |
| 65 | |
| 66 public Set<String> getInterceptedUrls() | |
| 67 { | |
| 68 return interceptedUrls; | |
| 69 } | |
| 70 | |
| 71 @Override | |
| 72 public synchronized void put(String url) | |
| 73 { | |
| 74 interceptedUrls.add(url); | |
| 75 } | |
| 76 | |
| 77 @Override | |
| 78 public synchronized boolean contains(String url) | |
| 79 { | |
| 80 return interceptedUrls.contains(url); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 private static final class TestWrapperListener implements AndroidWebRequestRes
ourceWrapper.Listener | |
| 85 { | |
| 86 private Map<String, Integer> urlsToResourceId = new HashMap<String, Integer>
(); | |
| 87 | |
| 88 public Map<String, Integer> getUrlsToResourceId() | |
| 89 { | |
| 90 return urlsToResourceId; | |
| 91 } | |
| 92 | |
| 93 @Override | |
| 94 public void onIntercepted(String url, int resourceId) | |
| 95 { | |
| 96 urlsToResourceId.put(url, resourceId); | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 private TestRequest request; | |
| 101 private Map<String, Integer> preloadMap; | |
| 102 private TestStorage storage; | |
| 103 private AndroidWebRequestResourceWrapper wrapper; | |
| 104 private TestWrapperListener wrapperListener; | |
| 105 | |
| 106 @Override | |
| 107 protected void setUp() throws Exception | |
| 108 { | |
| 109 request = new TestRequest(); | |
| 110 preloadMap = new HashMap<String, Integer>(); | |
| 111 storage = new TestStorage(); | |
| 112 wrapper = new AndroidWebRequestResourceWrapper( | |
| 113 getInstrumentation().getContext(), request, preloadMap, storage); | |
| 114 wrapperListener = new TestWrapperListener(); | |
| 115 wrapper.setListener(wrapperListener); | |
| 116 | |
| 117 super.setUp(); | |
| 118 } | |
| 119 | |
| 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
) | |
| 142 { | |
| 143 List<String> list = new LinkedList<String>(); | |
| 144 for (String eachUrl : urlWithParams) | |
| 145 { | |
| 146 list.add(Utils.getUrlWithoutParams(eachUrl)); | |
| 147 } | |
| 148 return list; | |
| 149 } | |
| 150 | |
| 151 private void testIntercepted(final String preloadUrl, final int resourceId) | |
| 152 { | |
| 153 preloadMap.clear(); | |
| 154 preloadMap.put(preloadUrl, resourceId); | |
| 155 | |
| 156 assertEquals(0, request.getUrls().size()); | |
| 157 | |
| 158 assertEquals(0, storage.getInterceptedUrls().size()); | |
| 159 | |
| 160 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); | |
| 161 | |
| 162 updateSubscriptions(); | |
| 163 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); | |
| 164 | |
| 165 if (request.getUrls().size() > 0) | |
| 166 { | |
| 167 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU
rls()); | |
| 168 assertFalse(requestsWithoutParams.contains(preloadUrl)); | |
| 169 } | |
| 170 | |
| 171 assertEquals(1, storage.getInterceptedUrls().size()); | |
| 172 assertTrue(storage.getInterceptedUrls().contains(preloadUrl)); | |
| 173 | |
| 174 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0); | |
| 175 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams( | |
| 176 wrapperListener.getUrlsToResourceId().keySet()); | |
| 177 assertTrue(notifiedInterceptedUrls.contains(preloadUrl)); | |
| 178 | |
| 179 for (String eachString : wrapperListener.getUrlsToResourceId().keySet()) | |
| 180 { | |
| 181 if (Utils.getUrlWithoutParams(eachString).equals(preloadUrl)) | |
| 182 { | |
| 183 assertEquals(resourceId, wrapperListener.getUrlsToResourceId().get(eachS
tring).intValue()); | |
| 184 break; | |
| 185 } | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 @Test | |
| 190 public void testIntercepted_Easylist() | |
| 191 { | |
| 192 testIntercepted( | |
| 193 AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist); | |
| 194 } | |
| 195 | |
| 196 @Test | |
| 197 public void testIntercepted_AcceptableAds() | |
| 198 { | |
| 199 testIntercepted( | |
| 200 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules); | |
| 201 } | |
| 202 | |
| 203 @Test | |
| 204 public void testIntercepted_OnceOnly() | |
| 205 { | |
| 206 final String preloadUrl = AndroidWebRequestResourceWrapper.EASYLIST; | |
| 207 | |
| 208 preloadMap.clear(); | |
| 209 preloadMap.put(preloadUrl, R.raw.easylist); | |
| 210 | |
| 211 assertEquals(0, request.getUrls().size()); | |
| 212 | |
| 213 assertEquals(0, storage.getInterceptedUrls().size()); | |
| 214 | |
| 215 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); | |
| 216 | |
| 217 // update #1 - should be intercepted | |
| 218 updateSubscriptions(); | |
| 219 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); | |
| 220 | |
| 221 int requestsCount = request.getUrls().size(); | |
| 222 if (requestsCount > 0) | |
| 223 { | |
| 224 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getU
rls()); | |
| 225 assertFalse(requestsWithoutParams.contains(preloadUrl)); | |
| 226 } | |
| 227 | |
| 228 assertEquals(1, storage.getInterceptedUrls().size()); | |
| 229 assertTrue(storage.getInterceptedUrls().contains(preloadUrl)); | |
| 230 | |
| 231 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0); | |
| 232 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams( | |
| 233 wrapperListener.getUrlsToResourceId().keySet()); | |
| 234 assertTrue(notifiedInterceptedUrls.contains(preloadUrl)); | |
| 235 | |
| 236 // update #2 - should NOT be intercepted but actually requested from the we
b | |
| 237 wrapperListener.getUrlsToResourceId().clear(); | |
| 238 | |
| 239 updateSubscriptions(); | |
| 240 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); | |
| 241 | |
| 242 assertTrue(request.getUrls().size() > requestsCount); | |
| 243 List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrl
s()); | |
| 244 assertTrue(requestsWithoutParams.contains(preloadUrl)); | |
| 245 | |
| 246 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); | |
| 247 } | |
| 248 | |
| 249 private void testNotIntercepted(final String interceptedUrl, final int resourc
eId, | |
| 250 final String notInterceptedUrl) | |
| 251 { | |
| 252 preloadMap.clear(); | |
| 253 preloadMap.put(interceptedUrl, resourceId); | |
| 254 | |
| 255 assertEquals(0, request.getUrls().size()); | |
| 256 assertEquals(0, storage.getInterceptedUrls().size()); | |
| 257 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); | |
| 258 | |
| 259 updateSubscriptions(); | |
| 260 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); | |
| 261 | |
| 262 assertEquals(1, request.getUrls().size()); | |
| 263 List<String> requestUrlsWithoutParams = getUrlsListWithoutParams(request.get
Urls()); | |
| 264 assertFalse(requestUrlsWithoutParams.contains(interceptedUrl)); | |
| 265 assertTrue(requestUrlsWithoutParams.contains(notInterceptedUrl)); | |
| 266 assertEquals(1, storage.getInterceptedUrls().size()); | |
| 267 assertTrue(storage.getInterceptedUrls().contains(interceptedUrl)); | |
| 268 assertFalse(storage.getInterceptedUrls().contains(notInterceptedUrl)); | |
| 269 assertTrue(wrapperListener.getUrlsToResourceId().size() > 0); | |
| 270 | |
| 271 for (String eachString : wrapperListener.getUrlsToResourceId().keySet()) | |
| 272 { | |
| 273 if (Utils.getUrlWithoutParams(eachString).equals(notInterceptedUrl)) | |
| 274 { | |
| 275 fail(); | |
| 276 } | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 @Test | |
| 281 public void testInterceptedAll() | |
| 282 { | |
| 283 preloadMap.clear(); | |
| 284 preloadMap.put(AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist); | |
| 285 preloadMap.put(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.except
ionrules); | |
| 286 | |
| 287 assertEquals(0, request.getUrls().size()); | |
| 288 | |
| 289 assertEquals(0, storage.getInterceptedUrls().size()); | |
| 290 | |
| 291 assertEquals(0, wrapperListener.getUrlsToResourceId().size()); | |
| 292 | |
| 293 updateSubscriptions(); | |
| 294 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); | |
| 295 | |
| 296 assertEquals(0, request.getUrls().size()); | |
| 297 assertEquals(2, storage.getInterceptedUrls().size()); | |
| 298 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr
apper.EASYLIST)); | |
| 299 assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWr
apper.ACCEPTABLE_ADS)); | |
| 300 | |
| 301 assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0); | |
| 302 List<String> notifiedInterceptedUrls = getUrlsListWithoutParams( | |
| 303 wrapperListener.getUrlsToResourceId().keySet()); | |
| 304 assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper
.EASYLIST)); | |
| 305 assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper
.ACCEPTABLE_ADS)); | |
| 306 | |
| 307 for (String eachString : wrapperListener.getUrlsToResourceId().keySet()) | |
| 308 { | |
| 309 String urlWithoutParams = Utils.getUrlWithoutParams(eachString); | |
| 310 if (urlWithoutParams.equals(AndroidWebRequestResourceWrapper.EASYLIST)) | |
| 311 { | |
| 312 assertEquals(R.raw.easylist, wrapperListener.getUrlsToResourceId().get(e
achString).intValue()); | |
| 313 } | |
| 314 | |
| 315 if (urlWithoutParams.equals(AndroidWebRequestResourceWrapper.ACCEPTABLE_AD
S)) | |
| 316 { | |
| 317 assertEquals(R.raw.exceptionrules, wrapperListener.getUrlsToResourceId()
.get(eachString).intValue()); | |
| 318 } | |
| 319 } | |
| 320 } | |
| 321 | |
| 322 @Test | |
| 323 public void testNotIntercepted_Easylist() | |
| 324 { | |
| 325 testNotIntercepted( | |
| 326 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules, | |
| 327 AndroidWebRequestResourceWrapper.EASYLIST); | |
| 328 } | |
| 329 | |
| 330 @Test | |
| 331 public void testNotIntercepted_AcceptableAds() | |
| 332 { | |
| 333 testNotIntercepted( | |
| 334 AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist, | |
| 335 AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS); | |
| 336 } | |
| 337 } | |
| OLD | NEW |