AuthState { public boolean beginAuthorize(Activity activity, AuthHandler authHandler) { boolean result = false; if (isAuthorizeInProgress()) { Twitter.getLogger().w(TwitterCore.TAG, "Authorize already in progress"); } else if (authHandler.authorize(activity)) { result = authHandlerRef.compareAndSet(null, authHandler); if (!result) { Twitter.getLogger().w(TwitterCore.TAG, "Failed to update authHandler, authorize" + " already in progress."); } } return result; }  boolean beginAuthorize(Activity activity, AuthHandler authHandler); void endAuthorize(); boolean isAuthorizeInProgress(); AuthHandler getAuthHandler();  }
@Test public void testBeginAuthorize() { final AuthState authState = new AuthState(); final boolean result = authState.beginAuthorize(mockActivity, mockAuthHandler); assertTrue(result); assertTrue(authState.isAuthorizeInProgress()); assertEquals(mockAuthHandler, authState.getAuthHandler()); }
@Test public void testBeginAuthorize_authorizeInProgress() { final AuthState authState = new AuthState(); final boolean result = authState.beginAuthorize(mockActivity, mockAuthHandler); assertTrue(result); assertFalse(authState.beginAuthorize(mockActivity, mockAuthHandler)); }
@Test public void testBeginAuthorize_authHandlerCompareAndSetFails() { final AuthState authState = new AuthState(); final boolean result = authState.beginAuthorize(mockActivity, new AuthHandler(mock(TwitterAuthConfig.class), mock(Callback.class), TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE) { @Override public boolean authorize(Activity activity) { authState.authHandlerRef.set(mock(AuthHandler.class)); return true; } }); assertFalse(result); }