From 888e5b7ce994daf3e033473a59a28b294311616c Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Tue, 4 Jun 2024 15:36:22 -0400 Subject: [PATCH] First test which mocks hive and riverpod also made add/remove methods async for stored devices --- lib/Backend/Bluetooth/bluetooth_manager.dart | 8 +- pubspec.lock | 100 ++++++++++++++++--- pubspec.yaml | 8 +- test/Backend/device_registry_test.dart | 32 ++++++ test/testing_utils/gear_utils.dart | 20 ++++ test/testing_utils/hive_utils.dart | 58 +++++++++++ 6 files changed, 208 insertions(+), 18 deletions(-) create mode 100644 test/Backend/device_registry_test.dart create mode 100644 test/testing_utils/gear_utils.dart create mode 100644 test/testing_utils/hive_utils.dart diff --git a/lib/Backend/Bluetooth/bluetooth_manager.dart b/lib/Backend/Bluetooth/bluetooth_manager.dart index bd238498..f2ac8480 100644 --- a/lib/Backend/Bluetooth/bluetooth_manager.dart +++ b/lib/Backend/Bluetooth/bluetooth_manager.dart @@ -35,18 +35,18 @@ class KnownDevices extends _$KnownDevices { return results; } - void add(BaseStatefulDevice baseStatefulDevice) { + Future add(BaseStatefulDevice baseStatefulDevice) async { Map state2 = Map.from(state); state2[baseStatefulDevice.baseStoredDevice.btMACAddress] = baseStatefulDevice; state = state2; - store(); + await store(); } - void remove(String id) { + Future remove(String id) async { Map state2 = Map.from(state); state2.remove(id); state = state2; - store(); + await store(); } Future store() async { diff --git a/pubspec.lock b/pubspec.lock index de924847..0c7d52b8 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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: @@ -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 @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/pubspec.yaml b/pubspec.yaml index bb94531f..5469d526 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -129,7 +129,7 @@ dependencies: path: feedback_sentry mockito: ^5.4.4 platform: ^3.1.4 - + dev_dependencies: build_runner: # Required for build flutter_gen_runner: @@ -137,7 +137,11 @@ dev_dependencies: 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 diff --git a/test/Backend/device_registry_test.dart b/test/Backend/device_registry_test.dart new file mode 100644 index 00000000..3575c9a8 --- /dev/null +++ b/test/Backend/device_registry_test.dart @@ -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('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('devices').length, 1); + expect(HiveProxy.getAll('devices').first, baseStatefulDevice.baseStoredDevice); + }); +} diff --git a/test/testing_utils/gear_utils.dart b/test/testing_utils/gear_utils.dart new file mode 100644 index 00000000..31414cf0 --- /dev/null +++ b/test/testing_utils/gear_utils.dart @@ -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 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; +} diff --git a/test/testing_utils/hive_utils.dart b/test/testing_utils/hive_utils.dart new file mode 100644 index 00000000..7f71883a --- /dev/null +++ b/test/testing_utils/hive_utils.dart @@ -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 deleteHive() async { + await SentryHive.deleteFromDisk(); +} + +class FakePathProviderPlatform extends Fake with MockPlatformInterfaceMixin implements PathProviderPlatform { + @override + Future getTemporaryPath() async { + return 'test/temp/'; + } + + @override + Future getApplicationSupportPath() async { + return 'test/support/'; + } + + @override + Future getLibraryPath() async { + return 'test/library/'; + } + + @override + Future getApplicationDocumentsPath() async { + return 'test/application/'; + } + + @override + Future getExternalStoragePath() async { + return 'test/external/'; + } + + @override + Future?> getExternalCachePaths() async { + return ['test/externalCache/']; + } + + @override + Future?> getExternalStoragePaths({ + StorageDirectory? type, + }) async { + return ['test/external/']; + } + + @override + Future getDownloadsPath() async { + return 'test/downloads/'; + } +} + +Future setupHive() async { + PathProviderPlatform.instance = FakePathProviderPlatform(); + await initHive(); +}