run_app_async_test.dart 1.64 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 'package:fake_async/fake_async.dart';
6
import 'package:flutter/foundation.dart';
7 8 9
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// This test is very fragile and bypasses some zone-related checks.
// It is written this way to verify some invariants that would otherwise
// be difficult to check.
// Do not use this test as a guide for writing good Flutter code.

class TestBinding extends WidgetsFlutterBinding {
  @override
  void initInstances() {
    super.initInstances();
    _instance = this;
  }

  @override
  bool debugCheckZone(String entryPoint) { return true; }

  static TestBinding get instance => BindingBase.checkInstance(_instance);
  static TestBinding? _instance;

  static TestBinding ensureInitialized() {
    if (TestBinding._instance == null) {
      TestBinding();
    }
    return TestBinding.instance;
  }
}

36 37
void main() {
  setUp(() {
38
    TestBinding.ensureInitialized();
39
    WidgetsBinding.instance.resetEpoch();
40 41 42 43 44 45 46 47 48 49 50 51 52
  });

  test('WidgetBinding build rendering tree and warm up frame back to back', () {
    final FakeAsync fakeAsync = FakeAsync();
    fakeAsync.run((FakeAsync async) {
      runApp(
        const MaterialApp(
          home: Material(
            child: Text('test'),
          ),
        ),
      );
      // Rendering tree is not built synchronously.
53
      expect(WidgetsBinding.instance.rootElement, isNull);
54
      fakeAsync.flushTimers();
55
      expect(WidgetsBinding.instance.rootElement, isNotNull);
56 57 58
    });
  });
}