Java源码示例:org.robolectric.shadows.ShadowToast

示例1
@Test
public void showToast() {
    Toast latestToast = ShadowToast.getLatestToast();
    Assert.assertNull(latestToast);

    Button button = (Button) mTestActivity.findViewById(R.id.button2);
    button.performClick();

    latestToast = ShadowToast.getLatestToast();
    Assert.assertNotNull(latestToast);

    ShadowToast shadowToast = Shadows.shadowOf(latestToast);
    ShadowLog.d("toast_time", shadowToast.getDuration() + "");
    Assert.assertEquals(Toast.LENGTH_SHORT, shadowToast.getDuration());
    Assert.assertEquals("hahaha", ShadowToast.getTextOfLatestToast());
}
 
示例2
private void checkRuntimeException(int buttonId) {
    when(taskValidation
            .isNewNameValid(any(String.class)))
            .thenReturn(new ValidationResult(ValidationStatus.VALID));

    when(taskValidation
            .isDurationValid(any(String.class)))
            .thenReturn(new ValidationResult(ValidationStatus.VALID));

    when(taskTemplateRepository
            .create(any(String.class), any(Integer.class), any(Long.class), any(Long.class), any(Integer.class)))
            .thenThrow(new RuntimeException());
    Button button = (Button) activity.findViewById(buttonId);
    button.performClick();
    String expectedMessage = activity.getResources()
            .getString(R.string.save_task_error_message);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例3
@Test
public void testSubmitError() {
    ((EditText) activity.findViewById(R.id.edittext_title)).setText("title");
    ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com");
    shadowOf(activity).clickMenuItem(R.id.menu_send);
    ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE)
            .performClick();
    verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"),
            eq(true), submitCallback.capture());
    Uri redirect = Uri.parse(BuildConfig.APPLICATION_ID + "://item?id=1234");
    UserServices.Exception exception = new UserServices.Exception(R.string.item_exist);
    exception.data = redirect;
    submitCallback.getValue().onError(exception);
    assertEquals(activity.getString(R.string.item_exist), ShadowToast.getTextOfLatestToast());
    assertThat(shadowOf(activity).getNextStartedActivity())
            .hasAction(Intent.ACTION_VIEW)
            .hasData(redirect);
}
 
示例4
@Test
public void testNavButtonHint() {
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putString(activity.getString(R.string.pref_story_display),
                    activity.getString(R.string.pref_story_display_value_comments))
            .putBoolean(activity.getString(R.string.pref_navigation), true)
            .apply();
    startWithIntent();
    View navButton = activity.findViewById(R.id.navigation_button);
    assertThat(navButton).isVisible();
    ((GestureDetector.SimpleOnGestureListener) getDetector(navButton).getListener())
            .onSingleTapConfirmed(mock(MotionEvent.class));
    assertThat(ShadowToast.getTextOfLatestToast())
            .contains(activity.getString(R.string.hint_nav_short));
}
 
示例5
@Test
public void testNavButtonDrag() {
    PreferenceManager.getDefaultSharedPreferences(activity)
            .edit()
            .putString(activity.getString(R.string.pref_story_display),
                    activity.getString(R.string.pref_story_display_value_comments))
            .putBoolean(activity.getString(R.string.pref_navigation), true)
            .apply();
    startWithIntent();
    View navButton = activity.findViewById(R.id.navigation_button);
    assertThat(navButton).isVisible();
    getDetector(navButton).getListener().onLongPress(mock(MotionEvent.class));
    assertThat(ShadowToast.getTextOfLatestToast())
            .contains(activity.getString(R.string.hint_drag));
    MotionEvent motionEvent = mock(MotionEvent.class);
    when(motionEvent.getAction()).thenReturn(MotionEvent.ACTION_MOVE);
    when(motionEvent.getRawX()).thenReturn(1f);
    when(motionEvent.getRawY()).thenReturn(1f);
    shadowOf(navButton).getOnTouchListener().onTouch(navButton, motionEvent);
    motionEvent = mock(MotionEvent.class);
    when(motionEvent.getAction()).thenReturn(MotionEvent.ACTION_UP);
    shadowOf(navButton).getOnTouchListener().onTouch(navButton, motionEvent);
    assertThat(navButton).hasX(1f).hasY(1f);
}
 
