Skip to content

Commit b3c7582

Browse files
authored
[google_sign_in] Slight cleanup in GoogleSignInPlugin (flutter#7013)
* 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() * Run formatter and fix some compilation errors * Version bump * Declare throws * Update error text expectation * Diagnose failing test * Code * Code * Fix test
1 parent 2daa072 commit b3c7582

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

packages/google_sign_in/google_sign_in_android/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 6.1.6
22

3+
* Minor implementation cleanup
34
* Updates minimum Flutter version to 3.0.
45

56
## 6.1.5

packages/google_sign_in/google_sign_in_android/android/src/main/java/io/flutter/plugins/googlesignin/GoogleSignInPlugin.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,9 +404,9 @@ public void init(
404404
public void signInSilently(Result result) {
405405
checkAndSetPendingOperation(METHOD_SIGN_IN_SILENTLY, result);
406406
Task<GoogleSignInAccount> task = signInClient.silentSignIn();
407-
if (task.isSuccessful()) {
407+
if (task.isComplete()) {
408408
// There's immediate result available.
409-
onSignInAccount(task.getResult());
409+
onSignInResult(task);
410410
} else {
411411
task.addOnCompleteListener(
412412
new OnCompleteListener<GoogleSignInAccount>() {
@@ -516,7 +516,7 @@ private void onSignInResult(Task<GoogleSignInAccount> completedTask) {
516516
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
517517
onSignInAccount(account);
518518
} catch (ApiException e) {
519-
// Forward all errors and let Dart side decide how to handle.
519+
// Forward all errors and let Dart decide how to handle.
520520
String errorCode = errorCodeForStatus(e.getStatusCode());
521521
finishWithError(errorCode, e.toString());
522522
} catch (RuntimeExecutionException e) {
@@ -538,14 +538,20 @@ private void onSignInAccount(GoogleSignInAccount account) {
538538
}
539539

540540
private String errorCodeForStatus(int statusCode) {
541-
if (statusCode == GoogleSignInStatusCodes.SIGN_IN_CANCELLED) {
542-
return ERROR_REASON_SIGN_IN_CANCELED;
543-
} else if (statusCode == CommonStatusCodes.SIGN_IN_REQUIRED) {
544-
return ERROR_REASON_SIGN_IN_REQUIRED;
545-
} else if (statusCode == CommonStatusCodes.NETWORK_ERROR) {
546-
return ERROR_REASON_NETWORK_ERROR;
547-
} else {
548-
return ERROR_REASON_SIGN_IN_FAILED;
541+
switch (statusCode) {
542+
case GoogleSignInStatusCodes.SIGN_IN_CANCELLED:
543+
return ERROR_REASON_SIGN_IN_CANCELED;
544+
case CommonStatusCodes.SIGN_IN_REQUIRED:
545+
return ERROR_REASON_SIGN_IN_REQUIRED;
546+
case CommonStatusCodes.NETWORK_ERROR:
547+
return ERROR_REASON_NETWORK_ERROR;
548+
case GoogleSignInStatusCodes.SIGN_IN_CURRENTLY_IN_PROGRESS:
549+
case GoogleSignInStatusCodes.SIGN_IN_FAILED:
550+
case CommonStatusCodes.INVALID_ACCOUNT:
551+
case CommonStatusCodes.INTERNAL_ERROR:
552+
return ERROR_REASON_SIGN_IN_FAILED;
553+
default:
554+
return ERROR_REASON_SIGN_IN_FAILED;
549555
}
550556
}
551557

packages/google_sign_in/google_sign_in_android/android/src/test/java/io/flutter/plugins/googlesignin/GoogleSignInTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
1818
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
1919
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
20+
import com.google.android.gms.common.api.ApiException;
21+
import com.google.android.gms.common.api.CommonStatusCodes;
2022
import com.google.android.gms.common.api.Scope;
23+
import com.google.android.gms.common.api.Status;
24+
import com.google.android.gms.tasks.Task;
2125
import io.flutter.plugin.common.BinaryMessenger;
2226
import io.flutter.plugin.common.MethodCall;
2327
import io.flutter.plugin.common.MethodChannel;
@@ -43,6 +47,7 @@ public class GoogleSignInTest {
4347
@Mock GoogleSignInWrapper mockGoogleSignIn;
4448
@Mock GoogleSignInAccount account;
4549
@Mock GoogleSignInClient mockClient;
50+
@Mock Task<GoogleSignInAccount> mockSignInTask;
4651
private GoogleSignInPlugin plugin;
4752

4853
@Before
@@ -204,6 +209,27 @@ public void signInThrowsWithoutActivity() {
204209
plugin.onMethodCall(new MethodCall("signIn", null), null);
205210
}
206211

212+
@Test
213+
public void signInSilentlyThatImmediatelyCompletesWithoutResultFinishesWithError()
214+
throws ApiException {
215+
final String clientId = "fakeClientId";
216+
MethodCall methodCall = buildInitMethodCall(clientId, null);
217+
initAndAssertServerClientId(methodCall, clientId);
218+
219+
ApiException exception =
220+
new ApiException(new Status(CommonStatusCodes.SIGN_IN_REQUIRED, "Error text"));
221+
when(mockClient.silentSignIn()).thenReturn(mockSignInTask);
222+
when(mockSignInTask.isComplete()).thenReturn(true);
223+
when(mockSignInTask.getResult(ApiException.class)).thenThrow(exception);
224+
225+
plugin.onMethodCall(new MethodCall("signInSilently", null), result);
226+
verify(result)
227+
.error(
228+
"sign_in_required",
229+
"com.google.android.gms.common.api.ApiException: 4: Error text",
230+
null);
231+
}
232+
207233
@Test
208234
public void init_LoadsServerClientIdFromResources() {
209235
final String packageName = "fakePackageName";

packages/google_sign_in/google_sign_in_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: google_sign_in_android
22
description: Android implementation of the google_sign_in plugin.
33
repository: https://github.com/flutter/plugins/tree/main/packages/google_sign_in/google_sign_in_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+google_sign_in%22
5-
version: 6.1.5
5+
version: 6.1.6
66

77
environment:
88
sdk: ">=2.14.0 <3.0.0"

0 commit comments

Comments
 (0)