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

import 'dart:ui' show window;

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

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.
16
    expect(WidgetsBinding.instance, isNull);
17 18 19 20
    expect(window.onBeginFrame, isNull);
    expect(window.onDrawFrame, isNull);

    // Instantiation does nothing with regards to frame scheduling.
21 22
    final WidgetsFlutterBinding binding = WidgetsFlutterBinding.ensureInitialized() as WidgetsFlutterBinding;
    expect(binding.hasScheduledFrame, isFalse);
23 24 25
    expect(window.onBeginFrame, isNull);
    expect(window.onDrawFrame, isNull);

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

    // A frame can only be scheduled when there is a root widget.
    binding.attachRootWidget(const Placeholder());
32

33 34
    // Frame callbacks are registered lazily when a frame is scheduled.
    binding.scheduleFrame();
35 36 37 38
    expect(window.onBeginFrame, isNotNull);
    expect(window.onDrawFrame, isNotNull);
  });
}