diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index 273320a85b..a5793a7cd5 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -763,38 +763,34 @@ mixin SchedulerBinding on BindingBase, ServicesBinding { _warmUpFrame = true; Timeline.startSync('Warm-up frame'); final bool hadScheduledFrame = _hasScheduledFrame; - final Completer _warmUpFrameCompleter = Completer(); + // 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(); + }); // Lock events so touch events etc don't insert themselves until the // scheduled frame has finished. lockEvents(() async { - await _warmUpFrameCompleter.future; + await endOfFrame; 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 fe14690879..75eb269fdd 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -19,26 +19,6 @@ 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(() { @@ -112,27 +92,27 @@ void main() { }); test('2 calls to scheduleWarmUpFrame just schedules it once', () { - final List microtaskQueue = runWithMicrotaskQueueSpy(() { - scheduler.scheduleWarmUpFrame(); - scheduler.scheduleWarmUpFrame(); - }); - - // scheduleWarmUpFrame scheduled 1 microtask - expect(microtaskQueue.length, 1); - }); - - test('Tasks are not executed before scheduleWarmUpFrame finishes', () async { + final List timerQueueTasks = []; bool taskExecuted = false; - final List microtaskQueue = runWithMicrotaskQueueSpy(() { - scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch); - Timer.run(() { taskExecuted = true; }); + 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; + }, + ), + ); - expect(microtaskQueue.isNotEmpty, true); - await scheduler.endOfFrame; - - expect(scheduler.schedulerPhase, SchedulerPhase.idle); + // scheduleWarmUpFrame scheduled 2 Timers, scheduleTask scheduled 0 because + // events are locked. + expect(timerQueueTasks.length, 2); expect(taskExecuted, false); }); }