binding_frame_scheduling_test.dart 2.05 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
    final WidgetsFlutterBindingWithTestBinaryMessenger binding = WidgetsFlutterBindingWithTestBinaryMessenger();
    expect(binding, isA<WidgetsFlutterBinding>());
23
    expect(SchedulerBinding.instance.hasScheduledFrame, isFalse);
24 25
    expect(PlatformDispatcher.instance.onBeginFrame, isNull);
    expect(PlatformDispatcher.instance.onDrawFrame, isNull);
26

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

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

class WidgetsFlutterBindingWithTestBinaryMessenger extends WidgetsFlutterBinding with TestDefaultBinaryMessengerBinding { }