示例6
/**
 * Ensure user sees invalid path toast when the file browser doesn't return a path uri
 */
@Test
public void emptyFileSelectionTest() {
    MultimediaInflaterActivity multimediaInflaterActivity =
            Robolectric.buildActivity(MultimediaInflaterActivity.class)
                    .create().start().resume().get();

    ImageButton selectFileButton =
            multimediaInflaterActivity.findViewById(
                    R.id.screen_multimedia_inflater_filefetch);
    selectFileButton.performClick();

    Intent fileSelectIntent = new Intent(Intent.ACTION_GET_CONTENT);
    fileSelectIntent.setType("application/zip");

    Intent emptyFileSelectResult = new Intent();

    ShadowActivity shadowActivity =
            Shadows.shadowOf(multimediaInflaterActivity);
    shadowActivity.receiveResult(fileSelectIntent,
            Activity.RESULT_OK,
            emptyFileSelectResult);

    Assert.assertEquals(Localization.get("file.invalid.path"),
            ShadowToast.getTextOfLatestToast());
}
 
示例7
@Test
public void currentActivity() throws Exception {
    /* now select an activity */
    DiaryActivity someAct = new DiaryActivity(1, "Test", Color.BLACK);

    ActivityHelper.helper.insertActivity(someAct);
    assertNotNull(someAct);

    ActivityHelper.helper.setCurrentActivity(someAct);
    assertEquals(ActivityHelper.helper.getCurrentActivity(), someAct);

    MainActivity activity = Robolectric.setupActivity(MainActivity.class);

    View card = activity.findViewById(R.id.card);
    TextView nameView = (TextView) card.findViewById(R.id.activity_name);
    assertNotNull("Current activity Text available", nameView);

    assertEquals(nameView.getText(), "Test");

    FloatingActionButton fabNoteEdit = (FloatingActionButton) activity.findViewById(R.id.fab_edit_note);
    FloatingActionButton fabAttachPicture = (FloatingActionButton) activity.findViewById(R.id.fab_attach_picture);

    assertNotNull("we have two FABs", fabNoteEdit);
    assertNotNull("we have two FABs", fabAttachPicture);

    fabNoteEdit.performClick();

    DialogFragment dialogFragment = (DialogFragment) activity.getSupportFragmentManager()
            .findFragmentByTag("NoteEditDialogFragment");
    assertNotNull(dialogFragment);

    ShadowLooper.idleMainLooper(100, TimeUnit.MILLISECONDS);
    assertNull(ShadowToast.getTextOfLatestToast());

    fabAttachPicture.performClick();

    ShadowLooper.idleMainLooper(100, TimeUnit.MILLISECONDS);
    assertNull(ShadowToast.getTextOfLatestToast());
}
 
示例8
@Test
public void testShow() {
    impl.show();
    assertEquals(message, ShadowToast.getTextOfLatestToast());
    Toast toast = ShadowToast.getLatestToast();
    assertEquals(length, toast.getDuration());
    assertEquals(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, toast.getGravity());
    assertEquals(0, toast.getXOffset());
    assertEquals(
            context.getResources().getDimensionPixelSize(R.dimen.tb_toast_y_offset),
            toast.getYOffset()
    );
}
 
示例9
@Test
public void testCancel() {
    impl.show();
    ShadowToast toast = Shadows.shadowOf(ShadowToast.getLatestToast());
    assertFalse(toast.isCancelled());
    impl.cancel();
    assertTrue(toast.isCancelled());
}
 
示例10
@Test
public void testShowToast() {
    U.showToast(context, R.string.tb_pin_shortcut_not_supported);
    Toast toast = ShadowToast.getLatestToast();
    assertEquals(Toast.LENGTH_SHORT, toast.getDuration());
    assertEquals(
            context.getResources().getString(R.string.tb_pin_shortcut_not_supported),
            ShadowToast.getTextOfLatestToast()
    );
}
 
示例11
@Test
public void testShowLongToast() {
    U.showToastLong(context, R.string.tb_pin_shortcut_not_supported);
    Toast toast = ShadowToast.getLatestToast();
    assertEquals(Toast.LENGTH_LONG, toast.getDuration());
    assertEquals(
            context.getResources().getString(R.string.tb_pin_shortcut_not_supported),
            ShadowToast.getTextOfLatestToast()
    );
}
 
