|
5 | 5 | package io.flutter.plugins.googlesignin;
|
6 | 6 |
|
7 | 7 | import static org.mockito.Mockito.mock;
|
| 8 | +import static org.mockito.Mockito.times; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | +import static org.mockito.Mockito.when; |
8 | 11 |
|
| 12 | +import android.app.Activity; |
9 | 13 | import android.content.Context;
|
| 14 | +import android.content.Intent; |
| 15 | +import com.google.android.gms.auth.api.signin.GoogleSignInAccount; |
| 16 | +import com.google.android.gms.common.api.Scope; |
10 | 17 | import io.flutter.plugin.common.BinaryMessenger;
|
11 | 18 | import io.flutter.plugin.common.MethodCall;
|
| 19 | +import io.flutter.plugin.common.MethodChannel; |
| 20 | +import io.flutter.plugin.common.PluginRegistry; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.List; |
| 24 | +import org.junit.Before; |
12 | 25 | import org.junit.Test;
|
| 26 | +import org.mockito.ArgumentCaptor; |
| 27 | +import org.mockito.Mock; |
| 28 | +import org.mockito.MockitoAnnotations; |
| 29 | +import org.mockito.Spy; |
13 | 30 |
|
14 | 31 | public class GoogleSignInTest {
|
| 32 | + @Mock Context mockContext; |
| 33 | + @Mock Activity mockActivity; |
| 34 | + @Mock PluginRegistry.Registrar mockRegistrar; |
| 35 | + @Mock BinaryMessenger mockMessenger; |
| 36 | + @Spy MethodChannel.Result result; |
| 37 | + @Mock GoogleSignInWrapper mockGoogleSignIn; |
| 38 | + @Mock GoogleSignInAccount account; |
| 39 | + private GoogleSignInPlugin plugin; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + MockitoAnnotations.initMocks(this); |
| 44 | + when(mockRegistrar.messenger()).thenReturn(mockMessenger); |
| 45 | + when(mockRegistrar.context()).thenReturn(mockContext); |
| 46 | + when(mockRegistrar.activity()).thenReturn(mockActivity); |
| 47 | + plugin = new GoogleSignInPlugin(); |
| 48 | + plugin.initInstance(mockRegistrar.messenger(), mockRegistrar.context(), mockGoogleSignIn); |
| 49 | + plugin.setUpRegistrar(mockRegistrar); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void requestScopes_ResultErrorIfAccountIsNull() { |
| 54 | + MethodCall methodCall = new MethodCall("requestScopes", null); |
| 55 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(null); |
| 56 | + plugin.onMethodCall(methodCall, result); |
| 57 | + verify(result).error("sign_in_required", "No account to grant scopes.", null); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void requestScopes_ResultTrueIfAlreadyGranted() { |
| 62 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 63 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 64 | + |
| 65 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 66 | + Scope requestedScope = new Scope("requestedScope"); |
| 67 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account); |
| 68 | + when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope)); |
| 69 | + when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(true); |
| 70 | + |
| 71 | + plugin.onMethodCall(methodCall, result); |
| 72 | + verify(result).success(true); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void requestScopes_RequestsPermissionIfNotGranted() { |
| 77 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 78 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 79 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 80 | + Scope requestedScope = new Scope("requestedScope"); |
| 81 | + |
| 82 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account); |
| 83 | + when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope)); |
| 84 | + when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false); |
| 85 | + |
| 86 | + plugin.onMethodCall(methodCall, result); |
| 87 | + |
| 88 | + verify(mockGoogleSignIn) |
| 89 | + .requestPermissions(mockActivity, 53295, account, new Scope[] {requestedScope}); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void requestScopes_ReturnsFalseIfPermissionDenied() { |
| 94 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 95 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 96 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 97 | + Scope requestedScope = new Scope("requestedScope"); |
| 98 | + |
| 99 | + ArgumentCaptor<PluginRegistry.ActivityResultListener> captor = |
| 100 | + ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class); |
| 101 | + verify(mockRegistrar).addActivityResultListener(captor.capture()); |
| 102 | + PluginRegistry.ActivityResultListener listener = captor.getValue(); |
| 103 | + |
| 104 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account); |
| 105 | + when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope)); |
| 106 | + when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false); |
| 107 | + |
| 108 | + plugin.onMethodCall(methodCall, result); |
| 109 | + listener.onActivityResult( |
| 110 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, |
| 111 | + Activity.RESULT_CANCELED, |
| 112 | + new Intent()); |
| 113 | + |
| 114 | + verify(result).success(false); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void requestScopes_ReturnsTrueIfPermissionGranted() { |
| 119 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 120 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 121 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 122 | + Scope requestedScope = new Scope("requestedScope"); |
| 123 | + |
| 124 | + ArgumentCaptor<PluginRegistry.ActivityResultListener> captor = |
| 125 | + ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class); |
| 126 | + verify(mockRegistrar).addActivityResultListener(captor.capture()); |
| 127 | + PluginRegistry.ActivityResultListener listener = captor.getValue(); |
| 128 | + |
| 129 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account); |
| 130 | + when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope)); |
| 131 | + when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false); |
| 132 | + |
| 133 | + plugin.onMethodCall(methodCall, result); |
| 134 | + listener.onActivityResult( |
| 135 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent()); |
| 136 | + |
| 137 | + verify(result).success(true); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void requestScopes_mayBeCalledRepeatedly_ifAlreadyGranted() { |
| 142 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 143 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 144 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 145 | + Scope requestedScope = new Scope("requestedScope"); |
| 146 | + |
| 147 | + ArgumentCaptor<PluginRegistry.ActivityResultListener> captor = |
| 148 | + ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class); |
| 149 | + verify(mockRegistrar).addActivityResultListener(captor.capture()); |
| 150 | + PluginRegistry.ActivityResultListener listener = captor.getValue(); |
| 151 | + |
| 152 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account); |
| 153 | + when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope)); |
| 154 | + when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false); |
| 155 | + |
| 156 | + plugin.onMethodCall(methodCall, result); |
| 157 | + listener.onActivityResult( |
| 158 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent()); |
| 159 | + plugin.onMethodCall(methodCall, result); |
| 160 | + listener.onActivityResult( |
| 161 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent()); |
| 162 | + |
| 163 | + verify(result, times(2)).success(true); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + public void requestScopes_mayBeCalledRepeatedly_ifNotSignedIn() { |
| 168 | + HashMap<String, List<String>> arguments = new HashMap<>(); |
| 169 | + arguments.put("scopes", Collections.singletonList("requestedScope")); |
| 170 | + MethodCall methodCall = new MethodCall("requestScopes", arguments); |
| 171 | + Scope requestedScope = new Scope("requestedScope"); |
| 172 | + |
| 173 | + ArgumentCaptor<PluginRegistry.ActivityResultListener> captor = |
| 174 | + ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class); |
| 175 | + verify(mockRegistrar).addActivityResultListener(captor.capture()); |
| 176 | + PluginRegistry.ActivityResultListener listener = captor.getValue(); |
| 177 | + |
| 178 | + when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(null); |
| 179 | + |
| 180 | + plugin.onMethodCall(methodCall, result); |
| 181 | + listener.onActivityResult( |
| 182 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent()); |
| 183 | + plugin.onMethodCall(methodCall, result); |
| 184 | + listener.onActivityResult( |
| 185 | + GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent()); |
| 186 | + |
| 187 | + verify(result, times(2)).error("sign_in_required", "No account to grant scopes.", null); |
| 188 | + } |
| 189 | + |
15 | 190 | @Test(expected = IllegalStateException.class)
|
16 | 191 | public void signInThrowsWithoutActivity() {
|
17 | 192 | final GoogleSignInPlugin plugin = new GoogleSignInPlugin();
|
|
0 commit comments