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); } }
@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(); }
@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()); }
@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(); }
@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)); }
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 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(); }
@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)); }
@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)); }
@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)); }
@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)); }
@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)); }
@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)); }
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()); }
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()); }
public void connect() { NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, 0, 0, true, true); shadowConnectivityManager.setActiveNetworkInfo(networkInfo); }
void connect() { NetworkInfo networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, 0, 0, true, true); shadowConnectivityManager.setActiveNetworkInfo(networkInfo); }
private void setNetworkType(int type) { shadowOf((ConnectivityManager) service.getSystemService(Context.CONNECTIVITY_SERVICE)) .setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, type, 0, true, true)); }
private NetworkInfo createNetworkInfo(int type) { return ShadowNetworkInfo.newInstance(null, type, NETWORK_TYPE_UNKNOWN, true, true); }
public static ShadowNetworkInfo shadowOf(NetworkInfo instance) { return (ShadowNetworkInfo) shadowOf_(instance); }
private void simulateNoNetworkConnection() { ConnectivityManager cm = (ConnectivityManager) act.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); ShadowNetworkInfo shadowNetworkInfo = shadowOf(networkInfo); shadowNetworkInfo.setConnectionStatus(false); }