flutter_tester_test.dart 6.37 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/logger.dart';
13
import 'package:flutter_tools/src/base/platform.dart';
14
import 'package:flutter_tools/src/build_info.dart';
15
import 'package:flutter_tools/src/build_system/build_system.dart';
16 17 18
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/tester/flutter_tester.dart';

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

void main() {
25
  MemoryFileSystem fileSystem;
26 27

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

31 32 33 34
  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;
35

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

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

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

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

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

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

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

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

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

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

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

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

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

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

107
    testWithoutContext('default settings', () async {
108 109 110
      expect(device.id, 'flutter-tester');
      expect(await device.isLocalEmulator, isFalse);
      expect(device.name, 'Flutter test device');
111
      expect(device.portForwarder, isNot(isNull));
112 113 114 115 116 117 118 119 120 121
      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);
    });

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
    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);
    });
140

141 142 143
    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/');
144
      final Completer<void> completer = Completer<void>();
145
      fakeProcessManager.addCommand(FakeCommand(
146
        command: const <String>[
147 148 149 150 151
          'Artifact.flutterTester',
          '--run-forever',
          '--non-interactive',
          '--enable-dart-profiling',
          '--packages=.packages',
152 153
          '--flutter-assets-dir=/.tmp_rand0/flutter_tester.rand0',
          '/.tmp_rand0/flutter_tester.rand0/flutter-tester-app.dill',
154
        ],
155
        completer: completer,
156
        stdout:
157
        '''
158 159
Observatory listening on $observatoryUri
Hello!
160 161
''',
      ));
162 163 164

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

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

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