OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2017 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.FilterEngine; |
| 23 import org.adblockplus.libadblockplus.HeaderEntry; |
| 24 import org.adblockplus.libadblockplus.IsAllowedConnectionCallback; |
| 25 import org.adblockplus.libadblockplus.ServerResponse; |
| 26 import org.adblockplus.libadblockplus.Subscription; |
| 27 import org.adblockplus.libadblockplus.android.AndroidWebRequest; |
| 28 import org.junit.Test; |
| 29 |
| 30 import java.util.LinkedList; |
| 31 import java.util.List; |
| 32 |
| 33 public class IsAllowedConnectionCallbackTest extends BaseJsTest |
| 34 { |
| 35 private static final int UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS = 5 * 1000; // 5s |
| 36 |
| 37 private static final class TestRequest extends AndroidWebRequest |
| 38 { |
| 39 private List<String> urls = new LinkedList<String>(); |
| 40 |
| 41 public List<String> getUrls() |
| 42 { |
| 43 return urls; |
| 44 } |
| 45 |
| 46 @Override |
| 47 public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
| 48 { |
| 49 urls.add(url); |
| 50 return super.httpGET(url, headers); |
| 51 } |
| 52 } |
| 53 |
| 54 private static final class TestCallback extends IsAllowedConnectionCallback |
| 55 { |
| 56 private boolean result; |
| 57 private boolean invoked; |
| 58 private String connectionType; |
| 59 |
| 60 public boolean isResult() |
| 61 { |
| 62 return result; |
| 63 } |
| 64 |
| 65 public void setResult(boolean result) |
| 66 { |
| 67 this.result = result; |
| 68 } |
| 69 |
| 70 public String getConnectionType() |
| 71 { |
| 72 return connectionType; |
| 73 } |
| 74 |
| 75 public boolean isInvoked() |
| 76 { |
| 77 return invoked; |
| 78 } |
| 79 |
| 80 @Override |
| 81 public boolean isConnectionAllowed(String connectionType) |
| 82 { |
| 83 this.invoked = true; |
| 84 this.connectionType = connectionType; |
| 85 |
| 86 return result; |
| 87 } |
| 88 } |
| 89 |
| 90 private TestRequest request; |
| 91 private TestCallback callback; |
| 92 private FilterEngine filterEngine; |
| 93 |
| 94 @Override |
| 95 protected void setUp() throws Exception |
| 96 { |
| 97 super.setUp(); |
| 98 |
| 99 request = new TestRequest(); |
| 100 jsEngine.setWebRequest(request); |
| 101 callback = new TestCallback(); |
| 102 |
| 103 filterEngine = new FilterEngine(jsEngine, callback); |
| 104 } |
| 105 |
| 106 private void updateSubscriptions() |
| 107 { |
| 108 for (final Subscription s : this.filterEngine.getListedSubscriptions()) |
| 109 { |
| 110 try |
| 111 { |
| 112 s.updateFilters(); |
| 113 } |
| 114 finally |
| 115 { |
| 116 s.dispose(); |
| 117 } |
| 118 } |
| 119 } |
| 120 |
| 121 @Test |
| 122 public void testAllow() |
| 123 { |
| 124 final String allowedConnectionType = "wifi1"; |
| 125 filterEngine.setAllowedConnectionType(allowedConnectionType); |
| 126 callback.setResult(true); |
| 127 |
| 128 assertEquals(0, request.getUrls().size()); |
| 129 assertFalse(callback.isInvoked()); |
| 130 |
| 131 updateSubscriptions(); |
| 132 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); |
| 133 |
| 134 assertTrue(callback.isInvoked()); |
| 135 assertNotNull(callback.getConnectionType()); |
| 136 assertEquals(allowedConnectionType, callback.getConnectionType()); |
| 137 |
| 138 assertTrue(request.getUrls().size() > 0); |
| 139 } |
| 140 |
| 141 @Test |
| 142 public void testDeny() |
| 143 { |
| 144 final String allowedConnectionType = "wifi2"; |
| 145 filterEngine.setAllowedConnectionType(allowedConnectionType); |
| 146 |
| 147 callback.setResult(false); |
| 148 assertEquals(0, request.getUrls().size()); |
| 149 |
| 150 updateSubscriptions(); |
| 151 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS); |
| 152 |
| 153 assertTrue(callback.isInvoked()); |
| 154 assertNotNull(callback.getConnectionType()); |
| 155 assertEquals(allowedConnectionType, callback.getConnectionType()); |
| 156 |
| 157 assertEquals(0, request.getUrls().size()); |
| 158 } |
| 159 } |
OLD | NEW |