main
  1
  2import 'package:blood_pressure_app/features/statistics/value_distribution.dart';
  3import 'package:flutter/material.dart';
  4import 'package:flutter/rendering.dart';
  5import 'package:blood_pressure_app/l10n/app_localizations.dart';
  6import 'package:flutter_test/flutter_test.dart';
  7
  8import '../../util.dart';
  9
 10void main() {
 11  testWidgets('should show centered info when values are empty', (tester) async {
 12    await tester.pumpWidget(materialApp(const ValueDistribution(
 13      color: Colors.red,
 14      values: [],
 15    ),),);
 16    expect(find.byType(ValueDistribution), findsOneWidget);
 17    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
 18    expect(find.byType(Text), findsOneWidget);
 19    expect(find.text(localizations.errNoData), findsOneWidget);
 20
 21    final errorCenter = tester.getCenter(find.byType(Text));
 22    final canvasCenter = tester.getCenter(find.byType(MaterialApp));
 23    expect(errorCenter, equals(canvasCenter));
 24  },);
 25
 26  testWidgets('should draw labels at correct positions', (tester) async {
 27    await tester.pumpWidget(materialApp(const SizedBox(
 28      height: 50,
 29      width: 180,
 30      child: ValueDistribution(
 31        color: Colors.red,
 32        values: [5,6,3,8,8,10], // min 3, max 10, avg 6 + 2/3
 33      ),
 34    ),),);
 35
 36    expect(find.byType(ValueDistribution), findsOneWidget);
 37    expect(find.byType(ValueDistribution),
 38        paintsExactlyCountTimes(#drawParagraph, 3),);
 39    const posBelowCenter = 9.0;
 40    expect(find.byType(ValueDistribution), paints
 41      ..paragraph(offset: const Offset(0.0, posBelowCenter))
 42      ..paragraph(offset: const Offset(66.0, posBelowCenter)) // overflows at the end
 43      ..paragraph(offset: const Offset(68.0, posBelowCenter)),
 44    );
 45  },);
 46  testWidgets('should correct amount of value bars', (tester) async {
 47    await tester.pumpWidget(materialApp(const SizedBox(
 48      height: 50,
 49      width: 180,
 50      child: ValueDistribution(
 51        color: Colors.red,
 52        values: [1,2,3,3,5],
 53      ),
 54    ),),);
 55
 56    expect(find.byType(ValueDistribution), findsOneWidget);
 57    expect(find.byType(ValueDistribution), paints
 58      ..line(color: Colors.red.shade500)
 59      ..line(color: Colors.red.shade500)
 60      ..line(color: Colors.red.shade500)
 61      ..line(color: Colors.red.shade500)
 62      ..line(color: Colors.red.shade500)
 63      ..line(color: Colors.white70) // start drawing decoration
 64    ,);
 65  },);
 66  testWidgets('should have semantics labels with correct values', (tester) async {
 67    await tester.pumpWidget(materialApp(const SizedBox(
 68      height: 50,
 69      width: 180,
 70      child: ValueDistribution(
 71        color: Colors.red,
 72        values: [5,6,3,8,8,10], // min 3, max 10, avg 6 + 2/3
 73      ),
 74    ),),);
 75
 76    final localizations = await AppLocalizations.delegate.load(const Locale('en'));
 77    final labels = _getAllLabels(tester.getSemantics(find.byType(ValueDistribution)));
 78
 79    expect(labels, contains(localizations.minOf('3')));
 80    expect(labels, contains(localizations.maxOf('10')));
 81    expect(labels, contains(localizations.avgOf('7')));
 82  },);
 83  testWidgets('draws bars in correct order', (tester) async {
 84    await tester.pumpWidget(materialApp(const SizedBox(
 85      height: 50,
 86      width: 180,
 87      child: ValueDistribution(
 88        color: Colors.red,
 89        values: [1,2,3,3,5],
 90        // 1: 1, 2: 1, 3:2, 4:0, 5:1
 91      ),
 92    ),),);
 93
 94    expect(find.byType(ValueDistribution), paints
 95      ..line(p1: Offset(16.4, 24.75), p2: Offset(16.4, 25.25))
 96      ..line(p1: Offset(53.199999999999996, 24.75), p2: Offset(53.199999999999996, 25.25))
 97      ..line(p1: Offset(90.0, 24.5), p2: Offset(90.0, 25.5))
 98      ..line(p1: Offset(126.8, 25.0), p2: Offset(126.8, 25.0))
 99      ..line(p1: Offset(163.6, 24.75), p2: Offset(163.6, 25.25)),
100    );
101  },);
102}
103
104/// Recursively fetches the labels of the semantics node and all its children.
105List<String> _getAllLabels(SemanticsNode node) {
106  final labels = [node.label];
107  node.visitChildren((node) {
108    labels.addAll(_getAllLabels(node));
109    return true;
110  });
111  return labels;
112}