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
import 'package:flutter_devicelab/framework/devices.dart';
9
import 'package:flutter_devicelab/framework/framework.dart';
10 11 12 13 14 15 16
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 {
17
  BackButtonMemoryTest() : super('${flutterDirectory.path}/dev/integration_tests/flutter_gallery', 'test_memory/back_button.dart', packageName);
18 19

  @override
20
  AndroidDevice? get device => super.device as AndroidDevice?;
21

22 23 24
  @override
  int get iterationCount => 5;

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

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

      // Give Android time to settle (e.g. run GCs) after closing the app.
39
      await Future<void>.delayed(const Duration(milliseconds: 100));
40 41 42

      // Relaunch the app, wait for it to launch.
      prepareForNextMessage('READY');
43
      final String output = await device!.shellEval('am', <String>['start', '-n', '$packageName/$activityName']);
44 45 46 47 48 49
      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).
50
      await Future<void>.delayed(const Duration(milliseconds: 100));
51 52 53 54
    }
    await recordEnd();
  }
}
55

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