Commit 1c4a00e

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-08-09 09:07:36
Fix tests (#383)
* bump emulator setup timeout Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com> * enable bluetooth input in tests Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com> * reduce logging in tests Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com> --------- Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 1e83481
Changed files (6)
.github
workflows
app
.github/workflows/app-CI.yml
@@ -98,7 +98,7 @@ jobs:
         with:
           channel: ${{ env.FLUTTER_CHANNEL }}
       - name: Start Android emulator
-        timeout-minutes: 15
+        timeout-minutes: 25
         run: |
           export ANDROID_TOOLS="$ANDROID_HOME/cmdline-tools/latest/bin"
           echo "Starting emulator"
app/lib/model/storage/settings_store.dart
@@ -6,6 +6,7 @@ import 'package:blood_pressure_app/model/blood_pressure/medicine/medicine.dart';
 import 'package:blood_pressure_app/model/blood_pressure/pressure_unit.dart';
 import 'package:blood_pressure_app/model/horizontal_graph_line.dart';
 import 'package:blood_pressure_app/model/storage/convert_util.dart';
+import 'package:flutter/foundation.dart';
 import 'package:flutter/material.dart';
 
 /// Stores settings that are directly controllable by the user through the
@@ -396,7 +397,9 @@ class Settings extends ChangeNotifier {
 
   bool _bleInput = true;
   /// Whether to show bluetooth input on add measurement page.
-  bool get bleInput => (Platform.isAndroid || Platform.isIOS || Platform.isMacOS) && _bleInput;
+  bool get bleInput => (Platform.isAndroid || Platform.isIOS || Platform.isMacOS
+    || (kDebugMode && Platform.environment['FLUTTER_TEST'] == 'true'))
+      && _bleInput;
   set bleInput(bool value) {
     _bleInput = value;
     notifyListeners();
app/lib/logging.dart
@@ -1,13 +1,12 @@
+import 'dart:io';
+
 import 'package:flutter/foundation.dart';
 
 /// Simple class for manually logging in debug builds.
 class Log {
-  /// Disable error logging during testing.
-  static bool testExpectError = false;
-
   /// Log an error with stack trace in debug builds.
   static void err(String message, [List<Object>? dumps]) {
-    if (kDebugMode && !testExpectError) {
+    if (kDebugMode && !(Platform.environment['FLUTTER_TEST'] == 'true')) {
       debugPrint('-----------------------------');
       debugPrint('ERROR $message:');
       debugPrintStack();
@@ -19,7 +18,7 @@ class Log {
 
   /// Log a message in debug more
   static void trace(String message) {
-    if (kDebugMode && !testExpectError) {
+    if (kDebugMode && !(Platform.environment['FLUTTER_TEST'] == 'true')) {
       debugPrint('TRACE: $message');
     }
   }
app/test/features/bluetooth/logic/bluetooth_cubit_test.dart
@@ -2,7 +2,6 @@ import 'dart:async';
 
 import 'package:blood_pressure_app/features/bluetooth/logic/bluetooth_cubit.dart';
 import 'package:blood_pressure_app/features/bluetooth/logic/flutter_blue_plus_mockable.dart';
-import 'package:blood_pressure_app/logging.dart';
 import 'package:flutter/widgets.dart';
 import 'package:flutter_blue_plus/flutter_blue_plus.dart';
 import 'package:flutter_test/flutter_test.dart';
@@ -28,7 +27,6 @@ void main() {
         BluetoothAdapterState.turningOn,
         BluetoothAdapterState.on,
     ]));
-    Log.testExpectError = true;
     final cubit = BluetoothCubit(flutterBluePlus: bluePlus);
     expect(cubit.state, isA<BluetoothInitial>());
 
@@ -41,6 +39,5 @@ void main() {
       isA<BluetoothDisabled>(),
       isA<BluetoothReady>(),
     ]));
-    Log.testExpectError = false;
   });
 }
app/test/features/bluetooth/logic/device_scan_cubit_test.dart
@@ -2,7 +2,6 @@ import 'dart:async';
 
 import 'package:blood_pressure_app/features/bluetooth/logic/device_scan_cubit.dart';
 import 'package:blood_pressure_app/features/bluetooth/logic/flutter_blue_plus_mockable.dart';
-import 'package:blood_pressure_app/logging.dart';
 import 'package:blood_pressure_app/model/storage/settings_store.dart';
 import 'package:flutter_blue_plus/flutter_blue_plus.dart';
 import 'package:flutter_test/flutter_test.dart';
@@ -20,7 +19,6 @@ import 'device_scan_cubit_test.mocks.dart';
 
 void main() {
   test('finds and connects to devices', () async {
-    Log.testExpectError = true;
     final StreamController<List<ScanResult>> mockResults = StreamController.broadcast();
     final settings = Settings();
 
@@ -70,10 +68,8 @@ void main() {
     // state should be set as we await above
     await expectLater(cubit.state, isA<DeviceSelected>()
       .having((s) => s.device, 'device', dev));
-    Log.testExpectError = false;
   });
   test('recognizes devices', () async {
-    Log.testExpectError = true;
     final StreamController<List<ScanResult>> mockResults = StreamController.broadcast();
     final settings = Settings(
       knownBleDev: ['testDev']
@@ -110,6 +106,5 @@ void main() {
     mockResults.sink.add([wrongRes0, res]);
     // No prompt when finding the correct device again
     await expectLater(cubit.stream, emits(isA<DeviceSelected>()));
-    Log.testExpectError = false;
   });
 }
app/test/model/config_db_test.dart
@@ -14,7 +14,8 @@ void main() {
   group('ConfigDB', () {
     setUpAll(() {
       sqfliteFfiInit();
-      databaseFactory = databaseFactoryFfi;
+      // Avoid warning in logs by avoiding `databaseFactory` setter
+      databaseFactoryOrNull = databaseFactoryFfi;
     });
 
     test('should initialize database without error', () async {
@@ -46,7 +47,7 @@ void main() {
   group('ConfigDAO', () {
     setUpAll(() {
       sqfliteFfiInit();
-      databaseFactory = databaseFactoryFfi;
+      databaseFactoryOrNull = databaseFactoryFfi;
     });
 
     test('should initialize', () async {