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