• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2019 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 'package:flutter_tools/src/base/file_system.dart';
6import 'package:flutter_tools/src/compile.dart';
7import 'package:flutter_tools/src/project.dart';
8import 'package:flutter_tools/src/test/test_compiler.dart';
9import 'package:mockito/mockito.dart';
10
11import '../src/common.dart';
12import '../src/testbed.dart';
13
14void main() {
15  group(TestCompiler, () {
16    Testbed testbed;
17    FakeTestCompiler testCompiler;
18    MockResidentCompiler residentCompiler;
19
20    setUp(() {
21      testbed = Testbed(
22        setup: () async {
23          fs.file('pubspec.yaml').createSync();
24          fs.file('.packages').createSync();
25          fs.file('test/foo.dart').createSync(recursive: true);
26          residentCompiler = MockResidentCompiler();
27          testCompiler = FakeTestCompiler(
28            false,
29            FlutterProject.current(),
30            residentCompiler,
31          );
32        },
33      );
34    });
35
36    test('Reports a dill file when compile is successful', () => testbed.run(() async {
37      when(residentCompiler.recompile(
38        'test/foo.dart',
39        <Uri>[Uri.parse('test/foo.dart')],
40        outputPath: testCompiler.outputDill.path,
41      )).thenAnswer((Invocation invocation) async {
42        fs.file('abc.dill').createSync();
43        return const CompilerOutput('abc.dill', 0, <Uri>[]);
44      });
45
46      expect(await testCompiler.compile('test/foo.dart'), 'test/foo.dart.dill');
47      expect(fs.file('test/foo.dart.dill').existsSync(), true);
48    }));
49
50    test('Reports null when a compile fails', () => testbed.run(() async {
51      when(residentCompiler.recompile(
52        'test/foo.dart',
53        <Uri>[Uri.parse('test/foo.dart')],
54        outputPath: testCompiler.outputDill.path,
55      )).thenAnswer((Invocation invocation) async {
56        fs.file('abc.dill').createSync();
57        return const CompilerOutput('abc.dill', 1, <Uri>[]);
58      });
59
60      expect(await testCompiler.compile('test/foo.dart'), null);
61      expect(fs.file('test/foo.dart.dill').existsSync(), false);
62      verify(residentCompiler.shutdown()).called(1);
63    }));
64
65    test('Disposing test compiler shuts down backing compiler', () => testbed.run(() async {
66      testCompiler.compiler = residentCompiler;
67      expect(testCompiler.compilerController.isClosed, false);
68      await testCompiler.dispose();
69      expect(testCompiler.compilerController.isClosed, true);
70      verify(residentCompiler.shutdown()).called(1);
71    }));
72  });
73}
74
75/// Override the creation of the Resident Compiler to simplify testing.
76class FakeTestCompiler extends TestCompiler {
77  FakeTestCompiler(
78    bool trackWidgetCreation,
79    FlutterProject flutterProject,
80    this.residentCompiler,
81  ) : super(trackWidgetCreation, flutterProject);
82
83  final MockResidentCompiler residentCompiler;
84
85  @override
86  Future<ResidentCompiler> createCompiler() async {
87    return residentCompiler;
88  }
89}
90
91class MockResidentCompiler extends Mock implements ResidentCompiler {}
92