linux_device_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
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 16
import 'package:mockito/mockito.dart';

17 18
import '../../src/common.dart';
import '../../src/context.dart';
19
import '../../src/testbed.dart';
20

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

28
void main() {
29 30

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

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

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

54
  testWithoutContext('LinuxDevice: no devices listed if platform unsupported', () async {
55
    expect(await LinuxDevices(
56
      fileSystem: MemoryFileSystem.test(),
57
      platform: windows,
58
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
59 60
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
61
      operatingSystemUtils: FakeOperatingSystemUtils(),
62 63 64 65 66
    ).devices, <Device>[]);
  });

  testWithoutContext('LinuxDevice: no devices listed if Linux feature flag disabled', () async {
    expect(await LinuxDevices(
67
      fileSystem: MemoryFileSystem.test(),
68
      platform: linux,
69
      featureFlags: TestFeatureFlags(isLinuxEnabled: false),
70 71
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
72
      operatingSystemUtils: FakeOperatingSystemUtils(),
73
    ).devices, <Device>[]);
74
  });
75

76 77
  testWithoutContext('LinuxDevice: devices', () async {
    expect(await LinuxDevices(
78
      fileSystem: MemoryFileSystem.test(),
79
      platform: linux,
80
      featureFlags: TestFeatureFlags(isLinuxEnabled: true),
81 82
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
83
      operatingSystemUtils: FakeOperatingSystemUtils(),
84 85 86 87 88 89
    ).devices, hasLength(1));
  });

  testWithoutContext('LinuxDevice: discoverDevices', () async {
    // Timeout ignored.
    final List<Device> devices = 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
    ).discoverDevices(timeout: const Duration(seconds: 10));
    expect(devices, hasLength(1));
  });

100 101 102 103 104 105
  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);
106

107 108 109
    expect(LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
110 111
      fileSystem: fileSystem,
      operatingSystemUtils: FakeOperatingSystemUtils(),
112
    ).isSupportedForProject(flutterProject), true);
113 114
  });

115 116 117 118 119
  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);
120

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

129
  testWithoutContext('LinuxDevice.executablePathForDevice uses the correct package executable', () async {
130
    final MockLinuxApp mockApp = MockLinuxApp();
131 132 133
    final LinuxDevice device = LinuxDevice(
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.any(),
134
      fileSystem: MemoryFileSystem.test(),
135
      operatingSystemUtils: FakeOperatingSystemUtils(),
136
    );
137 138 139 140 141 142 143
    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);

144 145 146
    expect(device.executablePathForDevice(mockApp, BuildMode.debug), debugPath);
    expect(device.executablePathForDevice(mockApp, BuildMode.profile), profilePath);
    expect(device.executablePathForDevice(mockApp, BuildMode.release), releasePath);
147
  });
148 149
}

150 151 152 153 154 155 156 157
FlutterProject setUpFlutterProject(Directory directory) {
  final FlutterProjectFactory flutterProjectFactory = FlutterProjectFactory(
    fileSystem: directory.fileSystem,
    logger: BufferLogger.test(),
  );
  return flutterProjectFactory.fromDirectory(directory);
}

158
class MockLinuxApp extends Mock implements LinuxApp {}
159 160 161 162
class FakeOperatingSystemUtils extends Fake implements OperatingSystemUtils {
  @override
  String get name => 'Linux';
}