binding_frame_scheduling_test.dart 1.87 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:ui';
6

7
import 'package:flutter/scheduler.dart';
8
import 'package:flutter/services.dart';
9
import 'package:flutter/widgets.dart';
10
import 'package:flutter_test/flutter_test.dart';
11 12 13 14 15 16

void main() {
  test('Instantiating WidgetsFlutterBinding does neither schedule a frame nor register frame callbacks', () async {
    // Regression test for https://github.com/flutter/flutter/issues/39494.

    // Preconditions.
17 18
    expect(PlatformDispatcher.instance.onBeginFrame, isNull);
    expect(PlatformDispatcher.instance.onDrawFrame, isNull);
19 20

    // Instantiation does nothing with regards to frame scheduling.
21 22
    expect(WidgetsFlutterBinding.ensureInitialized(), isA<WidgetsFlutterBinding>());
    expect(SchedulerBinding.instance.hasScheduledFrame, isFalse);
23 24
    expect(PlatformDispatcher.instance.onBeginFrame, isNull);
    expect(PlatformDispatcher.instance.onDrawFrame, isNull);
25

26
    // Framework starts with detached statue. Sends resumed signal to enable frame.
27
    final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.resumed')!;
28
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
29 30
    expect(PlatformDispatcher.instance.onBeginFrame, isNull);
    expect(PlatformDispatcher.instance.onDrawFrame, isNull);
31
    expect(SchedulerBinding.instance.hasScheduledFrame, isFalse);
32

33 34
    // Frame callbacks are registered lazily (and a frame scheduled) when the root widget is attached.
    WidgetsBinding.instance.attachRootWidget(const Placeholder());
35 36
    expect(PlatformDispatcher.instance.onBeginFrame, isNotNull);
    expect(PlatformDispatcher.instance.onDrawFrame, isNotNull);
37
    expect(SchedulerBinding.instance.hasScheduledFrame, isTrue);
38 39
  });
}