Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add methods: signInSilently,signOut,disconnect #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/src/google_sign_in_mocks_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class MockGoogleSignIn implements GoogleSignIn {
_isCancelled = val;
}

/// Used to simulate user already being signed in to google.
void enableSilentSignIn() {
_currentUser = MockGoogleSignInAccount();
}

@override
GoogleSignInAccount? get currentUser => _currentUser;

Expand All @@ -19,6 +24,26 @@ class MockGoogleSignIn implements GoogleSignIn {
return Future.value(_isCancelled ? null : _currentUser);
}

@override
Future<GoogleSignInAccount?> signInSilently({
bool suppressErrors = true,
bool reAuthenticate = false,
}) {
return Future.value(_currentUser);
}

@override
Future<GoogleSignInAccount?> signOut() {
_currentUser = null;
return Future.value(null);
}

@override
Future<GoogleSignInAccount?> disconnect() {
_currentUser = null;
return Future.value(null);
}

@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
Expand Down
47 changes: 43 additions & 4 deletions test/google_sign_in_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ void main() {
googleSignIn = MockGoogleSignIn();
});

test('should return idToken and accessToken when authenticating', () async {
test('signIn should return idToken and accessToken when authenticating',
() async {
final signInAccount = await googleSignIn.signIn();
final signInAuthentication = await signInAccount!.authentication;
expect(signInAuthentication, isNotNull);
Expand All @@ -16,18 +17,56 @@ void main() {
expect(signInAuthentication.idToken, isNotNull);
});

test('should return null when google login is cancelled by the user',
test('signIn should return null when google login is cancelled by the user',
() async {
googleSignIn.setIsCancelled(true);
final signInAccount = await googleSignIn.signIn();
expect(signInAccount, isNull);
});
test('testing google login twice, once cancelled, once not cancelled at the same test.', () async {
googleSignIn.setIsCancelled(true);

test(
'testing signIn twice, once cancelled, once not cancelled at the same test.',
() async {
googleSignIn.setIsCancelled(true);
final signInAccount = await googleSignIn.signIn();
expect(signInAccount, isNull);
googleSignIn.setIsCancelled(false);
final signInAccountSecondAttempt = await googleSignIn.signIn();
expect(signInAccountSecondAttempt, isNotNull);
});

test('signInSilently should return user when user is already signed in',
() async {
final signInAccount = await googleSignIn.signIn();
final silentSignInAccount = await googleSignIn.signInSilently();
expect(silentSignInAccount, isNotNull);
expect(silentSignInAccount, equals(signInAccount));
});

test('signInSilently should return user after calling enableSilentSignIn',
() async {
googleSignIn.enableSilentSignIn();
final silentSignInAccount = await googleSignIn.signInSilently();
expect(silentSignInAccount, isNotNull);
});

test('signInSilently should return null when user is not signed in',
() async {
final silentSignInAccount = await googleSignIn.signInSilently();
expect(silentSignInAccount, isNull);
});

test('signOut should return null when user is signed out', () async {
final signInAccount = await googleSignIn.signIn();
expect(signInAccount, isNotNull);
final signOutAccount = await googleSignIn.signOut();
expect(signOutAccount, isNull);
});

test('disconnect should return null when user is disconnected', () async {
final signInAccount = await googleSignIn.signIn();
expect(signInAccount, isNotNull);
final disconnectAccount = await googleSignIn.disconnect();
expect(disconnectAccount, isNull);
});
}