| 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 package org.adblockplus.libadblockplus.tests; | |
| 18 | |
| 19 import android.net.ConnectivityManager; | |
| 20 import android.net.NetworkInfo; | |
| 21 import android.test.InstrumentationTestCase; | |
| 22 | |
| 23 import org.adblockplus.libadblockplus.android.ConnectionType; | |
| 24 import org.adblockplus.libadblockplus.android.IsAllowedConnectionCallbackImpl; | |
| 25 import org.junit.Test; | |
| 26 import org.junit.runner.RunWith; | |
| 27 import org.mockito.Mockito; | |
| 28 import org.mockito.internal.stubbing.answers.Returns; | |
| 29 import org.mockito.runners.MockitoJUnitRunner; | |
| 30 | |
| 31 @RunWith(MockitoJUnitRunner.class) | |
| 32 public class IsAllowedConnectionCallbackImplTest extends InstrumentationTestCase | |
| 33 { | |
| 34 private final int[] connectionTypes = new int[] | |
| 35 { | |
| 36 ConnectivityManager.TYPE_WIFI, | |
| 37 ConnectivityManager.TYPE_MOBILE, | |
| 38 ConnectivityManager.TYPE_ETHERNET, | |
| 39 }; | |
| 40 | |
| 41 @Override | |
| 42 protected void setUp() throws Exception | |
| 43 { | |
| 44 super.setUp(); | |
| 45 | |
| 46 System.setProperty( | |
| 47 "dexmaker.dexcache", | |
| 48 getInstrumentation().getTargetContext().getCacheDir().getPath()); | |
| 49 } | |
| 50 | |
| 51 public static ConnectivityManager buildNoActiveConnectionCM() | |
| 52 { | |
| 53 final ConnectivityManager connectivityManager = Mockito.mock(ConnectivityMan
ager.class); | |
| 54 | |
| 55 Mockito | |
| 56 .when(connectivityManager.getActiveNetworkInfo()) | |
| 57 .thenReturn(null); | |
| 58 | |
| 59 return connectivityManager; | |
| 60 } | |
| 61 | |
| 62 public static ConnectivityManager buildConnectivityManager(boolean connected,
int type, boolean metered) | |
| 63 { | |
| 64 final ConnectivityManager connectivityManager = Mockito.mock(ConnectivityMan
ager.class); | |
| 65 final NetworkInfo networkInfo = Mockito.mock(NetworkInfo.class); | |
| 66 | |
| 67 Mockito | |
| 68 .when(connectivityManager.getActiveNetworkInfo()) | |
| 69 .thenReturn(networkInfo); | |
| 70 | |
| 71 Mockito | |
| 72 .when(connectivityManager.isActiveNetworkMetered()) | |
| 73 .thenReturn(metered); | |
| 74 | |
| 75 Mockito | |
| 76 .when(networkInfo.isConnected()) | |
| 77 .thenReturn(connected); | |
| 78 | |
| 79 Mockito | |
| 80 .when(networkInfo.getType()) | |
| 81 .then(new Returns(type)); | |
| 82 | |
| 83 return connectivityManager; | |
| 84 } | |
| 85 | |
| 86 @Test | |
| 87 public void testNullAllowedConnection() | |
| 88 { | |
| 89 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCall
backImpl(null); | |
| 90 assertTrue(callback.isConnectionAllowed(null)); | |
| 91 } | |
| 92 | |
| 93 @Test | |
| 94 public void testNoActiveConnection() | |
| 95 { | |
| 96 ConnectivityManager connectivityManager = buildNoActiveConnectionCM(); | |
| 97 final IsAllowedConnectionCallbackImpl callback = | |
| 98 new IsAllowedConnectionCallbackImpl(connectivityManager); | |
| 99 | |
| 100 for (ConnectionType eachConnectionType : ConnectionType.values()) | |
| 101 { | |
| 102 assertFalse(callback.isConnectionAllowed(eachConnectionType.getValue())); | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 @Test | |
| 107 public void testFalseForAnyTypeIfNotConnected() | |
| 108 { | |
| 109 for (int currentConnectionType : connectionTypes) | |
| 110 { | |
| 111 ConnectivityManager cm = buildConnectivityManager(false, currentConnection
Type, false); | |
| 112 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCa
llbackImpl(cm); | |
| 113 | |
| 114 for (ConnectionType eachConnectionType : ConnectionType.values()) | |
| 115 { | |
| 116 assertFalse(callback.isConnectionAllowed(eachConnectionType.getValue()))
; | |
| 117 } | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 @Test | |
| 122 public void testFalseForUnknownConnectionType() | |
| 123 { | |
| 124 final String allowedConnectionType = "unknown"; | |
| 125 | |
| 126 for (int currentConnectionType : connectionTypes) | |
| 127 { | |
| 128 ConnectivityManager cm = buildConnectivityManager(true, currentConnectionT
ype, false); | |
| 129 final IsAllowedConnectionCallbackImpl callback = new IsAllowedConnectionCa
llbackImpl(cm); | |
| 130 | |
| 131 assertFalse(callback.isConnectionAllowed(allowedConnectionType)); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 @Test | |
| 136 public void testWifiNonMetered() | |
| 137 { | |
| 138 // current connection is wifi non metered | |
| 139 final IsAllowedConnectionCallbackImpl callback = | |
| 140 new IsAllowedConnectionCallbackImpl( | |
| 141 buildConnectivityManager(true, ConnectivityManager.TYPE_WIFI, false)); | |
| 142 | |
| 143 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue())); | |
| 144 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI.getValue())); | |
| 145 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.getV
alue())); | |
| 146 } | |
| 147 | |
| 148 @Test | |
| 149 public void testWifiMetered() | |
| 150 { | |
| 151 // current connection is wifi metered | |
| 152 final IsAllowedConnectionCallbackImpl callback = | |
| 153 new IsAllowedConnectionCallbackImpl( | |
| 154 buildConnectivityManager(true, ConnectivityManager.TYPE_WIFI, true)); | |
| 155 | |
| 156 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue())); | |
| 157 assertTrue(callback.isConnectionAllowed(ConnectionType.WIFI.getValue())); | |
| 158 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get
Value())); | |
| 159 } | |
| 160 | |
| 161 @Test | |
| 162 public void testMobile() | |
| 163 { | |
| 164 // current connection is mobile | |
| 165 final IsAllowedConnectionCallbackImpl callback = | |
| 166 new IsAllowedConnectionCallbackImpl( | |
| 167 buildConnectivityManager(true, ConnectivityManager.TYPE_MOBILE, false)); | |
| 168 | |
| 169 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue())); | |
| 170 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI.getValue())); | |
| 171 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get
Value())); | |
| 172 } | |
| 173 | |
| 174 @Test | |
| 175 public void testEthernet() | |
| 176 { | |
| 177 // current connection is ethernet | |
| 178 final IsAllowedConnectionCallbackImpl callback = | |
| 179 new IsAllowedConnectionCallbackImpl( | |
| 180 buildConnectivityManager(true, ConnectivityManager.TYPE_ETHERNET, false)
); | |
| 181 | |
| 182 assertTrue(callback.isConnectionAllowed(ConnectionType.ANY.getValue())); | |
| 183 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI.getValue())); | |
| 184 assertFalse(callback.isConnectionAllowed(ConnectionType.WIFI_NON_METERED.get
Value())); | |
| 185 } | |
| 186 } | |
| OLD | NEW |