This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Move unit tests to Android modules #4193
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...r/example/android/app/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests were copied and pasted from the tests in |
||
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(); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added due to an error that complained about missing sdk 20. I was only able to get it working after looking at https://stackoverflow.com/questions/56808485/robolectric-and-android-sdk-29/57261194#57261194. I'm assuming this could also be increased depending oon what version we want to run it on