binding_cannot_schedule_frame_test.dart 1.47 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter 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 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
8
import 'package:flutter_test/flutter_test.dart';
9 10 11 12

void main() {
  test('Can only schedule frames after widget binding attaches the root widget', () async {
    final WidgetsBinding binding = WidgetsFlutterBinding.ensureInitialized();
13 14
    expect(SchedulerBinding.instance.framesEnabled, isFalse);
    expect(SchedulerBinding.instance.hasScheduledFrame, isFalse);
15
    // Sends a message to notify that the engine is ready to accept frames.
16
    final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
17
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
18 19 20 21

    // Enables the semantics should not schedule any frames if the root widget
    // has not been attached.
    binding.setSemanticsEnabled(true);
22 23
    expect(SchedulerBinding.instance.framesEnabled, isFalse);
    expect(SchedulerBinding.instance.hasScheduledFrame, isFalse);
24 25 26 27

    // The widget binding should be ready to produce frames after it attaches
    // the root widget.
    binding.attachRootWidget(const Placeholder());
28 29
    expect(SchedulerBinding.instance.framesEnabled, isTrue);
    expect(SchedulerBinding.instance.hasScheduledFrame, isTrue);
30 31
  });
}