public static void setNetworkInfo(Context context, boolean isConnected, boolean isRoaming, boolean isWifi) { ShadowConnectivityManager manager = shadowOf((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)); NetworkInfo.DetailedState detailedState = isConnected ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED; int type = isConnected ? (isWifi ? ConnectivityManager.TYPE_WIFI : ConnectivityManager.TYPE_MOBILE) : -1; NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(detailedState, type, 0, isConnected, isConnected, isRoaming); manager.setActiveNetworkInfo(networkInfo); }
private void toggleNetwork(int networkType, NetworkInfo.DetailedState detailedState) { NetworkInfo networkInfo = null; switch (detailedState) { case CONNECTED: networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, networkType, 0, true, true); break; case DISCONNECTED: networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, networkType, 0, true, false); break; } ConnectivityManager cmMock = (ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE); ShadowConnectivityManager shadowConnectivityManager = shadowOf(cmMock); shadowConnectivityManager.setNetworkInfo(networkType, networkInfo); shadowConnectivityManager.setActiveNetworkInfo(shadowConnectivityManager.getNetworkInfo(networkType)); }
@Before public void connectToNetwork() { ShadowConnectivityManager shadowConnectivityManager = Shadows.shadowOf( (ConnectivityManager) RuntimeEnvironment.application .getSystemService(Context.CONNECTIVITY_SERVICE)); shadowConnectivityManager.setNetworkInfo(ConnectivityManager.TYPE_MOBILE, ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_MOBILE, ConnectivityManager.TYPE_MOBILE_MMS, true, false)); // Set the default network to WiFi, as downloads are always allowed over // WiFi regardless of the configuration settings. shadowConnectivityManager.setActiveNetworkInfo( ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true)); }
@Override public void setup() { super.setup(); connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); // Not using Shadows.shadowOf(connectivityManager) because of Robolectric bug when using API23+ // See: https://github.com/robolectric/robolectric/issues/1862 shadowConnectivityManager = (ShadowConnectivityManager) ShadowExtractor.extract(connectivityManager); }
public void disconnectFromNetwork() { ShadowConnectivityManager shadowConnectivityManager = Shadows.shadowOf( (ConnectivityManager) RuntimeEnvironment.application .getSystemService(Context.CONNECTIVITY_SERVICE)); shadowConnectivityManager.setActiveNetworkInfo(null); }
public static ShadowConnectivityManager shadowOf(ConnectivityManager instance) { return (ShadowConnectivityManager) shadowOf_(instance); }