mardi 29 décembre 2020

How to match default properties with Flutter widget testing

I'm testing a custom progress bar widget.

I initialize the buffered parameter with a default value of Duration.zero:

class ProgressBar extends LeafRenderObjectWidget {
  const ProgressBar({
    Key key,
    @required this.progress,
    @required this.total,
    this.buffered,
    this.onSeek,
    this.barHeight = 5.0,
    this.baseBarColor,
    this.progressBarColor,
    this.bufferedBarColor,
    this.thumbRadius = 10.0,
    this.thumbColor,
    this.timeLabelLocation,
  }) : super(key: key);

  final Duration progress;
  final Duration total;
  final Duration buffered;
  final ValueChanged<Duration> onSeek;
  final Color baseBarColor;
  final Color progressBarColor;
  final Color bufferedBarColor;
  final double barHeight;
  final double thumbRadius;
  final Color thumbColor;
  final TimeLabelLocation timeLabelLocation;

  @override
  _RenderProgressBar createRenderObject(BuildContext context) {
    final theme = Theme.of(context);
    final primaryColor = theme.colorScheme.primary;
    final textStyle = theme.textTheme.bodyText1;
    return _RenderProgressBar(
      progress: progress,
      total: total,
      buffered: buffered ?? Duration.zero, //        <-- here
      onSeek: onSeek,
      barHeight: barHeight,
      baseBarColor: baseBarColor ?? primaryColor.withOpacity(0.24),
      progressBarColor: progressBarColor ?? primaryColor,
      bufferedBarColor: bufferedBarColor ?? primaryColor.withOpacity(0.24),
      thumbRadius: thumbRadius,
      thumbColor: thumbColor ?? primaryColor,
      timeLabelLocation: timeLabelLocation ?? TimeLabelLocation.below,
      timeLabelTextStyle: textStyle,
    );
  }

  @override
  void updateRenderObject(
      BuildContext context, _RenderProgressBar renderObject) {
    final theme = Theme.of(context);
    final primaryColor = theme.colorScheme.primary;
    final textStyle = theme.textTheme.bodyText1;
    renderObject
      ..progress = progress
      ..total = total
      ..buffered = buffered ?? Duration.zero //        <-- and here
      ..onSeek = onSeek
      ..barHeight = barHeight
      ..baseBarColor = baseBarColor ?? primaryColor.withOpacity(0.24)
      ..progressBarColor = progressBarColor ?? primaryColor
      ..bufferedBarColor = bufferedBarColor ?? primaryColor.withOpacity(0.24)
      ..thumbRadius = thumbRadius
      ..thumbColor = thumbColor ?? primaryColor
      ..timeLabelLocation = timeLabelLocation ?? TimeLabelLocation.below
      ..timeLabelTextStyle = textStyle;
  }
}

However, when I test the buffered property the test fails.

Here is my test:

import 'dart:ui';

import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {

  testWidgets('Default properties are OK', (WidgetTester tester) async {
    await tester.pumpWidget(
      ProgressBar(
        progress: Duration.zero,
        total: Duration(minutes: 5),
      ),
    );

    ProgressBar progressBar = tester.firstWidget(find.byType(ProgressBar));
    expect(progressBar, isNotNull);

    expect(progressBar.progress, Duration.zero);      // OK
    expect(progressBar.total, Duration(minutes: 5));  // OK
    expect(progressBar.barHeight, 5.0);               // OK
    expect(progressBar.onSeek, isNull);               // OK
    expect(progressBar.thumbRadius, 10.0);            // OK

    expect(progressBar.buffered, Duration.zero);      // Fails
  });
}

The double defaults were OK but the buffered duration default failed:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: Duration:<0:00:00.000000>
  Actual: <null>

Do I have to do more than just pump the widget to allow the defaults to be applied?

Aucun commentaire:

Enregistrer un commentaire