macos_device_test.dart 4.02 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/context.dart';
7
import 'package:flutter_tools/src/project.dart';
8 9 10 11 12
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';

import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
13 14 15
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/device.dart';
16
import 'package:flutter_tools/src/macos/application_package.dart';
17 18
import 'package:flutter_tools/src/macos/macos_device.dart';

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

void main() {
  group(MacOSDevice, () {
    final MockPlatform notMac = MockPlatform();
    final MacOSDevice device = MacOSDevice();
26
    final MockProcessManager mockProcessManager = MockProcessManager();
27 28
    when(notMac.isMacOS).thenReturn(false);
    when(notMac.environment).thenReturn(const <String, String>{});
29 30 31
    when(mockProcessManager.run(any)).thenAnswer((Invocation invocation) async {
      return ProcessResult(0, 1, '', '');
    });
32

33 34
    testUsingContext('defaults', () async {
      final MockMacOSApp mockMacOSApp = MockMacOSApp();
35
      expect(await device.targetPlatform, TargetPlatform.darwin_x64);
36 37 38 39 40
      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);
41
      expect(device.category, Category.desktop);
42 43 44 45 46 47 48
    });

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

    testUsingContext('isSupportedForProject is true with editable host app', () async {
      fs.file('pubspec.yaml').createSync();
      fs.file('.packages').createSync();
      fs.directory('macos').createSync();
54
      final FlutterProject flutterProject = FlutterProject.current();
55 56 57 58

      expect(MacOSDevice().isSupportedForProject(flutterProject), true);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
59
      ProcessManager: () => FakeProcessManager.any(),
60 61 62 63 64
    });

    testUsingContext('isSupportedForProject is false with no host app', () async {
      fs.file('pubspec.yaml').createSync();
      fs.file('.packages').createSync();
65
      final FlutterProject flutterProject = FlutterProject.current();
66 67 68 69

      expect(MacOSDevice().isSupportedForProject(flutterProject), false);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
70
      ProcessManager: () => FakeProcessManager.any(),
71
    });
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    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(),
87
      ProcessManager: () => FakeProcessManager.any(),
88
    });
89 90 91 92
  });
}

class MockPlatform extends Mock implements Platform {}
93

94
class MockMacOSApp extends Mock implements MacOSApp {}
95 96

class MockProcessManager extends Mock implements ProcessManager {}