flutter_gallery_v2_chrome_run_test.dart 3.43 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2014 The Flutter 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';

9 10
import 'package:path/path.dart' as path;

11 12
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
13
import 'package:flutter_devicelab/versions/gallery.dart' show galleryVersion;
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

Future<void> main() async {
  await task(const NewGalleryChromeRunTest().run);
}

/// URI for the New Flutter Gallery repository.
const String galleryRepo = 'https://github.com/flutter/gallery.git';

/// After the gallery loads, a duration of [durationToWaitForError]
/// is waited, allowing any possible exceptions to be thrown.
const Duration durationToWaitForError = Duration(seconds: 5);

/// Flutter prints this string when an app is successfully loaded.
/// Used to check when the app is successfully loaded.
const String successfullyLoadedString = 'To hot restart';

/// Flutter prints this string when an exception is caught.
/// Used to check if there are any exceptions.
const String exceptionString = 'EXCEPTION CAUGHT';

/// Checks that the New Flutter Gallery runs successfully on Chrome.
class NewGalleryChromeRunTest {
  const NewGalleryChromeRunTest();

  /// Runs the test.
  Future<TaskResult> run() async {
40 41 42 43
    final Directory galleryParentDir =
        Directory.systemTemp.createTempSync('temp');
    final Directory galleryDir =
        Directory(path.join(galleryParentDir.path, 'gallery'));
44

45 46 47
    await getNewGallery(galleryVersion, galleryDir);

    final TaskResult result = await inDirectory<TaskResult>(galleryDir, () async {
48 49 50 51 52 53 54 55
      await flutter('doctor');
      await flutter('packages', options: <String>['get']);

      await flutter('build', options: <String>[
        'web',
        '-v',
        '--release',
        '--no-pub',
56
      ]);
57 58 59

      final List<String> options = <String>['-d', 'chrome', '--verbose', '--resident'];
      final Process process = await startProcess(
60
        path.join(flutterDirectory.path, 'bin', 'flutter'),
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        flutterCommandArgs('run', options),
      );

      final Completer<void> stdoutDone = Completer<void>();
      final Completer<void> stderrDone = Completer<void>();

      bool success = true;

      process.stdout
          .transform<String>(utf8.decoder)
          .transform<String>(const LineSplitter())
          .listen((String line) {
        if (line.contains(successfullyLoadedString)) {
          // Successfully started.
          Future<void>.delayed(
            durationToWaitForError,
            () {process.stdin.write('q');}
          );
        }
        if (line.contains(exceptionString)) {
          success = false;
        }
        print('stdout: $line');
      }, onDone: () {
        stdoutDone.complete();
      });

      process.stderr
          .transform<String>(utf8.decoder)
          .transform<String>(const LineSplitter())
          .listen((String line) {
        print('stderr: $line');
      }, onDone: () {
        stderrDone.complete();
      });

      await Future.wait<void>(<Future<void>>[
        stdoutDone.future,
        stderrDone.future,
      ]);

      await process.exitCode;

      if (success) {
        return TaskResult.success(<String, dynamic>{});
      } else {
        return TaskResult.failure('An exception was thrown.');
      }
    });

111
    rmTree(galleryParentDir);
112 113 114 115

    return result;
  }
}