run_release_test.dart 4.45 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:path/path.dart' as path;

import 'package:flutter_devicelab/framework/adb.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';

void main() {
  task(() async {
    final Device device = await devices.workingDevice;
    await device.unlock();
    final Directory appDir = dir(path.join(flutterDirectory.path, 'dev/integration_tests/ui'));
    await inDirectory(appDir, () async {
21
      final Completer<void> ready = Completer<void>();
22 23 24 25 26 27 28 29 30 31
      print('run: starting...');
      final Process run = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
        <String>['--suppress-analytics', 'run', '--release', '-d', device.deviceId, 'lib/main.dart'],
        isBot: false, // we just want to test the output, not have any debugging info
      );
      final List<String> stdout = <String>[];
      final List<String> stderr = <String>[];
      int runExitCode;
      run.stdout
32 33
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
34 35
        .listen((String line) {
          print('run:stdout: $line');
36 37 38 39 40
          if (
            !line.startsWith('Building flutter tool...') &&
            !line.startsWith('Running "flutter pub get" in ui...') &&
            !line.startsWith('Initializing gradle...') &&
            !line.contains('settings_aar.gradle') &&
41 42 43 44 45
            !line.startsWith('Resolving dependencies...') &&
            // Catch engine piped output from unrelated concurrent Flutter apps
            !line.contains(RegExp(r'[A-Z]\/flutter \([0-9]+\):')) &&
            // Empty lines could be due to the progress spinner breaking up.
            line.length > 1
46 47 48
          ) {
            stdout.add(line);
          }
49
          if (line.contains('Quit (terminate the application on the device).')) {
50
            ready.complete();
51
          }
52 53
        });
      run.stderr
54 55
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
56 57
        .listen((String line) {
          print('run:stderr: $line');
58
          stderr.add(line);
59
        });
60
      run.exitCode.then<void>((int exitCode) { runExitCode = exitCode; });
61
      await Future.any<dynamic>(<Future<dynamic>>[ ready.future, run.exitCode ]);
62
      if (runExitCode != null) {
63
        throw 'Failed to run test app; runner unexpected exited, with exit code $runExitCode.';
64
      }
65
      run.stdin.write('q');
66

67
      await run.exitCode;
68 69

      if (stderr.isNotEmpty) {
70
        throw 'flutter run --release had output on standard error.';
71
      }
72 73 74 75 76 77 78 79 80

      _findNextMatcherInList(
        stdout,
        (String line) => line.startsWith('Launching lib/main.dart on ') && line.endsWith(' in release mode...'),
        'Launching lib/main.dart on',
      );

      _findNextMatcherInList(
        stdout,
81 82
        (String line) => line.startsWith("Running Gradle task 'assembleRelease'..."),
        "Running Gradle task 'assembleRelease'...",
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      );

      _findNextMatcherInList(
        stdout,
        (String line) => line.contains('Built build/app/outputs/apk/release/app-release.apk (') && line.contains('MB).'),
        'Built build/app/outputs/apk/release/app-release.apk',
      );

      _findNextMatcherInList(
        stdout,
        (String line) => line.startsWith('Installing build/app/outputs/apk/app.apk...'),
        'Installing build/app/outputs/apk/app.apk...',
      );

      _findNextMatcherInList(
        stdout,
99 100
        (String line) => line.contains('Quit (terminate the application on the device).'),
        'q Quit (terminate the application on the device)',
101 102 103 104 105 106 107
      );

      _findNextMatcherInList(
        stdout,
        (String line) => line == 'Application finished.',
        'Application finished.',
      );
108
    });
109
    return TaskResult.success(null);
110 111
  });
}
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

void _findNextMatcherInList(
  List<String> list,
  bool Function(String testLine) matcher,
  String errorMessageExpectedLine
) {
  final List<String> copyOfListForErrorMessage = List<String>.from(list);

  while (list.isNotEmpty) {
    final String nextLine = list.first;
    list.removeAt(0);

    if (matcher(nextLine)) {
      return;
    }
  }

  throw '''
Did not find expected line

$errorMessageExpectedLine

in flutter run --release stdout

$copyOfListForErrorMessage
  ''';
}