memory_nav.dart 2.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright 2016 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.

// See //dev/devicelab/bin/tasks/flutter_gallery__memory_nav.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
import 'package:flutter_test/flutter_test.dart';

Future<void> endOfAnimation() async {
  do {
    await SchedulerBinding.instance.endOfFrame;
  } while (SchedulerBinding.instance.hasScheduledFrame);
}

20 21 22 23 24
Rect boundsFor(WidgetController controller, Finder item) {
  final RenderBox box = controller.renderObject<RenderBox>(item);
  return box.localToGlobal(Offset.zero) & box.size;
}

25
Future<void> main() async {
26 27
  final Completer<void> ready = Completer<void>();
  runApp(GestureDetector(
28 29 30 31 32 33 34 35 36 37 38
    onTap: () {
      debugPrint('Received tap.');
      ready.complete();
    },
    behavior: HitTestBehavior.opaque,
    child: const IgnorePointer(
      ignoring: true,
      child: GalleryApp(testMode: true),
    ),
  ));
  await SchedulerBinding.instance.endOfFrame;
39
  await Future<void>.delayed(const Duration(milliseconds: 50));
40 41 42 43 44 45
  debugPrint('==== MEMORY BENCHMARK ==== READY ====');

  await ready.future;
  debugPrint('Continuing...');

  // remove onTap handler, enable pointer events for app
46
  runApp(GestureDetector(
47 48 49 50 51 52 53
    child: const IgnorePointer(
      ignoring: false,
      child: GalleryApp(testMode: true),
    ),
  ));
  await SchedulerBinding.instance.endOfFrame;

54
  final WidgetController controller = LiveWidgetController(WidgetsBinding.instance);
55 56 57

  debugPrint('Navigating...');
  await controller.tap(find.text('Material'));
58
  await Future<void>.delayed(const Duration(milliseconds: 150));
59 60 61 62
  final Finder demoList = find.byKey(const Key('GalleryDemoList'));
  final Finder demoItem = find.text('Text fields');
  do {
    await controller.drag(demoList, const Offset(0.0, -300.0));
63
    await Future<void>.delayed(const Duration(milliseconds: 20));
64 65
  } while (!demoItem.precache());

66 67 68 69 70
  // Ensure that the center of the "Text fields" item is visible
  // because that's where we're going to tap
  final Rect demoItemBounds = boundsFor(controller, demoItem);
  final Rect demoListBounds = boundsFor(controller, demoList);
  if (!demoListBounds.contains(demoItemBounds.center)) {
71
    await controller.drag(demoList, Offset(0.0, demoListBounds.center.dy - demoItemBounds.center.dy));
72 73 74
    await endOfAnimation();
  }

75 76 77 78 79
  for (int iteration = 0; iteration < 15; iteration += 1) {
    debugPrint('Tapping... (iteration $iteration)');
    await controller.tap(demoItem);
    await endOfAnimation();
    debugPrint('Backing out...');
80
    await controller.tap(find.byTooltip('Back'));
81
    await endOfAnimation();
82
    await Future<void>.delayed(const Duration(milliseconds: 50));
83 84 85 86
  }

  debugPrint('==== MEMORY BENCHMARK ==== DONE ====');
}