commands_test.dart 4.88 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 24 25 26 27 28
    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 {
      final Completer<Null> ready = new Completer<Null>();
      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 = new StreamController<String>.broadcast();
32
      run.stdout
33
        .transform(utf8.decoder)
34 35 36
        .transform(const LineSplitter())
        .listen((String line) {
          print('run:stdout: $line');
37
          stdout.add(line);
38 39 40
          if (lineContainsServicePort(line)) {
            vmServicePort = parseServicePort(line);
            print('service protocol connection available at port $vmServicePort');
41 42 43 44 45 46
            print('run: ready!');
            ready.complete();
            ok ??= true;
          }
        });
      run.stderr
47
        .transform(utf8.decoder)
48 49 50 51 52 53 54 55
        .transform(const LineSplitter())
        .listen((String line) {
          stderr.writeln('run:stderr: $line');
        });
      run.exitCode.then((int exitCode) { ok = false; });
      await Future.any<dynamic>(<Future<dynamic>>[ ready.future, run.exitCode ]);
      if (!ok)
        throw 'Failed to run test app.';
56

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

      final DriveHelper driver = new DriveHelper(vmServicePort);
60

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

103 104 105 106 107 108 109 110 111 112 113 114
class DriveHelper {
  DriveHelper(this.vmServicePort);

  final int vmServicePort;

  Future<Null> drive(String name) async {
    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
115
        .transform(utf8.decoder)
116 117
        .transform(const LineSplitter())
        .listen((String line) {
118 119
      print('drive:stdout: $line');
    });
120
    drive.stderr
121
        .transform(utf8.decoder)
122 123
        .transform(const LineSplitter())
        .listen((String line) {
124 125
      stderr.writeln('drive:stderr: $line');
    });
126 127 128 129 130
    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.');
  }
131
}