Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[google_sign_in] Slight cleanup in GoogleSignInPlugin #7013

Merged
merged 9 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## NEXT
## 6.1.6

* Minor implementation cleanup
* Updates minimum Flutter version to 3.0.

## 6.1.5
Expand Down
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,11 @@
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.CommonStatusCodes;
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 +47,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 +209,27 @@ public void signInThrowsWithoutActivity() {
plugin.onMethodCall(new MethodCall("signIn", null), null);
}

@Test
public void signInSilentlyThatImmediatelyCompletesWithoutResultFinishesWithError()
throws ApiException {
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);

plugin.onMethodCall(new MethodCall("signInSilently", null), result);
verify(result)
.error(
"sign_in_required",
"com.google.android.gms.common.api.ApiException: 4: Error text",
null);
}

@Test
public void init_LoadsServerClientIdFromResources() {
final String packageName = "fakePackageName";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: google_sign_in_android
description: Android implementation of the google_sign_in plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
version: 6.1.5
version: 6.1.6

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down