-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathlocation_service_test.dart
More file actions
91 lines (83 loc) · 2.48 KB
/
Copy pathlocation_service_test.dart
File metadata and controls
91 lines (83 loc) · 2.48 KB
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
import 'package:dpip/core/geo/location_service.dart';
import 'package:dpip/core/geo/town_directory.dart';
import 'package:flutter_test/flutter_test.dart';
TownDirectory _dir() => TownDirectory.fromJson({
'100': {
'city': '臺北',
'town': '中正',
'lat': 25.03,
'lng': 121.52,
'cityLevel': '市',
'townLevel': '區',
},
'970': {
'city': '花蓮',
'town': '花蓮',
'lat': 23.99,
'lng': 121.60,
'cityLevel': '縣',
'townLevel': '市',
},
});
void main() {
test('resolves the nearest township from a GPS fix', () async {
final service = LocationService(
_dir(),
isAvailable: () async => true,
fix: () async => (lat: 25.05, lng: 121.55),
);
expect((await service.currentTown())?.code, '100');
});
test('null when location is unavailable (services off / denied)', () async {
final service = LocationService(
_dir(),
isAvailable: () async => false,
fix: () async => (lat: 25.05, lng: 121.55),
);
expect(await service.currentTown(), isNull);
});
test('null when no fix could be obtained', () async {
final service = LocationService(
_dir(),
isAvailable: () async => true,
fix: () async => null,
);
expect(await service.currentTown(), isNull);
});
test('a fix error degrades to null, never throws', () async {
final service = LocationService(
_dir(),
isAvailable: () async => true,
fix: () async => throw Exception('gps timeout'),
);
expect(await service.currentTown(), isNull);
});
test('lastKnownFix serves the cached fix without a live read', () async {
var liveReads = 0;
final service = LocationService(
_dir(),
lastKnown: () async => (lat: 25.05, lng: 121.55),
fix: () async {
liveReads++;
return (lat: 25.05, lng: 121.55);
},
);
expect(await service.lastKnownFix(), (lat: 25.05, lng: 121.55));
expect(liveReads, 0, reason: 'the live read must not run');
});
test('lastKnownFix is null when no cached fix exists', () async {
final service = LocationService(
_dir(),
lastKnown: () async => null,
fix: () async => (lat: 25.05, lng: 121.55),
);
expect(await service.lastKnownFix(), isNull);
});
test('a lastKnownFix fault degrades to null, never throws', () async {
final service = LocationService(
_dir(),
lastKnown: () async => throw Exception('platform fault'),
);
expect(await service.lastKnownFix(), isNull);
});
}