我正在测试一个自定义进度条小部件。
buffered
我用默认值初始化参数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;
}
}
但是,当我测试该buffered
属性时,测试失败。
这是我的测试:
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
});
}
double
默认值是好的,但持续buffered
时间默认值失败:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
Expected: Duration:<0:00:00.000000>
Actual: <null>
我是否需要做的不仅仅是泵小部件以允许应用默认值?