Skip to content

Commit

Permalink
First test which mocks hive and riverpod
Browse files Browse the repository at this point in the history
also made add/remove methods async for stored devices
  • Loading branch information
Codel1417 committed Jun 4, 2024
1 parent 5caa7d5 commit 888e5b7
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 18 deletions.
8 changes: 4 additions & 4 deletions lib/Backend/Bluetooth/bluetooth_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ class KnownDevices extends _$KnownDevices {
return results;
}

void add(BaseStatefulDevice baseStatefulDevice) {
Future<void> add(BaseStatefulDevice baseStatefulDevice) async {
Map<String, BaseStatefulDevice> state2 = Map.from(state);
state2[baseStatefulDevice.baseStoredDevice.btMACAddress] = baseStatefulDevice;
state = state2;
store();
await store();
}

void remove(String id) {
Future<void> remove(String id) async {

Check warning on line 45 in lib/Backend/Bluetooth/bluetooth_manager.dart

View check run for this annotation

Codecov / codecov/patch

lib/Backend/Bluetooth/bluetooth_manager.dart#L45

Added line #L45 was not covered by tests
Map<String, BaseStatefulDevice> state2 = Map.from(state);
state2.remove(id);
state = state2;
store();
await store();

Check warning on line 49 in lib/Backend/Bluetooth/bluetooth_manager.dart

View check run for this annotation

Codecov / codecov/patch

lib/Backend/Bluetooth/bluetooth_manager.dart#L49

Added line #L49 was not covered by tests
}

Future<void> store() async {
Expand Down
100 changes: 88 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.1"
fake_async:
dependency: transitive
description:
name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.dev"
source: hosted
version: "1.3.1"
feedback:
dependency: transitive
description:
Expand Down Expand Up @@ -526,6 +534,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.32.7"
flutter_driver:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
flutter_foreground_task:
dependency: "direct main"
description:
Expand Down Expand Up @@ -667,6 +680,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.0.2"
flutter_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
flutter_wear_os_connectivity:
dependency: "direct main"
description:
Expand Down Expand Up @@ -697,6 +715,11 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.0"
fuchsia_remote_debug_protocol:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
glob:
dependency: transitive
description:
Expand Down Expand Up @@ -834,6 +857,11 @@ packages:
url: "https://github.com/undreeyyy/flutter_plugin_install_referrer"
source: git
version: "1.2.1"
integration_test:
dependency: "direct dev"
description: flutter
source: sdk
version: "0.0.0"
intl:
dependency: "direct main"
description:
Expand Down Expand Up @@ -914,6 +942,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.4.11"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -1244,6 +1296,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
process:
dependency: transitive
description:
name: process
sha256: "21e54fd2faf1b5bdd5102afd25012184a6793927648ea81eea80552ac9405b32"
url: "https://pub.dev"
source: hosted
version: "5.0.2"
proximity_sensor:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1498,10 +1558,10 @@ packages:
dependency: transitive
description:
name: shelf_web_socket
sha256: "073c147238594ecd0d193f3456a5fe91c4b0abbcc68bf5cd95b36c4e194ac611"
sha256: "9ca081be41c60190ebcb4766b2486a7d50261db7bd0f5d9615f2d653637a84c1"
url: "https://pub.dev"
source: hosted
version: "2.0.0"
version: "1.0.4"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -1595,6 +1655,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.0"
sync_http:
dependency: transitive
description:
name: sync_http
sha256: "7f0cd72eca000d2e026bcd6f990b81d0ca06022ef4e32fb257b30d3d1014a961"
url: "https://pub.dev"
source: hosted
version: "0.3.1"
synchronized:
dependency: transitive
description:
Expand All @@ -1615,26 +1683,26 @@ packages:
dependency: "direct dev"
description:
name: test
sha256: "47e9b601e20f24c27d08002eb635e92ddc2195010a8b3a621f4ed44ef70f6864"
sha256: "7ee446762c2c50b3bd4ea96fe13ffac69919352bd3b4b17bac3f3465edc58073"
url: "https://pub.dev"
source: hosted
version: "1.25.6"
version: "1.25.2"
test_api:
dependency: transitive
description:
name: test_api
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
url: "https://pub.dev"
source: hosted
version: "0.7.2"
version: "0.7.0"
test_core:
dependency: transitive
description:
name: test_core
sha256: "0c583123c86e58ab5036c92db5c7116ffddd655f1f5be762967d09f0fc66bf2d"
sha256: "2bc4b4ecddd75309300d8096f781c0e3280ca1ef85beda558d33fcbedc2eead4"
url: "https://pub.dev"
source: hosted
version: "0.6.3"
version: "0.6.0"
time:
dependency: transitive
description:
Expand Down Expand Up @@ -1791,10 +1859,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "360c4271613beb44db559547d02f8b0dc044741d0eeb9aa6ccdb47e8ec54c63a"
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
url: "https://pub.dev"
source: hosted
version: "14.2.3"
version: "14.2.1"
wakelock_plus:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1839,10 +1907,18 @@ packages:
dependency: transitive
description:
name: web_socket_channel
sha256: a2d56211ee4d35d9b344d9d4ce60f362e4f5d1aafb988302906bd732bc731276
sha256: "58c6666b342a38816b2e7e50ed0f1e261959630becd4c879c4f26bfa14aa5a42"
url: "https://pub.dev"
source: hosted
version: "3.0.0"
version: "2.4.5"
webdriver:
dependency: transitive
description:
name: webdriver
sha256: "003d7da9519e1e5f329422b36c4dcdf18d7d2978d1ba099ea4e45ba490ed845e"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
webkit_inspection_protocol:
dependency: transitive
description:
Expand Down
8 changes: 6 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,19 @@ dependencies:
path: feedback_sentry
mockito: ^5.4.4
platform: ^3.1.4

dev_dependencies:
build_runner: # Required for build
flutter_gen_runner:
flutter_lints: # Dryer Lint
riverpod_generator: ^2.4.2 #required for @Riverpod annotations
json_serializable: ^6.8.0 # required for @JsonSerializable annotations
hive_generator: ^2.0.1 # required for @HiveType annotations
test: ^1.25.6
test:
flutter_test:
sdk: flutter
integration_test:
sdk: flutter

flutter:
uses-material-design: true
Expand Down
32 changes: 32 additions & 0 deletions test/Backend/device_registry_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart' as flTest;
import 'package:tail_app/Backend/Bluetooth/bluetooth_manager.dart';
import 'package:tail_app/Backend/Definitions/Device/device_definition.dart';
import 'package:tail_app/Backend/LoggingWrappers.dart';
import 'package:test/test.dart';

import '../testing_utils/gear_utils.dart';
import '../testing_utils/hive_utils.dart';

void main() {
setUpAll(() async {
flTest.TestWidgetsFlutterBinding.ensureInitialized();
await setupHive();
});
tearDownAll(() async {
await deleteHive();
});
test('Test storing gear to ref', () async {
final container = ProviderContainer(
overrides: [],
);
expect(container.read(knownDevicesProvider).length, 0);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 0);
BaseStatefulDevice baseStatefulDevice = await createAndStoreGear('MiTail', container);
expect(baseStatefulDevice.baseDeviceDefinition.btName, 'MiTail');
expect(container.read(knownDevicesProvider).length, 1);
expect(container.read(knownDevicesProvider).values.first, baseStatefulDevice);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').length, 1);
expect(HiveProxy.getAll<BaseStoredDevice>('devices').first, baseStatefulDevice.baseStoredDevice);
});
}
20 changes: 20 additions & 0 deletions test/testing_utils/gear_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:tail_app/Backend/Bluetooth/bluetooth_manager.dart';
import 'package:tail_app/Backend/Bluetooth/bluetooth_manager_plus.dart';
import 'package:tail_app/Backend/Definitions/Device/device_definition.dart';
import 'package:tail_app/Backend/device_registry.dart';

Future<BaseStatefulDevice> createAndStoreGear(String gearBtName, ProviderContainer ref) async {
BaseDeviceDefinition baseDeviceDefinition = DeviceRegistry.getByName(gearBtName)!;
BaseStoredDevice baseStoredDevice;
BaseStatefulDevice statefulDevice;
baseStoredDevice = BaseStoredDevice(baseDeviceDefinition.uuid, "DEV${baseDeviceDefinition.deviceType.name}", baseDeviceDefinition.deviceType.color(ref: ref).value);
baseStoredDevice.name = getNameFromBTName(baseDeviceDefinition.btName);
statefulDevice = BaseStatefulDevice(baseDeviceDefinition, baseStoredDevice);
statefulDevice.deviceConnectionState.value = ConnectivityState.connected;
isAnyGearConnected.value = true;
if (!ref.read(knownDevicesProvider).containsKey(baseStoredDevice.btMACAddress)) {
await ref.read(knownDevicesProvider.notifier).add(statefulDevice);
}
return statefulDevice;
}
58 changes: 58 additions & 0 deletions test/testing_utils/hive_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:path_provider_platform_interface/path_provider_platform_interface.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'package:sentry_hive/sentry_hive.dart';
import 'package:tail_app/main.dart';

Future<void> deleteHive() async {
await SentryHive.deleteFromDisk();
}

class FakePathProviderPlatform extends Fake with MockPlatformInterfaceMixin implements PathProviderPlatform {
@override
Future<String?> getTemporaryPath() async {
return 'test/temp/';
}

@override
Future<String?> getApplicationSupportPath() async {
return 'test/support/';
}

@override
Future<String?> getLibraryPath() async {
return 'test/library/';
}

@override
Future<String?> getApplicationDocumentsPath() async {
return 'test/application/';
}

@override
Future<String?> getExternalStoragePath() async {
return 'test/external/';
}

@override
Future<List<String>?> getExternalCachePaths() async {
return <String>['test/externalCache/'];
}

@override
Future<List<String>?> getExternalStoragePaths({
StorageDirectory? type,
}) async {
return <String>['test/external/'];
}

@override
Future<String?> getDownloadsPath() async {
return 'test/downloads/';
}
}

Future<void> setupHive() async {
PathProviderPlatform.instance = FakePathProviderPlatform();
await initHive();
}

0 comments on commit 888e5b7

Please sign in to comment.