@@ -3,8 +3,11 @@ import 'package:flutter_test/flutter_test.dart';
3
3
import 'package:geolocator_web/geolocator_web.dart' ;
4
4
import 'package:geolocator_web/src/geolocation_manager.dart' ;
5
5
import 'package:geolocator_web/src/permissions_manager.dart' ;
6
+ import 'package:mockito/annotations.dart' ;
6
7
import 'package:mockito/mockito.dart' ;
7
8
9
+ import 'geolocator_web_test.mocks.dart' ;
10
+
8
11
List <Position > get mockPositions => List .of (() sync * {
9
12
for (var i = 0 ; i < 5 ; i++ ) {
10
13
yield Position (
@@ -24,86 +27,49 @@ List<Position> get mockPositions => List.of(() sync* {
24
27
}
25
28
}());
26
29
30
+ @GenerateNiceMocks ([
31
+ MockSpec <GeolocationManager >(),
32
+ MockSpec <PermissionsManager >(),
33
+ ])
27
34
void main () {
28
- test (
29
- 'enableHighAccuracy returns the correct value depending on given accuracy in getPositionStream method' ,
30
- () async {
31
- final mockGeolocationManager = MockGeolocationManager ();
32
- final mockPermissionManager = MockPermissionManager ();
33
- final geolocatorPlugin =
34
- GeolocatorPlugin .private (mockGeolocationManager, mockPermissionManager);
35
-
36
- geolocatorPlugin.getPositionStream (
37
- locationSettings: const LocationSettings (
38
- accuracy: LocationAccuracy .medium,
39
- ),
40
- );
41
- expect (mockGeolocationManager.enableHighAccuracy, false );
42
-
43
- geolocatorPlugin.getPositionStream (
44
- locationSettings: const LocationSettings (
45
- accuracy: LocationAccuracy .best,
46
- ),
47
- );
48
- expect (mockGeolocationManager.enableHighAccuracy, true );
49
- });
50
-
51
- test (
52
- 'enableHighAccuracy returns the correct value depending on given accuracy in getCurrentPosition method' ,
53
- () async {
54
- final mockGeolocationManager = MockGeolocationManager ();
55
- final mockPermissionManager = MockPermissionManager ();
56
- final geolocatorPlugin =
57
- GeolocatorPlugin .private (mockGeolocationManager, mockPermissionManager);
58
-
59
- geolocatorPlugin.getCurrentPosition (
60
- locationSettings: const LocationSettings (
61
- accuracy: LocationAccuracy .medium,
62
- ),
63
- );
64
- expect (mockGeolocationManager.enableHighAccuracy, false );
65
-
66
- geolocatorPlugin.getCurrentPosition (
67
- locationSettings: const LocationSettings (
68
- accuracy: LocationAccuracy .high,
69
- ),
70
- );
71
- expect (mockGeolocationManager.enableHighAccuracy, true );
35
+ late MockGeolocationManager mockGeolocationManager;
36
+ late MockPermissionsManager mockPermissionsManager;
37
+ late GeolocatorPlugin geolocatorPlugin;
38
+
39
+ setUp (() {
40
+ mockGeolocationManager = MockGeolocationManager ();
41
+ mockPermissionsManager = MockPermissionsManager ();
42
+ geolocatorPlugin = GeolocatorPlugin .private (
43
+ mockGeolocationManager, mockPermissionsManager);
44
+
45
+ when (mockGeolocationManager.getCurrentPosition (
46
+ enableHighAccuracy: anyNamed ('enableHighAccuracy' ),
47
+ timeout: anyNamed ('timeout' )))
48
+ .thenAnswer ((_) async => mockPositions.first);
49
+ when (mockGeolocationManager.watchPosition (
50
+ enableHighAccuracy: anyNamed ('enableHighAccuracy' ),
51
+ timeout: anyNamed ('timeout' )))
52
+ .thenAnswer ((_) => Stream .fromIterable (mockPositions));
72
53
});
73
54
74
55
group ('Permission methods' , () {
75
56
test ('checkPermission throws exception when permissionsSupported is false' ,
76
- () {
77
- final mockGeolocationManager = MockGeolocationManager ();
78
- final mockPermissionManager = MockPermissionManager ();
79
- final geolocatorPlugin = GeolocatorPlugin .private (
80
- mockGeolocationManager, mockPermissionManager);
81
-
82
- when (mockPermissionManager.permissionsSupported).thenReturn (false );
57
+ () async {
58
+ when (mockPermissionsManager.permissionsSupported).thenReturn (false );
83
59
84
60
expect (
85
61
geolocatorPlugin.checkPermission, throwsA (isA <PlatformException >()));
86
62
});
87
63
88
64
test ('checkPermission returns the correct LocationPermission' , () async {
89
- final mockGeolocationManager = MockGeolocationManager ();
90
- final mockPermissionManager = MockPermissionManager ();
91
- final geolocatorPlugin = GeolocatorPlugin .private (
92
- mockGeolocationManager, mockPermissionManager);
93
-
94
- when (mockPermissionManager.permissionsSupported).thenReturn (true );
65
+ when (mockPermissionsManager.permissionsSupported).thenReturn (true );
95
66
96
67
await geolocatorPlugin.checkPermission ();
97
68
98
- verify (mockPermissionManager .query ({'name' : 'geolocation' })).called (1 );
69
+ verify (mockPermissionsManager .query ({'name' : 'geolocation' })).called (1 );
99
70
});
100
71
101
72
test ('requestPermission returns LocationPermission.whileInUse' , () async {
102
- final mockGeolocationManager = MockGeolocationManager ();
103
- final mockPermissionManager = MockPermissionManager ();
104
- final geolocatorPlugin = GeolocatorPlugin .private (
105
- mockGeolocationManager, mockPermissionManager);
106
-
107
73
final result = await geolocatorPlugin.requestPermission ();
108
74
109
75
expect (result, LocationPermission .whileInUse);
@@ -112,11 +78,6 @@ void main() {
112
78
113
79
group ('getCurrentPosition method' , () {
114
80
test ('getCurrentPosition should return a valid position' , () async {
115
- final mockGeolocationManager = MockGeolocationManager ();
116
- final mockPermissionManager = MockPermissionManager ();
117
- final geolocatorPlugin = GeolocatorPlugin .private (
118
- mockGeolocationManager, mockPermissionManager);
119
-
120
81
final position = await geolocatorPlugin.getCurrentPosition (
121
82
locationSettings: const LocationSettings ());
122
83
expect (position, mockPositions.first);
@@ -125,24 +86,14 @@ void main() {
125
86
126
87
group ('getPositionStream method' , () {
127
88
test ('getPositionStream should return all mocked positions' , () {
128
- final mockGeolocationManager = MockGeolocationManager ();
129
- final mockPermissionManager = MockPermissionManager ();
130
- final geolocatorPlugin = GeolocatorPlugin .private (
131
- mockGeolocationManager, mockPermissionManager);
132
-
133
89
final positionsStream = geolocatorPlugin.getPositionStream (
134
90
locationSettings: const LocationSettings ());
135
91
136
92
expect (positionsStream, emitsInOrder (mockPositions));
137
93
});
138
94
139
95
test ('getPositionStream should filter out mocked positions' , () {
140
- final mockGeolocationManager = MockGeolocationManager ();
141
- final mockPermissionManager = MockPermissionManager ();
142
- final geolocatorPlugin = GeolocatorPlugin .private (
143
- mockGeolocationManager, mockPermissionManager);
144
-
145
- var mockPositionsForFilter = List .of (() sync * {
96
+ final mockPositionsForFilter = List .of (() sync * {
146
97
for (var i = 0 ; i < 3 ; i++ ) {
147
98
yield Position (
148
99
latitude: 52.2669748 + i * 0.00005 ,
@@ -173,53 +124,18 @@ void main() {
173
124
174
125
group ('Unsupported exceptions' , () {
175
126
test ('getLastKnownPosition throws unsupported exception' , () async {
176
- final mockGeolocationManager = MockGeolocationManager ();
177
- final mockPermissionManager = MockPermissionManager ();
178
- final geolocatorPlugin = GeolocatorPlugin .private (
179
- mockGeolocationManager, mockPermissionManager);
180
-
181
127
expect (geolocatorPlugin.getLastKnownPosition,
182
128
throwsA (isA <PlatformException >()));
183
129
});
184
130
185
131
test ('openAppSettings throws unsupported exception' , () async {
186
- final mockGeolocationManager = MockGeolocationManager ();
187
- final mockPermissionManager = MockPermissionManager ();
188
- final geolocatorPlugin = GeolocatorPlugin .private (
189
- mockGeolocationManager, mockPermissionManager);
190
-
191
132
expect (
192
133
geolocatorPlugin.openAppSettings, throwsA (isA <PlatformException >()));
193
134
});
194
135
195
136
test ('openLocationSettings throws unsupported exception' , () async {
196
- final mockGeolocationManager = MockGeolocationManager ();
197
- final mockPermissionManager = MockPermissionManager ();
198
- final geolocatorPlugin = GeolocatorPlugin .private (
199
- mockGeolocationManager, mockPermissionManager);
200
-
201
137
expect (geolocatorPlugin.openLocationSettings,
202
138
throwsA (isA <PlatformException >()));
203
139
});
204
140
});
205
141
}
206
-
207
- class MockGeolocationManager implements GeolocationManager {
208
- bool ? enableHighAccuracy;
209
-
210
- @override
211
- Future <Position > getCurrentPosition (
212
- {bool ? enableHighAccuracy, Duration ? timeout}) {
213
- this .enableHighAccuracy = enableHighAccuracy;
214
- return Future .value (mockPositions.first);
215
- }
216
-
217
- @override
218
- Stream <Position > watchPosition (
219
- {bool ? enableHighAccuracy, Duration ? timeout}) {
220
- this .enableHighAccuracy = enableHighAccuracy;
221
- return Stream .fromIterable (mockPositions);
222
- }
223
- }
224
-
225
- class MockPermissionManager extends Mock implements PermissionsManager {}
0 commit comments