| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2015 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 package org.webrtc; | |
| 28 | |
| 29 import static org.webrtc.NetworkMonitorAutoDetect.ConnectionType; | |
| 30 import static org.webrtc.NetworkMonitorAutoDetect.ConnectivityManagerDelegate; | |
| 31 import static org.webrtc.NetworkMonitorAutoDetect.INVALID_NET_ID; | |
| 32 import static org.webrtc.NetworkMonitorAutoDetect.NetworkInformation; | |
| 33 import static org.webrtc.NetworkMonitorAutoDetect.NetworkState; | |
| 34 | |
| 35 import android.annotation.SuppressLint; | |
| 36 import android.content.Context; | |
| 37 import android.content.Intent; | |
| 38 import android.net.ConnectivityManager; | |
| 39 import android.net.Network; | |
| 40 import android.net.wifi.WifiManager; | |
| 41 import android.os.Build; | |
| 42 import android.os.Handler; | |
| 43 import android.os.Looper; | |
| 44 import android.telephony.TelephonyManager; | |
| 45 import android.test.ActivityTestCase; | |
| 46 import android.test.UiThreadTest; | |
| 47 import android.test.suitebuilder.annotation.MediumTest; | |
| 48 import android.test.suitebuilder.annotation.SmallTest; | |
| 49 | |
| 50 /** | |
| 51 * Tests for org.webrtc.NetworkMonitor. | |
| 52 */ | |
| 53 @SuppressLint("NewApi") | |
| 54 public class NetworkMonitorTest extends ActivityTestCase { | |
| 55 /** | |
| 56 * Listens for alerts fired by the NetworkMonitor when network status changes. | |
| 57 */ | |
| 58 private static class NetworkMonitorTestObserver | |
| 59 implements NetworkMonitor.NetworkObserver { | |
| 60 private boolean receivedNotification = false; | |
| 61 | |
| 62 @Override | |
| 63 public void onConnectionTypeChanged(ConnectionType connectionType) { | |
| 64 receivedNotification = true; | |
| 65 } | |
| 66 | |
| 67 public boolean hasReceivedNotification() { | |
| 68 return receivedNotification; | |
| 69 } | |
| 70 | |
| 71 public void resetHasReceivedNotification() { | |
| 72 receivedNotification = false; | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 /** | |
| 77 * Mocks out calls to the ConnectivityManager. | |
| 78 */ | |
| 79 private static class MockConnectivityManagerDelegate extends ConnectivityManag
erDelegate { | |
| 80 private boolean activeNetworkExists; | |
| 81 private int networkType; | |
| 82 private int networkSubtype; | |
| 83 | |
| 84 @Override | |
| 85 public NetworkState getNetworkState() { | |
| 86 return new NetworkState(activeNetworkExists, networkType, networkSubtype); | |
| 87 } | |
| 88 | |
| 89 // Dummy implementations to avoid NullPointerExceptions in default implement
ations: | |
| 90 | |
| 91 @Override | |
| 92 public int getDefaultNetId() { | |
| 93 return INVALID_NET_ID; | |
| 94 } | |
| 95 | |
| 96 @Override | |
| 97 public Network[] getAllNetworks() { | |
| 98 return new Network[0]; | |
| 99 } | |
| 100 | |
| 101 @Override | |
| 102 public NetworkState getNetworkState(Network network) { | |
| 103 return new NetworkState(false, -1, -1); | |
| 104 } | |
| 105 | |
| 106 public void setActiveNetworkExists(boolean networkExists) { | |
| 107 activeNetworkExists = networkExists; | |
| 108 } | |
| 109 | |
| 110 public void setNetworkType(int networkType) { | |
| 111 this.networkType = networkType; | |
| 112 } | |
| 113 | |
| 114 public void setNetworkSubtype(int networkSubtype) { | |
| 115 this.networkSubtype = networkSubtype; | |
| 116 } | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * Mocks out calls to the WifiManager. | |
| 121 */ | |
| 122 private static class MockWifiManagerDelegate | |
| 123 extends NetworkMonitorAutoDetect.WifiManagerDelegate { | |
| 124 private String wifiSSID; | |
| 125 | |
| 126 @Override | |
| 127 public String getWifiSSID() { | |
| 128 return wifiSSID; | |
| 129 } | |
| 130 | |
| 131 public void setWifiSSID(String wifiSSID) { | |
| 132 this.wifiSSID = wifiSSID; | |
| 133 } | |
| 134 } | |
| 135 | |
| 136 // A dummy NetworkMonitorAutoDetect.Observer. | |
| 137 private static class TestNetworkMonitorAutoDetectObserver | |
| 138 implements NetworkMonitorAutoDetect.Observer { | |
| 139 | |
| 140 @Override | |
| 141 public void onConnectionTypeChanged(ConnectionType newConnectionType) {} | |
| 142 | |
| 143 @Override | |
| 144 public void onNetworkConnect(NetworkInformation networkInfo) {} | |
| 145 | |
| 146 @Override | |
| 147 public void onNetworkDisconnect(int networkHandle) {} | |
| 148 } | |
| 149 | |
| 150 private static final Object lock = new Object(); | |
| 151 private static Handler uiThreadHandler = null; | |
| 152 | |
| 153 private NetworkMonitorAutoDetect receiver; | |
| 154 private MockConnectivityManagerDelegate connectivityDelegate; | |
| 155 private MockWifiManagerDelegate wifiDelegate; | |
| 156 | |
| 157 private static Handler getUiThreadHandler() { | |
| 158 synchronized (lock) { | |
| 159 if (uiThreadHandler == null ) { | |
| 160 uiThreadHandler = new Handler(Looper.getMainLooper()); | |
| 161 } | |
| 162 return uiThreadHandler; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 /** | |
| 167 * Helper method to create a network monitor and delegates for testing. | |
| 168 */ | |
| 169 private void createTestMonitor() { | |
| 170 Context context = getInstrumentation().getTargetContext(); | |
| 171 NetworkMonitor.resetInstanceForTests(context); | |
| 172 NetworkMonitor.setAutoDetectConnectivityState(true); | |
| 173 receiver = NetworkMonitor.getAutoDetectorForTest(); | |
| 174 assertNotNull(receiver); | |
| 175 | |
| 176 connectivityDelegate = new MockConnectivityManagerDelegate(); | |
| 177 connectivityDelegate.setActiveNetworkExists(true); | |
| 178 receiver.setConnectivityManagerDelegateForTests(connectivityDelegate); | |
| 179 | |
| 180 wifiDelegate = new MockWifiManagerDelegate(); | |
| 181 receiver.setWifiManagerDelegateForTests(wifiDelegate); | |
| 182 wifiDelegate.setWifiSSID("foo"); | |
| 183 } | |
| 184 | |
| 185 private NetworkMonitorAutoDetect.ConnectionType getCurrentConnectionType() { | |
| 186 final NetworkMonitorAutoDetect.NetworkState networkState = | |
| 187 receiver.getCurrentNetworkState(); | |
| 188 return receiver.getConnectionType(networkState); | |
| 189 } | |
| 190 | |
| 191 @Override | |
| 192 protected void setUp() throws Exception { | |
| 193 super.setUp(); | |
| 194 getUiThreadHandler().post(new Runnable() { | |
| 195 public void run() { | |
| 196 createTestMonitor(); | |
| 197 } | |
| 198 }); | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * Tests that the receiver registers for connectivity intents during construct
ion. | |
| 203 */ | |
| 204 @UiThreadTest | |
| 205 @SmallTest | |
| 206 public void testNetworkMonitorRegistersInConstructor() throws InterruptedExcep
tion { | |
| 207 Context context = getInstrumentation().getTargetContext(); | |
| 208 | |
| 209 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec
tObserver(); | |
| 210 | |
| 211 NetworkMonitorAutoDetect receiver = new NetworkMonitorAutoDetect(observer, c
ontext); | |
| 212 | |
| 213 assertTrue(receiver.isReceiverRegisteredForTesting()); | |
| 214 } | |
| 215 | |
| 216 /** | |
| 217 * Tests that when there is an intent indicating a change in network connectiv
ity, it sends a | |
| 218 * notification to Java observers. | |
| 219 */ | |
| 220 @UiThreadTest | |
| 221 @MediumTest | |
| 222 public void testNetworkMonitorJavaObservers() throws InterruptedException { | |
| 223 // Initialize the NetworkMonitor with a connection. | |
| 224 Intent connectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTI
ON); | |
| 225 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 226 | |
| 227 // We shouldn't be re-notified if the connection hasn't actually changed. | |
| 228 NetworkMonitorTestObserver observer = new NetworkMonitorTestObserver(); | |
| 229 NetworkMonitor.addNetworkObserver(observer); | |
| 230 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 231 assertFalse(observer.hasReceivedNotification()); | |
| 232 | |
| 233 // We shouldn't be notified if we're connected to non-Wifi and the Wifi SSID
changes. | |
| 234 wifiDelegate.setWifiSSID("bar"); | |
| 235 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 236 assertFalse(observer.hasReceivedNotification()); | |
| 237 | |
| 238 // We should be notified when we change to Wifi. | |
| 239 connectivityDelegate.setNetworkType(ConnectivityManager.TYPE_WIFI); | |
| 240 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 241 assertTrue(observer.hasReceivedNotification()); | |
| 242 observer.resetHasReceivedNotification(); | |
| 243 | |
| 244 // We should be notified when the Wifi SSID changes. | |
| 245 wifiDelegate.setWifiSSID("foo"); | |
| 246 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 247 assertTrue(observer.hasReceivedNotification()); | |
| 248 observer.resetHasReceivedNotification(); | |
| 249 | |
| 250 // We shouldn't be re-notified if the Wifi SSID hasn't actually changed. | |
| 251 receiver.onReceive(getInstrumentation().getTargetContext(), connectivityInte
nt); | |
| 252 assertFalse(observer.hasReceivedNotification()); | |
| 253 | |
| 254 // Mimic that connectivity has been lost and ensure that the observer gets t
he notification. | |
| 255 connectivityDelegate.setActiveNetworkExists(false); | |
| 256 Intent noConnectivityIntent = new Intent(ConnectivityManager.CONNECTIVITY_AC
TION); | |
| 257 receiver.onReceive(getInstrumentation().getTargetContext(), noConnectivityIn
tent); | |
| 258 assertTrue(observer.hasReceivedNotification()); | |
| 259 } | |
| 260 | |
| 261 /** | |
| 262 * Tests that ConnectivityManagerDelegate doesn't crash. This test cannot rely
on having any | |
| 263 * active network connections so it cannot usefully check results, but it can
at least check | |
| 264 * that the functions don't crash. | |
| 265 */ | |
| 266 @UiThreadTest | |
| 267 @SmallTest | |
| 268 public void testConnectivityManagerDelegateDoesNotCrash() { | |
| 269 ConnectivityManagerDelegate delegate = | |
| 270 new ConnectivityManagerDelegate(getInstrumentation().getTargetContext())
; | |
| 271 delegate.getNetworkState(); | |
| 272 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
| 273 Network[] networks = delegate.getAllNetworks(); | |
| 274 if (networks.length >= 1) { | |
| 275 delegate.getNetworkState(networks[0]); | |
| 276 delegate.hasInternetCapability(networks[0]); | |
| 277 } | |
| 278 delegate.getDefaultNetId(); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 /** | |
| 283 * Tests that NetworkMonitorAutoDetect queryable APIs don't crash. This test c
annot rely | |
| 284 * on having any active network connections so it cannot usefully check result
s, but it can at | |
| 285 * least check that the functions don't crash. | |
| 286 */ | |
| 287 @UiThreadTest | |
| 288 @SmallTest | |
| 289 public void testQueryableAPIsDoNotCrash() { | |
| 290 NetworkMonitorAutoDetect.Observer observer = new TestNetworkMonitorAutoDetec
tObserver(); | |
| 291 NetworkMonitorAutoDetect ncn = | |
| 292 new NetworkMonitorAutoDetect(observer, getInstrumentation().getTargetCon
text()); | |
| 293 ncn.getDefaultNetId(); | |
| 294 } | |
| 295 } | |
| OLD | NEW |