TwitterAuthClient { public void authorize(Activity activity, Callback<TwitterSession> callback) { if (activity == null) { throw new IllegalArgumentException("Activity must not be null."); } if (callback == null) { throw new IllegalArgumentException("Callback must not be null."); } if (activity.isFinishing()) { Twitter.getLogger() .e(TwitterCore.TAG, "Cannot authorize, activity is finishing.", null); } else { handleAuthorize(activity, callback); } } TwitterAuthClient();   TwitterAuthClient(TwitterCore twitterCore, TwitterAuthConfig authConfig,
                      SessionManager<TwitterSession> sessionManager, AuthState authState); int getRequestCode(); void authorize(Activity activity, Callback<TwitterSession> callback); void cancelAuthorize(); void onActivityResult(int requestCode, int resultCode, Intent data); void requestEmail(TwitterSession session, final Callback<String> callback);  }
@Test public void testAuthorize_activityNull() { try { authClient.authorize(null, mock(Callback.class)); fail("Expected IllegalArgumentException to be thrown"); } catch (IllegalArgumentException e) { assertEquals("Activity must not be null.", e.getMessage()); } }
@Test public void testAuthorize_activityIsFinishing() { final Activity mockActivity = mock(Activity.class); when(mockActivity.isFinishing()).thenReturn(true); authClient.authorize(mockActivity, mockCallback); verifyZeroInteractions(mockAuthState); }
@Test public void testAuthorize_callbackNull() { try { authClient.authorize(mock(Activity.class), null); fail("Expected IllegalArgumentException to be thrown"); } catch (IllegalArgumentException e) { assertEquals("Callback must not be null.", e.getMessage()); } }
@Test public void testAuthorize_authorizeInProgress() throws PackageManager.NameNotFoundException { final Activity mockActivity = mock(Activity.class); TestUtils.setupNoSSOAppInstalled(mockActivity); when(mockAuthState.isAuthorizeInProgress()).thenReturn(true); authClient.authorize(mockActivity, mockCallback); verify(mockCallback).failure(any(TwitterAuthException.class)); }
@Test public void testAuthorize_ssoAvailable() throws PackageManager.NameNotFoundException { final Activity mockActivity = mock(Activity.class); TestUtils.setupTwitterInstalled(mockActivity); when(mockAuthState.beginAuthorize(any(Activity.class), any(AuthHandler.class))) .thenReturn(true); authClient.authorize(mockActivity, mockCallback); verify(mockAuthState).beginAuthorize(eq(mockActivity), any(SSOAuthHandler.class)); }
@Test public void testAuthorize_ssoAvailableViaTwitterDogfood() throws PackageManager.NameNotFoundException { final Activity mockActivity = mock(Activity.class); TestUtils.setupTwitterInstalled(mockActivity); when(mockAuthState.beginAuthorize(any(Activity.class), any(AuthHandler.class))) .thenReturn(true); authClient.authorize(mockActivity, mockCallback); verify(mockAuthState).beginAuthorize(eq(mockActivity), any(SSOAuthHandler.class)); }
@Test public void testAuthorize_ssoNotAvailable() throws PackageManager.NameNotFoundException { final Activity mockActivity = mock(Activity.class); TestUtils.setupNoSSOAppInstalled(mockActivity); when(mockAuthState.beginAuthorize(any(Activity.class), any(AuthHandler.class))) .thenReturn(true); authClient.authorize(mockActivity, mockCallback); verify(mockAuthState).beginAuthorize(eq(mockActivity), any(OAuthHandler.class)); }
@Test public void testAuthorize_bothSsoAndOAuthFail() throws PackageManager.NameNotFoundException { final Activity mockActivity = mock(Activity.class); TestUtils.setupTwitterInstalled(mockActivity); when(mockAuthState.beginAuthorize(any(Activity.class), any(AuthHandler.class))) .thenReturn(false); authClient.authorize(mockActivity, mockCallback); verify(mockAuthState, times(2)).beginAuthorize(eq(mockActivity), any(AuthHandler.class)); final ArgumentCaptor<TwitterAuthException> argCaptor = ArgumentCaptor.forClass(TwitterAuthException.class); verify(mockCallback).failure(argCaptor.capture()); assertEquals("Authorize failed.", argCaptor.getValue().getMessage()); }