@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); }
@Test public void startActivities_withBundle_shouldStartAllActivities() { activity = Robolectric.setupActivity(DialogLifeCycleActivity.class); final Intent view = new Intent(Intent.ACTION_VIEW); final Intent pick = new Intent(Intent.ACTION_PICK); activity.startActivities(new Intent[] {view, pick}, new Bundle()); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(pick); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(view); }
@Test public void startActivities_shouldStartAllActivities() { activity = Robolectric.setupActivity(DialogLifeCycleActivity.class); final Intent view = new Intent(Intent.ACTION_VIEW); final Intent pick = new Intent(Intent.ACTION_PICK); activity.startActivities(new Intent[] {view, pick}); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(pick); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(view); }
@Test public void send_shouldNotFillIn_whenPendingIntentIsImmutable() throws Exception { Intent intent = new Intent(); Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, FLAG_IMMUTABLE); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent fillIntent = new Intent().putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); Intent i = shadowOf(otherContext).getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i).isSameAs(intent); assertThat(i.hasExtra("TEST")).isFalse(); }
@Test public void send_shouldFillInIntentData() 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 fillIntent = new Intent().putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); Intent i = shadowOf(otherContext).getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i.filterEquals(intent)).isTrue(); // Ignore extras. assertThat(i.getIntExtra("TEST", -1)).isEqualTo(23); }
@Test public void testSelectItemOpenWeb() { activity.onItemSelected(new TestWebItem() { @Override public String getUrl() { return "http://example.com"; } }); Intent actual = shadowOf(activity).getNextStartedActivity(); assertEquals(ItemActivity.class.getName(), actual.getComponent().getClassName()); }
@Test public void testItemClick() { assertEquals(2, adapter.getItemCount()); RecyclerView.ViewHolder holder = shadowAdapter.getViewHolder(0); holder.itemView.performClick(); assertNotNull(shadowOf(activity).getNextStartedActivity()); }
@Test public void send_shouldFillInLastIntentData() throws Exception { Intent[] intents = {new Intent("first"), new Intent("second")}; Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent fillIntent = new Intent(); fillIntent.putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); Intent second = shadowActivity.getNextStartedActivity(); assertThat(second).isNotNull(); assertThat(second.filterEquals(intents[1])).isTrue(); // Ignore extras. assertThat(second.getIntExtra("TEST", -1)).isEqualTo(23); Intent first = shadowActivity.getNextStartedActivity(); assertThat(first).isNotNull(); assertThat(first).isSameAs(intents[0]); assertThat(first.hasExtra("TEST")).isFalse(); }
@Test public void testSelectionToggle() { RecyclerView.ViewHolder holder = shadowAdapter.getViewHolder(0); holder.itemView.performLongClick(); holder.itemView.performClick(); assertNull(shadowOf(activity).getNextStartedActivity()); holder.itemView.performClick(); assertNull(shadowOf(activity).getNextStartedActivity()); }
@Test public void testRate() { activity.findViewById(R.id.button_rate).performClick(); assertNotNull(shadowOf(activity).getNextStartedActivity()); assertThat(activity).isFinishing(); }
@Test public void testLoginNoAccounts() { AppUtils.showLogin(context, null); assertThat(shadowOf(context).getNextStartedActivity()) .hasComponent(context, LoginActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testShare() { TestApplication.addResolver(new Intent(Intent.ACTION_SEND)); shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_share)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasAction(Intent.ACTION_SEND); }
@Test public void testReleaseNotes() { activity.findViewById(R.id.drawer_release).performClick(); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, ReleaseNotesActivity.class); }
@Test public void testList() { activity.findViewById(R.id.menu_list).performClick(); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, PreferencesActivity.class); }
@Test public void testDisplay() { activity.findViewById(R.id.drawer_display).performClick(); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, PreferencesActivity.class); }
@Test public void testReadability() { activity.findViewById(R.id.menu_readability).performClick(); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, PreferencesActivity.class); }
@Test public void testComments() { activity.findViewById(R.id.menu_comments).performClick(); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, PreferencesActivity.class); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testReply() { shadowAdapter.getViewHolder(0).itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); Assert.assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_comment)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, ComposeActivity.class); }
@Test public void testOpenUserProfile() { Preferences.setUsername(activity, "username"); drawerUser.performClick(); ((ShadowSupportDrawerLayout) Shadow.extract(activity.findViewById(R.id.drawer_layout))) .getDrawerListeners().get(0) .onDrawerClosed(activity.findViewById(R.id.drawer)); assertThat(shadowOf(activity).getNextStartedActivity()) .hasComponent(activity, UserActivity.class) .hasExtra(UserActivity.EXTRA_USERNAME, "username"); }
@Test public void testLoginStaleAccount() { Preferences.setUsername(context, "username"); shadowOf(ShadowAccountManager.get(context)) .addAccount(new Account("username", BuildConfig.APPLICATION_ID)); AppUtils.showLogin(context, null); assertThat(shadowOf(context).getNextStartedActivity()) .hasComponent(context, LoginActivity.class); }