routing_test.dart 3.59 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/framework/devices.dart';
10
import 'package:flutter_devicelab/framework/framework.dart';
11
import 'package:flutter_devicelab/framework/task_result.dart';
12
import 'package:flutter_devicelab/framework/utils.dart';
13
import 'package:path/path.dart' as path;
14 15 16

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

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