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 16 17 18 19 20

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

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

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

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

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