@Test public void onActivityResult_validRequestCode_apiException() throws Throwable { FuturePromise promise = Mockito.mock(FuturePromise.class); GoogleSignInListener listener = new GoogleSignInListener(promise); Intent intent = PowerMockito.mock(Intent.class); int requestCode = Const.GOOGLE_SIGN_IN; int resultCode = -1; final Task task = Mockito.mock(Task.class); Mockito.when(task.getResult(ApiException.class)).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { throw new ApiException(Status.RESULT_CANCELED); } }); Mockito.when(GoogleSignIn.getSignedInAccountFromIntent(Mockito.any(Intent.class))).thenReturn(task); listener.onActivityResult(requestCode, resultCode, intent); Mockito.verify(((AndroidApplication) Gdx.app), VerificationModeFactory.times(1)).removeAndroidEventListener(Mockito.refEq(listener)); Mockito.verify(promise, VerificationModeFactory.times(1)).doFail(Mockito.nullable(Exception.class)); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.GOOGLE_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener((AndroidApplication) Gdx.app, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { promise.doComplete(GdxFIRAuth.instance().getCurrentUser()); } else { promise.doFail(task.getException()); } } }); } catch (ApiException e) { promise.doFail(new Exception(CommonStatusCodes.getStatusCodeString(e.getStatusCode()), e)); } } ((AndroidApplication) Gdx.app).removeAndroidEventListener(this); }
GoogleSignInListener implements AndroidEventListener { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.GOOGLE_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener((AndroidApplication) Gdx.app, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { promise.doComplete(GdxFIRAuth.instance().getCurrentUser()); } else { promise.doFail(task.getException()); } } }); } catch (ApiException e) { promise.doFail(new Exception(CommonStatusCodes.getStatusCodeString(e.getStatusCode()), e)); } } ((AndroidApplication) Gdx.app).removeAndroidEventListener(this); } }
GoogleSignInListener implements AndroidEventListener { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.GOOGLE_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener((AndroidApplication) Gdx.app, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { promise.doComplete(GdxFIRAuth.instance().getCurrentUser()); } else { promise.doFail(task.getException()); } } }); } catch (ApiException e) { promise.doFail(new Exception(CommonStatusCodes.getStatusCodeString(e.getStatusCode()), e)); } } ((AndroidApplication) Gdx.app).removeAndroidEventListener(this); } GoogleSignInListener(FuturePromise<GdxFirebaseUser> promise); }
GoogleSignInListener implements AndroidEventListener { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.GOOGLE_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener((AndroidApplication) Gdx.app, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { promise.doComplete(GdxFIRAuth.instance().getCurrentUser()); } else { promise.doFail(task.getException()); } } }); } catch (ApiException e) { promise.doFail(new Exception(CommonStatusCodes.getStatusCodeString(e.getStatusCode()), e)); } } ((AndroidApplication) Gdx.app).removeAndroidEventListener(this); } GoogleSignInListener(FuturePromise<GdxFirebaseUser> promise); @Override void onActivityResult(int requestCode, int resultCode, Intent data); }
GoogleSignInListener implements AndroidEventListener { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == Const.GOOGLE_SIGN_IN) { Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); FirebaseAuth.getInstance().signInWithCredential(credential) .addOnCompleteListener((AndroidApplication) Gdx.app, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { promise.doComplete(GdxFIRAuth.instance().getCurrentUser()); } else { promise.doFail(task.getException()); } } }); } catch (ApiException e) { promise.doFail(new Exception(CommonStatusCodes.getStatusCodeString(e.getStatusCode()), e)); } } ((AndroidApplication) Gdx.app).removeAndroidEventListener(this); } GoogleSignInListener(FuturePromise<GdxFirebaseUser> promise); @Override void onActivityResult(int requestCode, int resultCode, Intent data);  }