Skip to content

Commit d97c318

Browse files
bparrishMinesfotiDim
authored andcommitted
Move unit tests to Android modules (flutter#4193)
1 parent 80dd258 commit d97c318

File tree

13 files changed

+191
-196
lines changed

13 files changed

+191
-196
lines changed

packages/google_maps_flutter/google_maps_flutter/android/build.gradle

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ android {
3838
androidTestImplementation 'androidx.test:runner:1.2.0'
3939
androidTestImplementation 'androidx.test:rules:1.2.0'
4040
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
41+
testImplementation 'junit:junit:4.12'
42+
testImplementation 'org.mockito:mockito-core:3.2.4'
43+
testImplementation 'androidx.test:core:1.2.0'
44+
testImplementation "org.robolectric:robolectric:4.3.1"
4145
}
4246

4347
compileOptions {
4448
sourceCompatibility JavaVersion.VERSION_1_8
4549
targetCompatibility JavaVersion.VERSION_1_8
4650
}
4751

48-
4952
testOptions {
5053
unitTests.includeAndroidResources = true
5154
unitTests.returnDefaultValues = true
@@ -58,8 +61,3 @@ android {
5861
}
5962
}
6063
}
61-
62-
dependencies {
63-
testImplementation 'junit:junit:4.12'
64-
testImplementation 'org.mockito:mockito-core:3.2.4'
65-
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static org.junit.Assert.assertTrue;
99

1010
import android.content.Context;
11+
import android.os.Build;
1112
import androidx.activity.ComponentActivity;
1213
import androidx.test.core.app.ApplicationProvider;
1314
import com.google.android.gms.maps.GoogleMap;
@@ -19,8 +20,10 @@
1920
import org.mockito.MockitoAnnotations;
2021
import org.robolectric.Robolectric;
2122
import org.robolectric.RobolectricTestRunner;
23+
import org.robolectric.annotation.Config;
2224

2325
@RunWith(RobolectricTestRunner.class)
26+
@Config(sdk = Build.VERSION_CODES.P)
2427
public class GoogleMapControllerTest {
2528

2629
private Context context;

packages/google_maps_flutter/google_maps_flutter/example/android/app/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,188 @@
55
package io.flutter.plugins.googlesignin;
66

77
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;
811

12+
import android.app.Activity;
913
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;
1017
import io.flutter.plugin.common.BinaryMessenger;
1118
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;
1225
import org.junit.Test;
26+
import org.mockito.ArgumentCaptor;
27+
import org.mockito.Mock;
28+
import org.mockito.MockitoAnnotations;
29+
import org.mockito.Spy;
1330

1431
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+
15190
@Test(expected = IllegalStateException.class)
16191
public void signInThrowsWithoutActivity() {
17192
final GoogleSignInPlugin plugin = new GoogleSignInPlugin();

0 commit comments

Comments
 (0)