LoginPresenter extends BaseViperPresenter<LoginView, LoginInteractor> { @Override public void subscribe() { compositeDisposable.add(Observable.concat( interactor.getLastUserCredentialsPayload().toObservable(), Observable.merge( Observable.combineLatest( view.getEmailTextChanges(), view.getPasswordTextChanges(), (BiFunction<CharSequence, CharSequence, UserCredentialsPayload>) SmartReceiptsUserLogin::new) .flatMap(userCredentialsPayload -> view.getLoginButtonClicks().map(ignored -> userCredentialsPayload)), Observable.combineLatest( view.getEmailTextChanges(), view.getPasswordTextChanges(), (BiFunction<CharSequence, CharSequence, UserCredentialsPayload>) SmartReceiptsUserSignUp::new) .flatMap(userCredentialsPayload -> view.getSignUpButtonClicks().map(ignored -> userCredentialsPayload))) ) .flatMap(interactor::loginOrSignUp) .startWith(UiIndicator.idle()) .subscribe(uiIndicator -> { view.present(uiIndicator); if (uiIndicator.getState() == UiIndicator.State.Success || uiIndicator.getState() == UiIndicator.State.Error) { interactor.onLoginResultsConsumed(); } })); compositeDisposable.add(Observable.combineLatest(simpleEmailFieldValidator(), simplePasswordFieldValidator(), (isEmailValid, isPasswordValid) -> { final String message; if (!isEmailValid) { message = context.getString(R.string.login_fields_hint_email); } else if (!isPasswordValid) { message = context.getString(R.string.login_fields_hint_password); } else { message = context.getString(R.string.login_fields_hint_valid); } return new UiInputValidationIndicator(message, isEmailValid, isPasswordValid); }) .distinctUntilChanged() .subscribe(view::present)); } @Inject  LoginPresenter(@NonNull Context context, @NonNull LoginView view, @NonNull LoginInteractor interactor); @Override void subscribe();  }
@Test public void onResumeRestoresCachedPayload() { when(interactor.getLastUserCredentialsPayload()).thenReturn(Maybe.just(userCredentialsPayload)); presenter.subscribe(); verify(view).present(UiIndicator.idle()); verify(view).present(uiIndicator); verify(interactor, never()).onLoginResultsConsumed(); }
@Test public void onResumeRestoresCachedPayloadAndConsumesResultsOnSuccess() { when(uiIndicator.getState()).thenReturn(UiIndicator.State.Success); when(interactor.getLastUserCredentialsPayload()).thenReturn(Maybe.just(userCredentialsPayload)); presenter.subscribe(); verify(view).present(UiIndicator.idle()); verify(view).present(uiIndicator); verify(interactor).onLoginResultsConsumed(); }
@Test public void onResumeRestoresCachedPayloadAndConsumesResultsOnError() { when(uiIndicator.getState()).thenReturn(UiIndicator.State.Error); when(interactor.getLastUserCredentialsPayload()).thenReturn(Maybe.just(userCredentialsPayload)); presenter.subscribe(); verify(view).present(UiIndicator.idle()); verify(view).present(uiIndicator); verify(interactor).onLoginResultsConsumed(); }
@Test public void loginClickStartsLogin() { when(view.getLoginButtonClicks()).thenReturn(Observable.just(Unit.INSTANCE)); presenter.subscribe(); verify(interactor).loginOrSignUp(userCredentialsPayloadCaptor.capture()); verify(view).present(UiIndicator.idle()); verify(view).present(uiIndicator); verify(interactor, never()).onLoginResultsConsumed(); assertEquals(userCredentialsPayloadCaptor.getValue(), new SmartReceiptsUserLogin(EMAIL, PASSWORD)); }
@Test public void signUpClickStartsSignUp() { when(view.getSignUpButtonClicks()).thenReturn(Observable.just(Unit.INSTANCE)); presenter.subscribe(); verify(interactor).loginOrSignUp(userCredentialsPayloadCaptor.capture()); verify(view).present(UiIndicator.idle()); verify(view).present(uiIndicator); verify(interactor, never()).onLoginResultsConsumed(); assertEquals(userCredentialsPayloadCaptor.getValue(), new SmartReceiptsUserSignUp(EMAIL, PASSWORD)); }
@Test public void shortPasswordsAreInvalid() { when(view.getPasswordTextChanges()).thenReturn(Observable.just("*")); presenter.subscribe(); verify(view).present(new UiInputValidationIndicator(context.getString(R.string.login_fields_hint_password), true, false)); }
@Test public void emailsWithoutAtSymbolAreInvalid() { when(view.getEmailTextChanges()).thenReturn(Observable.just(".email.")); presenter.subscribe(); verify(view).present(new UiInputValidationIndicator(context.getString(R.string.login_fields_hint_email), false, true)); }
@Test public void emailsWithoutDotSymbolAreInvalid() { when(view.getEmailTextChanges()).thenReturn(Observable.just("email@@@email")); presenter.subscribe(); verify(view).present(new UiInputValidationIndicator(context.getString(R.string.login_fields_hint_email), false, true)); }
@Test public void validCredentialsAreHintingForLogin() { presenter.subscribe(); verify(view).present(new UiInputValidationIndicator(context.getString(R.string.login_fields_hint_valid), true, true)); }