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

Unified Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestResourceWrapperTest.java

Issue 29411633: Issue 5136 - Add test for AndroidWebRequestResourceWrapper (Closed)
Patch Set: Created April 13, 2017, 1:40 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestResourceWrapperTest.java
diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestResourceWrapperTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestResourceWrapperTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad0df91436813f455f59ba6f108dd0e6a6bf33e4
--- /dev/null
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidWebRequestResourceWrapperTest.java
@@ -0,0 +1,332 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.libadblockplus.tests;
+
+import android.os.SystemClock;
+
+import org.adblockplus.libadblockplus.HeaderEntry;
+import org.adblockplus.libadblockplus.ServerResponse;
+import org.adblockplus.libadblockplus.Subscription;
+import org.adblockplus.libadblockplus.android.AndroidWebRequest;
+import org.adblockplus.libadblockplus.android.AndroidWebRequestResourceWrapper;
+import org.adblockplus.libadblockplus.android.Utils;
+import org.adblockplus.libadblockplus.tests.test.R;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+public class AndroidWebRequestResourceWrapperTest extends FilterEngineGenericTest
+{
+ private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s
+
+ private class TestRequest extends AndroidWebRequest
diegocarloslima 2017/04/27 18:57:46 This inner class can be static
anton 2017/04/28 06:04:48 Acknowledged. Also it can be final. Taking into ac
anton 2017/04/28 06:10:35 uploaded new patch set
+ {
+ private List<String> urls = new LinkedList<String>();
+
+ public List<String> getUrls()
+ {
+ return urls;
+ }
+
+ @Override
+ public ServerResponse httpGET(String urlStr, List<HeaderEntry> headers)
+ {
+ urls.add(urlStr);
+ return super.httpGET(urlStr, headers);
+ }
+ }
+
+ // in-memory storage for testing only
+ private class TestStorage implements AndroidWebRequestResourceWrapper.Storage
diegocarloslima 2017/04/27 18:57:46 This inner class can be static
anton 2017/04/28 06:04:49 Acknowledged.
+ {
+ private Set<String> interceptedUrls = new HashSet<String>();
+
+ public Set<String> getInterceptedUrls()
+ {
+ return interceptedUrls;
+ }
+
+ @Override
+ public synchronized void put(String url)
+ {
+ interceptedUrls.add(url);
+ }
+
+ @Override
+ public synchronized boolean contains(String url)
+ {
+ return interceptedUrls.contains(url);
+ }
+ }
+
+ private class TestWrapperListener implements AndroidWebRequestResourceWrapper.Listener
diegocarloslima 2017/04/27 18:57:46 This inner class can be static
anton 2017/04/28 06:04:48 Acknowledged.
+ {
+ private Map<String, Integer> urlsToResourceId = new HashMap<String, Integer>();
+
+ public Map<String, Integer> getUrlsToResourceId()
+ {
+ return urlsToResourceId;
+ }
+
+ @Override
+ public void onIntercepted(String url, int resourceId)
+ {
+ urlsToResourceId.put(url, resourceId);
+ }
+ }
+
+ private TestRequest request;
+ private Map<String, Integer> preloadMap;
+ private TestStorage storage;
+ private AndroidWebRequestResourceWrapper wrapper;
+ private TestWrapperListener wrapperListener;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ request = new TestRequest();
+ preloadMap = new HashMap<String, Integer>();
+ storage = new TestStorage();
+ wrapper = new AndroidWebRequestResourceWrapper(
+ getInstrumentation().getContext(), request, preloadMap, storage);
+ wrapperListener = new TestWrapperListener();
+ wrapper.setListener(wrapperListener);
+
+ jsEngine.setWebRequest(wrapper);
+ }
+
+ private void updateSubscriptions()
anton 2017/04/13 13:51:41 actually it's copy-paste from AdblockEngine but it
diegocarloslima 2017/04/27 18:57:46 Yeah, since it's just for testing... I think you c
anton 2017/04/28 06:04:48 Acknowledged.
+ {
+ for (final Subscription s : this.filterEngine.getListedSubscriptions())
+ {
+ try
+ {
+ s.updateFilters();
+ }
+ finally
+ {
+ s.dispose();
+ }
+ }
+ }
+
+ private List<String> getUrlsListWithoutParams(Collection<String> urlWithParams)
+ {
+ List<String> list = new LinkedList<String>();
+ for (String eachUrl : urlWithParams)
+ {
+ list.add(Utils.getUrlWithoutParams(eachUrl));
+ }
+ return list;
+ }
+
+ private void testIntercepted(final String preloadUrl, final int resourceId)
+ {
+ preloadMap.clear();
+ preloadMap.put(preloadUrl, resourceId);
+
+ assertEquals(0, request.getUrls().size());
+
+ assertEquals(0, storage.getInterceptedUrls().size());
+
+ assertEquals(0, wrapperListener.getUrlsToResourceId().size());
+
+ updateSubscriptions();
+ SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
+
+ if (request.getUrls().size() > 0)
+ {
+ List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrls());
+ assertFalse(requestsWithoutParams.contains(preloadUrl));
+ }
+
+ assertEquals(1, storage.getInterceptedUrls().size());
+ assertTrue(storage.getInterceptedUrls().contains(preloadUrl));
+
+ assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0);
+ List<String> notifiedInterceptedUrls = getUrlsListWithoutParams(
+ wrapperListener.getUrlsToResourceId().keySet());
+ assertTrue(notifiedInterceptedUrls.contains(preloadUrl));
+
+ for (String eachString : wrapperListener.getUrlsToResourceId().keySet())
+ {
+ if (Utils.getUrlWithoutParams(eachString).equals(preloadUrl))
+ {
+ assertEquals(resourceId, wrapperListener.getUrlsToResourceId().get(eachString).intValue());
+ break;
+ }
+ }
+ }
+
+ @Test
+ public void testIntercepted_Easylist()
diegocarloslima 2017/04/27 18:57:46 Why there's this underscore on the method name? I
anton 2017/04/28 06:04:48 "testIntercepted" is a general test direction (for
diegocarloslima 2017/04/28 12:54:35 Acknowledged.
+ {
+ testIntercepted(
+ AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist);
+ }
+
+ @Test
+ public void testIntercepted_AcceptableAds()
diegocarloslima 2017/04/27 18:57:47 Same here, I think you should stick to the camelCa
anton 2017/04/28 06:04:48 Replied above
+ {
+ testIntercepted(
+ AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules);
+ }
+
+ @Test
+ public void testIntercepted_OnceOnly()
diegocarloslima 2017/04/27 18:57:46 Same here, I think you should stick to the camelCa
anton 2017/04/28 06:04:49 Replied above
+ {
+ final String preloadUrl = AndroidWebRequestResourceWrapper.EASYLIST;
+
+ preloadMap.clear();
+ preloadMap.put(preloadUrl, R.raw.easylist);
+
+ assertEquals(0, request.getUrls().size());
+
+ assertEquals(0, storage.getInterceptedUrls().size());
+
+ assertEquals(0, wrapperListener.getUrlsToResourceId().size());
+
+ // update #1 - should be intercepted
+ updateSubscriptions();
+ SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
+
+ int requestsCount = request.getUrls().size();
+ if (requestsCount > 0)
+ {
+ List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrls());
+ assertFalse(requestsWithoutParams.contains(preloadUrl));
+ }
+
+ assertEquals(1, storage.getInterceptedUrls().size());
+ assertTrue(storage.getInterceptedUrls().contains(preloadUrl));
+
+ assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0);
+ List<String> notifiedInterceptedUrls = getUrlsListWithoutParams(
+ wrapperListener.getUrlsToResourceId().keySet());
+ assertTrue(notifiedInterceptedUrls.contains(preloadUrl));
+
+ // update #2 - should NOT be intercepted but actually requested from the web
+ wrapperListener.getUrlsToResourceId().clear();
+
+ updateSubscriptions();
+ SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
+
+ assertTrue(request.getUrls().size() > requestsCount);
+ List<String> requestsWithoutParams = getUrlsListWithoutParams(request.getUrls());
+ assertTrue(requestsWithoutParams.contains(preloadUrl));
+
+ assertEquals(0, wrapperListener.getUrlsToResourceId().size());
+ }
+
+ private void testNotIntercepted(final String interceptedUrl, final int resourceId,
+ final String notInterceptedUrl)
+ {
+ preloadMap.clear();
+ preloadMap.put(interceptedUrl, resourceId);
+
+ assertEquals(0, request.getUrls().size());
+ assertEquals(0, storage.getInterceptedUrls().size());
+ assertEquals(0, wrapperListener.getUrlsToResourceId().size());
+
+ updateSubscriptions();
+ SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
+
+ assertEquals(1, request.getUrls().size());
+ List<String> requestUrlsWithoutParams = getUrlsListWithoutParams(request.getUrls());
+ assertFalse(requestUrlsWithoutParams.contains(interceptedUrl));
+ assertTrue(requestUrlsWithoutParams.contains(notInterceptedUrl));
+ assertEquals(1, storage.getInterceptedUrls().size());
+ assertTrue(storage.getInterceptedUrls().contains(interceptedUrl));
+ assertFalse(storage.getInterceptedUrls().contains(notInterceptedUrl));
+ assertTrue(wrapperListener.getUrlsToResourceId().size() > 0);
+
+ for (String eachString : wrapperListener.getUrlsToResourceId().keySet())
+ {
+ if (Utils.getUrlWithoutParams(eachString).equals(notInterceptedUrl))
+ {
+ fail();
+ }
+ }
+ }
+
+ @Test
+ public void testInterceptedAll()
+ {
+ preloadMap.clear();
+ preloadMap.put(AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist);
+ preloadMap.put(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules);
+
+ assertEquals(0, request.getUrls().size());
+
+ assertEquals(0, storage.getInterceptedUrls().size());
+
+ assertEquals(0, wrapperListener.getUrlsToResourceId().size());
+
+ updateSubscriptions();
+ SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS);
+
+ assertEquals(0, request.getUrls().size());
+ assertEquals(2, storage.getInterceptedUrls().size());
+ assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWrapper.EASYLIST));
+ assertTrue(storage.getInterceptedUrls().contains(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS));
+
+ assertTrue(wrapperListener.getUrlsToResourceId().size() >= 0);
+ List<String> notifiedInterceptedUrls = getUrlsListWithoutParams(
+ wrapperListener.getUrlsToResourceId().keySet());
+ assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper.EASYLIST));
+ assertTrue(notifiedInterceptedUrls.contains(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS));
+
+ for (String eachString : wrapperListener.getUrlsToResourceId().keySet())
+ {
+ String urlWithoutParams = Utils.getUrlWithoutParams(eachString);
+ if (urlWithoutParams.equals(AndroidWebRequestResourceWrapper.EASYLIST))
+ {
+ assertEquals(R.raw.easylist, wrapperListener.getUrlsToResourceId().get(eachString).intValue());
+ }
+
+ if (urlWithoutParams.equals(AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS))
+ {
+ assertEquals(R.raw.exceptionrules, wrapperListener.getUrlsToResourceId().get(eachString).intValue());
+ }
+ }
+ }
+
+ @Test
+ public void testNotIntercepted_Easylist()
+ {
+ testNotIntercepted(
+ AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS, R.raw.exceptionrules,
+ AndroidWebRequestResourceWrapper.EASYLIST);
+ }
+
+ @Test
+ public void testNotIntercepted_AcceptableAds()
+ {
+ testNotIntercepted(
+ AndroidWebRequestResourceWrapper.EASYLIST, R.raw.easylist,
+ AndroidWebRequestResourceWrapper.ACCEPTABLE_ADS);
+ }
+}

Powered by Google App Engine
This is Rietveld