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

import 'dart:async';
6

7
import 'package:flutter_driver/flutter_driver.dart';
8
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
9

10 11
final RegExp calibrationRegExp = RegExp('Flutter frame rate is (.*)fps');
final RegExp statsRegExp = RegExp('Produced: (.*)fps\nConsumed: (.*)fps\nWidget builds: (.*)');
12
const Duration samplingTime = Duration(seconds: 8);
13

14
Future<void> main() async {
15
  group('texture suite', () {
16
    late FlutterDriver driver;
17 18 19 20 21

    setUpAll(() async {
      driver = await FlutterDriver.connect();
    });

22 23 24 25 26
    // This test verifies that we can consume texture frames at a rate
    // close to the minimum of the rate at which they are produced
    // and Flutter's frame rate. It also verifies that we do not rebuild the
    // Widget tree during texture consumption. The test starts by measuring
    // Flutter's frame rate.
27 28 29 30 31
    test('texture rendering', () async {
      final SerializableFinder fab = find.byValueKey('fab');
      final SerializableFinder summary = find.byValueKey('summary');

      // Wait for calibration to complete and fab to appear.
32
      await driver.waitFor(fab);
33 34

      final String calibrationResult = await driver.getText(summary);
35
      final Match? matchCalibration = calibrationRegExp.matchAsPrefix(calibrationResult);
36
      expect(matchCalibration, isNotNull);
37
      final double flutterFrameRate = double.parse(matchCalibration?.group(1) ?? '0');
38 39 40

      // Texture frame stats at 0.5x Flutter frame rate
      await driver.tap(fab);
41
      await Future<void>.delayed(samplingTime);
42 43 44
      await driver.tap(fab);

      final String statsSlow = await driver.getText(summary);
45
      final Match matchSlow = statsRegExp.matchAsPrefix(statsSlow)!;
46
      expect(matchSlow, isNotNull);
47 48 49
      expect(double.parse(matchSlow.group(1)!), closeTo(flutterFrameRate / 2.0, 5.0));
      expect(double.parse(matchSlow.group(2)!), closeTo(flutterFrameRate / 2.0, 5.0));
      expect(int.parse(matchSlow.group(3)!), 1);
50 51 52

      // Texture frame stats at 2.0x Flutter frame rate
      await driver.tap(fab);
53
      await Future<void>.delayed(samplingTime);
54 55 56
      await driver.tap(fab);

      final String statsFast = await driver.getText(summary);
57
      final Match matchFast = statsRegExp.matchAsPrefix(statsFast)!;
58
      expect(matchFast, isNotNull);
59 60 61
      expect(double.parse(matchFast.group(1)!), closeTo(flutterFrameRate * 2.0, 5.0));
      expect(double.parse(matchFast.group(2)!), closeTo(flutterFrameRate, 10.0));
      expect(int.parse(matchFast.group(3)!), 1);
62
    }, timeout: Timeout.none);
63 64

    tearDownAll(() async {
65
      driver.close();
66 67 68
    });
  });
}