@Test public void shouldSupportStartActivityForResult() throws Exception { activity = Robolectric.setupActivity(DialogLifeCycleActivity.class); ShadowActivity shadowActivity = 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); }
@Implementation protected void onBackPressed() { finish(); }
@Test public void testCreate() { assertNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_share)); }
@Override public ActivityResult getActivityResult() { checkNotNull(controller); checkState(controller.get().isFinishing(), "You must finish your Activity first"); ShadowActivity shadowActivity = Shadow.extract(controller.get()); return new ActivityResult(shadowActivity.getResultCode(), shadowActivity.getResultIntent()); }
@Test public void send_shouldNotReusePreviouslyFilledInIntentData() throws Exception { Intent intent = new Intent("action"); Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent firstFillIntent = new Intent().putExtra("KEY1", 23).putExtra("KEY2", 24); pendingIntent.send(otherContext, 0, firstFillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); shadowActivity.clearNextStartedActivities(); Intent secondFillIntent = new Intent().putExtra("KEY1", 50); pendingIntent.send(otherContext, 0, secondFillIntent); Intent i = shadowActivity.getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i.filterEquals(intent)).isTrue(); // Ignore extras. assertThat(i.getIntExtra("KEY1", -1)).isEqualTo(50); assertThat(i.hasExtra("KEY2")).isFalse(); }
@Test public void testClearRecentSearches() { ShadowSearchRecentSuggestions.historyClearCount = 0; assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_clear_recent)); shadowOf(activity).clickMenuItem(R.id.menu_clear_recent); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertEquals(1, ShadowSearchRecentSuggestions.historyClearCount); }
@Implementation protected void recreate() { Bundle outState = new Bundle(); final ActivityInvoker invoker = new ActivityInvoker(); invoker.call("onSaveInstanceState", Bundle.class).with(outState); invoker.call("onPause").withNothing(); invoker.call("onStop").withNothing(); Object nonConfigInstance = invoker.call("onRetainNonConfigurationInstance").withNothing(); setLastNonConfigurationInstance(nonConfigInstance); invoker.call("onDestroy").withNothing(); invoker.call("onCreate", Bundle.class).with(outState); invoker.call("onStart").withNothing(); invoker.call("onRestoreInstanceState", Bundle.class).with(outState); invoker.call("onResume").withNothing(); }
/** * @return the {@code contentView} set by one of the {@code setContentView()} methods */ public View getContentView() { return ((ViewGroup) getWindow().findViewById(android.R.id.content)).getChildAt(0); }
@Test public void testDelayedFailedResponse() { doSend(); shadowOf(activity).clickMenuItem(android.R.id.home); replyCallback.getValue().onDone(false); assertNull(shadowOf(activity).getNextStartedActivity()); }
public void receiveResult(Intent requestIntent, int resultCode, Intent resultIntent) { ActivityThread activityThread = (ActivityThread) RuntimeEnvironment.getActivityThread(); ShadowInstrumentation shadowInstrumentation = Shadow.extract(activityThread.getInstrumentation()); int requestCode = shadowInstrumentation.getRequestCodeForIntent(requestIntent); callOnActivityResult(requestCode, resultCode, resultIntent); }
@Implementation protected final void showDialog(int id) { showDialog(id, null); }
/** * Constructs a new Window (a {@link com.android.internal.policy.impl.PhoneWindow}) if no window * has previously been set. * * @return the window associated with this Activity */ @Implementation protected Window getWindow() { Window window = directlyOn(realActivity, Activity.class).getWindow(); if (window == null) { try { window = ShadowWindow.create(realActivity); setWindow(window); } catch (Exception e) { throw new RuntimeException("Window creation failed!", e); } } return window; }
@Test public void testOnOptionsItemSelected_postSelected_shouldSetResultCommentAndFinish() { ShadowActivity shadowActivity = Shadows.shadowOf(activity); RoboMenuItem roboMenuItem = new RoboMenuItem(); roboMenuItem.setItemId(R.id.action_post); EditText editText = (EditText) activity.findViewById(R.id.edit_response); editText.setText(POSTED_COMMENT_TEXT); activity.onOptionsItemSelected(roboMenuItem); assertEquals(shadowActivity.getResultIntent().getStringExtra(ReplyActivity .KEY_POSTED_COMMENT), POSTED_COMMENT_TEXT); assertEquals(shadowActivity.getResultCode(), RESULT_OK); assertTrue(shadowActivity.isFinishing()); }
@Test public void testCancel() { activity.onBackPressed(); assertThat(shadowOf(activity).getResultCode()).isEqualTo(Activity.RESULT_CANCELED); }
@Override public ActivityResult getActivityResult() { checkNotNull(controller); checkState(controller.get().isFinishing(), "You must finish your Activity first"); ShadowActivity shadowActivity = Shadow.extract(controller.get()); return new ActivityResult(shadowActivity.getResultCode(), shadowActivity.getResultIntent()); }
shadowActivity.setLastNonConfigurationInstance(activityConfigInstance);
@Test public void testReset() { PreferenceManager.getDefaultSharedPreferences(activity) .edit() .putBoolean(activity.getString(R.string.pref_color_code), false) .apply(); assertFalse(Preferences.colorCodeEnabled(activity)); assertNotNull(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_reset)); shadowOf(activity).clickMenuItem(R.id.menu_reset); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick(); assertTrue(Preferences.colorCodeEnabled(activity)); }
/** * Checks to ensure that the{@code contentView} has been set * * @param id ID of the view to find * @return the view * @throws RuntimeException if the {@code contentView} has not been called first */ @Implementation protected View findViewById(int id) { return getWindow().findViewById(id); }