macos_device_test.dart 4.92 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 7 8
import 'package:flutter_tools/src/base/context.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
9
import 'package:flutter_tools/src/base/platform.dart';
10 11
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/device.dart';
12
import 'package:flutter_tools/src/features.dart';
13
import 'package:flutter_tools/src/globals.dart' as globals;
14
import 'package:flutter_tools/src/macos/application_package.dart';
15
import 'package:flutter_tools/src/macos/macos_device.dart';
16 17 18
import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
19

20 21
import '../../src/common.dart';
import '../../src/context.dart';
22
import '../../src/testbed.dart';
23 24 25 26

void main() {
  group(MacOSDevice, () {
    final MacOSDevice device = MacOSDevice();
27
    final MockProcessManager mockProcessManager = MockProcessManager();
28 29

    final MockPlatform notMac = MockPlatform();
30 31
    when(notMac.isMacOS).thenReturn(false);
    when(notMac.environment).thenReturn(const <String, String>{});
32 33 34 35

    final MockPlatform mockMacPlatform = MockPlatform();
    when(mockMacPlatform.isMacOS).thenReturn(true);

36 37 38
    when(mockProcessManager.run(any)).thenAnswer((Invocation invocation) async {
      return ProcessResult(0, 1, '', '');
    });
39

40 41
    testUsingContext('defaults', () async {
      final MockMacOSApp mockMacOSApp = MockMacOSApp();
42
      expect(await device.targetPlatform, TargetPlatform.darwin_x64);
43 44 45 46 47
      expect(device.name, 'macOS');
      expect(await device.installApp(mockMacOSApp), true);
      expect(await device.uninstallApp(mockMacOSApp), true);
      expect(await device.isLatestBuildInstalled(mockMacOSApp), true);
      expect(await device.isAppInstalled(mockMacOSApp), true);
48
      expect(device.category, Category.desktop);
49 50 51 52 53 54 55
    });

    testUsingContext('No devices listed if platform unsupported', () async {
      expect(await MacOSDevices().devices, <Device>[]);
    }, overrides: <Type, Generator>{
      Platform: () => notMac,
    });
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    testUsingContext('devices', () async {
      expect(await MacOSDevices().devices, hasLength(1));
    }, overrides: <Type, Generator>{
      Platform: () => mockMacPlatform,
      FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
    });

    testUsingContext('discoverDevices', () async {
      // Timeout ignored.
      final List<Device> devices = await MacOSDevices().discoverDevices(timeout: const Duration(seconds: 10));
      expect(devices, hasLength(1));
    }, overrides: <Type, Generator>{
      Platform: () => mockMacPlatform,
      FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
    });

73
    testUsingContext('isSupportedForProject is true with editable host app', () async {
74 75 76
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
      globals.fs.directory('macos').createSync();
77
      final FlutterProject flutterProject = FlutterProject.current();
78 79 80 81

      expect(MacOSDevice().isSupportedForProject(flutterProject), true);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
82
      ProcessManager: () => FakeProcessManager.any(),
83 84 85
    });

    testUsingContext('isSupportedForProject is false with no host app', () async {
86 87
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
88
      final FlutterProject flutterProject = FlutterProject.current();
89 90 91 92

      expect(MacOSDevice().isSupportedForProject(flutterProject), false);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
93
      ProcessManager: () => FakeProcessManager.any(),
94
    });
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    testUsingContext('executablePathForDevice uses the correct package executable', () async {
      final MockMacOSApp mockApp = MockMacOSApp();
      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);

      expect(MacOSDevice().executablePathForDevice(mockApp, BuildMode.debug), debugPath);
      expect(MacOSDevice().executablePathForDevice(mockApp, BuildMode.profile), profilePath);
      expect(MacOSDevice().executablePathForDevice(mockApp, BuildMode.release), releasePath);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
110
      ProcessManager: () => FakeProcessManager.any(),
111
    });
112 113 114 115
  });
}

class MockPlatform extends Mock implements Platform {}
116

117
class MockMacOSApp extends Mock implements MacOSApp {}
118 119

class MockProcessManager extends Mock implements ProcessManager {}