linux_device_test.dart 7.03 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
import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/os.dart';
11
import 'package:flutter_tools/src/base/platform.dart';
12
import 'package:flutter_tools/src/build_info.dart';
13
import 'package:flutter_tools/src/device.dart';
14
import 'package:flutter_tools/src/linux/application_package.dart';
15
import 'package:flutter_tools/src/linux/linux_device.dart';
16
import 'package:flutter_tools/src/project.dart';
17 18
import 'package:mockito/mockito.dart';

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

23 24 25 26 27 28
final FakePlatform linux = FakePlatform(
  operatingSystem: 'linux',
);
final FakePlatform windows = FakePlatform(
  operatingSystem: 'windows',
);
29

30
void main() {
31 32

  testWithoutContext('LinuxDevice defaults', () async {
33 34 35
    final LinuxDevice device = LinuxDevice(
      processManager: FakeProcessManager.any(),
      logger: BufferLogger.test(),
36
      fileSystem: MemoryFileSystem.test(),
37
      operatingSystemUtils: FakeOperatingSystemUtils(),
38 39
    );

40
    final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'foo');
41
    expect(await device.targetPlatform, TargetPlatform.linux_x64);
42
    expect(device.name, 'Linux');
43 44 45 46 47 48
    expect(await device.installApp(linuxApp), true);
    expect(await device.uninstallApp(linuxApp), true);
    expect(await device.isLatestBuildInstalled(linuxApp), true);
    expect(await device.isAppInstalled(linuxApp), true);
    expect(await device.stopApp(linuxApp), true);
    expect(device.category, Category.desktop);
49 50 51 52 53

    expect(device.supportsRuntimeMode(BuildMode.debug), true);
    expect(device.supportsRuntimeMode(BuildMode.profile), true);
    expect(device.supportsRuntimeMode(BuildMode.release), true);
    expect(device.supportsRuntimeMode(BuildMode.jitRelease), false);
54
  });
55

56 57 58 59 60 61 62 63 64 65
  testWithoutContext('LinuxDevice on arm64 hosts is arm64', () async {
    final LinuxDevice deviceArm64Host = LinuxDevice(
      processManager: FakeProcessManager.any(),
      logger: BufferLogger.test(),
      fileSystem: MemoryFileSystem.test(),
      operatingSystemUtils: FakeOperatingSystemUtils(hostPlatform: HostPlatform.linux_arm64),
    );
    expect(await deviceArm64Host.targetPlatform, TargetPlatform.linux_arm64);
  });

66
  testWithoutContext('LinuxDevice: no devices listed if platform unsupported', () async {
67
    expect(await LinuxDevices(
68
      fileSystem: MemoryFileSystem.test(),
69
      platform: windows,
70
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
71 72
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
73
      operatingSystemUtils: FakeOperatingSystemUtils(),
74 75 76 77 78
    ).devices, <Device>[]);
  });

  testWithoutContext('LinuxDevice: no devices listed if Linux feature flag disabled', () async {
    expect(await LinuxDevices(
79
      fileSystem: MemoryFileSystem.test(),
80
      platform: linux,
81
      featureFlags: TestFeatureFlags(isLinuxEnabled: false),
82 83
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
84
      operatingSystemUtils: FakeOperatingSystemUtils(),
85
    ).devices, <Device>[]);
86
  });
87

88 89
  testWithoutContext('LinuxDevice: devices', () async {
    expect(await LinuxDevices(
90
      fileSystem: MemoryFileSystem.test(),
91
      platform: linux,
92
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
93 94
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
95
      operatingSystemUtils: FakeOperatingSystemUtils(),
96 97 98 99 100 101
    ).devices, hasLength(1));
  });

  testWithoutContext('LinuxDevice: discoverDevices', () async {
    // Timeout ignored.
    final List<Device> devices = await LinuxDevices(
102
      fileSystem: MemoryFileSystem.test(),
103
      platform: linux,
104
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
105 106
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
107
      operatingSystemUtils: FakeOperatingSystemUtils(),
108 109 110 111
    ).discoverDevices(timeout: const Duration(seconds: 10));
    expect(devices, hasLength(1));
  });

112 113 114 115 116 117
  testWithoutContext('LinuxDevice.isSupportedForProject is true with editable host app', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    fileSystem.directory('linux').createSync();
    final FlutterProject flutterProject = setUpFlutterProject(fileSystem.currentDirectory);
118

119 120 121
    expect(LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
122 123
      fileSystem: fileSystem,
      operatingSystemUtils: FakeOperatingSystemUtils(),
124
    ).isSupportedForProject(flutterProject), true);
125 126
  });

127 128 129 130 131
  testWithoutContext('LinuxDevice.isSupportedForProject is false with no host app', () async {
    final FileSystem fileSystem = MemoryFileSystem.test();
    fileSystem.file('pubspec.yaml').createSync();
    fileSystem.file('.packages').createSync();
    final FlutterProject flutterProject = setUpFlutterProject(fileSystem.currentDirectory);
132

133 134 135
    expect(LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
136 137
      fileSystem: fileSystem,
      operatingSystemUtils: FakeOperatingSystemUtils(),
138
    ).isSupportedForProject(flutterProject), false);
139
  });
140

141
  testWithoutContext('LinuxDevice.executablePathForDevice uses the correct package executable', () async {
142
    final MockLinuxApp mockApp = MockLinuxApp();
143 144 145
    final LinuxDevice device = LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
146
      fileSystem: MemoryFileSystem.test(),
147
      operatingSystemUtils: FakeOperatingSystemUtils(),
148
    );
149 150 151 152 153 154 155
    const String debugPath = 'debug/executable';
    const String profilePath = 'profile/executable';
    const String releasePath = 'release/executable';
    when(mockApp.executable(BuildMode.debug)).thenReturn(debugPath);
    when(mockApp.executable(BuildMode.profile)).thenReturn(profilePath);
    when(mockApp.executable(BuildMode.release)).thenReturn(releasePath);

156 157 158
    expect(device.executablePathForDevice(mockApp, BuildMode.debug), debugPath);
    expect(device.executablePathForDevice(mockApp, BuildMode.profile), profilePath);
    expect(device.executablePathForDevice(mockApp, BuildMode.release), releasePath);
159
  });
160 161
}

162 163 164 165 166 167 168 169
FlutterProject setUpFlutterProject(Directory directory) {
  final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(
    fileSystem: directory.fileSystem,
    logger: BufferLogger.test(),
  );
  return flutterProjectFactory.fromDirectory(directory);
}

170
class MockLinuxApp extends Mock implements LinuxApp {}
171
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
172 173 174 175 176 177
  FakeOperatingSystemUtils({
    HostPlatform hostPlatform = HostPlatform.linux_x64
  })  : _hostPlatform = hostPlatform;

  final HostPlatform _hostPlatform;

178 179
  @override
  String get name => 'Linux';
180 181 182

  @override
  HostPlatform get hostPlatform => _hostPlatform;
183
}