main
  1import 'package:blood_pressure_app/features/data_picker/interval_picker.dart';
  2import 'package:blood_pressure_app/model/storage/interval_store.dart';
  3import 'package:flutter/material.dart';
  4import 'package:blood_pressure_app/l10n/app_localizations.dart';
  5import 'package:flutter_test/flutter_test.dart';
  6import 'package:health_data_store/health_data_store.dart';
  7import 'package:provider/provider.dart';
  8
  9import '../util.dart';
 10
 11void main() {
 12  testWidgets('shows controls and dropdown', (tester) async {
 13    await tester.pumpWidget(materialApp(ChangeNotifierProvider(
 14      create: (_) => IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage()),
 15      child: const IntervalPicker(type: IntervalStoreManagerLocation.mainPage)
 16    )));
 17    expect(find.byType(DropdownButton<TimeStep>), findsOneWidget);
 18    expect(find.byType(MaterialButton), findsNWidgets(2));
 19    expect(find.byIcon(Icons.chevron_left), findsOneWidget);
 20    expect(find.byIcon(Icons.chevron_right), findsOneWidget);
 21    expect(
 22      tester.getCenter(find.byIcon(Icons.chevron_left)).dx,
 23      lessThan(tester.getCenter(find.byIcon(Icons.chevron_right)).dx),
 24    );
 25  });
 26  testWidgets('shows custom intervall start and end', (tester) async {
 27    final s = IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage());
 28    s.mainPage.changeStepSize(TimeStep.custom);
 29    s.mainPage.currentRange = DateRange(start: DateTime(2000), end: DateTime(2001));
 30
 31    await tester.pumpWidget(materialApp(ChangeNotifierProvider.value(
 32      value: s,
 33      child: const IntervalPicker(type: IntervalStoreManagerLocation.mainPage)
 34    )));
 35    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
 36
 37    expect(find.text('Jan 1, 2000 - Jan 1, 2001'), findsOneWidget);
 38    expect(find.text(localizations.custom), findsOneWidget);
 39  });
 40  testWidgets('allows switching interval', (tester) async {
 41    final s = IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage());
 42    s.mainPage.changeStepSize(TimeStep.last7Days);
 43
 44    await tester.pumpWidget(materialApp(ChangeNotifierProvider.value(
 45      value: s,
 46      child: const IntervalPicker(type: IntervalStoreManagerLocation.mainPage),
 47    )));
 48    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
 49
 50    expect(s.mainPage.stepSize, TimeStep.last7Days);
 51    await tester.tap(find.text(localizations.last7Days));
 52    await tester.pumpAndSettle();
 53    await tester.tap(find.text(localizations.month));
 54    await tester.pumpAndSettle();
 55
 56    expect(s.mainPage.stepSize, TimeStep.month);
 57    await tester.tap(find.text(localizations.month));
 58    await tester.pumpAndSettle();
 59    expect(find.byType(DateRangePickerDialog), findsNothing);
 60    await tester.tap(find.text(localizations.custom));
 61    await tester.pumpAndSettle();
 62    expect(find.byType(DateRangePickerDialog), findsOneWidget);
 63  });
 64  testWidgets('steps date stepper by one', (tester) async {
 65    final s = IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage());
 66    s.mainPage.changeStepSize(TimeStep.year);
 67    final year = s.mainPage.currentRange.start
 68      .add(s.mainPage.currentRange.duration ~/ 2)
 69      .year;
 70
 71    await tester.pumpWidget(materialApp(ChangeNotifierProvider.value(
 72      value: s,
 73      child: const IntervalPicker(type: IntervalStoreManagerLocation.mainPage),
 74    )));
 75
 76    expect(find.text('$year'), findsOneWidget);
 77
 78    await tester.tap(find.byIcon(Icons.chevron_left));
 79    await tester.pumpAndSettle();
 80    expect(find.text('${year - 1}'), findsOneWidget);
 81
 82    await tester.tap(find.byIcon(Icons.chevron_right));
 83    await tester.tap(find.byIcon(Icons.chevron_right));
 84    await tester.pumpAndSettle();
 85    expect(find.text('${year + 1}'), findsOneWidget);
 86  });
 87  testWidgets('selected custom interval gets interpreted correctly', (tester) async {
 88    final s = IntervalStoreManager(IntervalStorage(),IntervalStorage(),IntervalStorage());
 89
 90    await tester.pumpWidget(materialApp(ChangeNotifierProvider.value(
 91      value: s,
 92      child: IntervalPicker(
 93        type: IntervalStoreManagerLocation.mainPage,
 94        customRangePickerCurrentDay: DateTime(2024, 1, 25),
 95      ),
 96    )));
 97    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
 98    final materialLocalizations = await DefaultMaterialLocalizations.delegate.load(const Locale('en'));
 99
100    await tester.tap(find.byType(DropdownButton<TimeStep>));
101    await tester.pumpAndSettle();
102    
103    await tester.tap(find.text(localizations.custom));
104    await tester.pumpAndSettle(); // opens date interval selection
105
106    await tester.tap(find.text('20').first);
107    await tester.pump();
108    await tester.tap(find.text('25').first);
109    await tester.pump();
110    await tester.tap(find.text(materialLocalizations.saveButtonLabel).first);
111    await tester.pumpAndSettle();
112
113    expect(find.byType(DateRangePickerDialog), findsNothing);
114
115    expect(s.mainPage.stepSize, TimeStep.custom);
116    expect(s.mainPage.currentRange.start.year, 2024);
117    expect(s.mainPage.currentRange.start.month, 1);
118    expect(s.mainPage.currentRange.start.day, 20);
119    expect(s.mainPage.currentRange.end.year, 2024);
120    expect(s.mainPage.currentRange.end.month, 1);
121    expect(s.mainPage.currentRange.end.day, 25);
122
123    expect(s.mainPage.currentRange.end.hour, 23, reason: 'should always be after newer measurements (#466)');
124    expect(s.mainPage.currentRange.end.minute, 59, reason: 'should always be after newer measurements (#466)');
125    expect(s.mainPage.currentRange.end.second, 59, reason: 'should always be after newer measurements (#466)');
126
127    expect(s.mainPage.currentRange.start.hour, 0);
128    expect(s.mainPage.currentRange.start.minute, 0);
129    expect(s.mainPage.currentRange.start.second, 0);
130  });
131}