SignInPresenter extends BasePresenter<SignInView> { public void signIn(String email, String password) { Integer emailError = null; Integer passwordError = null; getViewState().hideFormError(); if (TextUtils.isEmpty(email)) { emailError = R.string.error_field_required; } if (TextUtils.isEmpty(password)) { passwordError = R.string.error_invalid_password; } if (emailError != null || passwordError != null) { getViewState().showFormError(emailError, passwordError); return; } getViewState().startSignIn(); String credentials = String.format("%s:%s", email, password); final String token = "Basic " + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); Subscription subscription = mGithubService.signIn(token) .doOnNext(user -> AuthUtils.setToken(token)) .compose(Utils.applySchedulers()) .subscribe(user -> { getViewState().finishSignIn(); getViewState().successSignIn(); }, exception -> { getViewState().finishSignIn(); getViewState().failedSignIn(exception.getMessage()); }); unsubscribeOnDestroy(subscription); } SignInPresenter(); void signIn(String email, String password); void onErrorCancel();  }
@Test public void signin_shouldSignSuccessfull() { String token = token(); when(githubService.signIn(token)).thenReturn(Observable.just(new User())); presenter.signIn(email(), password()); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Assert.assertEquals(token, AuthUtils.getToken()); isSignInAndHideShowProgressCalled(); verify(signInViewState).successSignIn(); }
@Test public void signin_shouldShowError() { when(githubService.signIn(token())).thenReturn(Observable.error(new Throwable())); presenter.signIn(email(), password()); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Assert.assertEquals("", ""); isSignInAndHideShowProgressCalled(); verify(signInViewState).failedSignIn(anyString()); }
@Test public void signin_shouldShowPasswordAndEmailEmptyErros() { presenter.signIn(null, null); verify(signInViewState).showFormError(R.string.error_field_required, R.string.error_invalid_password); }