From cd34593ca8eec0ad670c7613d48cf6a539a6cb2f Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 9 Jan 2017 21:35:26 -0800 Subject: [PATCH] Test print.dart (#7406) --- .../flutter/lib/src/foundation/print.dart | 6 +- .../flutter/test/foundation/print_test.dart | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 packages/flutter/test/foundation/print_test.dart diff --git a/packages/flutter/lib/src/foundation/print.dart b/packages/flutter/lib/src/foundation/print.dart index 623d88f23dc..d1b72550693 100644 --- a/packages/flutter/lib/src/foundation/print.dart +++ b/packages/flutter/lib/src/foundation/print.dart @@ -51,9 +51,9 @@ void debugPrintThrottled(String message, { int wrapWidth }) { } int _debugPrintedCharacters = 0; const int _kDebugPrintCapacity = 16 * 1024; -Duration _kDebugPrintPauseTime = const Duration(seconds: 1); -Queue _debugPrintBuffer = new Queue(); -Stopwatch _debugPrintStopwatch = new Stopwatch(); +const Duration _kDebugPrintPauseTime = const Duration(seconds: 1); +final Queue _debugPrintBuffer = new Queue(); +final Stopwatch _debugPrintStopwatch = new Stopwatch(); bool _debugPrintScheduled = false; void _debugPrintTask() { _debugPrintScheduled = false; diff --git a/packages/flutter/test/foundation/print_test.dart b/packages/flutter/test/foundation/print_test.dart new file mode 100644 index 00000000000..3e257b3301b --- /dev/null +++ b/packages/flutter/test/foundation/print_test.dart @@ -0,0 +1,75 @@ +// 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:async'; + +import 'package:flutter/foundation.dart'; +import 'package:quiver/testing/async.dart'; +import 'package:test/test.dart'; + +List captureOutput(VoidCallback fn) { + List log = []; + + runZoned(fn, zoneSpecification: new ZoneSpecification( + print: (Zone self, + ZoneDelegate parent, + Zone zone, + String line) { + log.add(line); + }, + )); + + return log; +} + +void main() { + test('debugPrint', () { + expect( + captureOutput(() { debugPrintSynchronously('Hello, world'); }), + equals(['Hello, world']) + ); + + expect( + captureOutput(() { debugPrintSynchronously('Hello, world', wrapWidth: 10); }), + equals(['Hello,\nworld']) + ); + + for (int i = 0; i < 14; ++i) { + expect( + captureOutput(() { debugPrintSynchronously('Hello, world', wrapWidth: i); }), + equals(['Hello,\nworld']) + ); + } + + expect( + captureOutput(() { debugPrintThrottled('Hello, world'); }), + equals(['Hello, world']) + ); + + expect( + captureOutput(() { debugPrintThrottled('Hello, world', wrapWidth: 10); }), + equals(['Hello,', 'world']) + ); + }); + + test('debugPrint throttling', () { + new FakeAsync().run((FakeAsync async) { + List log = captureOutput(() { + debugPrintThrottled('A' * (22 * 1024) + '\nB'); + }); + expect(log.length, 1); + async.elapse(const Duration(seconds: 2)); + expect(log.length, 2); + + log = captureOutput(() { + debugPrintThrottled('C' * (22 * 1024)); + debugPrintThrottled('D'); + }); + + expect(log.length, 1); + async.elapse(const Duration(seconds: 2)); + expect(log.length, 2); + }); + }); +}