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