This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
/
testing.dart
99 lines (88 loc) · 2.73 KB
/
testing.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/services.dart' show MethodCall;
/// A fake backend that can be used to test components that require a valid
/// [GoogleSignInAccount].
///
/// Example usage:
///
/// ```
/// GoogleSignIn googleSignIn;
/// FakeSignInBackend fakeSignInBackend;
///
/// setUp(() {
/// googleSignIn = GoogleSignIn();
/// fakeSignInBackend = FakeSignInBackend();
/// fakeSignInBackend.user = FakeUser(
/// id: 123,
/// email: 'jdoe@example.org',
/// );
/// googleSignIn.channel.setMockMethodCallHandler(
/// fakeSignInBackend.handleMethodCall);
/// });
/// ```
///
class FakeSignInBackend {
/// A [FakeUser] object.
///
/// This does not represent the signed-in user, but rather an object that will
/// be returned when [GoogleSignIn.signIn] or [GoogleSignIn.signInSilently] is
/// called.
late FakeUser user;
/// Handles method calls that would normally be sent to the native backend.
/// Returns with the expected values based on the current [user].
Future<dynamic> handleMethodCall(MethodCall methodCall) async {
switch (methodCall.method) {
case 'init':
// do nothing
return null;
case 'getTokens':
return <String, String?>{
'idToken': user.idToken,
'accessToken': user.accessToken,
};
case 'signIn':
return user._asMap;
case 'signInSilently':
return user._asMap;
case 'signOut':
return <String, String>{};
case 'disconnect':
return <String, String>{};
}
}
}
/// Represents a fake user that can be used with the [FakeSignInBackend] to
/// obtain a [GoogleSignInAccount] and simulate authentication.
class FakeUser {
/// Any of the given parameters can be null.
const FakeUser({
this.id,
this.email,
this.displayName,
this.photoUrl,
this.idToken,
this.accessToken,
});
/// Will be converted into [GoogleSignInUserData.id].
final String? id;
/// Will be converted into [GoogleSignInUserData.email].
final String? email;
/// Will be converted into [GoogleSignInUserData.displayName].
final String? displayName;
/// Will be converted into [GoogleSignInUserData.photoUrl].
final String? photoUrl;
/// Will be converted into [GoogleSignInTokenData.idToken].
final String? idToken;
/// Will be converted into [GoogleSignInTokenData.accessToken].
final String? accessToken;
Map<String, String?> get _asMap => <String, String?>{
'id': id,
'email': email,
'displayName': displayName,
'photoUrl': photoUrl,
'idToken': idToken,
};
}