main
  1
  2import 'package:blood_pressure_app/model/blood_pressure/pressure_unit.dart';
  3import 'package:blood_pressure_app/model/export_import/column.dart';
  4import 'package:blood_pressure_app/model/export_import/export_configuration.dart';
  5import 'package:blood_pressure_app/model/horizontal_graph_line.dart';
  6import 'package:blood_pressure_app/model/storage/bluetooth_input_mode.dart';
  7import 'package:blood_pressure_app/model/storage/export_columns_store.dart';
  8import 'package:blood_pressure_app/model/storage/export_csv_settings_store.dart';
  9import 'package:blood_pressure_app/model/storage/export_pdf_settings_store.dart';
 10import 'package:blood_pressure_app/model/storage/export_settings_store.dart';
 11import 'package:blood_pressure_app/model/storage/interval_store.dart';
 12import 'package:blood_pressure_app/model/storage/settings_store.dart';
 13import 'package:blood_pressure_app/model/weight_unit.dart';
 14import 'package:flutter/material.dart';
 15import 'package:flutter_test/flutter_test.dart';
 16import 'package:health_data_store/health_data_store.dart';
 17
 18void main() {
 19  group('IntervallStorage', () {
 20    test('should create json without error', () {
 21      final intervall = IntervalStorage(stepSize: TimeStep.year);
 22      final json = intervall.toJson();
 23      expect(json.length, greaterThan(0));
 24    });
 25
 26    test('should load same data from json', () {
 27      final initialData = IntervalStorage();
 28      final json = initialData.toJson();
 29      final recreatedData = IntervalStorage.fromJson(json);
 30
 31      expect(initialData.stepSize, recreatedData.stepSize);
 32      expect(initialData.currentRange.start.millisecondsSinceEpoch,
 33          recreatedData.currentRange.start.millisecondsSinceEpoch,);
 34      expect(initialData.currentRange.end.millisecondsSinceEpoch,
 35          recreatedData.currentRange.end.millisecondsSinceEpoch,);
 36    });
 37
 38    test('should load same data from json in edge cases', () {
 39      final initialData = IntervalStorage(stepSize: TimeStep.month, range: DateRange(
 40          start: DateTime.fromMillisecondsSinceEpoch(1234),
 41          end: DateTime.fromMillisecondsSinceEpoch(5678),
 42      ),);
 43      final json = initialData.toJson();
 44      final recreatedData = IntervalStorage.fromJson(json);
 45
 46      expect(initialData.stepSize, TimeStep.month);
 47      expect(recreatedData.currentRange.start.millisecondsSinceEpoch, 1234);
 48      expect(recreatedData.currentRange.end.millisecondsSinceEpoch, 5678);
 49    });
 50
 51    test('should not crash when parsing incorrect json', () {
 52      IntervalStorage.fromJson('banana');
 53      IntervalStorage.fromJson('{"stepSize" = 1}');
 54      IntervalStorage.fromJson('{"stepSize": 1');
 55      IntervalStorage.fromJson('{stepSize: 1}');
 56      IntervalStorage.fromJson('green{stepSize: 1}');
 57    });
 58
 59    test('should not crash when parsing invalid values and ignore them', () {
 60      final v1 = IntervalStorage.fromJson('{"stepSize": true}');
 61      final v2 = IntervalStorage.fromJson('{"stepSize": "month"}');
 62      final v3 = IntervalStorage.fromJson('{"start": "month", "end": 10.5}');
 63      final v4 = IntervalStorage.fromJson('{"start": 18.6, "end": 90.65}');
 64
 65      expect(v1.stepSize, TimeStep.last7Days);
 66      expect(v2.stepSize, TimeStep.last7Days);
 67      expect(v3.stepSize, TimeStep.last7Days);
 68
 69      // in minutes to avoid failing through performance
 70      expect(v2.currentRange.duration.inMinutes, v1.currentRange.duration.inMinutes);
 71      expect(v3.currentRange.duration.inMinutes, v1.currentRange.duration.inMinutes);
 72      expect(v4.currentRange.duration.inMinutes, v1.currentRange.duration.inMinutes);
 73    });
 74  });
 75
 76  group('Settings', (){
 77    test('should be able to recreate all values from json', () {
 78      final initial = Settings(
 79        language: const Locale('en'),
 80        accentColor: Colors.deepOrange,
 81        sysColor: Colors.deepOrange,
 82        diaColor: Colors.deepOrange,
 83        pulColor: Colors.deepOrange,
 84        needlePinBarWidth: 123.456789,
 85        dateFormatString: 'Lorem Ipsum',
 86        graphLineThickness: 134.23123,
 87        animationSpeed: 78,
 88        sysWarn: 78,
 89        diaWarn: 78,
 90        allowManualTimeInput: false,
 91        confirmDeletion: false,
 92        themeMode: ThemeMode.light,
 93        validateInputs: false,
 94        allowMissingValues: false,
 95        drawRegressionLines: false,
 96        startWithAddMeasurementPage: false,
 97        useLegacyList: false,
 98        horizontalGraphLines: [HorizontalGraphLine(Colors.blue, 1230)],
 99        bottomAppBars: true,
100        knownBleDev: ['a', 'b'],
101        bleInput: BluetoothInputMode.newBluetoothInputCrossPlatform,
102        weightInput: true,
103        weightUnit: WeightUnit.st,
104        preferredPressureUnit: PressureUnit.kPa,
105      );
106      final fromJson = Settings.fromJson(initial.toJson());
107
108      expect(initial.language, fromJson.language);
109      expect(initial.accentColor.toARGB32(), fromJson.accentColor.toARGB32());
110      expect(initial.sysColor.toARGB32(), fromJson.sysColor.toARGB32());
111      expect(initial.diaColor.toARGB32(), fromJson.diaColor.toARGB32());
112      expect(initial.pulColor.toARGB32(), fromJson.pulColor.toARGB32());
113      expect(initial.dateFormatString, fromJson.dateFormatString);
114      expect(initial.graphLineThickness, fromJson.graphLineThickness);
115      expect(initial.animationSpeed, fromJson.animationSpeed);
116      expect(initial.sysWarn, fromJson.sysWarn);
117      expect(initial.diaWarn, fromJson.diaWarn);
118      expect(initial.allowManualTimeInput, fromJson.allowManualTimeInput);
119      expect(initial.confirmDeletion, fromJson.confirmDeletion);
120      expect(initial.themeMode, fromJson.themeMode);
121      expect(initial.validateInputs, fromJson.validateInputs);
122      expect(initial.allowMissingValues, fromJson.allowMissingValues);
123      expect(initial.drawRegressionLines, fromJson.drawRegressionLines);
124      expect(initial.startWithAddMeasurementPage, fromJson.startWithAddMeasurementPage);
125      expect(initial.compactList, fromJson.compactList);
126      expect(initial.horizontalGraphLines.length, fromJson.horizontalGraphLines.length);
127      expect(initial.horizontalGraphLines.first.color.toARGB32(), fromJson.horizontalGraphLines.first.color.toARGB32());
128      expect(initial.horizontalGraphLines.first.height, fromJson.horizontalGraphLines.first.height);
129      expect(initial.needlePinBarWidth, fromJson.needlePinBarWidth);
130      expect(initial.bottomAppBars, fromJson.bottomAppBars);
131      expect(initial.knownBleDev, fromJson.knownBleDev);
132      expect(initial.bleInput, fromJson.bleInput);
133      expect(initial.weightInput, fromJson.weightInput);
134      expect(initial.preferredPressureUnit, fromJson.preferredPressureUnit);
135      expect(initial.weightUnit, fromJson.weightUnit);
136
137      expect(initial.toJson(), fromJson.toJson());
138    });
139
140    test('should not crash when parsing incorrect json', () {
141      Settings.fromJson('banana');
142      Settings.fromJson('{"stepSize" = 1}');
143      Settings.fromJson('{"stepSize": 1');
144      Settings.fromJson('{stepSize: 1}');
145      Settings.fromJson('green{stepSize: 1}');
146    });
147
148    test('should not crash when parsing invalid values and ignore them', () {
149      final v1 = Settings.fromJson('{"pulColor": true}');
150      final v2 = Settings.fromJson('{"validateInputs": "red"}');
151      final v3 = Settings.fromJson('{"validateInputs": "month", "useLegacyList": 10.5}');
152      Settings.fromJson('{"sysWarn": 18.6, "diaWarn": 90.65}');
153
154      expect(v1.pulColor.toARGB32(), Settings().pulColor.toARGB32());
155      expect(v2.validateInputs, Settings().validateInputs);
156      expect(v3.compactList, Settings().compactList);
157    });
158  });
159
160  group('ExportSettings', (){
161    test('should be able to recreate all values from json', () {
162      final initial = ExportSettings(
163        exportFormat: ExportFormat.db,
164        defaultExportDir: 'lorem ipsum',
165        exportAfterEveryEntry: true,
166      );
167      final fromJson = ExportSettings.fromJson(initial.toJson());
168
169      expect(initial.exportFormat, fromJson.exportFormat);
170      expect(initial.defaultExportDir, fromJson.defaultExportDir);
171      expect(initial.exportAfterEveryEntry, fromJson.exportAfterEveryEntry);
172
173      expect(initial.toJson(), fromJson.toJson());
174    });
175
176    test('should not crash when parsing incorrect json', () {
177      ExportSettings.fromJson('banana');
178      ExportSettings.fromJson('{"defaultExportDir" = 1}');
179      ExportSettings.fromJson('{"defaultExportDir": 1');
180      ExportSettings.fromJson('{defaultExportDir: 1}');
181      ExportSettings.fromJson('green{exportFormat: 1}');
182    });
183
184    test('should not crash when parsing invalid values and ignore them', () {
185      final v1 = ExportSettings.fromJson('{"defaultExportDir": ["test"]}');
186      final v2 = ExportSettings.fromJson('{"exportFormat": "red"}');
187      final v3 = ExportSettings.fromJson('{"exportFormat": "month", "exportAfterEveryEntry": 15}');
188
189      expect(v1.defaultExportDir, ExportSettings().defaultExportDir);
190      expect(v2.exportFormat, ExportSettings().exportFormat);
191      expect(v3.exportFormat, ExportSettings().exportFormat);
192      expect(v3.exportAfterEveryEntry, ExportSettings().exportAfterEveryEntry);
193    });
194  });
195  group('ActiveExportColumnConfiguration', () {
196    test('should be able to recreate all values from json', () {
197      final initial = ActiveExportColumnConfiguration(
198        activePreset: ExportImportPreset.myHeart,
199        userSelectedColumnIds: ['a', 'b', 'c'],
200      );
201      final fromJson = ActiveExportColumnConfiguration.fromJson(initial.toJson());
202
203      expect(initial.activePreset, fromJson.activePreset);
204      expect(initial.toJson(), fromJson.toJson());
205    });
206    test('should not crash when parsing incorrect json', () {
207      ActiveExportColumnConfiguration.fromJson('banana');
208      ActiveExportColumnConfiguration.fromJson('{"preset" = false}');
209      ActiveExportColumnConfiguration.fromJson('{"preset": false');
210      ActiveExportColumnConfiguration.fromJson('{"preset": null');
211      ActiveExportColumnConfiguration.fromJson('{"columns": null');
212      ActiveExportColumnConfiguration.fromJson('{"columns": [123]');
213      ActiveExportColumnConfiguration.fromJson('{preset: false}');
214      ActiveExportColumnConfiguration.fromJson('green{preset: false}');
215    });
216    test('should not crash when parsing invalid values and ignore them', () {
217      final v1 = ActiveExportColumnConfiguration.fromJson('{"preset": ["test"]}');
218      final v2 = ActiveExportColumnConfiguration.fromJson('{"columns": [123, 456]}');
219
220      expect(v1.activePreset, ActiveExportColumnConfiguration().activePreset);
221      expect(v2.toJson(), ActiveExportColumnConfiguration().toJson());
222    });
223  });
224
225  group('CsvExportSettings', (){
226    test('should be able to recreate all values from json', () {
227      final initial = CsvExportSettings(
228        fieldDelimiter: 'asdfghjklö',
229        textDelimiter: 'asdfghjklö2',
230        exportHeadline: false,
231        exportFieldsConfiguration: ActiveExportColumnConfiguration(
232          activePreset: ExportImportPreset.myHeart,
233          userSelectedColumnIds: ['a', 'b', 'c'],
234        ),
235      );
236      final fromJson = CsvExportSettings.fromJson(initial.toJson());
237
238      expect(initial.fieldDelimiter, fromJson.fieldDelimiter);
239      expect(initial.textDelimiter, fromJson.textDelimiter);
240      expect(initial.exportHeadline, fromJson.exportHeadline);
241      expect(initial.exportFieldsConfiguration.toJson(), fromJson.exportFieldsConfiguration.toJson());
242
243      expect(initial.toJson(), fromJson.toJson());
244    });
245
246    test('should not crash when parsing incorrect json', () {
247      CsvExportSettings.fromJson('banana');
248      CsvExportSettings.fromJson('{"fieldDelimiter" = 1}');
249      CsvExportSettings.fromJson('{"fieldDelimiter": 1');
250      CsvExportSettings.fromJson('{fieldDelimiter: 1}');
251      CsvExportSettings.fromJson('green{fieldDelimiter: 1}');
252    });
253
254    test('should not crash when parsing invalid values and ignore them', () {
255      final v1 = CsvExportSettings.fromJson('{"fieldDelimiter": ["test"]}');
256      final v2 = CsvExportSettings.fromJson('{"exportHeadline": "red"}');
257      final v3 = CsvExportSettings.fromJson('{"textDelimiter": "month", "textDelimiter": {"test": 10.5}}');
258
259      expect(v1.fieldDelimiter, CsvExportSettings().fieldDelimiter);
260      expect(v2.exportHeadline, CsvExportSettings().exportHeadline);
261      expect(v3.textDelimiter, CsvExportSettings().textDelimiter);
262      expect(v3.exportFieldsConfiguration.toJson(), CsvExportSettings().exportFieldsConfiguration.toJson());
263    });
264    test('should load settings from 1.5.7 and earlier', () {
265      final settings = CsvExportSettings.fromJson('{"fieldDelimiter":"A","textDelimiter":"B","exportHeadline":false,"exportCustomFields":true,"customFields":["timestampUnixMs","systolic","diastolic","pulse","notes","color"]}');
266      expect(settings.fieldDelimiter, 'A');
267      expect(settings.textDelimiter, 'B');
268      expect(settings.exportHeadline, false);
269      expect(settings.exportFieldsConfiguration.activePreset, ExportImportPreset.none);
270    });
271  });
272
273  group('PdfExportSettings', (){
274    test('should be able to recreate all values from json', () {
275      final initial = PdfExportSettings(
276        exportTitle: false,
277        exportStatistics: false,
278        exportData: false,
279        headerHeight: 67.89,
280        cellHeight: 67.89,
281        headerFontSize: 67.89,
282        cellFontSize: 67.89,
283        exportFieldsConfiguration: ActiveExportColumnConfiguration(
284          activePreset: ExportImportPreset.myHeart,
285          userSelectedColumnIds: ['a', 'b', 'c'],
286        ),
287      );
288      final fromJson = PdfExportSettings.fromJson(initial.toJson());
289
290      expect(initial.exportTitle, fromJson.exportTitle);
291      expect(initial.exportStatistics, fromJson.exportStatistics);
292      expect(initial.exportData, fromJson.exportData);
293      expect(initial.headerHeight, fromJson.headerHeight);
294      expect(initial.cellHeight, fromJson.cellHeight);
295      expect(initial.headerFontSize, fromJson.headerFontSize);
296      expect(initial.cellFontSize, fromJson.cellFontSize);
297      expect(initial.exportFieldsConfiguration.toJson(), fromJson.exportFieldsConfiguration.toJson());
298
299      expect(initial.toJson(), fromJson.toJson());
300    });
301
302    test('should not crash when parsing incorrect json', () {
303      PdfExportSettings.fromJson('banana');
304      PdfExportSettings.fromJson('{"cellFontSize" = 1}');
305      PdfExportSettings.fromJson('{"cellFontSize": 1');
306      PdfExportSettings.fromJson('{cellFontSize: 1}');
307      PdfExportSettings.fromJson('green{fieldDelimiter: 1}');
308    });
309
310    test('should not crash when parsing invalid values and ignore them', () {
311      final v1 = PdfExportSettings.fromJson('{"cellFontSize": ["test"]}');
312      final v2 = PdfExportSettings.fromJson('{"cellFontSize": "red"}');
313      final v3 = PdfExportSettings.fromJson('{"headerFontSize": "month", "exportData": 15}');
314
315      expect(v1.cellFontSize, PdfExportSettings().cellFontSize);
316      expect(v2.cellFontSize, PdfExportSettings().cellFontSize);
317      expect(v3.headerFontSize, PdfExportSettings().headerFontSize);
318      expect(v3.exportData, PdfExportSettings().exportData);
319    });
320    test('should load settings from 1.5.7 and earlier', () {
321      final settings = PdfExportSettings.fromJson('{"exportTitle":false,"exportStatistics":true,"exportData":false,"headerHeight":42.0,"cellHeight":42.0,"headerFontSize":42.0,"cellFontSize":42.0,"exportCustomFields":true,"customFields":["formattedTimestamp","systolic","diastolic","pulse","notes"]}');
322      expect(settings.exportTitle, false);
323      expect(settings.exportStatistics, true);
324      expect(settings.exportData, false);
325      expect(settings.headerHeight, 42);
326      expect(settings.cellHeight, 42);
327      expect(settings.headerFontSize, 42);
328      expect(settings.cellFontSize, 42);
329      expect(settings.exportFieldsConfiguration.activePreset, ExportImportPreset.none);
330    });
331  });
332
333  group('ExportColumnsManager', (){
334    test('should be able to recreate all values from json', () {
335      final initial = ExportColumnsManager();
336      final c1 = UserColumn('test', 'test', '\$SYS');
337      final c2 = TimeColumn('testB', 'mmm');
338      initial.addOrUpdate(c1);
339      initial.addOrUpdate(c2);
340      final fromJson = ExportColumnsManager.fromJson(initial.toJson());
341
342      expect(initial.toJson(), fromJson.toJson());
343    });
344
345    test('should not crash when parsing incorrect json', () {
346      ExportColumnsManager.fromJson('banana');
347      ExportColumnsManager.fromJson('{"userColumns" = 1}');
348      ExportColumnsManager.fromJson('{"userColumns": 1');
349      ExportColumnsManager.fromJson('{userColumns: 1}');
350      ExportColumnsManager.fromJson('green{userColumns: 1}');
351    });
352
353    test('should not crash when parsing invalid values and ignore them', () {
354      final v1 = ExportColumnsManager.fromJson('{"userColumns": [1]}');
355      final v2 = ExportColumnsManager.fromJson('{"cellFontSize": "red"}');
356
357      expect(v1.userColumns.length, ExportColumnsManager().userColumns.length);
358      expect(v2.userColumns.length, ExportColumnsManager().userColumns.length);
359    });
360  });
361}