LoginManager { public void onActivityResult( @NonNull Activity activity, int requestCode, int resultCode, @Nullable Intent data) { if (requestCode != this.requestCode) { return; } if (resultCode == Activity.RESULT_OK) { handleResultOk(data); } else if (resultCode == Activity.RESULT_CANCELED) { handleResultCancelled(activity, data); } } LoginManager(
            @NonNull AccessTokenStorage accessTokenStorage,
            @NonNull LoginCallback loginCallback);  LoginManager(
            @NonNull AccessTokenStorage accessTokenStorage,
            @NonNull LoginCallback loginCallback,
            @NonNull SessionConfiguration configuration);  LoginManager(
            @NonNull AccessTokenStorage accessTokenStorage,
            @NonNull LoginCallback loginCallback,
            @NonNull SessionConfiguration configuration,
            int requestCode);   LoginManager(
            @NonNull AccessTokenStorage accessTokenStorage,
            @NonNull LoginCallback loginCallback,
            @NonNull SessionConfiguration configuration,
            int requestCode,
            @NonNull LegacyUriRedirectHandler legacyUriRedirectHandler); void login(final @NonNull Activity activity); @Deprecated void loginForImplicitGrant(@NonNull Activity activity); void loginForAuthorizationCode(@NonNull Activity activity); @Deprecated @NonNull @SuppressWarnings("unchecked") T getAccessTokenManager(); @NonNull AccessTokenStorage getAccessTokenStorage(); LoginCallback getLoginCallback(); @NonNull SessionConfiguration getSessionConfiguration(); @NonNull Session<?> getSession(); boolean isAuthenticated(); @Deprecated LoginManager setRedirectForAuthorizationCode(boolean redirectForAuthorizationCode); @Deprecated boolean isRedirectForAuthorizationCode(); LoginManager setAuthCodeFlowEnabled(boolean authCodeFlowEnabled); LoginManager setProductFlowPriority(@NonNull Collection<SupportedAppType> productFlowPriority); boolean isAuthCodeFlowEnabled(); void onActivityResult(
            @NonNull Activity activity,
            int requestCode,
            int resultCode,
            @Nullable Intent data);  }
@Test public void onActivityResult_whenResultOkAndHasToken_shouldCallbackSuccess() { Intent intent = new Intent() .putExtra(EXTRA_ACCESS_TOKEN, ACCESS_TOKEN.getToken()) .putExtra(EXTRA_SCOPE, AuthUtils.scopeCollectionToString(ACCESS_TOKEN.getScopes())) .putExtra(EXTRA_REFRESH_TOKEN, ACCESS_TOKEN.getRefreshToken()) .putExtra(EXTRA_EXPIRES_IN, ACCESS_TOKEN.getExpiresIn()) .putExtra(EXTRA_TOKEN_TYPE, ACCESS_TOKEN.getTokenType()); loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_OK, intent); ArgumentCaptor<AccessToken> storedToken = ArgumentCaptor.forClass(AccessToken.class); ArgumentCaptor<AccessToken> returnedToken = ArgumentCaptor.forClass(AccessToken.class); verify(accessTokenStorage).setAccessToken(storedToken.capture()); verify(callback).onLoginSuccess(returnedToken.capture()); assertThat(storedToken.getValue()).isEqualTo(ACCESS_TOKEN); assertThat(returnedToken.getValue()).isEqualTo(ACCESS_TOKEN); }
@Test public void onActivityResult_whenResultOkAndHasCode_shouldCallbackSuccess() { Intent intent = new Intent() .putExtra(EXTRA_CODE_RECEIVED, AUTHORIZATION_CODE); loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_OK, intent); ArgumentCaptor<String> capturedCode = ArgumentCaptor.forClass(String.class); verify(callback).onAuthorizationCodeReceived(capturedCode.capture()); assertThat(capturedCode.getValue()).isEqualTo(AUTHORIZATION_CODE); }
@Test public void onActivityResult_whenResultCanceledAndNoData_shouldCallbackCancel() { loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_CANCELED, null); verify(callback).onLoginCancel(); }
@Test public void onActivityResult_whenResultCanceledAndHasData_shouldCallbackError() { Intent intent = new Intent().putExtra(EXTRA_ERROR, AuthenticationError.INVALID_RESPONSE .toStandardString()); loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_CANCELED, intent); verify(callback).onLoginError(AuthenticationError.INVALID_RESPONSE); }
@Test public void onActivityResult_whenResultCanceledAndNoData_shouldCancel() { loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_CANCELED, null); verify(callback).onLoginCancel(); }
@Test public void onActivityResult_whenResultOkAndNoData_shouldCallbackErrorUnknown() { loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_OK, null); verify(callback).onLoginError(AuthenticationError.UNKNOWN); }
@Test public void onActivityResult_whenRequestCodeDoesNotMatch_nothingShouldHappen() { Intent intent = mock(Intent.class); loginManager.onActivityResult(activity, 1337, Activity.RESULT_OK, intent); verifyZeroInteractions(intent); verifyZeroInteractions(callback); }
@Test public void onActivityResult_whenResultCanceledAndDataButNoCallback_nothingShouldHappen() { Intent intent = mock(Intent.class); loginManager.onActivityResult(activity, 1337, Activity.RESULT_OK, intent); verifyZeroInteractions(intent); }
@Test public void onActivityResult_whenUnavailableAndGeneralScopes_shouldTriggerImplicitGrant() { sessionConfiguration = sessionConfiguration.newBuilder().setScopes(GENERAL_SCOPES).build(); loginManager = new LoginManager(accessTokenStorage, callback, sessionConfiguration); Intent intent = new Intent().putExtra(EXTRA_ERROR, AuthenticationError.UNAVAILABLE.toStandardString()); loginManager.onActivityResult(activity, REQUEST_CODE_LOGIN_DEFAULT, Activity.RESULT_CANCELED, intent); verify(callback, never()).onLoginError(AuthenticationError.UNAVAILABLE); ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); verify(activity).startActivityForResult(intentCaptor.capture(), eq(REQUEST_CODE_LOGIN_DEFAULT)); final Intent capturedIntent = intentCaptor.getValue(); final ResponseType responseType = (ResponseType) capturedIntent .getSerializableExtra(EXTRA_RESPONSE_TYPE); final SessionConfiguration loginConfiguration = (SessionConfiguration) capturedIntent .getSerializableExtra(LoginActivity.EXTRA_SESSION_CONFIGURATION); assertThat(responseType).isEqualTo(ResponseType.TOKEN); assertThat(loginConfiguration.getScopes()).containsAll(GENERAL_SCOPES); }