flutter/packages/flutter_driver/test/src/timeline_summary_test.dart
pq bcede8dffb Literals get type annotations.
As per the recent fix to the `always_specify_types` lint (https://github.com/dart-lang/linter/issues/199), literal maps and lists are now expected to be explicitly typed.

Running that lint on the repo identifies quite a few spots to update.  This focuses on `flutter_driver` and `flutter_sprites` (somewhat arbitrarily) but the changes are fairly representative.

Note there are a number of places where I made a quick judgement on how specific to make the types.  Feedback on those is welcome.  (Especially as we move forward with more.)
2016-05-04 09:18:31 -07:00

187 lines
5.5 KiB
Dart

// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert' show JSON;
import 'package:test/test.dart';
import 'package:flutter_driver/src/common.dart';
import 'package:flutter_driver/flutter_driver.dart';
void main() {
group('TimelineSummary', () {
TimelineSummary summarize(List<Map<String, dynamic>> testEvents) {
return new TimelineSummary.summarize(new Timeline.fromJson(<String, dynamic>{
'traceEvents': testEvents,
}));
}
Map<String, dynamic> begin(int timeStamp) => <String, dynamic>{
'name': 'Engine::BeginFrame', 'ph': 'B', 'ts': timeStamp
};
Map<String, dynamic> end(int timeStamp) => <String, dynamic>{
'name': 'Engine::BeginFrame', 'ph': 'E', 'ts': timeStamp
};
group('frame_count', () {
test('counts frames', () {
expect(
summarize(<Map<String, dynamic>>[
begin(1000), end(2000),
begin(3000), end(5000),
]).countFrames(),
2
);
});
});
group('average_frame_build_time_millis', () {
test('returns null when there is no data', () {
expect(summarize(<Map<String, dynamic>>[]).computeAverageFrameBuildTimeMillis(), isNull);
});
test('computes average frame build time in milliseconds', () {
expect(
summarize(<Map<String, dynamic>>[
begin(1000), end(2000),
begin(3000), end(5000),
]).computeAverageFrameBuildTimeMillis(),
1.5
);
});
test('skips leading "end" events', () {
expect(
summarize(<Map<String, dynamic>>[
end(1000),
begin(2000), end(4000),
]).computeAverageFrameBuildTimeMillis(),
2.0
);
});
test('skips trailing "begin" events', () {
expect(
summarize(<Map<String, dynamic>>[
begin(2000), end(4000),
begin(5000),
]).computeAverageFrameBuildTimeMillis(),
2.0
);
});
});
group('worst_frame_build_time_millis', () {
test('returns null when there is no data', () {
expect(summarize(<Map<String, dynamic>>[]).computeWorstFrameBuildTimeMillis(), isNull);
});
test('computes worst frame build time in milliseconds', () {
expect(
summarize(<Map<String, dynamic>>[
begin(1000), end(2000),
begin(3000), end(5000),
]).computeWorstFrameBuildTimeMillis(),
2.0
);
expect(
summarize(<Map<String, dynamic>>[
begin(3000), end(5000),
begin(1000), end(2000),
]).computeWorstFrameBuildTimeMillis(),
2.0
);
});
test('skips leading "end" events', () {
expect(
summarize(<Map<String, dynamic>>[
end(1000),
begin(2000), end(4000),
]).computeWorstFrameBuildTimeMillis(),
2.0
);
});
test('skips trailing "begin" events', () {
expect(
summarize(<Map<String, dynamic>>[
begin(2000), end(4000),
begin(5000),
]).computeWorstFrameBuildTimeMillis(),
2.0
);
});
});
group('computeMissedFrameBuildBudgetCount', () {
test('computes the number of missed build budgets', () {
TimelineSummary summary = summarize(<Map<String, dynamic>>[
begin(1000), end(10000),
begin(11000), end(12000),
begin(13000), end(23000),
]);
expect(summary.countFrames(), 3);
expect(summary.computeMissedFrameBuildBudgetCount(), 2);
});
});
group('summaryJson', () {
test('computes and returns summary as JSON', () {
expect(
summarize(<Map<String, dynamic>>[
begin(1000), end(10000),
begin(11000), end(12000),
begin(13000), end(24000),
]).summaryJson,
<String, dynamic>{
'average_frame_build_time_millis': 7.0,
'worst_frame_build_time_millis': 11.0,
'missed_frame_build_budget_count': 2,
'frame_count': 3,
'frame_build_times': <int>[9000, 1000, 11000],
}
);
});
});
group('writeTimelineToFile', () {
setUp(() {
useMemoryFileSystemForTesting();
});
tearDown(() {
restoreFileSystem();
});
test('writes timeline to JSON file', () async {
await summarize(<Map<String, String>>[<String, String>{'foo': 'bar'}])
.writeTimelineToFile('test', destinationDirectory: '/temp');
String written =
await fs.file('/temp/test.timeline.json').readAsString();
expect(written, '{"traceEvents":[{"foo":"bar"}]}');
});
test('writes summary to JSON file', () async {
await summarize(<Map<String, dynamic>>[
begin(1000), end(10000),
begin(11000), end(12000),
begin(13000), end(24000),
]).writeSummaryToFile('test', destinationDirectory: '/temp');
String written =
await fs.file('/temp/test.timeline_summary.json').readAsString();
expect(JSON.decode(written), <String, dynamic>{
'average_frame_build_time_millis': 7.0,
'worst_frame_build_time_millis': 11.0,
'missed_frame_build_budget_count': 2,
'frame_count': 3,
'frame_build_times': <int>[9000, 1000, 11000],
});
});
});
});
}