1 package com.xtremelabs.robolectric.shadows; 2 3 import android.content.Context; 4 import android.net.ConnectivityManager; 5 import android.net.NetworkInfo; 6 import android.telephony.TelephonyManager; 7 8 import com.xtremelabs.robolectric.Robolectric; 9 import com.xtremelabs.robolectric.WithTestDefaultsRunner; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 14 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 15 import static org.hamcrest.CoreMatchers.is; 16 import static org.hamcrest.CoreMatchers.sameInstance; 17 import static org.junit.Assert.*; 18 19 @RunWith(WithTestDefaultsRunner.class) 20 public class ConnectivityManagerTest { 21 private ConnectivityManager connectivityManager; 22 private ShadowNetworkInfo shadowOfActiveNetworkInfo; 23 private ShadowConnectivityManager shadowConnectivityManager; 24 25 @Before setUp()26 public void setUp() throws Exception { 27 connectivityManager = (ConnectivityManager) Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE); 28 shadowConnectivityManager = shadowOf(connectivityManager); 29 shadowOfActiveNetworkInfo = shadowOf(connectivityManager.getActiveNetworkInfo()); 30 } 31 32 @Test getConnectivityManagerShouldNotBeNull()33 public void getConnectivityManagerShouldNotBeNull() { 34 assertNotNull(connectivityManager); 35 assertNotNull(connectivityManager.getActiveNetworkInfo()); 36 } 37 38 @Test networkInfoShouldReturnTrueCorrectly()39 public void networkInfoShouldReturnTrueCorrectly() { 40 shadowOfActiveNetworkInfo.setConnectionStatus(true); 41 42 assertTrue(connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting()); 43 assertTrue(connectivityManager.getActiveNetworkInfo().isConnected()); 44 } 45 46 @Test getNetworkInfoShouldReturnAssignedValue()47 public void getNetworkInfoShouldReturnAssignedValue() throws Exception { 48 NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTING); 49 shadowConnectivityManager.setNetworkInfo(ConnectivityManager.TYPE_WIFI, networkInfo); 50 NetworkInfo actual = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 51 assertThat(actual, sameInstance(networkInfo)); 52 assertThat(actual.getDetailedState(), is(NetworkInfo.DetailedState.CONNECTING)); 53 } 54 55 @Test networkInfoShouldReturnFalseCorrectly()56 public void networkInfoShouldReturnFalseCorrectly() { 57 shadowOfActiveNetworkInfo.setConnectionStatus(false); 58 59 assertFalse(connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting()); 60 assertFalse(connectivityManager.getActiveNetworkInfo().isConnected()); 61 } 62 63 @Test networkInfoShouldReturnTypeCorrectly()64 public void networkInfoShouldReturnTypeCorrectly(){ 65 shadowOfActiveNetworkInfo.setConnectionType(ConnectivityManager.TYPE_MOBILE); 66 assertEquals(ConnectivityManager.TYPE_MOBILE, shadowOfActiveNetworkInfo.getType()); 67 68 shadowOfActiveNetworkInfo.setConnectionType(ConnectivityManager.TYPE_WIFI); 69 assertEquals(ConnectivityManager.TYPE_WIFI, shadowOfActiveNetworkInfo.getType()); 70 } 71 72 @Test shouldGetAndSetBackgroundDataSetting()73 public void shouldGetAndSetBackgroundDataSetting() throws Exception { 74 assertFalse(connectivityManager.getBackgroundDataSetting()); 75 shadowConnectivityManager.setBackgroundDataSetting(true); 76 assertTrue(connectivityManager.getBackgroundDataSetting()); 77 } 78 79 @Test shouldSetActiveNetworkInfo()80 public void shouldSetActiveNetworkInfo() throws Exception { 81 shadowConnectivityManager.setActiveNetworkInfo(null); 82 assertNull(connectivityManager.getActiveNetworkInfo()); 83 shadowConnectivityManager.setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, 84 ConnectivityManager.TYPE_MOBILE_HIPRI, 85 TelephonyManager.NETWORK_TYPE_EDGE, true, false)); 86 87 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); 88 89 assertEquals(ConnectivityManager.TYPE_MOBILE_HIPRI, info.getType()); 90 assertEquals(TelephonyManager.NETWORK_TYPE_EDGE, info.getSubtype()); 91 assertTrue(info.isAvailable()); 92 assertFalse(info.isConnected()); 93 } 94 95 @Test shouldGetAllNetworkInfo()96 public void shouldGetAllNetworkInfo() throws Exception { 97 NetworkInfo[] infos = connectivityManager.getAllNetworkInfo(); 98 assertNotNull(infos); 99 assertEquals(1, infos.length); 100 assertSame(connectivityManager.getActiveNetworkInfo(), infos[0]); 101 102 shadowConnectivityManager.setActiveNetworkInfo(null); 103 assertEquals(0, connectivityManager.getAllNetworkInfo().length); 104 } 105 } 106