flutter_tester_test.dart 6.48 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7 8
import 'dart:async';

9 10
import 'package:file/file.dart';
import 'package:file/memory.dart';
11
import 'package:flutter_tools/src/artifacts.dart';
12
import 'package:flutter_tools/src/base/config.dart';
13
import 'package:flutter_tools/src/base/logger.dart';
14
import 'package:flutter_tools/src/base/platform.dart';
15
import 'package:flutter_tools/src/build_info.dart';
16
import 'package:flutter_tools/src/build_system/build_system.dart';
17 18 19
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/tester/flutter_tester.dart';

20 21
import '../../src/common.dart';
import '../../src/context.dart';
22
import '../../src/fakes.dart';
23
import '../../src/test_build_system.dart';
24 25

void main() {
26
  MemoryFileSystem fileSystem;
27 28

  setUp(() {
29
    fileSystem = MemoryFileSystem.test();
30 31
  });

32 33 34 35
  testWithoutContext('FlutterTesterApp can be created from the current directory', () async {
    const String projectPath = '/home/my/projects/my_project';
    await fileSystem.directory(projectPath).create(recursive: true);
    fileSystem.currentDirectory = projectPath;
36

37 38 39 40
    final FlutterTesterApp app = FlutterTesterApp.fromCurrentDirectory(fileSystem);

    expect(app.name, 'my_project');
    expect(app.packagesFile.path, fileSystem.path.join(projectPath, '.packages'));
41 42 43 44 45 46 47
  });

  group('FlutterTesterDevices', () {
    tearDown(() {
      FlutterTesterDevices.showFlutterTesterDevice = false;
    });

48 49
    testWithoutContext('no device', () async {
      final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
50 51 52 53 54

      final List<Device> devices = await discoverer.devices;
      expect(devices, isEmpty);
    });

55
    testWithoutContext('has device', () async {
56
      FlutterTesterDevices.showFlutterTesterDevice = true;
57
      final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
58 59 60 61 62

      final List<Device> devices = await discoverer.devices;
      expect(devices, hasLength(1));

      final Device device = devices.single;
Dan Field's avatar
Dan Field committed
63
      expect(device, isA<FlutterTesterDevice>());
64 65
      expect(device.id, 'flutter-tester');
    });
66

67
    testWithoutContext('discoverDevices', () async {
68
      FlutterTesterDevices.showFlutterTesterDevice = true;
69
      final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
70 71 72 73 74

      // Timeout ignored.
      final List<Device> devices = await discoverer.discoverDevices(timeout: const Duration(seconds: 10));
      expect(devices, hasLength(1));
    });
75
  });
76

77
  group('startApp', () {
78 79
    FlutterTesterDevice device;
    List<String> logLines;
80
    String mainPath;
81

82
    FakeProcessManager fakeProcessManager;
83
    TestBuildSystem buildSystem;
84 85 86 87

    final Map<Type, Generator> startOverrides = <Type, Generator>{
      Platform: () => FakePlatform(operatingSystem: 'linux'),
      FileSystem: () => fileSystem,
88
      ProcessManager: () => fakeProcessManager,
89
      Artifacts: () => Artifacts.test(),
90
      BuildSystem: () => buildSystem,
91
    };
92

93
    setUp(() {
94
      buildSystem = TestBuildSystem.all(BuildResult(success: true));
95
      fakeProcessManager = FakeProcessManager.empty();
96 97
      device = FlutterTesterDevice('flutter-tester',
        fileSystem: fileSystem,
98
        processManager: fakeProcessManager,
99 100 101
        artifacts: Artifacts.test(),
        buildDirectory: 'build',
        logger: BufferLogger.test(),
102
        flutterVersion: FakeFlutterVersion(),
103
        operatingSystemUtils: FakeOperatingSystemUtils(),
104
      );
105 106 107 108
      logLines = <String>[];
      device.getLogReader().logLines.listen(logLines.add);
    });

109
    testWithoutContext('default settings', () async {
110 111 112
      expect(device.id, 'flutter-tester');
      expect(await device.isLocalEmulator, isFalse);
      expect(device.name, 'Flutter test device');
113
      expect(device.portForwarder, isNot(isNull));
114 115 116 117 118 119 120 121 122 123
      expect(await device.targetPlatform, TargetPlatform.tester);

      expect(await device.installApp(null), isTrue);
      expect(await device.isAppInstalled(null), isFalse);
      expect(await device.isLatestBuildInstalled(null), isFalse);
      expect(await device.uninstallApp(null), isTrue);

      expect(device.isSupported(), isTrue);
    });

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    testWithoutContext('does not accept profile, release, or jit-release builds', () async {
      final LaunchResult releaseResult = await device.startApp(null,
        mainPath: mainPath,
        debuggingOptions: DebuggingOptions.disabled(BuildInfo.release),
      );
      final LaunchResult profileResult = await device.startApp(null,
        mainPath: mainPath,
        debuggingOptions: DebuggingOptions.disabled(BuildInfo.profile),
      );
      final LaunchResult jitReleaseResult = await device.startApp(null,
        mainPath: mainPath,
        debuggingOptions: DebuggingOptions.disabled(BuildInfo.jitRelease),
      );

      expect(releaseResult.started, isFalse);
      expect(profileResult.started, isFalse);
      expect(jitReleaseResult.started, isFalse);
    });
142

143 144 145
    testUsingContext('performs a build and starts in debug mode', () async {
      final FlutterTesterApp app = FlutterTesterApp.fromCurrentDirectory(fileSystem);
      final Uri observatoryUri = Uri.parse('http://127.0.0.1:6666/');
146
      final Completer<void> completer = Completer<void>();
147
      fakeProcessManager.addCommand(FakeCommand(
148
        command: const <String>[
149 150 151 152 153
          'Artifact.flutterTester',
          '--run-forever',
          '--non-interactive',
          '--enable-dart-profiling',
          '--packages=.packages',
154 155
          '--flutter-assets-dir=/.tmp_rand0/flutter_tester.rand0',
          '/.tmp_rand0/flutter_tester.rand0/flutter-tester-app.dill',
156
        ],
157
        completer: completer,
158
        stdout:
159
        '''
160 161
Observatory listening on $observatoryUri
Hello!
162 163
''',
      ));
164 165 166

      final LaunchResult result = await device.startApp(app,
        mainPath: mainPath,
167
        debuggingOptions: DebuggingOptions.enabled(BuildInfo.debug),
168 169 170 171 172
      );

      expect(result.started, isTrue);
      expect(result.observatoryUri, observatoryUri);
      expect(logLines.last, 'Hello!');
173
      expect(fakeProcessManager.hasRemainingExpectations, isFalse);
174
    }, overrides: startOverrides);
175 176
  });
}
177

178 179
FlutterTesterDevices setUpFlutterTesterDevices() {
  return FlutterTesterDevices(
180
    logger: BufferLogger.test(),
181 182 183
    artifacts: Artifacts.test(),
    processManager: FakeProcessManager.any(),
    fileSystem: MemoryFileSystem.test(),
184
    config: Config.test(),
185
    flutterVersion: FakeFlutterVersion(),
186
    operatingSystemUtils: FakeOperatingSystemUtils(),
187 188
  );
}