Skip to content

Commit d4b5304

Browse files
Migrate web tests to support sound null safety (#1380)
* Migrate geolocator web tests to support sound null safety * Update version information and change log * Add generated mock files * Fix mock files --------- Co-authored-by: Maurits van Beusekom <[email protected]>
1 parent 18a0b21 commit d4b5304

File tree

6 files changed

+194
-122
lines changed

6 files changed

+194
-122
lines changed

geolocator_web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.2.1
2+
3+
- Migrate tests to support sound null safety.
4+
15
## 2.2.0
26

37
- Exposes altitude accuracy field in `Position` object.

geolocator_web/pubspec.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: geolocator_web
22
description: Official web implementation of the geolocator plugin.
33
repository: https://github.com/baseflow/flutter-geolocator/tree/main/geolocator_web
44
issue_tracker: https://github.com/baseflow/flutter-geolocator/issues?q=is%3Aissue+is%3Aopen
5-
version: 2.2.0
5+
version: 2.2.1
66

77
flutter:
88
plugin:
@@ -23,7 +23,8 @@ dev_dependencies:
2323
flutter_test:
2424
sdk: flutter
2525
flutter_lints: ^3.0.1
26-
mockito: ^5.0.0
26+
mockito: ^5.4.0
27+
2728

2829
environment:
2930
sdk: '>=2.15.0 <4.0.0'

geolocator_web/test/geolocator_utils_test.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import 'dart:html' as html;
21
import 'dart:async';
2+
import 'dart:html';
3+
34
import 'package:flutter/services.dart';
45
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:mockito/annotations.dart';
7+
import 'package:mockito/mockito.dart';
58
import 'package:geolocator_platform_interface/geolocator_platform_interface.dart';
69
import 'package:geolocator_web/src/utils.dart';
7-
import 'package:mockito/mockito.dart';
810

11+
import 'geolocator_utils_test.mocks.dart';
12+
13+
@GenerateNiceMocks([
14+
MockSpec<Geoposition>(),
15+
MockSpec<PositionError>(),
16+
])
917
void main() {
1018
test('toPosition should throw a exception if coords is null', () {
1119
final geoposition = MockGeoposition();
@@ -39,7 +47,3 @@ void main() {
3947
expect(convertPositionError(positionError), isA<PlatformException>());
4048
});
4149
}
42-
43-
class MockGeoposition extends Mock implements html.Geoposition {}
44-
45-
class MockPositionError extends Mock implements html.PositionError {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Mocks generated by Mockito 5.4.1-wip from annotations
2+
// in geolocator.web/test/geolocator_utils_test.dart.
3+
// Do not manually edit this file.
4+
5+
// ignore_for_file: no_leading_underscores_for_library_prefixes
6+
import 'dart:html' as _i2;
7+
8+
import 'package:mockito/mockito.dart' as _i1;
9+
10+
// ignore_for_file: type=lint
11+
// ignore_for_file: avoid_redundant_argument_values
12+
// ignore_for_file: avoid_setters_without_getters
13+
// ignore_for_file: comment_references
14+
// ignore_for_file: deprecated_member_use
15+
// ignore_for_file: deprecated_member_use_from_same_package
16+
// ignore_for_file: implementation_imports
17+
// ignore_for_file: invalid_use_of_visible_for_testing_member
18+
// ignore_for_file: prefer_const_constructors
19+
// ignore_for_file: unnecessary_parenthesis
20+
// ignore_for_file: camel_case_types
21+
// ignore_for_file: subtype_of_sealed_class
22+
23+
/// A class which mocks [Geoposition].
24+
///
25+
/// See the documentation for Mockito's code generation for more information.
26+
class MockGeoposition extends _i1.Mock implements _i2.Geoposition {}
27+
28+
/// A class which mocks [PositionError].
29+
///
30+
/// See the documentation for Mockito's code generation for more information.
31+
class MockPositionError extends _i1.Mock implements _i2.PositionError {}

geolocator_web/test/geolocator_web_test.dart

Lines changed: 30 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import 'package:flutter_test/flutter_test.dart';
33
import 'package:geolocator_web/geolocator_web.dart';
44
import 'package:geolocator_web/src/geolocation_manager.dart';
55
import 'package:geolocator_web/src/permissions_manager.dart';
6+
import 'package:mockito/annotations.dart';
67
import 'package:mockito/mockito.dart';
78

9+
import 'geolocator_web_test.mocks.dart';
10+
811
List<Position> get mockPositions => List.of(() sync* {
912
for (var i = 0; i < 5; i++) {
1013
yield Position(
@@ -24,86 +27,49 @@ List<Position> get mockPositions => List.of(() sync* {
2427
}
2528
}());
2629

30+
@GenerateNiceMocks([
31+
MockSpec<GeolocationManager>(),
32+
MockSpec<PermissionsManager>(),
33+
])
2734
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));
7253
});
7354

7455
group('Permission methods', () {
7556
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);
8359

8460
expect(
8561
geolocatorPlugin.checkPermission, throwsA(isA<PlatformException>()));
8662
});
8763

8864
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);
9566

