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

Commit

Permalink
Move unit tests to Android modules (#4193)
Browse files Browse the repository at this point in the history
  • Loading branch information
bparrishMines authored Jul 26, 2021
1 parent 1dce6ab commit 6960795
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ android {
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.2.4'
testImplementation 'androidx.test:core:1.2.0'
testImplementation "org.robolectric:robolectric:4.3.1"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}


testOptions {
unitTests.includeAndroidResources = true
unitTests.returnDefaultValues = true
Expand All @@ -58,8 +61,3 @@ android {
}
}
}

dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:3.2.4'
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.Assert.assertTrue;

import android.content.Context;
import android.os.Build;
import androidx.activity.ComponentActivity;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.gms.maps.GoogleMap;
Expand All @@ -19,8 +20,10 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.P)
public class GoogleMapControllerTest {

private Context context;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,188 @@
package io.flutter.plugins.googlesignin;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.common.api.Scope;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

public class GoogleSignInTest {
@Mock Context mockContext;
@Mock Activity mockActivity;
@Mock PluginRegistry.Registrar mockRegistrar;
@Mock BinaryMessenger mockMessenger;
@Spy MethodChannel.Result result;
@Mock GoogleSignInWrapper mockGoogleSignIn;
@Mock GoogleSignInAccount account;
private GoogleSignInPlugin plugin;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mockRegistrar.messenger()).thenReturn(mockMessenger);
when(mockRegistrar.context()).thenReturn(mockContext);
when(mockRegistrar.activity()).thenReturn(mockActivity);
plugin = new GoogleSignInPlugin();
plugin.initInstance(mockRegistrar.messenger(), mockRegistrar.context(), mockGoogleSignIn);
plugin.setUpRegistrar(mockRegistrar);
}

@Test
public void requestScopes_ResultErrorIfAccountIsNull() {
MethodCall methodCall = new MethodCall("requestScopes", null);
when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(null);
plugin.onMethodCall(methodCall, result);
verify(result).error("sign_in_required", "No account to grant scopes.", null);
}

@Test
public void requestScopes_ResultTrueIfAlreadyGranted() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));

MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");
when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account);
when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope));
when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(true);

plugin.onMethodCall(methodCall, result);
verify(result).success(true);
}

@Test
public void requestScopes_RequestsPermissionIfNotGranted() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));
MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");

when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account);
when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope));
when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false);

plugin.onMethodCall(methodCall, result);

verify(mockGoogleSignIn)
.requestPermissions(mockActivity, 53295, account, new Scope[] {requestedScope});
}

@Test
public void requestScopes_ReturnsFalseIfPermissionDenied() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));
MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");

ArgumentCaptor<PluginRegistry.ActivityResultListener> captor =
ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class);
verify(mockRegistrar).addActivityResultListener(captor.capture());
PluginRegistry.ActivityResultListener listener = captor.getValue();

when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account);
when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope));
when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false);

plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE,
Activity.RESULT_CANCELED,
new Intent());

verify(result).success(false);
}

@Test
public void requestScopes_ReturnsTrueIfPermissionGranted() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));
MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");

ArgumentCaptor<PluginRegistry.ActivityResultListener> captor =
ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class);
verify(mockRegistrar).addActivityResultListener(captor.capture());
PluginRegistry.ActivityResultListener listener = captor.getValue();

when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account);
when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope));
when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false);

plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent());

verify(result).success(true);
}

@Test
public void requestScopes_mayBeCalledRepeatedly_ifAlreadyGranted() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));
MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");

ArgumentCaptor<PluginRegistry.ActivityResultListener> captor =
ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class);
verify(mockRegistrar).addActivityResultListener(captor.capture());
PluginRegistry.ActivityResultListener listener = captor.getValue();

when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(account);
when(account.getGrantedScopes()).thenReturn(Collections.singleton(requestedScope));
when(mockGoogleSignIn.hasPermissions(account, requestedScope)).thenReturn(false);

plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent());
plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent());

verify(result, times(2)).success(true);
}

@Test
public void requestScopes_mayBeCalledRepeatedly_ifNotSignedIn() {
HashMap<String, List<String>> arguments = new HashMap<>();
arguments.put("scopes", Collections.singletonList("requestedScope"));
MethodCall methodCall = new MethodCall("requestScopes", arguments);
Scope requestedScope = new Scope("requestedScope");

ArgumentCaptor<PluginRegistry.ActivityResultListener> captor =
ArgumentCaptor.forClass(PluginRegistry.ActivityResultListener.class);
verify(mockRegistrar).addActivityResultListener(captor.capture());
PluginRegistry.ActivityResultListener listener = captor.getValue();

when(mockGoogleSignIn.getLastSignedInAccount(mockContext)).thenReturn(null);

plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent());
plugin.onMethodCall(methodCall, result);
listener.onActivityResult(
GoogleSignInPlugin.Delegate.REQUEST_CODE_REQUEST_SCOPE, Activity.RESULT_OK, new Intent());

verify(result, times(2)).error("sign_in_required", "No account to grant scopes.", null);
}

@Test(expected = IllegalStateException.class)
public void signInThrowsWithoutActivity() {
final GoogleSignInPlugin plugin = new GoogleSignInPlugin();
Expand Down
Loading

0 comments on commit 6960795

Please sign in to comment.