Java 类org.robolectric.shadows.ShadowNetworkInfo 实例源码

项目:RxNetwork    文件:ShadowConnectivityManagerWithCallback.java   
public ShadowConnectivityManagerWithCallback() {
  NetworkInfo wifi = ShadowNetworkInfo.newInstance(DISCONNECTED, TYPE_WIFI, 0, true, false);
  networkTypeToNetworkInfo.put(TYPE_WIFI, wifi);

  NetworkInfo mobile =
      ShadowNetworkInfo.newInstance(CONNECTED, TYPE_MOBILE, TYPE_MOBILE_MMS, true, true);
  networkTypeToNetworkInfo.put(TYPE_MOBILE, mobile);

  this.activeNetworkInfo = mobile;

  if (getApiLevel() >= LOLLIPOP) {
    netIdToNetwork.put(NET_ID_WIFI, ShadowNetwork.newInstance(NET_ID_WIFI));
    netIdToNetwork.put(NET_ID_MOBILE, ShadowNetwork.newInstance(NET_ID_MOBILE));
    netIdToNetworkInfo.put(NET_ID_WIFI, wifi);
    netIdToNetworkInfo.put(NET_ID_MOBILE, mobile);
  }
}
项目:materialistic    文件:ItemFragmentMultiPageTest.java   
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(hackerNewsClient);
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application)
            .edit()
            .putString(RuntimeEnvironment.application.getString(R.string.pref_comment_display),
                    RuntimeEnvironment.application.getString(R.string.pref_comment_display_value_multiple))
            .putBoolean(RuntimeEnvironment.application.getString(R.string.pref_lazy_load), false)
            .apply();
    controller = Robolectric.buildActivity(TestItemActivity.class);
    activity = controller.create().start().resume().get();
}
项目:materialistic    文件:ItemSyncJobServiceTest.java   
@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, true));
    new SyncScheduler().scheduleSync(RuntimeEnvironment.application, "1");
    List<JobInfo> pendingJobs = 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());
}
项目:materialistic    文件:WebFragmentTest.java   
@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);
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    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.withIntent(intent).create().start().resume().visible();
}
项目:materialistic    文件:WebFragmentLocalTest.java   
@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, true));
}
项目:openwebnet-android    文件:UtilityServiceTest.java   
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));
}
项目:materialistic    文件:ReadabilityFragmentTest.java   
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(readabilityClient);
    controller = Robolectric.buildActivity(TestReadabilityActivity.class);
    activity = controller.create().start().resume().visible().get();
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putBoolean(activity.getString(R.string.pref_lazy_load), false)
            .putString(activity.getString(R.string.pref_story_display),
                    activity.getString(R.string.pref_story_display_value_readability))
            .apply();
    shadowOf((ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    Bundle args = new Bundle();
    WebItem item = new TestWebItem() {
        @Override
        public String getId() {
            return "1";
        }

        @Override
        public String getUrl() {
            return "http://example.com/article.html";
        }
    };
    args.putParcelable(WebFragment.EXTRA_ITEM, item);
    fragment = (WebFragment) Fragment.instantiate(activity, WebFragment.class.getName(), args);
    activity.getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.content_frame, fragment, "tag")
            .commit();
}
项目:materialistic    文件:FavoriteManagerTest.java   
@Config(sdk = 18)
@Test
public void testAdd() {
    PreferenceManager.getDefaultSharedPreferences(RuntimeEnvironment.application)
            .edit()
            .putBoolean(RuntimeEnvironment.application
                    .getString(R.string.pref_saved_item_sync), true)
            .putBoolean(RuntimeEnvironment.application
                    .getString(R.string.pref_offline_article), true)
            .apply();
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    manager.add(RuntimeEnvironment.application, new TestWebItem() {
        @Override
        public String getId() {
            return "3";
        }

        @Override
        public String getUrl() {
            return "http://newitem.com";
        }

        @Override
        public String getDisplayedTitle() {
            return "new title";
        }
    });
    assertThat(resolver.getNotifiedUris()).isNotEmpty();
    assertTrue(ShadowContentResolver.isSyncActive(new Account("Materialistic", BuildConfig.APPLICATION_ID),
            MaterialisticProvider.PROVIDER_AUTHORITY));
}
项目:materialistic    文件:AppUtilsTest.java   
@Test
public void testDisconnectedNetwork() {
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, 0, 0, false, false));
    assertFalse(AppUtils.isOnWiFi(RuntimeEnvironment.application));
}
项目:materialistic    文件:AppUtilsTest.java   
@Test
public void testNonWiFiNetwork() {
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_MOBILE, 0, true, true));
    assertFalse(AppUtils.isOnWiFi(RuntimeEnvironment.application));
}
项目:materialistic    文件:AppUtilsTest.java   
@Test
public void testWiFiNetwork() {
    shadowOf((ConnectivityManager) RuntimeEnvironment.application
            .getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null,
                    ConnectivityManager.TYPE_WIFI, 0, true, true));
    assertTrue(AppUtils.isOnWiFi(RuntimeEnvironment.application));
}
项目:edx-app-android    文件:UiTest.java   
@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));
}
项目:OPFUtils    文件:OPFUtilsTest.java   
@Test
public void testIsConnected() {
    final ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    final ShadowNetworkInfo shadowNetworkInfo = shadowOf(connectivityManager.getActiveNetworkInfo());

    shadowNetworkInfo.setConnectionStatus(true);
    assertTrue(OPFUtils.isConnected(ctx));

    shadowNetworkInfo.setConnectionStatus(false);
    assertFalse(OPFUtils.isConnected(ctx));
}
项目:mobile-sdk-android    文件:BannerImpressionTests.java   
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());
}
项目:mobile-sdk-android    文件:BannerImpressionTests.java   
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());
}
项目:GitHub    文件:DefaultConnectivityMonitorTest.java   
public void connect() {
  NetworkInfo networkInfo =
      ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, 0, 0, true, true);
  shadowConnectivityManager.setActiveNetworkInfo(networkInfo);
}
项目:GitHub    文件:DefaultConnectivityMonitorTest.java   
void connect() {
  NetworkInfo networkInfo =
      ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, 0, 0, true, true);
  shadowConnectivityManager.setActiveNetworkInfo(networkInfo);
}
项目:materialistic    文件:ItemSyncAdapterTest.java   
private void setNetworkType(int type) {
    shadowOf((ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE))
            .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, type, 0, true, true));
}
项目:Fabric-Example-App-Android    文件:WebViewAdUrlGeneratorTest.java   
private NetworkInfo createNetworkInfo(int type) {
    return ShadowNetworkInfo.newInstance(null,
            type,
            NETWORK_TYPE_UNKNOWN, true, true);
}
项目:FullRobolectricTestSample    文件:Robolectric.java   
public static ShadowNetworkInfo shadowOf(NetworkInfo instance) {
  return (ShadowNetworkInfo) shadowOf_(instance);
}
项目:open    文件:PagerResultsFragmentTest.java   
private void simulateNoNetworkConnection() {
    ConnectivityManager cm = (ConnectivityManager) act.getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    ShadowNetworkInfo shadowNetworkInfo = shadowOf(networkInfo);
    shadowNetworkInfo.setConnectionStatus(false);
}