From 34da4a5ffd036304c4219510076ada0915f3fe68 Mon Sep 17 00:00:00 2001 From: liyuqian Date: Mon, 2 Jul 2018 13:30:02 -0700 Subject: [PATCH] Throw instead of return null if durations is empty (#18957) See https://github.com/flutter/flutter/pull/18881#discussion_r199285256 --- .../lib/src/driver/timeline_summary.dart | 6 +-- .../test/src/timeline_summary_test.dart | 37 ++++++++++++++----- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver/timeline_summary.dart b/packages/flutter_driver/lib/src/driver/timeline_summary.dart index 5a613e1105..8c97417104 100644 --- a/packages/flutter_driver/lib/src/driver/timeline_summary.dart +++ b/packages/flutter_driver/lib/src/driver/timeline_summary.dart @@ -162,7 +162,7 @@ class TimelineSummary { double _averageInMillis(Iterable durations) { if (durations.isEmpty) - return null; + throw new ArgumentError('durations is empty!'); final int total = durations.fold(0, (int t, Duration duration) => t + duration.inMilliseconds); return total / durations.length; @@ -170,7 +170,7 @@ class TimelineSummary { double _percentileInMillis(Iterable durations, double percentile) { if (durations.isEmpty) - return null; + throw new ArgumentError('durations is empty!'); assert(percentile >= 0.0 && percentile <= 100.0); final List doubles = durations.map((Duration duration) => duration.inMilliseconds.toDouble()).toList(); @@ -181,7 +181,7 @@ class TimelineSummary { double _maxInMillis(Iterable durations) { if (durations.isEmpty) - return null; + throw new ArgumentError('durations is empty!'); return durations .map((Duration duration) => duration.inMilliseconds.toDouble()) diff --git a/packages/flutter_driver/test/src/timeline_summary_test.dart b/packages/flutter_driver/test/src/timeline_summary_test.dart index 4a35dbf594..142a19892e 100644 --- a/packages/flutter_driver/test/src/timeline_summary_test.dart +++ b/packages/flutter_driver/test/src/timeline_summary_test.dart @@ -55,8 +55,11 @@ void main() { }); group('average_frame_build_time_millis', () { - test('returns null when there is no data', () { - expect(summarize(>[]).computeAverageFrameBuildTimeMillis(), isNull); + test('throws when there is no data', () { + expect( + () => summarize(>[]).computeAverageFrameBuildTimeMillis(), + throwsA(predicate((ArgumentError e) => e.message == 'durations is empty!')) + ); }); test('computes average frame build time in milliseconds', () { @@ -71,8 +74,11 @@ void main() { }); group('worst_frame_build_time_millis', () { - test('returns null when there is no data', () { - expect(summarize(>[]).computeWorstFrameBuildTimeMillis(), isNull); + test('throws when there is no data', () { + expect( + () => summarize(>[]).computeWorstFrameBuildTimeMillis(), + throwsA(predicate((ArgumentError e) => e.message == 'durations is empty!')) + ); }); test('computes worst frame build time in milliseconds', () { @@ -107,8 +113,11 @@ void main() { }); group('average_frame_rasterizer_time_millis', () { - test('returns null when there is no data', () { - expect(summarize(>[]).computeAverageFrameRasterizerTimeMillis(), isNull); + test('throws when there is no data', () { + expect( + () => summarize(>[]).computeAverageFrameRasterizerTimeMillis(), + throwsA(predicate((ArgumentError e) => e.message == 'durations is empty!')) + ); }); test('computes average frame rasterizer time in milliseconds', () { @@ -143,10 +152,14 @@ void main() { }); group('worst_frame_rasterizer_time_millis', () { - test('returns null when there is no data', () { - expect(summarize(>[]).computeWorstFrameRasterizerTimeMillis(), isNull); + test('throws when there is no data', () { + expect( + () => summarize(>[]).computeWorstFrameRasterizerTimeMillis(), + throwsA(predicate((ArgumentError e) => e.message == 'durations is empty!')) + ); }); + test('computes worst frame rasterizer time in milliseconds', () { expect( summarize(>[ @@ -186,10 +199,14 @@ void main() { }); group('percentile_frame_rasterizer_time_millis', () { - test('returns null when there is no data', () { - expect(summarize(>[]).computeWorstFrameRasterizerTimeMillis(), isNull); + test('throws when there is no data', () { + expect( + () => summarize(>[]).computePercentileFrameRasterizerTimeMillis(90.0), + throwsA(predicate((ArgumentError e) => e.message == 'durations is empty!')) + ); }); + const List> sequences = >[ [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [1, 2, 3, 4, 5],