@Test
  public void signInFailsWithGeneralError() {
    main.launchActivity(SignInActivity.getStartIntent(component.getContext(), false));
    stubAccountPickerIntent();

    // Stub an error when calling sign in
    doReturn(Observable.error(new RuntimeException("Error")))
        .when(component.getMockDataManager())
        .signIn(mSelectedAccount);

    onView(withId(R.id.button_sign_in)).perform(click());
    allowPermissionsIfNeeded();

    onView(withText(R.string.error_sign_in)).check(matches(isDisplayed()));
  }
  @Test
  public void signInSuccessfulNavigatesToWelcome() {
    main.launchActivity(SignInActivity.getStartIntent(component.getContext(), false));
    stubAccountPickerIntent();

    // Stub sign in method in the DataManager
    Ribot ribot = MockModelFabric.newRibot();
    doReturn(Observable.just(ribot)).when(component.getMockDataManager()).signIn(mSelectedAccount);

    onView(withId(R.id.button_sign_in)).perform(click());
    allowPermissionsIfNeeded();

    // Check that it navigates correctly to the welcome screen
    String expectedWelcome =
        main.getActivity().getString(R.string.welcome_greetings, ribot.profile.name.first);
    onView(withText(expectedWelcome)).check(matches(isDisplayed()));
  }
  @Test
  public void checkViewsDisplay() {
    main.launchActivity(SignInActivity.getStartIntent(component.getContext(), false));

    onView(withId(R.id.button_sign_in)).check(matches(isDisplayed()));
    onView(withText(R.string.sign_in_message)).check(matches(isDisplayed()));
    onView(withId(R.id.image_ribot_logo)).check(matches(isDisplayed()));
  }
  @Test
  public void checkPopUpMessageDisplays() {
    String popUpMessage = "You have been signed out";
    Intent intent = SignInActivity.getStartIntent(component.getContext(), false, popUpMessage);
    main.launchActivity(intent);

    onView(withText(popUpMessage)).check(matches(isDisplayed()));
  }
  @Test
  public void signInFailsWithProfileNotFound() {
    main.launchActivity(SignInActivity.getStartIntent(component.getContext(), false));
    stubAccountPickerIntent();

    // Stub with http 403 error
    HttpException http403Exception = new HttpException(Response.error(403, null));
    doReturn(Observable.error(http403Exception))
        .when(component.getMockDataManager())
        .signIn(mSelectedAccount);

    onView(withId(R.id.button_sign_in)).perform(click());
    allowPermissionsIfNeeded();

    String expectedWelcome =
        main.getActivity().getString(R.string.error_ribot_profile_not_found, mSelectedAccount.name);
    onView(withText(expectedWelcome)).check(matches(isDisplayed()));
  }
 @Before
 public void setup() {
   when(component.getMockAccountManager().getAccountsByType(mSelectedAccount.type))
       .thenReturn(new Account[] {mSelectedAccount});
   mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
 }