AuthenticatorActivity extends AccountAuthenticatorFragmentActivity implements Injectable { @OnClick(R.id.auth_btn_login) public void login() { String username = mTxtUsername.getText().toString(); String password = mTxtPassword.getText().toString(); if (username.isEmpty() || password.isEmpty()) { return; } mProgressDisposable = ProgressDialog.create(R.string.authenticating).show(this); String accountType = getIntent().getStringExtra( android.accounts.AccountManager.KEY_ACCOUNT_TYPE); Account account = new Account(username, accountType); Disposable unused = mAuthenticator.login(account, password, mCkbRememberPassword.isChecked()) .subscribe(result -> mLoginResult.onNext(new LoginResult(account, result)), error -> mLoginResult.onNext(new LoginResult(account, error))); }  @OnClick(R.id.auth_btn_login) void login(); @OnClick(R.id.auth_btn_forgot_password) void forgotPassword(); @Override void onBackPressed();  }
@Test public void testSuccessfulLogin() { final String username = "username"; final String password = "password"; final AuthToken authToken = new AuthToken("authTokenName", "authTokenId"); authenticator.mAuthenticator = mock(fi.bitrite.android.ws.auth.Authenticator.class); when(authenticator.mAuthenticator.login( new Account(username, MOCK_ACCOUNT_TYPE), password, false)) .thenReturn(Observable.just(Authenticator.AuthResult.success(authToken))); authenticator.mTxtUsername.setText(username); authenticator.mTxtPassword.setText(password); authenticator.mCkbRememberPassword.setChecked(false); authenticator.mBtnLogin.performClick(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertThat(authenticator.isFinishing()).isTrue(); }
@Test public void testFailedLogin() { final String username = "username"; final String password = "password"; authenticator.mAuthenticator = mock(fi.bitrite.android.ws.auth.Authenticator.class); when(authenticator.mAuthenticator.login( new Account(username, MOCK_ACCOUNT_TYPE), password, false)) .thenReturn(Observable.just(Authenticator.AuthResult.error( 401, "[\"Wrong username or password.\"]", Authenticator.AuthResult.ErrorCause.WrongUsernameOrPassword))); authenticator.mTxtUsername.setText(username); authenticator.mTxtPassword.setText(password); authenticator.mCkbRememberPassword.setChecked(false); authenticator.mBtnLogin.performClick(); Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); assertThat(authenticator.isFinishing()).isFalse(); }