benchmarks_test.dart 2.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

class TestBinding extends LiveTestWidgetsFlutterBinding {
  TestBinding();

  int framesBegun = 0;
  int framesDrawn = 0;

  bool handleBeginFrameMicrotaskRun;

  @override
  void handleBeginFrame(Duration rawTimeStamp) {
    handleBeginFrameMicrotaskRun = false;
    framesBegun += 1;
    Future<void>.microtask(() { handleBeginFrameMicrotaskRun = true; });
    super.handleBeginFrame(rawTimeStamp);
  }

  @override
  void handleDrawFrame() {
    if (!handleBeginFrameMicrotaskRun) {
      throw "Microtasks scheduled by 'handledBeginFrame' must be run before 'handleDrawFrame'.";
    }
    framesDrawn += 1;
    super.handleDrawFrame();
  }
}

Future<void> main() async {
  final TestBinding binding = TestBinding();

  test('test pumpBenchmark() only runs one frame', () async {
    await benchmarkWidgets((WidgetTester tester) async {
      const Key root = Key('root');
      binding.attachRootWidget(Container(key: root));
      await tester.pump();

      expect(binding.framesBegun, greaterThan(0));
      expect(binding.framesDrawn, greaterThan(0));

      final Element appState = tester.element(find.byKey(root));
      binding.framePolicy = LiveTestWidgetsFlutterBindingFramePolicy.benchmark;

      final int startFramesBegun = binding.framesBegun;
      final int startFramesDrawn = binding.framesDrawn;
      expect(startFramesBegun, equals(startFramesDrawn));

      appState.markNeedsBuild();

      await tester.pumpBenchmark(const Duration(milliseconds: 16));

      final int endFramesBegun = binding.framesBegun;
      final int endFramesDrawn = binding.framesDrawn;
      expect(endFramesBegun, equals(endFramesDrawn));

      expect(endFramesBegun, equals(startFramesBegun + 1));
      expect(endFramesDrawn, equals(startFramesDrawn + 1));
    });
  });
}