9667
await geolocatorPlugin.checkPermission();
9768

98-
verify(mockPermissionManager.query({'name': 'geolocation'})).called(1);
69+
verify(mockPermissionsManager.query({'name': 'geolocation'})).called(1);
9970
});
10071

10172
test('requestPermission returns LocationPermission.whileInUse', () async {
102-
final mockGeolocationManager = MockGeolocationManager();
103-
final mockPermissionManager = MockPermissionManager();
104-
final geolocatorPlugin = GeolocatorPlugin.private(
105-
mockGeolocationManager, mockPermissionManager);
106-
10773
final result = await geolocatorPlugin.requestPermission();
10874

10975
expect(result, LocationPermission.whileInUse);
@@ -112,11 +78,6 @@ void main() {
11278

11379
group('getCurrentPosition method', () {
11480
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-
12081
final position = await geolocatorPlugin.getCurrentPosition(
12182
locationSettings: const LocationSettings());
12283
expect(position, mockPositions.first);
@@ -125,24 +86,14 @@ void main() {
12586

12687
group('getPositionStream method', () {
12788
test('getPositionStream should return all mocked positions', () {
128-
final mockGeolocationManager = MockGeolocationManager();
129-
final mockPermissionManager = MockPermissionManager();
130-
final geolocatorPlugin = GeolocatorPlugin.private(
131-
mockGeolocationManager, mockPermissionManager);
132-
13389
final positionsStream = geolocatorPlugin.getPositionStream(
13490
locationSettings: const LocationSettings());
13591

13692
expect(positionsStream, emitsInOrder(mockPositions));
13793
});
13894

13995
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* {
14697
for (var i = 0; i < 3; i++) {
14798
yield Position(
14899
latitude: 52.2669748 + i * 0.00005,
@@ -173,53 +124,18 @@ void main() {
173124

174125
group('Unsupported exceptions', () {
175126
test('getLastKnownPosition throws unsupported exception', () async {
176-
final mockGeolocationManager = MockGeolocationManager();
177-
final mockPermissionManager = MockPermissionManager();
178-
final geolocatorPlugin = GeolocatorPlugin.private(
179-
mockGeolocationManager, mockPermissionManager);
180-
181127
expect(geolocatorPlugin.getLastKnownPosition,
182128
throwsA(isA<PlatformException>()));
183129
});
184130

185131
test('openAppSettings throws unsupported exception', () async {
186-
final mockGeolocationManager = MockGeolocationManager();
187-
final mockPermissionManager = MockPermissionManager();
188-
final geolocatorPlugin = GeolocatorPlugin.private(
189-
mockGeolocationManager, mockPermissionManager);
190-
191132
expect(
192133
geolocatorPlugin.openAppSettings, throwsA(isA<PlatformException>()));
193134
});
194135

195136
test('openLocationSettings throws unsupported exception', () async {
196-
final mockGeolocationManager = MockGeolocationManager();
197-
final mockPermissionManager = MockPermissionManager();
198-
final geolocatorPlugin = GeolocatorPlugin.private(
199-
mockGeolocationManager, mockPermissionManager);
200-
201137
expect(geolocatorPlugin.openLocationSettings,
202138
throwsA(isA<PlatformException>()));
203139
});
204140
});
205141
}
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

Comments
 (0)