diff --git a/packages/flutter/lib/src/scheduler/binding.dart b/packages/flutter/lib/src/scheduler/binding.dart index a315bfcade..035c0c65a6 100644 --- a/packages/flutter/lib/src/scheduler/binding.dart +++ b/packages/flutter/lib/src/scheduler/binding.dart @@ -669,9 +669,14 @@ abstract class SchedulerBinding extends BindingBase with ServicesBinding { /// If a frame has already been scheduled with [scheduleFrame] or /// [scheduleForcedFrame], this call may delay that frame. /// + /// If any scheduled frame has already begun or if another + /// [scheduleWarmUpFrame] was already called, this call will be ignored. + /// /// Prefer [scheduleFrame] to update the display in normal operation. void scheduleWarmUpFrame() { - assert(!_warmUpFrame); + if (_warmUpFrame || schedulerPhase != SchedulerPhase.idle) + return; + final bool hadScheduledFrame = _hasScheduledFrame; _warmUpFrame = true; // We use timers here to ensure that microtasks flush in between. diff --git a/packages/flutter/test/scheduler/scheduler_test.dart b/packages/flutter/test/scheduler/scheduler_test.dart index 85401ee69e..2f7345628c 100644 --- a/packages/flutter/test/scheduler/scheduler_test.dart +++ b/packages/flutter/test/scheduler/scheduler_test.dart @@ -1,6 +1,7 @@ // Copyright 2015 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:flutter/scheduler.dart'; @@ -18,8 +19,12 @@ class TestStrategy { } void main() { + SchedulerBinding scheduler; + setUpAll(() { + scheduler = new TestSchedulerBinding(); + }); + test('Tasks are executed in the right order', () { - final SchedulerBinding scheduler = new TestSchedulerBinding(); final TestStrategy strategy = new TestStrategy(); scheduler.schedulingStrategy = strategy.shouldRunTaskWithPriority; final List input = [2, 23, 23, 11, 0, 80, 3]; @@ -84,4 +89,25 @@ void main() { expect(executedTasks, hasLength(1)); expect(executedTasks[0], equals(0)); }); + + test('2 calls to scheduleWarmUpFrame just schedules it once', () { + final List timerQueueTasks = []; + runZoned( + () { + // Run it twice without processing the queued tasks. + scheduler.scheduleWarmUpFrame(); + scheduler.scheduleWarmUpFrame(); + }, + zoneSpecification: new 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; + }, + ), + ); + + // A single call to scheduleWarmUpFrame queues up 2 Timer tasks. + expect(timerQueueTasks.length, 2); + }); }