util.dart 2.19 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 'package:flutter_driver/flutter_driver.dart';
6
import 'package:macrobenchmarks/common.dart';
7
import 'package:test/test.dart' hide TypeMatcher, isInstanceOf;
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
typedef DriverTestCallBack = Future<void> Function(FlutterDriver driver);

Future<void> runDriverTestForRoute(String routeName, DriverTestCallBack body) async {
  final FlutterDriver driver = await FlutterDriver.connect();

  // The slight initial delay avoids starting the timing during a
  // period of increased load on the device. Without this delay, the
  // benchmark has greater noise.
  // See: https://github.com/flutter/flutter/issues/19434
  await Future<void>.delayed(const Duration(milliseconds: 250));

  await driver.forceGC();

  final SerializableFinder scrollable = find.byValueKey(kScrollableName);
  expect(scrollable, isNotNull);
  final SerializableFinder button = find.byValueKey(routeName);
  expect(button, isNotNull);
  await driver.scrollUntilVisible(scrollable, button, dyScroll: -50.0);
  await driver.tap(button);

  await body(driver);

  driver.close();
}

34
void macroPerfTest(
35 36
  String testName,
  String routeName, {
37
  Duration? pageDelay,
38
  Duration duration = const Duration(seconds: 3),
39 40
  Future<void> Function(FlutterDriver driver)? driverOps,
  Future<void> Function(FlutterDriver driver)? setupOps,
41
}) {
42
  test(testName, () async {
43
    late Timeline timeline;
44 45 46 47 48
    await runDriverTestForRoute(routeName, (FlutterDriver driver) async {
      if (pageDelay != null) {
        // Wait for the page to load
        await Future<void>.delayed(pageDelay);
      }
49

50
      if (setupOps != null) {
51
        await setupOps(driver);
52
      }
53

54
      timeline = await driver.traceAction(() async {
55 56 57 58
      final Future<void> durationFuture = Future<void>.delayed(duration);
      if (driverOps != null) {
        await driverOps(driver);
      }
59 60
        await durationFuture;
      });
61 62
    });

63
    expect(timeline, isNotNull);
64 65 66

    final TimelineSummary summary = TimelineSummary.summarize(timeline);
    await summary.writeTimelineToFile(testName, pretty: true);
67
  }, timeout: Timeout.none);
68
}