AuthHandler { public boolean handleOnActivityResult(int requestCode, int resultCode, Intent data) { if (this.requestCode != requestCode) { return false; } final Callback<TwitterSession> callback = getCallback(); if (callback != null) { if (resultCode == Activity.RESULT_OK) { final String token = data.getStringExtra(EXTRA_TOKEN); final String tokenSecret = data.getStringExtra(EXTRA_TOKEN_SECRET); final String screenName = data.getStringExtra(EXTRA_SCREEN_NAME); final long userId = data.getLongExtra(EXTRA_USER_ID, 0L); callback.success(new Result<>(new TwitterSession( new TwitterAuthToken(token, tokenSecret), userId, screenName), null)); } else if (data != null && data.hasExtra(EXTRA_AUTH_ERROR)) { callback.failure( (TwitterAuthException) data.getSerializableExtra(EXTRA_AUTH_ERROR)); } else { callback.failure(new TwitterAuthException("Authorize failed.")); } } return true; } AuthHandler(TwitterAuthConfig authConfig, Callback<TwitterSession> callback, int requestCode); abstract boolean authorize(Activity activity); boolean handleOnActivityResult(int requestCode, int resultCode, Intent data);  }
@Test public void testHandleOnActivityResult_resultCodeOK() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, Activity.RESULT_OK, new Intent() .putExtra(AuthHandler.EXTRA_TOKEN, TestFixtures.TOKEN) .putExtra(AuthHandler.EXTRA_TOKEN_SECRET, TestFixtures.SECRET) .putExtra(AuthHandler.EXTRA_SCREEN_NAME, TestFixtures.SCREEN_NAME) .putExtra(AuthHandler.EXTRA_USER_ID, TestFixtures.USER_ID)); assertTrue(result); final ArgumentCaptor<Result> argCaptor = ArgumentCaptor.forClass(Result.class); verify(mockCallback).success(argCaptor.capture()); final TwitterSession session = (TwitterSession) argCaptor.getValue().data; final TwitterAuthToken authToken = session.getAuthToken(); assertEquals(TestFixtures.TOKEN, authToken.token); assertEquals(TestFixtures.SECRET, authToken.secret); assertEquals(TestFixtures.SCREEN_NAME, session.getUserName()); assertEquals(TestFixtures.USER_ID, session.getUserId()); }
@Test public void testHandleOnActivityResult_resultCodeCancel() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final TwitterAuthException authException = new TwitterAuthException(TEST_CANCEL_MESSAGE); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, Activity.RESULT_CANCELED, new Intent().putExtra(AuthHandler.EXTRA_AUTH_ERROR, authException)); assertTrue(result); assertCallbackFailureErrorMsg(mockCallback, authException.getMessage()); }
@Test public void testHandleOnActivityResult_resultCodeError() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final TwitterAuthException authException = new TwitterAuthException(TEST_ERROR_MESSAGE); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, AuthHandler.RESULT_CODE_ERROR, new Intent().putExtra(AuthHandler.EXTRA_AUTH_ERROR, authException)); assertTrue(result); assertCallbackFailureErrorMsg(mockCallback, authException.getMessage()); }
@Test public void testHandleOnActivityResult_resultCodeErrorDataNull() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, AuthHandler.RESULT_CODE_ERROR, null); assertTrue(result); assertCallbackFailureErrorMsg(mockCallback, "Authorize failed."); }
@Test public void testHandleOnActivityResult_resultCodeErrorDataWithNoAuthError() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, AuthHandler.RESULT_CODE_ERROR, new Intent()); assertTrue(result); assertCallbackFailureErrorMsg(mockCallback, "Authorize failed."); }
@Test public void testHandleOnActivityResult_unrecognizedRequestCode() { final Callback<TwitterSession> mockCallback = mock(Callback.class); final AuthHandler authHandler = newAuthHandler(mockCallback); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE + 1, Activity.RESULT_CANCELED, null); assertFalse(result); verifyZeroInteractions(mockCallback); }
@Test public void testHandleOnActivityResult_nullCallback() { final AuthHandler authHandler = newAuthHandler(null); final boolean result = authHandler.handleOnActivityResult(TEST_REQUEST_CODE, Activity.RESULT_OK, null); assertTrue(result); }