Skip to content

Commit

Permalink
Added location model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dchristl committed Mar 30, 2024
1 parent a562ce7 commit 085cb9d
Show file tree
Hide file tree
Showing 7 changed files with 642 additions and 9 deletions.
8 changes: 6 additions & 2 deletions macless_haystack/lib/accessory/accessory_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class Accessory {
/// Stores address information about the current location.
Future<Placemark?> place = Future.value(null);

LocationModel locationModel = LocationModel();

/// Creates an accessory with the given properties.
Accessory(
{required this.id,
Expand All @@ -113,7 +115,7 @@ class Accessory {

void _init() {
if (_lastLocation != null) {
place = LocationModel.getAddress(_lastLocation!);
place = locationModel.getAddress(_lastLocation!);
}
}

Expand Down Expand Up @@ -160,10 +162,12 @@ class Accessory {
set lastLocation(LatLng? newLocation) {
_lastLocation = newLocation;
if (_lastLocation != null) {
place = LocationModel.getAddress(_lastLocation!);
place = locationModel.getAddress(_lastLocation!);
}
}



/// The display icon of the accessory.
IconData get icon {
IconData? icon = AccessoryIconModel.mapIcon(_icon);
Expand Down
5 changes: 4 additions & 1 deletion macless_haystack/lib/accessory/accessory_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const accessoryStorageKey = 'ACCESSORIES';
const historStorageKey = 'HISTORY';

class AccessoryRegistry extends ChangeNotifier {
final _storage = const FlutterSecureStorage();
var _storage = const FlutterSecureStorage();
List<Accessory> _accessories = [];
bool loading = false;
bool initialLoadFinished = false;
Expand Down Expand Up @@ -55,6 +55,9 @@ class AccessoryRegistry extends ChangeNotifier {

notifyListeners();
}
set setStorage(FlutterSecureStorage s){
_storage = s;
}

Future<void> loadHistory() async {
String? history = await _storage.read(key: historStorageKey);
Expand Down
8 changes: 7 additions & 1 deletion macless_haystack/lib/findMy/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class FindMyLocationReport {
FindMyLocationReport(this.latitude, this.longitude, this.accuracy,
this.published, this.timestamp, this.confidence);

FindMyLocationReport.withHash(
this.latitude, this.longitude, this.timestamp, this.hash) {
accuracy = 0;
}

FindMyLocationReport.decrypted(this.result, this.base64privateKey, this.id) {
hash = result['payload'];
}
Expand All @@ -50,7 +55,8 @@ class FindMyLocationReport {
await Future.delayed(const Duration(
milliseconds: 1)); //Is needed otherwise is executed synchron
if (isEncrypted()) {
logger.d('Decrypting report with private key of ${getId()!.substring(0, 4)}');
logger.d(
'Decrypting report with private key of ${getId()!.substring(0, 4)}');
final unixTimestampInMillis = result["datePublished"];
final datePublished =
DateTime.fromMillisecondsSinceEpoch(unixTimestampInMillis);
Expand Down
2 changes: 1 addition & 1 deletion macless_haystack/lib/location/location_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class LocationModel extends ChangeNotifier {
/// Returns the address for a given geolocation (latitude & longitude).
///
/// Only works on mobile platforms with their local APIs.
static Future<geocode.Placemark?> getAddress(LatLng? location) async {
Future<geocode.Placemark?> getAddress(LatLng? location) async {
if (location == null) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions macless_haystack/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
flutter_slidable: ^3.0.0

# Networking
http: ^1.1.0
http: ^1.2.1
universal_html: ^2.2.3

# Cryptography
Expand Down Expand Up @@ -70,9 +70,6 @@ dependencies:
intl: ^0.19.0
logger: ^2.0.1

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
#cupertino_icons: ^1.0.2

dev_dependencies:
flutter_test:
Expand All @@ -86,6 +83,9 @@ dev_dependencies:
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^3.0.1
test: ^1.24.9
mockito: ^5.4.4
build_runner: ^2.4.9

# Configuration for flutter_launcher_icons
flutter_icons:
Expand Down
114 changes: 114 additions & 0 deletions macless_haystack/test/accessory/accessory_registry_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:geocoding/geocoding.dart';
import 'package:latlong2/latlong.dart';
import 'package:macless_haystack/accessory/accessory_model.dart';
import 'package:macless_haystack/accessory/accessory_registry.dart';
import 'package:macless_haystack/findMy/models.dart';
import 'package:macless_haystack/location/location_model.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import 'accessory_registry_test.mocks.dart';

// class MockLocationModel extends Mock implements LocationModel {}

@GenerateMocks([LocationModel, FlutterSecureStorage])
void main() {
var locationModel = MockLocationModel();
var registry = AccessoryRegistry();
Accessory accessory = Accessory(
id: '',
name: '',
hashedPublicKey: '',
datePublished: DateTime.now(),
additionalKeys: List.empty());
setUp(() {
when(locationModel.getAddress(any))
.thenAnswer((_) async => const Placemark());
registry.setStorage = MockFlutterSecureStorage();
accessory.locationModel = locationModel;
});

test('Add location history same location unsorted with no entries before',
() async {
List<FindMyLocationReport> reports = [];
// 8 o'clock
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 8, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
//6 o'clock
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 6, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
//10 o'clock
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 10, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));

await registry.fillLocationHistory(reports, accessory);
var latest = accessory.latestHistoryEntry();
expect(DateTime(2024, 1, 1, 10, 0, 0), latest);
expect(1, accessory.locationHistory.length);
expect(DateTime(2024, 1, 1, 10, 0, 0),
accessory.locationHistory.elementAt(0).end);
expect(DateTime(2024, 1, 1, 6, 0, 0),
accessory.locationHistory.elementAt(0).start);
});

test(
'Add location history different location unsorted with no entries before',
() async {
List<FindMyLocationReport> reports = [];
// 8 o'clock 1st location
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 8, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
//10 o'clock second location
reports.add(FindMyLocationReport.withHash(
2,
2,
DateTime(2024, 1, 1, 10, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
// 9 o'clock first location
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 9, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
//12 o'clock 1st location
reports.add(FindMyLocationReport.withHash(
1,
2,
DateTime(2024, 1, 1, 12, 0, 0),
DateTime.now().microsecondsSinceEpoch.toString()));
await registry.fillLocationHistory(reports, accessory);
var locationHistory = accessory.locationHistory;
expect(3, locationHistory.length);

var latest = accessory.datePublished;
var lastLocation = accessory.lastLocation;
var endOfFirstEntry = accessory.latestHistoryEntry();

expect(endOfFirstEntry, DateTime(2024, 1, 1, 9, 0, 0));
expect(latest, DateTime(2024, 1, 1, 12, 0, 0));
expect(lastLocation, const LatLng(1, 2));

expect(locationHistory.elementAt(0).start, DateTime(2024, 1, 1, 8, 0, 0));
expect(locationHistory.elementAt(0).end, DateTime(2024, 1, 1, 9, 0, 0));

expect(locationHistory.elementAt(1).start, DateTime(2024, 1, 1, 10, 0, 0));
expect(locationHistory.elementAt(1).end, DateTime(2024, 1, 1, 10, 0, 0));

expect(locationHistory.elementAt(2).start, DateTime(2024, 1, 1, 12, 0, 0));
expect(locationHistory.elementAt(2).end, DateTime(2024, 1, 1, 12, 0, 0));
});
}
Loading

0 comments on commit 085cb9d

Please sign in to comment.