flutter_gallery__back_button_memory.dart 2.2 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 6 7
/// Measure application memory usage after pausing and resuming the app
/// with the Android back button.

8 9
import 'dart:async';

10
import 'package:flutter_devicelab/framework/adb.dart';
11
import 'package:flutter_devicelab/framework/framework.dart';
12 13 14 15 16 17 18 19 20 21
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:flutter_devicelab/tasks/perf_tests.dart';

const String packageName = 'io.flutter.demo.gallery';
const String activityName = 'io.flutter.demo.gallery.MainActivity';

class BackButtonMemoryTest extends MemoryTest {
  BackButtonMemoryTest() : super('${flutterDirectory.path}/examples/flutter_gallery', 'test_memory/back_button.dart', packageName);

  @override
22
  AndroidDevice get device => super.device as AndroidDevice;
23

24 25 26
  @override
  int get iterationCount => 5;

27 28 29 30 31
  /// Perform a series of back button suspend and resume cycles.
  @override
  Future<void> useMemory() async {
    await launchApp();
    await recordStart();
32
    for (int iteration = 0; iteration < 8; iteration += 1) {
33 34 35 36 37 38 39 40
      print('back/forward iteration $iteration');

      // Push back button, wait for it to be seen by the Flutter app.
      prepareForNextMessage('AppLifecycleState.paused');
      await device.shellExec('input', <String>['keyevent', 'KEYCODE_BACK']);
      await receivedNextMessage;

      // Give Android time to settle (e.g. run GCs) after closing the app.
41
      await Future<void>.delayed(const Duration(milliseconds: 100));
42 43 44 45 46 47 48 49 50 51

      // Relaunch the app, wait for it to launch.
      prepareForNextMessage('READY');
      final String output = await device.shellEval('am', <String>['start', '-n', '$packageName/$activityName']);
      print('adb shell am start: $output');
      if (output.contains('Error'))
        fail('unable to launch activity');
      await receivedNextMessage;

      // Wait for the Flutter app to settle (e.g. run GCs).
52
      await Future<void>.delayed(const Duration(milliseconds: 100));
53 54 55 56
    }
    await recordEnd();
  }
}
57

58
Future<void> main() async {
59
  deviceOperatingSystem = DeviceOperatingSystem.android;
60
  await task(BackButtonMemoryTest().run);
61
}