org.robolectric.shadows.ShadowNetworkInfo Java Examples

The following examples show how to use org.robolectric.shadows.ShadowNetworkInfo. 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: ItemSyncJobServiceTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Test
public void testScheduledJob() {
    PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application)
            .edit()
            .putBoolean(RuntimeEnvironment.application.getString(R.string.pref_saved_item_sync), true)
            .apply();
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, NetworkInfo.State.CONNECTED));
    new SyncScheduler().scheduleSync(RuntimeEnvironment.application, "1");
    List<JobInfo> pendingJobs = ((ShadowJobScheduler.ShadowJobSchedulerImpl)
            shadowOf((JobScheduler) RuntimeEnvironment.application
            .getSystemService(Context.JOB_SCHEDULER_SERVICE))).getAllPendingJobs();
    assertThat(pendingJobs).isNotEmpty();
    JobInfo actual = pendingJobs.get(0);
    assertThat(actual.getService().getClassName())
            .isEqualTo(ItemSyncJobService.class.getName());
}
 
Example #2
Source File: WebFragmentTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(favoriteManager);
    item = mock(WebItem.class);
    when(item.getType()).thenReturn(Item.STORY_TYPE);
    when(item.getUrl()).thenReturn("http://example.com");
    intent = new Intent();
    intent.putExtra(WebActivity.EXTRA_ITEM, item);
    controller = Robolectric.buildActivity(WebActivity.class, intent);
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, NetworkInfo.State.CONNECTED));
    activity = controller.get();
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putBoolean(activity.getString(R.string.pref_ad_block), true)
            .putBoolean(activity.getString(R.string.pref_lazy_load), false)
            .apply();
    controller.create().start().resume().visible();
}
 
Example #3
Source File: WebFragmentLocalTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(itemManager);
    controller = Robolectric.buildActivity(WebActivity.class);
    activity = controller.get();
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putBoolean(activity.getString(R.string.pref_lazy_load), false)
            .apply();
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, NetworkInfo.State.CONNECTED));
}
 
Example #4
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 #5
Source File: ResultCodeTest.java    From prebid-mobile-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testNetworkError() {
    PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
    PrebidMobile.setApplicationContext(activity.getApplicationContext());
    PrebidMobile.setPrebidServerAccountId("123456");
    ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
    ShadowNetworkInfo shadowOfActiveNetworkInfo = shadowOf(connectivityManager.getActiveNetworkInfo());
    shadowOfActiveNetworkInfo.setConnectionStatus(false);
    BannerAdUnit adUnit = new BannerAdUnit("123456", 300, 250);
    MoPubView testView = new MoPubView(activity);
    OnCompleteListener mockListener = mock(OnCompleteListener.class);
    adUnit.fetchDemand(testView, mockListener);
    verify(mockListener).onComplete(ResultCode.NETWORK_ERROR);
}
 
Example #6
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 #7
Source File: AppUtilsTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisconnectedNetwork() {
    shadowOf((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, 0, 0, false, false));
    assertFalse(AppUtils.isOnWiFi(context));
}
 
Example #8
Source File: AppUtilsTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testNonWiFiNetwork() {
    shadowOf((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_MOBILE, 0, true, true));
    assertFalse(AppUtils.isOnWiFi(context));
}
 
Example #9
Source File: AppUtilsTest.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@Test
public void testWiFiNetwork() {
    shadowOf((ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    assertTrue(AppUtils.isOnWiFi(context));
}
 
Example #10
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 #11
Source File: BannerImpressionTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void goOffline() {
    NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
    assertTrue(activeInfo != null && activeInfo.isConnected());

    shadowConnectivityManager.setActiveNetworkInfo(
            ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_MOBILE, 0, true, false)
    );
    NetworkInfo activeInfo2 = connectivityManager.getActiveNetworkInfo();
    assertTrue(activeInfo2 != null && !activeInfo2.isConnected());
}
 
Example #12
Source File: BannerImpressionTests.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void goOnline() {
    NetworkInfo activeInfo = connectivityManager.getActiveNetworkInfo();
    assertTrue(activeInfo != null && !activeInfo.isConnected());

    shadowConnectivityManager.setActiveNetworkInfo(
            ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, ConnectivityManager.TYPE_MOBILE, 0, true, true)
    );
    NetworkInfo activeInfo2 = connectivityManager.getActiveNetworkInfo();
    assertTrue(activeInfo2 != null && activeInfo2.isConnected());
}
 
Example #13
Source File: UploadTest.java    From firebase-android-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void badConnectivitySmallUpload() throws Exception {
  System.out.println("Starting test badConnectivitySmallUpload.");

  MockConnectionFactory factory = NetworkLayerMock.ensureNetworkMock("smallTextUpload", true);

  ConnectivityManager connectivityManager =
      (ConnectivityManager)
          RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
  final NetworkInfo originalNetwork = connectivityManager.getActiveNetworkInfo();

  try {
    Shadows.shadowOf(connectivityManager)
        .setActiveNetworkInfo(
            ShadowNetworkInfo.newInstance(
                NetworkInfo.DetailedState.DISCONNECTED,
                ConnectivityManager.TYPE_MOBILE,
                0,
                false,
                NetworkInfo.State.DISCONNECTED));
    // after 10 seconds of simulated time, turn the network back on.
    MockClockHelper.install(
        new MockClockHelper() {
          @Override
          public void advance(int millis) {
            super.advance(millis);
            if (this.currentTimeMillis() > 10000) {
              Shadows.shadowOf(connectivityManager).setActiveNetworkInfo(originalNetwork);
            }
          }
        });

    Task<StringBuilder> task = TestUploadHelper.smallTextUpload();

    // TODO(mrschmidt): Lower the timeout
    TestUtil.await(task, 300, TimeUnit.SECONDS);

    factory.verifyOldMock();
    TestUtil.verifyTaskStateChanges("smallTextUpload", task.getResult().toString());
  } finally {
    MockClockHelper.install(new MockClockHelper());
    Shadows.shadowOf(connectivityManager).setActiveNetworkInfo(originalNetwork);
  }
}
 
Example #14
Source File: ItemSyncAdapterTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
private void setNetworkType(int type) {
    shadowOf((ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, type, 0, true, NetworkInfo.State.CONNECTED));
}
 
Example #15
Source File: PagerResultsFragmentTest.java    From open with GNU General Public License v3.0 4 votes vote down vote up
private void simulateNoNetworkConnection() {
    ConnectivityManager cm = (ConnectivityManager) act.getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    ShadowNetworkInfo shadowNetworkInfo = shadowOf(networkInfo);
    shadowNetworkInfo.setConnectionStatus(false);
}