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

17
import '../../src/common.dart';
18
import '../../src/fake_process_manager.dart';
19
import '../../src/fakes.dart';
20

21
final FakePlatform linux = FakePlatform();
22 23 24
final FakePlatform windows = FakePlatform(
  operatingSystem: 'windows',
);
25

26
void main() {
27 28

  testWithoutContext('LinuxDevice defaults', () async {
29 30 31
    final LinuxDevice device = LinuxDevice(
      processManager: FakeProcessManager.any(),
      logger: BufferLogger.test(),
32
      fileSystem: MemoryFileSystem.test(),
33
      operatingSystemUtils: FakeOperatingSystemUtils(),
34 35
    );

36
    final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'foo');
37
    expect(await device.targetPlatform, TargetPlatform.linux_x64);
38
    expect(device.name, 'Linux');
39 40 41 42 43 44
    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);
45 46 47 48 49

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

52 53 54 55 56 57 58 59 60 61
  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);
  });

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

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

84 85
  testWithoutContext('LinuxDevice: devices', () async {
    expect(await LinuxDevices(
86
      fileSystem: MemoryFileSystem.test(),
87
      platform: linux,
88
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
89 90
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
91
      operatingSystemUtils: FakeOperatingSystemUtils(),
92 93 94
    ).devices, hasLength(1));
  });

95 96 97 98 99 100 101 102 103 104 105
  testWithoutContext('LinuxDevice has well known id "linux"', () async {
    expect(LinuxDevices(
      fileSystem: MemoryFileSystem.test(),
      platform: linux,
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
      operatingSystemUtils: FakeOperatingSystemUtils(),
    ).wellKnownIds, <String>['linux']);
  });

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

119 120 121 122 123 124
  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);
125

126 127 128
    expect(LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
129 130
      fileSystem: fileSystem,
      operatingSystemUtils: FakeOperatingSystemUtils(),
131
    ).isSupportedForProject(flutterProject), true);
132 133
  });

134 135 136 137 138
  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);
139

140 141 142
    expect(LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
143 144
      fileSystem: fileSystem,
      operatingSystemUtils: FakeOperatingSystemUtils(),
145
    ).isSupportedForProject(flutterProject), false);
146
  });
147

148
  testWithoutContext('LinuxDevice.executablePathForDevice uses the correct package executable', () async {
149
    final FakeLinuxApp mockApp = FakeLinuxApp();
150 151 152
    final LinuxDevice device = LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
153
      fileSystem: MemoryFileSystem.test(),
154
      operatingSystemUtils: FakeOperatingSystemUtils(),
155
    );
156 157 158 159

    expect(device.executablePathForDevice(mockApp, BuildMode.debug), 'debug/executable');
    expect(device.executablePathForDevice(mockApp, BuildMode.profile), 'profile/executable');
    expect(device.executablePathForDevice(mockApp, BuildMode.release), 'release/executable');
160
  });
161 162
}

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

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
class FakeLinuxApp extends Fake implements LinuxApp {
  @override
  String executable(BuildMode buildMode) {
    switch (buildMode) {
      case BuildMode.debug:
        return 'debug/executable';
      case BuildMode.profile:
        return 'profile/executable';
      case BuildMode.release:
        return 'release/executable';
      default:
        throw StateError('Invalid mode: $buildMode');
    }
  }
}
186
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
187 188 189 190 191 192
  FakeOperatingSystemUtils({
    HostPlatform hostPlatform = HostPlatform.linux_x64
  })  : _hostPlatform = hostPlatform;

  final HostPlatform _hostPlatform;

193 194
  @override
  String get name => 'Linux';
195 196 197

  @override
  HostPlatform get hostPlatform => _hostPlatform;
198
}