• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6import 'dart:io';
7
8import 'package:path/path.dart' as path;
9
10import 'package:flutter_devicelab/framework/adb.dart';
11import 'package:flutter_devicelab/framework/framework.dart';
12import 'package:flutter_devicelab/framework/utils.dart';
13
14final Directory flutterGalleryDir = dir(path.join(flutterDirectory.path, 'examples/hello_world'));
15final File runTestSource = File(path.join(
16  flutterDirectory.path, 'dev', 'automated_tests', 'flutter_run_test', 'flutter_run_test.dart',
17));
18const Pattern passedMessageMatch = '+0: example passed';
19const Pattern failedMessageMatch = '+1: example failed [E]';
20const Pattern skippedMessageMatch = '+1 -1: example skipped';
21const Pattern finishedMessageMatch = '+1 ~1 -1: Some tests failed.';
22
23Future<void> main() async {
24  deviceOperatingSystem = DeviceOperatingSystem.android;
25  await task(createFlutterRunTask);
26}
27
28// verifies that the messages above are printed as a test script is executed.
29Future<TaskResult> createFlutterRunTask() async {
30  bool passedTest = false;
31  bool failedTest = false;
32  bool skippedTest = false;
33  bool finishedMessage = false;
34  final Device device = await devices.workingDevice;
35  await device.unlock();
36  final List<String> options = <String>[
37    '-t', runTestSource.absolute.path, '-d', device.deviceId,
38  ];
39  await inDirectory<void>(flutterGalleryDir, () async {
40    startProcess(
41      path.join(flutterDirectory.path, 'bin', 'flutter'),
42      flutterCommandArgs('run', options),
43      environment: null,
44    );
45    final Completer<void> finished = Completer<void>();
46    final StreamSubscription<void> subscription = device.logcat.listen((String line) {
47      // tests execute in order.
48      if (line.contains(passedMessageMatch)) {
49        passedTest = true;
50      } else if (line.contains(failedMessageMatch)) {
51        failedTest = true;
52      } else if (line.contains(skippedMessageMatch)) {
53        skippedTest = true;
54      } else if (line.contains(finishedMessageMatch)) {
55        finishedMessage = true;
56        finished.complete();
57      }
58    });
59    await finished.future.timeout(const Duration(minutes: 1));
60    subscription.cancel();
61  });
62  return passedTest && failedTest && skippedTest && finishedMessage
63    ? TaskResult.success(<String, dynamic>{})
64    : TaskResult.failure('Test did not execute as expected.');
65}
66
67