From ab707aca8f82688c6a3cb46801ce9b39d138ac60 Mon Sep 17 00:00:00 2001 From: "K. P. Sroka" Date: Wed, 5 Jun 2019 22:30:59 +0200 Subject: [PATCH] Prevent exception being thrown on hasScrolledBody (#31485) Convert scheduling to use `scheduleMicrotask` instead of `Timer.run` --- .../flutter/lib/src/scheduler/binding.dart | 48 ++++++++------- .../test/scheduler/scheduler_test.dart | 60 ++++++++++++------- 2 files changed, 66 insertions(+), 42 deletions(-) diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index a5793a7cd5..273320a85b 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -763,34 +763,38 @@ mixin SchedulerBinding on BindingBase, ServicesBinding { _warmUpFrame = true; Timeline.startSync('Warm-up frame'); final bool hadScheduledFrame = _hasScheduledFrame; - // We use timers here to ensure that microtasks flush in between. - Timer.run(() { - assert(_warmUpFrame); - handleBeginFrame(null); - }); - Timer.run(() { - assert(_warmUpFrame); - handleDrawFrame(); - // We call resetEpoch after this frame so that, in the hot reload case, - // the very next frame pretends to have occurred immediately after this - // warm-up frame. The warm-up frame's timestamp will typically be far in - // the past (the time of the last real frame), so if we didn't reset the - // epoch we would see a sudden jump from the old time in the warm-up frame - // to the new time in the "real" frame. The biggest problem with this is - // that implicit animations end up being triggered at the old time and - // then skipping every frame and finishing in the new time. - resetEpoch(); - _warmUpFrame = false; - if (hadScheduledFrame) - scheduleFrame(); - }); + final Completer _warmUpFrameCompleter = Completer(); // Lock events so touch events etc don't insert themselves until the // scheduled frame has finished. lockEvents(() async { - await endOfFrame; + await _warmUpFrameCompleter.future; Timeline.finishSync(); }); + + // We use scheduleMicrotask here to ensure that microtasks flush in between. + scheduleMicrotask(() { + assert(_warmUpFrame); + handleBeginFrame(null); + + scheduleMicrotask(() { + assert(_warmUpFrame); + handleDrawFrame(); + // We call resetEpoch after this frame so that, in the hot reload case, + // the very next frame pretends to have occurred immediately after this + // warm-up frame. The warm-up frame's timestamp will typically be far in + // the past (the time of the last real frame), so if we didn't reset the + // epoch we would see a sudden jump from the old time in the warm-up frame + // to the new time in the "real" frame. The biggest problem with this is + // that implicit animations end up being triggered at the old time and + // then skipping every frame and finishing in the new time. + resetEpoch(); + _warmUpFrame = false; + _warmUpFrameCompleter.complete(); + if (hadScheduledFrame) + scheduleFrame(); + }); + }); } Duration _firstRawTimeStampInEpoch; diff --git a/packages/flutter/test/scheduler/scheduler_test.dart b/packages/flutter/test/scheduler/scheduler_test.dart index 75eb269fdd..fe14690879 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -19,6 +19,26 @@ class TestStrategy { } } +List runWithMicrotaskQueueSpy(VoidCallback callback) { + final List microtaskQueue = []; + runZoned( + () { + callback(); + }, + zoneSpecification: ZoneSpecification( + scheduleMicrotask: (Zone self, ZoneDelegate parent, Zone zone, void f()) { + // Don't actually run the tasks, just record that it was scheduled. + microtaskQueue.add(f); + self.parent.scheduleMicrotask(() { + f(); + microtaskQueue.remove(f); + }); + }, + ), + ); + return microtaskQueue; +} + void main() { SchedulerBinding scheduler; setUpAll(() { @@ -92,27 +112,27 @@ void main() { }); test('2 calls to scheduleWarmUpFrame just schedules it once', () { - final List timerQueueTasks = []; - bool taskExecuted = false; - runZoned( - () { - // Run it twice without processing the queued tasks. - scheduler.scheduleWarmUpFrame(); - scheduler.scheduleWarmUpFrame(); - scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch); - }, - zoneSpecification: ZoneSpecification( - createTimer: (Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()) { - // Don't actually run the tasks, just record that it was scheduled. - timerQueueTasks.add(f); - return null; - }, - ), - ); + final List microtaskQueue = runWithMicrotaskQueueSpy(() { + scheduler.scheduleWarmUpFrame(); + scheduler.scheduleWarmUpFrame(); + }); - // scheduleWarmUpFrame scheduled 2 Timers, scheduleTask scheduled 0 because - // events are locked. - expect(timerQueueTasks.length, 2); + // scheduleWarmUpFrame scheduled 1 microtask + expect(microtaskQueue.length, 1); + }); + + test('Tasks are not executed before scheduleWarmUpFrame finishes', () async { + bool taskExecuted = false; + final List microtaskQueue = runWithMicrotaskQueueSpy(() { + scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch); + Timer.run(() { taskExecuted = true; }); + scheduler.scheduleWarmUpFrame(); + }); + + expect(microtaskQueue.isNotEmpty, true); + await scheduler.endOfFrame; + + expect(scheduler.schedulerPhase, SchedulerPhase.idle); expect(taskExecuted, false); }); }