@Test public void testFailed() { verify(userManager).getUser(eq("username"), userCaptor.capture()); userCaptor.getValue().onError(null); assertEquals(activity.getString(R.string.user_failed), ShadowToast.getTextOfLatestToast()); }
@Implementation protected static Toast makeText(Context context, int resId, int duration) { return makeText(context, context.getResources().getString(resId), duration); }
@Test public void shouldCountToastsCorrectly() throws Exception { assertThat(ShadowToast.shownToastCount()).isEqualTo(0); Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); assertThat(toast).isNotNull(); toast.show(); toast.show(); toast.show(); assertThat(ShadowToast.shownToastCount()).isEqualTo(3); ShadowToast.reset(); assertThat(ShadowToast.shownToastCount()).isEqualTo(0); toast.show(); toast.show(); assertThat(ShadowToast.shownToastCount()).isEqualTo(2); }
@Test public void shouldSetTextWithIdCorrectly() throws Exception { Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); toast.setText(R.string.hello); toast.show(); assertThat(ShadowToast.getLatestToast()).isSameAs(toast); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("Hello"); assertThat(ShadowToast.showedToast("Hello")).isTrue(); }
private void assertToast(String message) { assertThat(getTextOfLatestToast()).isEqualTo(message); assertThat(shownToastCount()).isEqualTo(1); } }
@Test public void shouldBeCancelled() throws Exception { Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); toast.cancel(); ShadowToast shadowToast = shadowOf(toast); assertThat(shadowToast.isCancelled()).isTrue(); } }
@Test public void shouldSetTextCorrectly() throws Exception { Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); toast.setText("other toast"); toast.show(); assertThat(ShadowToast.getLatestToast()).isSameAs(toast); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("other toast"); assertThat(ShadowToast.showedToast("other toast")).isTrue(); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemFailed() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), voteCallback.capture()); voteCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@Test public void shouldMakeTextCorrectly() throws Exception { Toast toast = Toast.makeText(context, "short toast", Toast.LENGTH_SHORT); assertThat(toast).isNotNull(); assertThat(toast.getDuration()).isEqualTo(Toast.LENGTH_SHORT); toast.show(); assertThat(ShadowToast.getLatestToast()).isSameAs(toast); assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo("short toast"); assertThat(ShadowToast.showedToast("short toast")).isTrue(); }
@Implementation protected static Toast makeText(Context context, int resId, int duration) { return makeText(context, context.getResources().getString(resId), duration); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVote() { viewHolder.itemView.findViewById(R.id.button_more).performClick(); PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu(); assertNotNull(popupMenu); shadowOf(popupMenu).getOnMenuItemClickListener() .onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_vote)); verify(userServices).voteUp(any(Context.class), any(), voteCallback.capture()); voteCallback.getValue().onDone(true); assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast()); }
@Implementation public static Toast makeText(Context context, int resId, int duration) { return makeText(context, context.getResources().getString(resId), duration); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItemFailed() { 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_vote)); verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture()); userServicesCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@Implementation public static Toast makeText(Context context, int resId, int duration) { return makeText(context, context.getResources().getString(resId), duration); }
@Test public void testDelayedError() { doSend(); shadowOf(activity).clickMenuItem(android.R.id.home); replyCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.comment_failed), ShadowToast.getTextOfLatestToast()); }
@Implementation public static Toast makeText(Context context, int resId, int duration) { return makeText(context, context.getResources().getString(resId), duration); }
@Test public void testSendEmpty() { shadowOf(activity).clickMenuItem(R.id.menu_send); assertEquals(activity.getString(R.string.comment_required), ShadowToast.getTextOfLatestToast()); }
@Test public void testVoteError() { Intent intent = new Intent(); intent.putExtra(ItemActivity.EXTRA_ITEM, new TestHnItem(1)); controller = Robolectric.buildActivity(ItemActivity.class, intent); controller.create().start().resume(); activity = controller.get(); activity.findViewById(R.id.vote_button).performClick(); verify(userServices).voteUp(any(Context.class), eq("1"), userServicesCallback.capture()); userServicesCallback.getValue().onError(new IOException()); assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast()); }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Test public void testVoteItem() { 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_vote)); verify(userServices).voteUp(any(Context.class), any(), userServicesCallback.capture()); userServicesCallback.getValue().onDone(true); assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast()); }
@Test public void testVote() { Intent intent = new Intent(); intent.putExtra(ItemActivity.EXTRA_ITEM, new TestHnItem(1)); controller = Robolectric.buildActivity(ItemActivity.class, intent); controller.create().start().resume(); activity = controller.get(); activity.findViewById(R.id.vote_button).performClick(); verify(userServices).voteUp(any(Context.class), eq("1"), userServicesCallback.capture()); userServicesCallback.getValue().onDone(true); assertEquals(activity.getString(R.string.voted), ShadowToast.getTextOfLatestToast()); }