flutter_flutter/testing/dart/window_test.dart
liyuqian e97ed36e58
Add a BroadcastStream to FrameTiming (#11041)
Without this, developers have to override `onReportTimings` to listen for `FrameTiming`.
That can potentially break previous `onReportTimings` listeners if they forget to call
the old listener in their new callback.

This PR replaces the similar RP in the framework: https://github.com/flutter/flutter/pull/38574

Once this PR landed, we'll have to create another framework PR to use the stream to replace
`onReportTimings` usages.

Once that's done, we can then propose the breaking change of removing the deprecated
`onReportTimings`.
2019-08-19 10:52:16 -07:00

66 lines
2.4 KiB
Dart

// Copyright 2013 The Flutter 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:async';
import 'dart:typed_data';
import 'dart:ui';
import 'package:test/test.dart';
void main() {
test('window.sendPlatformMessage preserves callback zone', () {
runZoned(() {
final Zone innerZone = Zone.current;
window.sendPlatformMessage('test', ByteData.view(Uint8List(0).buffer), expectAsync1((ByteData data) {
final Zone runZone = Zone.current;
expect(runZone, isNotNull);
expect(runZone, same(innerZone));
}));
});
});
test('FrameTiming.toString has the correct format', () {
FrameTiming timing = FrameTiming(<int>[1000, 8000, 9000, 19500]);
expect(timing.toString(), 'FrameTiming(buildDuration: 7.0ms, rasterDuration: 10.5ms, totalSpan: 18.5ms)');
});
test('window.frameTimings works', () async {
// Test a single subscription. Check that debugNeedsReportTimings is
// properly reset after the subscription is cancelled.
expect(window.debugNeedsReportTimings, false);
final FrameTiming mockTiming = FrameTiming(<int>[1000, 8000, 9000, 19500]);
final Future<FrameTiming> frameTiming = window.frameTimings.first;
expect(window.debugNeedsReportTimings, true);
window.debugReportTimings(<FrameTiming>[mockTiming]);
expect(await frameTiming, equals(mockTiming));
expect(window.debugNeedsReportTimings, false);
// Test multiple (two) subscriptions after that debugNeedsReportTimings has
// been reset to false by the single subscription test above.
//
// Subscription 1
final Future<FrameTiming> timingFuture = window.frameTimings.first;
//
// Subscription 2
final List<FrameTiming> timings = <FrameTiming>[];
final Completer<void> completer = Completer<void>();
int frameCount = 0;
window.frameTimings.listen((FrameTiming t) {
timings.add(t);
frameCount += 1;
if (frameCount == 2) {
completer.complete();
}
});
final FrameTiming secondMock = FrameTiming(<int>[1, 2, 3, 4]);
window.debugReportTimings(<FrameTiming>[secondMock, secondMock]);
final FrameTiming timing = await timingFuture;
expect(timing != mockTiming, isTrue);
expect(timing, equals(secondMock));
await completer.future;
expect(timings, hasLength(2));
});
}