示例12
@Test
public void testCancelToast() {
    U.showToastLong(context, R.string.tb_pin_shortcut_not_supported);
    ShadowToast shadowToast = Shadows.shadowOf(ShadowToast.getLatestToast());
    assertFalse(shadowToast.isCancelled());
    U.cancelToast();
    assertTrue(shadowToast.isCancelled());
}
 
示例13
@Test
public void whenActivityResultIsCalledWithNonExistingPictureDataExpectToastWithErrorMessageIsShown() {
    fragment.onActivityResult(
            AssetType.PICTURE.ordinal(),
            FilePickerActivity.RESULT_OK,
            new Intent()
    );
    String expectedMessage = activity.getResources().getString(R.string.picking_file_error);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例14
@Test
public void whenActivityResultIsCalledWithNonExistingSoundDataExpectToastWithErrorMessageIsShown() {
    fragment.onActivityResult(
            AssetType.SOUND.ordinal(),
            FilePickerActivity.RESULT_OK,
            new Intent()
    );
    String expectedMessage = activity.getResources().getString(R.string.picking_file_error);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例15
@Test
public void whenClickPlayButtonWithoutSoundChosenExpectToastToBeDisplayed() {
    ImageButton playSound = (ImageButton) activity.findViewById(R.id.id_btn_play_step_sound);
    playSound.performClick();

    String expectedMessage = activity.getResources().getString(R.string.no_file_to_play_error);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例16
@Test
public void whenActivityResultIsCalledWithNonExistingPictureDataExpectToastWithErrorMessageIsShown() {
    fragment.onActivityResult(
            AssetType.PICTURE.ordinal(),
            FilePickerActivity.RESULT_OK,
            new Intent()
    );
    String expectedMessage = activity.getResources().getString(R.string.picking_file_error);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例17
@Test
public void whenActivityResultIsCalledWithNonExistingSoundDataExpectToastWithErrorMessageIsShown() {
    fragment.onActivityResult(
            AssetType.SOUND.ordinal(),
            FilePickerActivity.RESULT_OK,
            new Intent()
    );
    String expectedMessage = activity.getResources().getString(R.string.picking_file_error);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例18
private void checkRuntimeException(int buttonId) {
    when(stepValidation
            .isNewNameValid(any(Long.class), any(String.class)))
            .thenReturn(new ValidationResult(ValidationStatus.VALID));

    when(stepTemplateRepository
            .create(any(String.class), any(Integer.class), any(Long.class), any(Long.class), any(Long.class)))
            .thenThrow(new RuntimeException());
    Button button = (Button) activity.findViewById(buttonId);
    button.performClick();
    String expectedMessage = activity.getResources()
            .getString(R.string.save_step_error_message);
    assertThat(ShadowToast.getTextOfLatestToast(), equalTo(expectedMessage));
}
 
示例19
@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());
}
 
示例20
@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());
}
 
示例21
@Test
public void testSearch() {
    LocalBroadcastManager.getInstance(activity)
            .sendBroadcast(new Intent(WebFragment.ACTION_FULLSCREEN)
                    .putExtra(WebFragment.EXTRA_FULLSCREEN, true));
    activity.findViewById(R.id.button_find).performClick();
    ViewSwitcher controlSwitcher = activity.findViewById(R.id.control_switcher);
    assertThat(controlSwitcher.getDisplayedChild()).isEqualTo(1);
    ShadowWebView shadowWebView = Shadow.extract(activity.findViewById(R.id.web_view));

    // no query
    EditText editText = activity.findViewById(R.id.edittext);
    shadowOf(editText).getOnEditorActionListener().onEditorAction(null, 0, null);
    assertThat((View) activity.findViewById(R.id.button_next)).isDisabled();

    // with results
    shadowWebView.setFindCount(1);
    editText.setText("abc");
    shadowOf(editText).getOnEditorActionListener().onEditorAction(null, 0, null);
    assertThat((View) activity.findViewById(R.id.button_next)).isEnabled();
    activity.findViewById(R.id.button_next).performClick();
    assertThat(shadowWebView.getFindIndex()).isEqualTo(1);
    activity.findViewById(R.id.button_clear).performClick();
    assertThat(editText).isEmpty();
    assertThat(controlSwitcher.getDisplayedChild()).isEqualTo(0);

    // with no results
    shadowWebView.setFindCount(0);
    editText.setText("abc");
    shadowOf(editText).getOnEditorActionListener().onEditorAction(null, 0, null);
    assertThat((View) activity.findViewById(R.id.button_next)).isDisabled();
    assertThat(ShadowToast.getTextOfLatestToast()).contains(activity.getString(R.string.no_matches));
}
 
示例22
@Test
public void testSubmitDelayedError() {
    ((EditText) activity.findViewById(R.id.edittext_title)).setText("title");
    ((EditText) activity.findViewById(R.id.edittext_content)).setText("http://example.com");
    shadowOf(activity).clickMenuItem(R.id.menu_send);
    ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE)
            .performClick();
    verify(userServices).submit(any(Context.class), eq("title"), eq("http://example.com"),
            eq(true), submitCallback.capture());
    shadowOf(activity).clickMenuItem(android.R.id.home);
    ShadowAlertDialog.getLatestAlertDialog().getButton(DialogInterface.BUTTON_POSITIVE)
            .performClick();
    submitCallback.getValue().onError(new IOException());
    assertEquals(activity.getString(R.string.submit_failed), ShadowToast.getTextOfLatestToast());
}
 
