org.robolectric.shadows.ShadowConnectivityManager Java Examples

The following examples show how to use org.robolectric.shadows.ShadowConnectivityManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UtilityServiceTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
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));
}
 
Example #2
Source File: TrafficStatsNetworkBytesCollectorTest.java    From Battery-Metrics with MIT License 5 votes vote down vote up
@Test
public void testBroadcastNetworkChanges() throws Exception {
  ShadowTrafficStats.setUidRxBytes(10000);
  ShadowTrafficStats.setUidTxBytes(20000);

  TrafficStatsNetworkBytesCollector collector =
      new TrafficStatsNetworkBytesCollector(RuntimeEnvironment.application);
  assertThat(collector.getTotalBytes(mBytes)).isTrue();

  ShadowTrafficStats.setUidRxBytes(11000);
  ShadowTrafficStats.setUidTxBytes(22000);

  ConnectivityManager connectivityManager =
      (ConnectivityManager)
          RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
  ShadowConnectivityManager shadowConnectivityManager = Shadows.shadowOf(connectivityManager);
  NetworkInfo networkInfo =
      ShadowNetworkInfo.newInstance(null, ConnectivityManager.TYPE_WIFI, 0, true, true);
  shadowConnectivityManager.setActiveNetworkInfo(networkInfo);
  collector.mReceiver.onReceive(null, null);

  ShadowTrafficStats.setUidRxBytes(11100);
  ShadowTrafficStats.setUidTxBytes(22200);
  assertThat(collector.getTotalBytes(mBytes)).isTrue();

  assertThat(mBytes[RX | MOBILE]).isEqualTo(11000);
  assertThat(mBytes[TX | MOBILE]).isEqualTo(22000);
  assertThat(mBytes[RX | WIFI]).isEqualTo(100);
  assertThat(mBytes[TX | WIFI]).isEqualTo(200);
}
 
Example #3
Source File: UiTest.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example #4
Source File: BannerImpressionTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
@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) Shadow.extract(connectivityManager);
}
 
Example #5
Source File: UiTest.java    From edx-app-android with Apache License 2.0 4 votes vote down vote up
public void disconnectFromNetwork() {
    ShadowConnectivityManager shadowConnectivityManager = Shadows.shadowOf(
            (ConnectivityManager) RuntimeEnvironment.application
                    .getSystemService(Context.CONNECTIVITY_SERVICE));
    shadowConnectivityManager.setActiveNetworkInfo(null);
}