FacebookController { public Task<Map<String, String>> authenticateAsync( Activity activity, Fragment fragment, LoginAuthorizationType authorizationType, Collection<String> permissions) { if (callbackManager != null) { return Task.forError( new RuntimeException("Unable to authenticate when another authentication is in process")); } final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); LoginManager manager = facebookSdkDelegate.getLoginManager(); callbackManager = facebookSdkDelegate.createCallbackManager(); manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { AccessToken accessToken = loginResult.getAccessToken(); Map<String, String> authData = getAuthData(accessToken); tcs.trySetResult(authData); } @Override public void onCancel() { tcs.trySetCancelled(); } @Override public void onError(FacebookException e) { tcs.trySetError(e); } }); if (LoginAuthorizationType.PUBLISH.equals(authorizationType)) { if (fragment != null) { manager.logInWithPublishPermissions(fragment, permissions); } else { manager.logInWithPublishPermissions(activity, permissions); } } else { if (fragment != null) { manager.logInWithReadPermissions(fragment, permissions); } else { manager.logInWithReadPermissions(activity, permissions); } } return tcs.getTask(); } FacebookController(FacebookSdkDelegate facebookSdkDelegate);   FacebookController(); void initialize(Context context, int callbackRequestCodeOffset); boolean onActivityResult(int requestCode, int resultCode, Intent data); Task<Map<String, String>> authenticateAsync(
            Activity activity,
            Fragment fragment,
            LoginAuthorizationType authorizationType,
            Collection<String> permissions); Map<String, String> getAuthData(AccessToken accessToken); void setAuthData(Map<String, String> authData); static final int DEFAULT_AUTH_ACTIVITY_CODE; }
@Test @SuppressWarnings("unchecked") public void testAuthenticateAsyncCancel() { FacebookController.FacebookSdkDelegate facebookSdk = mock(FacebookController.FacebookSdkDelegate.class); LoginManager loginManager = mock(LoginManager.class); CallbackManager callbackManager = mock(CallbackManager.class); when(facebookSdk.getLoginManager()).thenReturn(loginManager); when(facebookSdk.createCallbackManager()).thenReturn(callbackManager); FacebookController controller = new FacebookController(facebookSdk); Collection<String> permissions = new ArrayList<>(); Task<Map<String, String>> task = controller.authenticateAsync( mock(Activity.class), null, FacebookController.LoginAuthorizationType.READ, permissions); ArgumentCaptor<FacebookCallback> callbackCapture = ArgumentCaptor.forClass(FacebookCallback.class); verify(loginManager).registerCallback(eq(callbackManager), (FacebookCallback<LoginResult>) callbackCapture.capture()); FacebookCallback<LoginResult> callback = callbackCapture.getValue(); callback.onCancel(); assertTrue(task.isCancelled()); }
@Test @SuppressWarnings("unchecked") public void testAuthenticateAsyncFailure() { FacebookController.FacebookSdkDelegate facebookSdk = mock(FacebookController.FacebookSdkDelegate.class); LoginManager loginManager = mock(LoginManager.class); CallbackManager callbackManager = mock(CallbackManager.class); when(facebookSdk.getLoginManager()).thenReturn(loginManager); when(facebookSdk.createCallbackManager()).thenReturn(callbackManager); FacebookController controller = new FacebookController(facebookSdk); Collection<String> permissions = new ArrayList<>(); Task<Map<String, String>> task = controller.authenticateAsync( mock(Activity.class), null, FacebookController.LoginAuthorizationType.READ, permissions); ArgumentCaptor<FacebookCallback> callbackCapture = ArgumentCaptor.forClass(FacebookCallback.class); verify(loginManager).registerCallback(eq(callbackManager), (FacebookCallback<LoginResult>) callbackCapture.capture()); FacebookCallback<LoginResult> callback = callbackCapture.getValue(); callback.onError(mock(FacebookException.class)); assertTrue(task.isFaulted()); }