routing_test.dart 3.63 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
// 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';

9
import 'package:flutter_devicelab/common.dart';
10
import 'package:flutter_devicelab/framework/devices.dart';
11
import 'package:flutter_devicelab/framework/framework.dart';
12
import 'package:flutter_devicelab/framework/task_result.dart';
13
import 'package:flutter_devicelab/framework/utils.dart';
14
import 'package:path/path.dart' as path;
15 16 17

void main() {
  task(() async {
18
    int? vmServicePort;
19

20 21 22 23 24
    final Device device = await devices.workingDevice;
    await device.unlock();
    final Directory appDir = dir(path.join(flutterDirectory.path, 'dev/integration_tests/ui'));
    section('TEST WHETHER `flutter drive --route` WORKS');
    await inDirectory(appDir, () async {
25
      return flutter(
26
        'drive',
27 28 29 30 31 32 33 34
        options: <String>[
          '--verbose',
          '-d',
          device.deviceId,
          '--route',
          '/smuggle-it',
          'lib/route.dart',
        ],
35 36 37 38 39
        canFail: false,
      );
    });
    section('TEST WHETHER `flutter run --route` WORKS');
    await inDirectory(appDir, () async {
40
      final Completer<void> ready = Completer<void>();
41
      late bool ok;
42 43 44
      print('run: starting...');
      final Process run = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
45
        // --fast-start does not support routes.
46
        <String>['run', '--verbose', '--disable-service-auth-codes', '--no-fast-start', '--no-publish-port', '-d', device.deviceId, '--route', '/smuggle-it', 'lib/route.dart'],
47 48
      );
      run.stdout
49 50
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
51 52
        .listen((String line) {
          print('run:stdout: $line');
53
          if (vmServicePort == null) {
54
            vmServicePort = parseServicePort(line);
55 56 57 58
            if (vmServicePort != null) {
              print('service protocol connection available at port $vmServicePort');
              print('run: ready!');
              ready.complete();
59
              ok = true;
60
            }
61 62 63
          }
        });
      run.stderr
64 65
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
66 67 68
        .listen((String line) {
          stderr.writeln('run:stderr: $line');
        });
69
      unawaited(run.exitCode.then<void>((int exitCode) { ok = false; }));
70 71 72 73 74 75
      await Future.any<dynamic>(<Future<dynamic>>[ ready.future, run.exitCode ]);
      if (!ok)
        throw 'Failed to run test app.';
      print('drive: starting...');
      final Process drive = await startProcess(
        path.join(flutterDirectory.path, 'bin', 'flutter'),
76
        <String>['drive', '--use-existing-app', 'http://127.0.0.1:$vmServicePort/', '--no-keep-app-running', 'lib/route.dart'],
77 78
      );
      drive.stdout
79 80
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
81 82 83 84
        .listen((String line) {
          print('drive:stdout: $line');
        });
      drive.stderr
85 86
        .transform<String>(utf8.decoder)
        .transform<String>(const LineSplitter())
87 88 89 90 91
        .listen((String line) {
          stderr.writeln('drive:stderr: $line');
        });
      int result;
      result = await drive.exitCode;
92 93 94
      await flutter('install', options: <String>[
        '--uninstall-only',
      ]);
95 96 97 98 99 100
      if (result != 0)
        throw 'Failed to drive test app (exit code $result).';
      result = await run.exitCode;
      if (result != 0)
        throw 'Received unexpected exit code $result from run process.';
    });
101
    return TaskResult.success(null);
102 103
  });
}