Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Slight cleanup in GoogleSignInPlugin
1) Use switch instead of if/else and show all possible states
2) Handle the case of synchronous failure in signInSilently()
   by checking isComplete() instead of isSuccessful()
  • Loading branch information
tvolkert committed Jan 25, 2023
commit 2898b577171293237742bddc68858a91367d26be
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,9 @@ public void init(
public void signInSilently(Result result) {
checkAndSetPendingOperation(METHOD_SIGN_IN_SILENTLY, result);
Task<GoogleSignInAccount> task = signInClient.silentSignIn();
if (task.isSuccessful()) {
if (task.isComplete()) {
// There's immediate result available.
onSignInAccount(task.getResult());
onSignInResult(task);
} else {
task.addOnCompleteListener(
new OnCompleteListener<GoogleSignInAccount>() {
Expand Down Expand Up @@ -516,7 +516,7 @@ private void onSignInResult(Task<GoogleSignInAccount> completedTask) {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
onSignInAccount(account);
} catch (ApiException e) {
// Forward all errors and let Dart side decide how to handle.
// Forward all errors and let Dart decide how to handle.
String errorCode = errorCodeForStatus(e.getStatusCode());
finishWithError(errorCode, e.toString());
} catch (RuntimeExecutionException e) {
Expand All @@ -538,14 +538,20 @@ private void onSignInAccount(GoogleSignInAccount account) {
}

private String errorCodeForStatus(int statusCode) {
if (statusCode == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {
return ERROR_REASON_SIGN_IN_CANCELED;
} else if (statusCode == CommonStatusCodes.SIGN_IN_REQUIRED) {
return ERROR_REASON_SIGN_IN_REQUIRED;
} else if (statusCode == CommonStatusCodes.NETWORK_ERROR) {
return ERROR_REASON_NETWORK_ERROR;
} else {
return ERROR_REASON_SIGN_IN_FAILED;
switch (statusCode) {
case GoogleSignInStatusCodes.SIGN_IN_CANCELLED:
return ERROR_REASON_SIGN_IN_CANCELED;
case CommonStatusCodes.SIGN_IN_REQUIRED:
return ERROR_REASON_SIGN_IN_REQUIRED;
case CommonStatusCodes.NETWORK_ERROR:
return ERROR_REASON_NETWORK_ERROR;
case GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS:
case GoogleSignInStatusCodes.SIGN_IN_FAILED:
case CommonStatusCodes.INVALID_ACCOUNT:
case CommonStatusCodes.INTERNAL_ERROR:
return ERROR_REASON_SIGN_IN_FAILED;
default:
return ERROR_REASON_SIGN_IN_FAILED;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.tasks.Task;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -43,6 +46,7 @@ public class GoogleSignInTest {
@Mock GoogleSignInWrapper mockGoogleSignIn;
@Mock GoogleSignInAccount account;
@Mock GoogleSignInClient mockClient;
@Mock Task<GoogleSignInAccount> mockSignInTask;
private GoogleSignInPlugin plugin;

@Before
Expand Down Expand Up @@ -204,6 +208,22 @@ public void signInThrowsWithoutActivity() {
plugin.onMethodCall(new MethodCall("signIn", null), null);
}

@Test
public void signInSilentlyThatImmediatelyCompletesWithoutResultFinishesWithError() {
final String clientId = "fakeClientId";
MethodCall methodCall = buildInitMethodCall(clientId, null);
initAndAssertServerClientId(methodCall, clientId);

ApiException exception = new ApiException(new Status(CommonStatusCodes.SIGN_IN_REQUIRED, "Error text"));
when(mockClient.silentSignIn()).thenReturn(mockSignInTask);
when(mockSignInTask.isComplete()).thenReturn(true);
when(mockSignInTask.getResult(ApiException.class)).thenThrow(exception);

MethodCall methodCall = new MethodCall("signInSilently", null);
plugin.onMethodCall(methodCall, result);
verify(result).error("sign_in_required", "Error text", null);
}

@Test
public void init_LoadsServerClientIdFromResources() {
final String packageName = "fakePackageName";
Expand Down