From 037a4aef1648dad6e46b26a692c509fd75459770 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 6 Apr 2018 13:09:07 -0700 Subject: [PATCH] Un(less)conditionally schedule first callback frame when creating ticker (#16261) * always schedule first frame when creating ticker, regardless of phase or frame --- .../flutter/lib/src/scheduler/ticker.dart | 6 ++-- .../flutter/test/scheduler/ticker_test.dart | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/scheduler/ticker.dart b/packages/flutter/lib/src/scheduler/ticker.dart index b5ce28d47a..3832dce1dc 100644 --- a/packages/flutter/lib/src/scheduler/ticker.dart +++ b/packages/flutter/lib/src/scheduler/ticker.dart @@ -155,8 +155,9 @@ class Ticker { }()); assert(_startTime == null); _future = new TickerFuture._(); - if (shouldScheduleTick) + if (shouldScheduleTick) { scheduleTick(); + } if (SchedulerBinding.instance.schedulerPhase.index > SchedulerPhase.idle.index && SchedulerBinding.instance.schedulerPhase.index < SchedulerPhase.postFrameCallbacks.index) _startTime = SchedulerBinding.instance.currentFrameTimeStamp; @@ -216,7 +217,7 @@ class Ticker { /// * The ticker is not active ([start] has not been called). /// * The ticker is not ticking, e.g. because it is [muted] (see [isTicking]). @protected - bool get shouldScheduleTick => isTicking && !scheduled; + bool get shouldScheduleTick => !muted && isActive && !scheduled; void _tick(Duration timeStamp) { assert(isTicking); @@ -238,7 +239,6 @@ class Ticker { /// This should only be called if [shouldScheduleTick] is true. @protected void scheduleTick({ bool rescheduling: false }) { - assert(isTicking); assert(!scheduled); assert(shouldScheduleTick); _animationId = SchedulerBinding.instance.scheduleFrameCallback(_tick, rescheduling: rescheduling); diff --git a/packages/flutter/test/scheduler/ticker_test.dart b/packages/flutter/test/scheduler/ticker_test.dart index 019329ca1e..fbaf01f6e5 100644 --- a/packages/flutter/test/scheduler/ticker_test.dart +++ b/packages/flutter/test/scheduler/ticker_test.dart @@ -103,4 +103,35 @@ void main() { ticker.stop(); }); + + testWidgets('Ticker can be created before application unpauses', (WidgetTester tester) async { + final ByteData pausedMessage = const StringCodec().encodeMessage('AppLifecycleState.paused'); + await BinaryMessages.handlePlatformMessage('flutter/lifecycle', pausedMessage, (_) {}); + + int tickCount = 0; + void handleTick(Duration duration) { + tickCount += 1; + } + + final Ticker ticker = new Ticker(handleTick); + ticker.start(); + + expect(tickCount, equals(0)); + expect(ticker.isTicking, isFalse); + + await tester.pump(const Duration(milliseconds: 10)); + + expect(tickCount, equals(0)); + expect(ticker.isTicking, isFalse); + + final ByteData resumedMessage = const StringCodec().encodeMessage('AppLifecycleState.resumed'); + await BinaryMessages.handlePlatformMessage('flutter/lifecycle', resumedMessage, (_) {}); + + await tester.pump(const Duration(milliseconds: 10)); + + expect(tickCount, equals(1)); + expect(ticker.isTicking, isTrue); + + ticker.stop(); + }); }