commands_test.dart 5.02 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright (c) 2017 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.

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

import 'package:path/path.dart' as path;
10
import 'package:vm_service_client/vm_service_client.dart';
11 12 13 14 15 16 17

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 {
18 19
    int vmServicePort;

20 21 22 23
    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 {
24
      final Completer<void> ready = Completer<void>();
25 26 27 28
      bool ok;
      print('run: starting...');
      final Process run = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
29
        <String>['run', '--verbose', '-d', device.deviceId, 'lib/commands.dart'],
30
      );
31
      final StreamController<String> stdout = StreamController<String>.broadcast();
32
      run.stdout
33 34
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
35 36
        .listen((String line) {
          print('run:stdout: $line');
37
          stdout.add(line);
38
          if (vmServicePort == null) {
39
            vmServicePort = parseServicePort(line);
40 41 42 43 44 45
            if (vmServicePort != null) {
              print('service protocol connection available at port $vmServicePort');
              print('run: ready!');
              ready.complete();
              ok ??= true;
            }
46 47 48
          }
        });
      run.stderr
49 50
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
51 52 53
        .listen((String line) {
          stderr.writeln('run:stderr: $line');
        });
54
      run.exitCode.then<void>((int exitCode) { ok = false; });
55 56 57
      await Future.any<dynamic>(<Future<dynamic>>[ ready.future, run.exitCode ]);
      if (!ok)
        throw 'Failed to run test app.';
58

59
      final VMServiceClient client = VMServiceClient.connect('ws://localhost:$vmServicePort/ws');
60

61
      final DriveHelper driver = DriveHelper(vmServicePort);
62

63
      await driver.drive('none');
64 65
      print('test: pressing "p" to enable debugPaintSize...');
      run.stdin.write('p');
66
      await driver.drive('debug_paint');
67 68
      print('test: pressing "p" again...');
      run.stdin.write('p');
69
      await driver.drive('none');
70 71
      print('test: pressing "P" to enable performance overlay...');
      run.stdin.write('P');
72
      await driver.drive('performance_overlay');
73 74
      print('test: pressing "P" again...');
      run.stdin.write('P');
75
      await driver.drive('none');
76
      final Future<String> reloadStartingText =
77
        stdout.stream.firstWhere((String line) => line.endsWith('] Initializing hot reload...'));
78
      final Future<String> reloadEndingText =
79
        stdout.stream.firstWhere((String line) => line.contains('] Reloaded ') && line.endsWith('ms.'));
80 81 82
      print('test: pressing "r" to perform a hot reload...');
      run.stdin.write('r');
      await reloadStartingText;
83
      await reloadEndingText;
84
      await driver.drive('none');
85
      final Future<String> restartStartingText =
86
        stdout.stream.firstWhere((String line) => line.endsWith('Performing hot restart...'));
87
      final Future<String> restartEndingText =
88
        stdout.stream.firstWhere((String line) => line.contains('] Restarted application in '));
89 90 91
      print('test: pressing "R" to perform a full reload...');
      run.stdin.write('R');
      await restartStartingText;
92
      await restartEndingText;
93
      await driver.drive('none');
94 95 96 97
      run.stdin.write('q');
      final int result = await run.exitCode;
      if (result != 0)
        throw 'Received unexpected exit code $result from run process.';
98
      print('test: validating that the app has in fact closed...');
99
      await client.done.timeout(const Duration(seconds: 5));
100
    });
101
    return TaskResult.success(null);
102 103 104
  });
}

105 106 107 108 109
class DriveHelper {
  DriveHelper(this.vmServicePort);

  final int vmServicePort;

110
  Future<void> drive(String name) async {
111 112 113 114 115 116
    print('drive: running commands_$name check...');
    final Process drive = await startProcess(
      path.join(flutterDirectory.path, 'bin', 'flutter'),
      <String>['drive', '--use-existing-app', 'http://127.0.0.1:$vmServicePort/', '--keep-app-running', '--driver', 'test_driver/commands_${name}_test.dart'],
    );
    drive.stdout
117 118
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
119
        .listen((String line) {
120 121
      print('drive:stdout: $line');
    });
122
    drive.stderr
123 124
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
125
        .listen((String line) {
126 127
      stderr.writeln('drive:stderr: $line');
    });
128 129 130 131 132
    final int result = await drive.exitCode;
    if (result != 0)
      throw 'Failed to drive test app (exit code $result).';
    print('drive: finished commands_$name check successfully.');
  }
133
}