binding_fail_test.dart 3.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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:convert';
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:path/path.dart' as path;

Dan Field's avatar
Dan Field committed
11
final String bat = Platform.isWindows ? '.bat' : '';
12
final String _flutterBin = path.join(Directory.current.parent.parent.path, 'bin', 'flutter$bat');
Dan Field's avatar
Dan Field committed
13 14 15
const String _integrationResultsPrefix =
    'IntegrationTestWidgetsFlutterBinding test results:';
const String _failureExcerpt = r'Expected: <false>\n  Actual: <true>';
16 17

Future<void> main() async {
Dan Field's avatar
Dan Field committed
18 19
  group('Integration binding result', () {
    test('when multiple tests pass', () async {
20
      final Map<String, dynamic>? results = await _runTest(path.join('test', 'data', 'pass_test_script.dart'));
21

Dan Field's avatar
Dan Field committed
22 23 24 25 26 27 28
      expect(
          results,
          equals(<String, dynamic>{
            'passing test 1': 'success',
            'passing test 2': 'success',
          }));
    });
29

Dan Field's avatar
Dan Field committed
30
    test('when multiple tests fail', () async {
31
      final Map<String, dynamic>? results = await _runTest(path.join('test', 'data', 'fail_test_script.dart'));
32

Dan Field's avatar
Dan Field committed
33 34 35 36
      expect(results, hasLength(2));
      expect(results, containsPair('failing test 1', contains(_failureExcerpt)));
      expect(results, containsPair('failing test 2', contains(_failureExcerpt)));
    });
37

Dan Field's avatar
Dan Field committed
38
    test('when one test passes, then another fails', () async {
39
      final Map<String, dynamic>? results = await _runTest(path.join('test', 'data', 'pass_then_fail_test_script.dart'));
40

Dan Field's avatar
Dan Field committed
41 42 43 44
      expect(results, hasLength(2));
      expect(results, containsPair('passing test', equals('success')));
      expect(results, containsPair('failing test', contains(_failureExcerpt)));
    });
45 46 47 48 49 50
  });
}

/// Runs a test script and returns the [IntegrationTestWidgetsFlutterBinding.result].
///
/// [scriptPath] is relative to the package root.
51
Future<Map<String, dynamic>?> _runTest(String scriptPath) async {
Dan Field's avatar
Dan Field committed
52 53
  final Process process =
      await Process.start(_flutterBin, <String>['test', '--machine', scriptPath]);
54 55 56 57 58 59 60 61 62

  /// In the test [tearDownAll] block, the test results are encoded into JSON and
  /// are printed with the [_integrationResultsPrefix] prefix.
  ///
  /// See the following for the test event spec which we parse the printed lines
  /// out of: https://github.com/dart-lang/test/blob/master/pkgs/test/doc/json_reporter.md
  final String testResults = (await process.stdout
          .transform(utf8.decoder)
          .expand((String text) => text.split('\n'))
63
          .map<dynamic>((String line) {
64
            try {
65
              return jsonDecode(line);
66 67 68 69
            } on FormatException {
              // Only interested in test events which are JSON.
            }
          })
70
          .expand<Map<String, dynamic>>((dynamic json) {
71 72 73 74 75 76 77
            if (json is List<dynamic>) {
              return json.cast();
            }
            return <Map<String, dynamic>>[
              if (json != null)
                json as Map<String, dynamic>
            ];
78
          })
79
          .where((Map<String, dynamic> testEvent) => testEvent['type'] == 'print')
80
          .map((Map<String, dynamic> printEvent) => printEvent['message'] as String)
81
          .firstWhere((String message) => message.startsWith(_integrationResultsPrefix)))
82 83
      .replaceAll(_integrationResultsPrefix, '');

84
  return jsonDecode(testResults) as Map<String, dynamic>?;
85
}