@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 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 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 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 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 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 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"); }