| Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/IsAllowedConnectionCallbackTest.java |
| diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/IsAllowedConnectionCallbackTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/IsAllowedConnectionCallbackTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6b1c83b1f7048e42ec3eec1edfd1cd4ae7a85fc9 |
| --- /dev/null |
| +++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/IsAllowedConnectionCallbackTest.java |
| @@ -0,0 +1,159 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-2017 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.FilterEngine; |
| +import org.adblockplus.libadblockplus.HeaderEntry; |
| +import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
| +import org.adblockplus.libadblockplus.ServerResponse; |
| +import org.adblockplus.libadblockplus.Subscription; |
| +import org.adblockplus.libadblockplus.android.AndroidWebRequest; |
| +import org.junit.Test; |
| + |
| +import java.util.LinkedList; |
| +import java.util.List; |
| + |
| +public class IsAllowedConnectionCallbackTest extends BaseJsTest |
| +{ |
| + private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s |
| + |
| + private class TestRequest extends AndroidWebRequest |
|
diegocarloslima
2017/04/27 19:04:58
This inner class can be static
anton
2017/04/28 06:16:23
not sure this is a significant and really required
|
| + { |
| + private List<String> urls = new LinkedList<String>(); |
| + |
| + public List<String> getUrls() |
| + { |
| + return urls; |
| + } |
| + |
| + @Override |
| + public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
| + { |
| + urls.add(url); |
| + return super.httpGET(url, headers); |
| + } |
| + } |
| + |
| + private class TestCallback extends IsAllowedConnectionCallback |
|
diegocarloslima
2017/04/27 19:04:58
This inner class can be static
anton
2017/04/28 06:16:23
Acknowledged.
|
| + { |
| + private boolean result; |
| + private boolean invoked; |
| + private String connectionType; |
| + |
| + public boolean isResult() |
| + { |
| + return result; |
| + } |
| + |
| + public void setResult(boolean result) |
| + { |
| + this.result = result; |
| + } |
| + |
| + public String getConnectionType() |
| + { |
| + return connectionType; |
| + } |
| + |
| + public boolean isInvoked() |
| + { |
| + return invoked; |
| + } |
| + |
| + @Override |
| + public boolean isConnectionAllowed(String connectionType) |
| + { |
| + this.invoked = true; |
| + this.connectionType = connectionType; |
| + |
| + return result; |
| + } |
| + } |
| + |
| + private TestRequest request; |
| + private TestCallback callback; |
| + private FilterEngine filterEngine; |
| + |
| + @Override |
| + protected void setUp() throws Exception |
| + { |
| + super.setUp(); |
| + |
| + request = new TestRequest(); |
| + jsEngine.setWebRequest(request); |
| + callback = new TestCallback(); |
| + |
| + filterEngine = new FilterEngine(jsEngine, callback); |
| + } |
| + |
| + private void updateSubscriptions() |
| + { |
| + for (final Subscription s : this.filterEngine.getListedSubscriptions()) |
| + { |
| + try |
| + { |
| + s.updateFilters(); |
| + } |
| + finally |
| + { |
| + s.dispose(); |
| + } |
| + } |
| + } |
| + |
| + @Test |
| + public void testAllow() |
| + { |
| + final String allowedConnectionType = "wifi1"; |
| + filterEngine.setAllowedConnectionType(allowedConnectionType); |
| + callback.setResult(true); |
| + |
| + assertEquals(0, request.getUrls().size()); |
| + assertFalse(callback.isInvoked()); |
| + |
| + updateSubscriptions(); |
| + SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); |
| + |
| + assertTrue(callback.isInvoked()); |
| + assertNotNull(callback.getConnectionType()); |
| + assertEquals(allowedConnectionType, callback.getConnectionType()); |
| + |
| + assertTrue(request.getUrls().size() > 0); |
| + } |
| + |
| + @Test |
| + public void testDeny() |
| + { |
| + final String allowedConnectionType = "wifi2"; |
| + filterEngine.setAllowedConnectionType(allowedConnectionType); |
| + |
| + callback.setResult(false); |
| + assertEquals(0, request.getUrls().size()); |
| + |
| + updateSubscriptions(); |
| + SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); |
| + |
| + assertTrue(callback.isInvoked()); |
| + assertNotNull(callback.getConnectionType()); |
| + assertEquals(allowedConnectionType, callback.getConnectionType()); |
| + |
| + assertEquals(0, request.getUrls().size()); |
| + } |
| +} |