@Test public void testWindowsFocusChanged() throws Exception { BaseChromiumApplication app = (BaseChromiumApplication) Robolectric.application; WindowFocusChangedListener mock = mock(WindowFocusChangedListener.class); app.registerWindowFocusChangedListener(mock); ActivityController<Activity> controller = Robolectric.buildActivity(Activity.class).create().start().visible(); TrackingShadowActivity shadow = (TrackingShadowActivity) Robolectric.shadowOf(controller.get()); controller.get().getWindow().getCallback().onWindowFocusChanged(true); // Assert that listeners were notified. verify(mock).onWindowFocusChanged(controller.get(), true); // Also ensure that the original activity is forwarded the notification. Assert.assertEquals(1, shadow.mWindowFocusCalls); }
@Override public void setUp() throws Exception { super.setUp(); RecipeListActivity recipeListActivity = new RecipeListActivity() { @Override public void setTheme(int resid) { super.setTheme(R.style.AppTheme_NoActionBar); } public RecipesAdapter getAdapter() { return adapter; } public RecipeListPresenter getPresenter() { return presenter; } }; controller = ActivityController.of(Robolectric.getShadowsAdapter(), recipeListActivity).create().visible(); activity = controller.get(); view = (RecipeListView)activity; onItemClickListener = (OnItemClickListener)activity; shadowActivity = shadowOf(activity); }
@Override public void setUp() throws Exception { super.setUp(); RecipeMainActivity recipeMainActivity = new RecipeMainActivity() { public ImageLoader getImageLoader() { return imageLoader; } public RecipeMainPresenter getPresenter() { return presenter; } }; controller = ActivityController.of(Robolectric.getShadowsAdapter(), recipeMainActivity).create().visible(); activity = controller.get(); view = (RecipeMainView) activity; shadowAcivity = shadowOf(activity); }
private void testLifecycle(ActivityController<? extends LifecycleOwner> controller) { LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver<Lifecycle.Event> testObserver = provider.lifecycle().test(); controller.create(); controller.start(); controller.resume(); controller.pause(); controller.stop(); controller.destroy(); testObserver.assertValues( Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY ); }
private void testBindUntilEvent(ActivityController<? extends LifecycleOwner> controller) { LifecycleProvider<Lifecycle.Event> activity = AndroidLifecycle.createLifecycleProvider(controller.get()); TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); controller.create(); testObserver.assertNotComplete(); controller.start(); testObserver.assertNotComplete(); controller.resume(); testObserver.assertNotComplete(); controller.pause(); testObserver.assertNotComplete(); controller.stop(); testObserver.assertComplete(); }
private void testLifecycle(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController<?> controller = startFragment(fragment); TestObserver<Lifecycle.Event> testObserver = AndroidLifecycle.createLifecycleProvider(owner).lifecycle().test(); controller.start(); controller.resume(); controller.pause(); controller.stop(); controller.destroy(); testObserver.assertValues( Lifecycle.Event.ON_CREATE, Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME, Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP, Lifecycle.Event.ON_DESTROY ); }
private void testBindUntilEvent(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; ActivityController<?> controller = startFragment(fragment); TestObserver<Object> testObserver = observable.compose(AndroidLifecycle.createLifecycleProvider(owner).bindUntilEvent(Lifecycle.Event.ON_STOP)).test(); testObserver.assertNotComplete(); controller.start(); testObserver.assertNotComplete(); controller.resume(); testObserver.assertNotComplete(); controller.pause(); testObserver.assertNotComplete(); controller.stop(); testObserver.assertComplete(); }
private void testLifecycle(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) { LifecycleProvider<ActivityEvent> activity = controller.get(); TestObserver<ActivityEvent> testObserver = activity.lifecycle().test(); controller.create(); controller.start(); controller.resume(); controller.pause(); controller.stop(); controller.destroy(); testObserver.assertValues( ActivityEvent.CREATE, ActivityEvent.START, ActivityEvent.RESUME, ActivityEvent.PAUSE, ActivityEvent.STOP, ActivityEvent.DESTROY ); }
private void testBindUntilEvent(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) { LifecycleProvider<ActivityEvent> activity = controller.get(); TestObserver<Object> testObserver = observable.compose(activity.bindUntilEvent(STOP)).test(); controller.create(); testObserver.assertNotComplete(); controller.start(); testObserver.assertNotComplete(); controller.resume(); testObserver.assertNotComplete(); controller.pause(); testObserver.assertNotComplete(); controller.stop(); testObserver.assertComplete(); }
@Test public void LoadReceipt() { Intent intent = new Intent(); final Bundle bundle = new Bundle(); bundle.putString("receipt", "receipt"); intent.putExtras(bundle); ActivityController activityController = Robolectric.buildActivity( ReceiptViewActivity.class).withIntent(intent).create(); activityController.start(); activityController.resume(); Activity activity = (Activity)activityController.get(); WebView receiptView = (WebView)activity.findViewById(R.id.imageView); ShadowWebView.LoadDataWithBaseURL loadedData = shadowOf(receiptView).getLastLoadDataWithBaseURL(); assertEquals("", loadedData.baseUrl); assertEquals("text/html", loadedData.mimeType); assertEquals("utf-8", loadedData.encoding); assertNull(loadedData.historyUrl); assertTrue(loadedData.data.contains("src=\"file://receipt\"")); }
private void applyState(ActivityState state, ActivityController controller) { switch (state) { case STARTED: controller.start(); break; case RESUMED: controller.resume(); break; case PAUSED: controller.pause(); break; case STOPPED: controller.stop(); break; case DESTROYED: controller.destroy(); break; case CREATED: default: controller.create(); break; } }
@Test public void testPresenterReceivesPermissionWhenRequestedFromActivity() { TodayWeatherPresenterBuilder builder = new TodayWeatherPresenterBuilder( new TodayWeatherFragmentController(new TodayWeatherFragment(), MainActivity.class), provider) .in(R.id.container); TodayWeatherPresenterBuilder.BindingResult bindingResult = configurePresenter(builder, ViewType.REAL, PresenterType.MOCK); TodayWeatherPresenter presenter = bindingResult.presenter(); ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class); controller.setup(); String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION}; int[] grantResults = {PackageManager.PERMISSION_GRANTED}; MainActivity mainActivity = controller.get(); mainActivity.onRequestPermissionsResult(1, permissions, grantResults); verify(presenter).onEventPermissionsResult(eq(new PermissionEvent(1, permissions, grantResults))); }
@Test public void testOpenExternalComment() { ActivityController<TestListActivity> controller = Robolectric.buildActivity(TestListActivity.class); TestListActivity activity = controller.create().start().resume().get(); AppUtils.openExternal(activity, mock(PopupMenu.class), new View(activity), new TestHnItem(1), null); assertNull(ShadowAlertDialog.getLatestAlertDialog()); AppUtils.openExternal(activity, mock(PopupMenu.class), new View(activity), new TestHnItem(1) { @Override public String getUrl() { return String.format(HackerNewsClient.WEB_ITEM_PATH, "1"); } }, null); assertNull(ShadowAlertDialog.getLatestAlertDialog()); controller.destroy(); }
@Test public void whenTheActivityIsStoppedThenTheGroceryLocatorServiceIsStartedWithoutGPSExtra() { ActivityController<GroceryStoresActivity> activityController = Robolectric.buildActivity(GroceryStoresActivity.class);; activityController.create().start().get(); ShadowLocation.setDistanceBetween(new float[]{(float) GroceryReminderConstants.LOCATION_SEARCH_RADIUS_METERS}); activityController.pause().stop(); ShadowActivity shadowActivity = (ShadowActivity)Shadows.shadowOf(activity); Intent serviceIntent = shadowActivity.peekNextStartedService(); assertEquals(GroceryLocatorService.class.getCanonicalName(), serviceIntent.getComponent().getClassName()); assertFalse(serviceIntent.getBooleanExtra(GroceryReminderConstants.LISTEN_FOR_GPS_EXTRA, false)); activityController.destroy(); }
@Test public void onLoginLoad_withEmptyScopes_shouldReturnErrorResultIntent() { Intent intent = new Intent(); intent.putExtra(LoginActivity.EXTRA_SESSION_CONFIGURATION, new SessionConfiguration.Builder().setClientId("clientId").build()); ActivityController<LoginActivity> controller = Robolectric.buildActivity(LoginActivity.class) .withIntent(intent) .create(); ShadowActivity shadowActivity = shadowOf(controller.get()); assertThat(shadowActivity.getResultCode()).isEqualTo(Activity.RESULT_CANCELED); assertThat(shadowActivity.getResultIntent()).isNotNull(); assertThat(getScopeFromIntent(shadowActivity.getResultIntent())) .isEqualTo(AuthenticationError.INVALID_SCOPE); assertThat(shadowActivity.isFinishing()).isTrue(); }
@Test public void onLoadLoginView_withNoRedirectUrl_shouldReturnError() { SessionConfiguration config = new SessionConfiguration.Builder().setClientId("clientId").build(); Intent intent = new Intent(); intent.putExtra(LoginActivity.EXTRA_SESSION_CONFIGURATION, config); final ActivityController<LoginActivity> controller = Robolectric.buildActivity(LoginActivity.class) .withIntent(intent); final ShadowActivity shadowActivity = Shadows.shadowOf(controller.get()); controller.create(); assertThat(shadowActivity.isFinishing()).isTrue(); assertThat(shadowActivity.getResultCode()).isEqualTo(Activity.RESULT_CANCELED); assertThat(shadowActivity.getResultIntent()).isNotNull(); assertThat(shadowActivity.getResultIntent().getStringExtra(LoginManager.EXTRA_ERROR)).isNotEmpty(); }
@Test public void itShouldVerifySessionOnResume() { // given LoginPresenter loginPresenter = mock(LoginPresenter.class); ActivityController<LoginActivity> controller = Robolectric.buildActivity(LoginActivity.class); LoginActivity activity = controller.create().get(); activity.presenter = loginPresenter; // when controller.resume(); //then verify(loginPresenter).verifySession(); }
@Test public void itShouldRequestFacebookLoginWhenClickFacebookButton() { // given LoginPresenter loginPresenter = mock(LoginPresenter.class); ActivityController<LoginActivity> controller = Robolectric.buildActivity(LoginActivity.class); LoginActivity activity = controller.create().get(); activity.presenter = loginPresenter; controller.resume(); // when activity.btnFacebookLogin.callOnClick(); // then verify(loginPresenter).requestLogin(any(LoginStrategyChooser.class), eq(LoginStrategy.FACEBOOK), eq(activity)); }
@Test public void itShouldFinishWhenGoToMain() { // given LoginPresenter loginPresenter = mock(LoginPresenter.class); ActivityController<LoginActivity> controller = Robolectric.buildActivity(LoginActivity.class); LoginActivity activity = controller.create().get(); activity.presenter = loginPresenter; controller.resume(); // when activity.gotoMainActivity(); // then assertTrue(activity.isFinishing()); }
@Test public void shouldVerifyInstance_stateTitle() { when(environmentService.findAll()).thenReturn(Observable.<List<EnvironmentModel>>empty()); ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class); activity = controller .create() .start() .resume() .visible() .get(); ButterKnife.bind(this, activity); assertEquals("wrong title", labelAppName, activity.getSupportActionBar().getTitle()); String CUSTOM_TITLE = "myNewTitle"; activity.getSupportActionBar().setTitle(CUSTOM_TITLE); activity = controller .stop() .resume() .visible() .get(); assertEquals("wrong title", CUSTOM_TITLE, activity.getSupportActionBar().getTitle()); }
@Before public void setUp() throws Exception { htmlInterstitialWebView = TestHtmlInterstitialWebViewFactory.getSingletonMock(); resetMockedView(htmlInterstitialWebView); Context context = Robolectric.buildActivity(Activity.class).create().get(); Intent moPubActivityIntent = MoPubActivity.createIntent(context, EXPECTED_HTML_DATA, mockAdReport, EXPECTED_IS_SCROLLABLE, EXPECTED_REDIRECT_URL, EXPECTED_CLICKTHROUGH_URL, EXPECTED_ORIENTATION, testBroadcastIdentifier); final ActivityController<MoPubActivity> subjectController = Robolectric.buildActivity(MoPubActivity.class).withIntent(moPubActivityIntent); subject = subjectController.get(); ShadowLocalBroadcastManager.getInstance(subject).registerReceiver(broadcastReceiver, getHtmlInterstitialIntentFilter()); subjectController.create(); customEventInterstitialListener = mock(CustomEventInterstitialListener.class); }
/** * Testing initialization */ @Test @SuppressLint("RtlHardcoded") public void initializeTest() { ActivityController<? extends CourseBaseActivity> controller = Robolectric.buildActivity(getActivityClass()).withIntent(getIntent()); CourseBaseActivity activity = controller.get(); controller.create(); assertNotNull(activity.findViewById(R.id.last_accessed_bar)); DrawerLayout drawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout); if (drawerLayout != null) { assertEquals(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, drawerLayout.getDrawerLockMode(Gravity.LEFT)); assertEquals(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, drawerLayout.getDrawerLockMode(Gravity.RIGHT)); } controller.postCreate(null).resume().postResume().visible(); }
/** * Testing process start and finish method functionality */ @Test public void processLifecycleTest() { // We need to retrieve the progressWheel view before calling visible(), since that // initializes fragment views as well, which might add other views with the same id ActivityController<? extends CourseBaseActivity> controller = Robolectric.buildActivity(getActivityClass()).withIntent(getIntent()) .create().start().postCreate(null).resume(); CourseBaseActivity activity = controller.get(); ProgressBar progressWheel = (ProgressBar) activity.findViewById(R.id.loading_indicator); controller.visible(); if (progressWheel == null) { activity.startProcess(); activity.finishProcess(); } else { assertThat(progressWheel).isNotVisible(); activity.startProcess(); assertThat(progressWheel).isVisible(); activity.finishProcess(); assertThat(progressWheel).isNotVisible(); } }
/** * Generic method for running the initialization tests and returning the * controller. * * @param intent The {@link Intent} to start the {@link CourseOutlineActivity} with * @return The {@link ActivityController} instance used to initialize the * {@link CourseOutlineActivity} */ private ActivityController<? extends CourseOutlineActivity> initialize(Intent intent) { ActivityController<? extends CourseOutlineActivity> controller = Robolectric.buildActivity(getActivityClass()).withIntent(intent); CourseOutlineActivity activity = controller.create(null).postCreate(null).get(); // TODO: Write a comprehensive and isolated test suite for the Fragment Fragment fragment = activity.getSupportFragmentManager() .findFragmentByTag(CourseOutlineFragment.TAG); assertNotNull(fragment); assertThat(fragment).isInstanceOf(CourseOutlineFragment.class); assertTrue(fragment.getRetainInstance()); Bundle args = fragment.getArguments(); assertNotNull(args); Bundle data = intent.getBundleExtra(Router.EXTRA_BUNDLE); assertEquals(data.getSerializable(Router.EXTRA_COURSE_DATA), args.getSerializable(Router.EXTRA_COURSE_DATA)); if (provideCourseId) { assertEquals(data.getString(Router.EXTRA_COURSE_COMPONENT_ID), args.getString(Router.EXTRA_COURSE_COMPONENT_ID)); } return controller; }
@Test public void testArgKeyGeneration() { String s = UUID.randomUUID().toString(); int i = new Random(42).nextInt(); Bundle bundle = new Bundle(); bundle.putString("ARG_A_STRING", s); bundle.putInt("ARG_AN_INT", i); ActivityController<SimpleFragmentActivity> initialController = Robolectric.buildActivity(SimpleFragmentActivity.class).create(); SimpleFragmentActivity simpleActivity = initialController.start().restart().visible().get(); KeyGenFragment fragment = KeyGenFragment.newInstance(); fragment.setArguments(bundle); simpleActivity.replaceFragment(fragment); assertEquals(s, fragment.aString); assertEquals(i, fragment.anInt); }
@Test public void testMultiAnnotationActivity() throws Exception { Random random = new Random(); Intent intent = new Intent(RuntimeEnvironment.application, MultiAnnotationActivity.class); int extraInt = random.nextInt(); intent.putExtra(MultiAnnotationActivity.EXTRA_INT, extraInt); ActivityController<MultiAnnotationActivity> saveController = Robolectric.buildActivity(MultiAnnotationActivity.class).withIntent(intent).create(); MultiAnnotationActivity activity = saveController.start().restart().visible().get(); assertEquals(extraInt, activity.i); int activityInt = random.nextInt(); activity.i = activityInt; Bundle saveState = new Bundle(); saveController.saveInstanceState(saveState); ActivityController<MultiAnnotationActivity> restoreController = Robolectric.buildActivity(MultiAnnotationActivity.class).withIntent(intent) .create(saveState); activity = restoreController.start().restart().visible().get(); assertEquals(activityInt, activity.i); }
@Test public void test_00_MainActivity_FullLifecycle() { final ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class); final MainActivity activity = controller.get(); assertThat(activity, notNullValue()); fullLifecycle(controller, new Runnable() { @Override public void run() { // binding manager instance should be available assertThat(activity.getBindingsManager(), notNullValue()); // main activity does not have any bindings assertThat(activity.getBindingsManager().getBindings(), hasSize(0)); } }); }
@Test public void test_00_FullLifeCycle_WithIntent() { final Intent intent = MainActivity.showPin(RuntimeEnvironment.application); final ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class).withIntent(intent); final MainActivity activity = controller.get(); assertThat(activity, notNullValue()); fullLifecycle(controller, new Runnable() { @Override public void run() { final TextView tv = (TextView) activity.findViewById(R.id.tv_feedback); // tv_feedback is available only on PinFragment assertThat(tv, notNullValue()); trace("PinFragment found"); } }); }
@Before public void prepare() { MockitoAnnotations.initMocks(this); ActivityController<MainActivity_> activityController = Robolectric.buildActivity(MainActivity_.class); activityController.create(); activity = activityController.get(); activity.service = oauthService; activity.networkHelper = networkHelper; TestUtil.mockBackgroundExecutor(); TestUtil.mockValidUser(activity.prefs); AQUtility.setExceptionHandler(exceptionHandler); }
@Before public void prepare() { MockitoAnnotations.initMocks(this); TestUtil.mockBackgroundExecutor(); projectFragment = new ProjectFragment_(); projectFragment.spiceManager = this.spiceManager; ActivityController<MainActivity_> activityController = Robolectric.buildActivity(MainActivity_.class).create(); MainActivity activity = activityController.get(); TestUtil.mockValidUser(activity.prefs); activity.fragmentFactory = this.fragmentFactory; given(fragmentFactory.getProjectsFragment()).willReturn(projectsFragment); given(fragmentFactory.getProjectFragment()).willReturn(projectFragment); activityController.start().resume(); activity.onProjectSelected(PROJECT_ID, USERNAME); }
@Before public void prepare() { MockitoAnnotations.initMocks(this); TestUtil.mockBackgroundExecutor(); favoritesFragment = new FavoritesFragment_(); favoritesFragment.spiceManager = this.spiceManager; ActivityController<MainActivity_> activityController = Robolectric.buildActivity(MainActivity_.class).create(); activity = activityController.get(); favoritesFragment.prefs = activity.prefs; TestUtil.mockValidUser(activity.prefs); activity.fragmentFactory = this.fragmentFactory; given(fragmentFactory.getProjectsFragment()).willReturn(projectsFragment); given(fragmentFactory.getFavoritesFragment()).willReturn(favoritesFragment); activityController.start().resume(); activity.menuMyFavoritesClicked(); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); ActivityController<PreferencesActivity_> activityController = Robolectric.buildActivity(PreferencesActivity_.class); activityController.create(); activity = activityController.get(); prefs = activity.prefs; doAutoUpdate = (CheckBox) activity.findViewById(R.id.doAutoUpdate); doAutoUpdateWifiOnly = (CheckBox) activity .findViewById(R.id.doAutoUpdateWifiOnly); accountList = (ListView) activity.findViewById(R.id.accountList); addAccount = (Button) activity.findViewById(R.id.addAccount); }
@Before public void setUp() { ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class); activity = spy(activityController.setup().get()); rxActivityResults = spy(new RxActivityResults(activity)); rxActivityResults.mRxActivityResultsFragment = spy(rxActivityResults.mRxActivityResultsFragment); when(rxActivityResults.mRxActivityResultsFragment.getActivity()) .thenReturn(activity); }
@Before public void init() { ActivityController<Activity> activityController = Robolectric.buildActivity(Activity.class).create().start().resume() .visible(); mActivity = activityController.get(); mView = new CoachmarkView(mActivity); }
private void testBindToLifecycle(ActivityController<? extends LifecycleOwner> controller) { LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(controller.get()); controller.create(); TestObserver<Object> createObserver = observable.compose(provider.bindToLifecycle()).test(); controller.start(); createObserver.assertNotComplete(); TestObserver<Object> startObserver = observable.compose(provider.bindToLifecycle()).test(); controller.resume(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); TestObserver<Object> resumeObserver = observable.compose(provider.bindToLifecycle()).test(); controller.pause(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); resumeObserver.assertComplete(); TestObserver<Object> pauseObserver = observable.compose(provider.bindToLifecycle()).test(); controller.stop(); createObserver.assertNotComplete(); startObserver.assertComplete(); pauseObserver.assertComplete(); TestObserver<Object> stopObserver = observable.compose(provider.bindToLifecycle()).test(); controller.destroy(); createObserver.assertComplete(); stopObserver.assertComplete(); }
private void testBindToLifecycle(LifecycleOwner owner) { Fragment fragment = (Fragment) owner; LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(owner); ActivityController<?> controller = startFragment(fragment); TestObserver<Object> createObserver = observable.compose(provider.bindToLifecycle()).test(); controller.start(); createObserver.assertNotComplete(); TestObserver<Object> startObserver = observable.compose(provider.bindToLifecycle()).test(); controller.resume(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); TestObserver<Object> resumeObserver = observable.compose(provider.bindToLifecycle()).test(); controller.pause(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); resumeObserver.assertComplete(); TestObserver<Object> pauseObserver = observable.compose(provider.bindToLifecycle()).test(); controller.stop(); createObserver.assertNotComplete(); startObserver.assertComplete(); pauseObserver.assertComplete(); TestObserver<Object> stopObserver = observable.compose(provider.bindToLifecycle()).test(); controller.destroy(); createObserver.assertComplete(); stopObserver.assertComplete(); }
private ActivityController<FragmentActivity> startFragment(Fragment fragment) { ActivityController<FragmentActivity> controller = Robolectric.buildActivity(FragmentActivity.class); controller.create(); controller.get().getSupportFragmentManager() .beginTransaction() .add(android.R.id.content, fragment) .commitNow(); return controller; }
private void testBindToLifecycle(ActivityController<? extends LifecycleProvider<ActivityEvent>> controller) { LifecycleProvider<ActivityEvent> activity = controller.get(); controller.create(); TestObserver<Object> createObserver = observable.compose(activity.bindToLifecycle()).test(); controller.start(); createObserver.assertNotComplete(); TestObserver<Object> startObserver = observable.compose(activity.bindToLifecycle()).test(); controller.resume(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); TestObserver<Object> resumeObserver = observable.compose(activity.bindToLifecycle()).test(); controller.pause(); createObserver.assertNotComplete(); startObserver.assertNotComplete(); resumeObserver.assertComplete(); TestObserver<Object> pauseObserver = observable.compose(activity.bindToLifecycle()).test(); controller.stop(); createObserver.assertNotComplete(); startObserver.assertComplete(); pauseObserver.assertComplete(); TestObserver<Object> stopObserver = observable.compose(activity.bindToLifecycle()).test(); controller.destroy(); createObserver.assertComplete(); stopObserver.assertComplete(); }
@Test public void addOneGiftCard() { ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create(); Activity mainActivity = (Activity)activityController.get(); activityController.start(); activityController.resume(); TextView helpText = (TextView)mainActivity.findViewById(R.id.helpText); ListView list = (ListView)mainActivity.findViewById(R.id.list); assertEquals(0, list.getCount()); DBHelper db = new DBHelper(mainActivity); db.insertGiftCard("store", "cardId", "value", "receipt"); assertEquals(View.VISIBLE, helpText.getVisibility()); assertEquals(View.GONE, list.getVisibility()); activityController.pause(); activityController.resume(); assertEquals(View.GONE, helpText.getVisibility()); assertEquals(View.VISIBLE, list.getVisibility()); assertEquals(1, list.getAdapter().getCount()); Cursor cursor = (Cursor)list.getAdapter().getItem(0); assertNotNull(cursor); }
@Test public void shouldFinishLoginActivityWhenCompleted() { Activity context = Robolectric .buildActivity(Activity.class) .create() .get(); AuthenticationRequest request = new AuthenticationRequest.Builder("test", AuthenticationResponse.Type.TOKEN, "test://test").build(); AuthenticationResponse response = new AuthenticationResponse.Builder() .setType(AuthenticationResponse.Type.TOKEN) .setAccessToken("test_token") .setExpiresIn(3600) .build(); Bundle bundle = new Bundle(); bundle.putParcelable(LoginActivity.REQUEST_KEY, request); Intent intent = new Intent(context, LoginActivity.class); intent.putExtra(LoginActivity.EXTRA_AUTH_REQUEST, bundle); ActivityController<LoginActivity> loginActivityActivityController = buildActivity(LoginActivity.class) .withIntent(intent); final LoginActivity loginActivity = loginActivityActivityController.get(); final ShadowActivity shadowLoginActivity = shadowOf(loginActivity); shadowLoginActivity.setCallingActivity(context.getComponentName()); loginActivityActivityController.create(); assertFalse(loginActivity.isFinishing()); loginActivity.onClientComplete(response); assertTrue(loginActivity.isFinishing()); assertEquals(Activity.RESULT_OK, shadowLoginActivity.getResultCode()); assertEquals(response, shadowLoginActivity.getResultIntent().getBundleExtra(LoginActivity.EXTRA_AUTH_RESPONSE).get(LoginActivity.RESPONSE_KEY)); }