@Test public void testOnStop() { ShadowActivity shadowActivity = Shadows.shadowOf(activity); activity.onStop(); assertFalse(shadowActivity.isFinishing()); }
@Test public void execute_whenUberAppInsalled_shouldPointToUberApp() throws IOException { String expectedUri = readUriResourceWithUserAgentParam( "src/test/resources/deeplinkuris/just_client_provided", USER_AGENT_DEEPLINK); Activity activity = Robolectric.setupActivity(Activity.class); ShadowActivity shadowActivity = shadowOf(activity); RobolectricPackageManager packageManager = (RobolectricPackageManager) shadowActivity.getPackageManager(); PackageInfo uberPackage = new PackageInfo(); uberPackage.packageName = UBER_PACKAGE_NAME; packageManager.addPackage(uberPackage); RideParameters rideParameters = new RideParameters.Builder().build(); RequestDeeplink requestDeeplink = new RequestDeeplink.Builder() .setClientId(CLIENT_ID) .setRideParameters(rideParameters) .build(); requestDeeplink.execute(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertEquals(expectedUri, startedIntent.getData().toString()); }
@Test public void shouldCallFinishInOnBackPressed() { Activity activity = new Activity(); activity.onBackPressed(); ShadowActivity shadowActivity = shadowOf(activity); assertTrue(shadowActivity.isFinishing()); }
@Test public void pauseAndThenResumeGoesThroughTheFullLifeCycle() throws Exception { TestActivity activity = new TestActivity(); ShadowActivity shadow = shadowOf(activity); shadow.pauseAndThenResume(); activity.transcript.assertEventsSoFar("onPause", "onStop", "onRestart", "onStart", "onResume"); }
@Test public void shouldOpenGallerySoUserCanPickAnPicture() throws Exception { imageButton(activity, R.id.profile_picture).performClick(); ShadowActivity shadowActivity = shadowOf(activity); ShadowActivity.IntentForResult activityForResult = shadowActivity.getNextStartedActivityForResult(); assertThat(activityForResult.requestCode, is(equalTo(SELECT_PICTURE))); }
@Test public void testRefreshBluetooth() { ShadowActivity shadowActivity = Shadows.shadowOf(activity); activity.findViewById(R.id.refresh_bluetooth).performClick(); assertEquals( "Unexpected toast text", shadowActivity.getString(R.string.scan_for_devices), ShadowToast.getTextOfLatestToast()); }
@Test public void shouldSupportCurrentFocus() { activity = create(DialogLifeCycleActivity.class); ShadowActivity shadow = shadowOf(activity); assertNull(shadow.getCurrentFocus()); View view = new View(activity); shadow.setCurrentFocus(view); assertEquals(view, shadow.getCurrentFocus()); }
@Test public void shouldOpenPreferencesWhenPreferenceButtonFromMainMenuSelected() { MenuItem item = new RoboMenuItem(R.id.main_menu_preferences); activity.onOptionsItemSelected(item); Intent expectedIntent = new Intent(activity, TestbedPreferenceActivity.class); ShadowActivity shadowActivity = shadowOf(activity); Intent actualIntent = shadowActivity.getNextStartedActivity(); assertTrue(actualIntent.filterEquals(expectedIntent)); }
@Test public void skipLoginAndCancel() { ShadowActivity shadowActivity = Robolectric.shadowOf(loginActivity); ((TextView) loginActivity.findViewById(R.id.skipLogin)).performClick(); AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog(); ShadowAlertDialog dialog = Robolectric.shadowOf(latestAlertDialog); assertEquals(loginActivity.getString(R.string.noLoginWarn), dialog.getMessage()); assertTrue(latestAlertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).performClick()); assertNull(shadowActivity.getNextStartedActivity()); }
@Test public void testJustFinishIfResumedAgain() throws Exception { ActivityController<LauncherSettingsActivity> controller = Robolectric.buildActivity(LauncherSettingsActivity.class).attach().create().resume(); ShadowActivity shadowActivity = Shadows.shadowOf(controller.get()); Assert.assertFalse(shadowActivity.isFinishing()); controller.pause().stop(); Assert.assertFalse(shadowActivity.isFinishing()); controller.restart().resume(); Assert.assertTrue(shadowActivity.isFinishing()); }
@Test public void pressingTheButtonShouldStartAppetizerFragmentActivity() throws Exception { pressMeButton.performClick(); ShadowActivity shadowActivity = shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); ShadowIntent shadowIntent = shadowOf(startedIntent); assertThat( shadowIntent.getComponent().getClassName(), equalTo(AppetizerFragmentActivity.class.getName())); }
@Test public void testBluetoothSelected_nochange() { ShadowActivity shadowActivity = Shadows.shadowOf(activity); activity.selectedItemTV.setText("No Device Selected!"); activity.findViewById(R.id.done_selecting).performClick(); assertEquals( "Unexpected toast text", shadowActivity.getString(R.string.no_change_to_device), ShadowToast.getTextOfLatestToast()); assertTrue("finish() was not called", shadowActivity.isFinishing()); }
@Test public void shouldSupportStartActivityForResult() throws Exception { activity = create(DialogLifeCycleActivity.class); ShadowActivity shadowActivity = Robolectric.shadowOf(activity); Intent intent = new Intent().setClass(activity, DialogLifeCycleActivity.class); assertThat(shadowActivity.getNextStartedActivity()).isNull(); activity.startActivityForResult(intent, 142); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertThat(startedIntent).isNotNull(); assertThat(startedIntent).isSameAs(intent); }
@Test @Ignore("Robolectric does not work with appcompact AlertDialogs yet") public void shouldOpenAboutBoxWhenAboutButtonFromMainMenuSelected() { MenuItem item = new RoboMenuItem(R.id.main_menu_about); activity.onOptionsItemSelected(item); ShadowActivity shadowActivity = shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertNull(startedIntent); AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); ShadowAlertDialog shadowDialog = shadowOf(dialog); assertEquals("Helsinki Testbed Viewer 2.0.13", shadowDialog.getTitle()); }
@Test public void testOnItemClick_startsFullPhotoViewIntent() { subject.setupPhotoList(); RecyclerView recyclerView = subject.getPhotoRecyclerView(); recyclerView.measure(0, 0); recyclerView.layout(0, 0, 100, 1000); RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(0); View view = viewHolder.itemView; view.performClick(); Intent expectedIntent = new Intent(subject, PhotoFullScreenActivity.class); ShadowActivity shadowActivity = Shadows.shadowOf(subject); Intent actualIntent = shadowActivity.getNextStartedActivity(); Assertions.assertThat(actualIntent).isNotNull(); assertTrue(actualIntent.filterEquals(expectedIntent)); }
@Test public void shouldSupportPeekStartedActitivitesForResult() throws Exception { activity = create(DialogLifeCycleActivity.class); ShadowActivity shadowActivity = Robolectric.shadowOf(activity); Intent intent = new Intent().setClass(activity, DialogLifeCycleActivity.class); activity.startActivityForResult(intent, 142); ShadowActivity.IntentForResult intentForResult = shadowActivity.peekNextStartedActivityForResult(); assertThat(intentForResult).isNotNull(); assertThat(shadowActivity.peekNextStartedActivityForResult()).isSameAs(intentForResult); assertThat(intentForResult.intent).isNotNull(); assertThat(intentForResult.intent).isSameAs(intent); assertThat(intentForResult.requestCode).isEqualTo(142); }
@Test public void setDefaultKeyMode_shouldSetKeyMode() { int[] modes = { Activity.DEFAULT_KEYS_DISABLE, Activity.DEFAULT_KEYS_SHORTCUT, Activity.DEFAULT_KEYS_DIALER, Activity.DEFAULT_KEYS_SEARCH_LOCAL, Activity.DEFAULT_KEYS_SEARCH_GLOBAL }; Activity activity = new Activity(); ShadowActivity shadow = shadowOf(activity); for (int mode : modes) { activity.setDefaultKeyMode(mode); assertThat(shadow.getDefaultKeymode()).isEqualTo(mode).as("Unexpected key mode"); } }
@Test public void clickWithIntent() { MyActivity activity = new MyActivity(); TestMenu testMenu = new TestMenu(activity); testMenu.add(0, 10, 0, org.robolectric.R.string.ok); TestMenuItem testMenuItem = (TestMenuItem) testMenu.findItem(10); Assert.assertNull(testMenuItem.getIntent()); Intent intent = new Intent(activity, MyActivity.class); testMenuItem.setIntent(intent); testMenuItem.click(); Assert.assertNotNull(testMenuItem); ShadowActivity shadowActivity = Robolectric.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertNotNull(startedIntent); }
@Test public void recreateGoesThroughFullLifeCycle() throws Exception { TestActivity activity = new TestActivity(); ShadowActivity shadow = shadowOf(activity); shadow.recreate(); activity.transcript.assertEventsSoFar( "onSaveInstanceState", "onPause", "onStop", "onRetainNonConfigurationInstance", "onDestroy", "onCreate", "onStart", "onRestoreInstanceState", "onResume"); Integer storedValue = (Integer) activity.getLastNonConfigurationInstance(); assertEquals(5, storedValue.intValue()); }
@Test public void startAndStopManagingCursorTracksCursors() throws Exception { TestActivity activity = new TestActivity(); ShadowActivity shadow = shadowOf(activity); assertThat(shadow.getManagedCursors()).isNotNull(); assertThat(shadow.getManagedCursors().size()).isEqualTo(0); Cursor c = Robolectric.newInstanceOf(SQLiteCursor.class); activity.startManagingCursor(c); assertThat(shadow.getManagedCursors()).isNotNull(); assertThat(shadow.getManagedCursors().size()).isEqualTo(1); assertThat(shadow.getManagedCursors().get(0)).isSameAs(c); activity.stopManagingCursor(c); assertThat(shadow.getManagedCursors()).isNotNull(); assertThat(shadow.getManagedCursors().size()).isEqualTo(0); }
@Test public void execute_whenNoUberApp_shouldPointToMobileSite() throws IOException { String expectedUri = readUriResourceWithUserAgentParam( "src/test/resources/deeplinkuris/no_app_installed", USER_AGENT_DEEPLINK); Activity activity = Robolectric.setupActivity(Activity.class); ShadowActivity shadowActivity = shadowOf(activity); RideParameters rideParameters = new RideParameters.Builder().build(); RequestDeeplink requestDeeplink = new RequestDeeplink.Builder() .setClientId(CLIENT_ID) .setRideParameters(rideParameters) .build(); requestDeeplink.execute(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); assertEquals(expectedUri, startedIntent.getData().toString()); }
@Test public void callOnXxxMethods_shouldCallProtectedVersions() throws Exception { final Transcript transcript = new Transcript(); Activity activity = new OnMethodTestActivity(transcript); ShadowActivity shadowActivity = shadowOf(activity); Bundle bundle = new Bundle(); bundle.putString("key", "value"); shadowActivity.callOnCreate(bundle); transcript.assertEventsSoFar("onCreate was called with value"); shadowActivity.callOnStart(); transcript.assertEventsSoFar("onStart was called"); shadowActivity.callOnRestoreInstanceState(null); transcript.assertEventsSoFar("onRestoreInstanceState was called"); shadowActivity.callOnPostCreate(null); transcript.assertEventsSoFar("onPostCreate was called"); shadowActivity.callOnRestart(); transcript.assertEventsSoFar("onRestart was called"); shadowActivity.callOnResume(); transcript.assertEventsSoFar("onResume was called"); shadowActivity.callOnPostResume(); transcript.assertEventsSoFar("onPostResume was called"); Intent intent = new Intent("some action"); shadowActivity.callOnNewIntent(intent); transcript.assertEventsSoFar("onNewIntent was called with " + intent); shadowActivity.callOnSaveInstanceState(null); transcript.assertEventsSoFar("onSaveInstanceState was called"); shadowActivity.callOnPause(); transcript.assertEventsSoFar("onPause was called"); shadowActivity.callOnUserLeaveHint(); transcript.assertEventsSoFar("onUserLeaveHint was called"); shadowActivity.callOnStop(); transcript.assertEventsSoFar("onStop was called"); shadowActivity.callOnDestroy(); transcript.assertEventsSoFar("onDestroy was called"); }