示例23
@Test
public void testSendFailed() {
    doSend();
    assertFalse(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_send).isEnabled());
    assertFalse(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_quote).isVisible());
    assertFalse(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_save_draft).isEnabled());
    assertFalse(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_discard_draft).isEnabled());
    replyCallback.getValue().onError(new IOException());
    assertTrue(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_send).isEnabled());
    assertTrue(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_quote).isVisible());
    assertTrue(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_save_draft).isEnabled());
    assertTrue(shadowOf(activity).getOptionsMenu().findItem(R.id.menu_discard_draft).isEnabled());
    assertThat(activity).isNotFinishing();
    assertEquals(activity.getString(R.string.comment_failed), ShadowToast.getTextOfLatestToast());
}
 
示例24
@Test
public void testDelayedSuccessfulResponse() {
    doSend();
    shadowOf(activity).clickMenuItem(android.R.id.home);
    assertThat(activity).isFinishing();
    replyCallback.getValue().onDone(true);
    assertEquals(activity.getString(R.string.comment_successful), ShadowToast.getTextOfLatestToast());
}
 
示例25
@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());
}
 
示例26
@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());
}
 
示例27
@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());
}
 
示例28
@Test
public void testSuccessful() {
    ((EditText) activity.findViewById(R.id.edittext_title)).setText("title");
    ((EditText) activity.findViewById(R.id.edittext_body)).setText("body");
    activity.findViewById(R.id.feedback_button).performClick();
    verify(feedbackClient).send(eq("title"), eq("body"), callback.capture());
    callback.getValue().onSent(true);
    assertThat(activity).isFinishing();
    assertEquals(activity.getString(R.string.feedback_sent), ShadowToast.getTextOfLatestToast());
    controller.pause().stop().destroy();
}
 
示例29
@Test
public void testFailed() {
    ((EditText) activity.findViewById(R.id.edittext_title)).setText("title");
    ((EditText) activity.findViewById(R.id.edittext_body)).setText("body");
    activity.findViewById(R.id.feedback_button).performClick();
    verify(feedbackClient).send(eq("title"), eq("body"), callback.capture());
    callback.getValue().onSent(false);
    assertThat(activity).isNotFinishing();
    assertEquals(activity.getString(R.string.feedback_failed), ShadowToast.getTextOfLatestToast());
    controller.pause().stop().destroy();
}
 
示例30
@Test
public void instantiateItem_inActiveLoginButtonShouldToast() throws Exception {
    TestLoginListener listener = new TestLoginListener();
    loginAdapter.setLoginListener(listener);
    View view = (View) loginAdapter.instantiateItem(new FrameLayout(application), PAGE_4);
    Button loginButton = (Button) view.findViewById(R.id.login_button);
    loginButton.performClick();
    String expected = application.getString(R.string.agree_to_terms_please);
    assertThat(ShadowToast.getTextOfLatestToast()).isEqualTo(expected);
}