windows_device_test.dart 5.51 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/platform.dart';
8
import 'package:flutter_tools/src/build_info.dart';
9
import 'package:flutter_tools/src/device.dart';
10
import 'package:flutter_tools/src/features.dart';
11
import 'package:flutter_tools/src/globals.dart' as globals;
12
import 'package:flutter_tools/src/project.dart';
13
import 'package:flutter_tools/src/windows/application_package.dart';
14 15 16
import 'package:flutter_tools/src/windows/windows_device.dart';
import 'package:mockito/mockito.dart';

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

void main() {
  group(WindowsDevice, () {
    final WindowsDevice device = WindowsDevice();
24

25
    final MockPlatform notWindows = MockPlatform();
26 27 28
    when(notWindows.isWindows).thenReturn(false);
    when(notWindows.environment).thenReturn(const <String, String>{});

29 30 31
    final MockPlatform mockWindowsPlatform = MockPlatform();
    when(mockWindowsPlatform.isWindows).thenReturn(true);

32 33
    testUsingContext('defaults', () async {
      final PrebuiltWindowsApp windowsApp = PrebuiltWindowsApp(executable: 'foo');
34
      expect(await device.targetPlatform, TargetPlatform.windows_x64);
35
      expect(device.name, 'Windows');
36 37 38 39
      expect(await device.installApp(windowsApp), true);
      expect(await device.uninstallApp(windowsApp), true);
      expect(await device.isLatestBuildInstalled(windowsApp), true);
      expect(await device.isAppInstalled(windowsApp), true);
40
      expect(device.category, Category.desktop);
41 42 43 44 45

      expect(device.supportsRuntimeMode(BuildMode.debug), true);
      expect(device.supportsRuntimeMode(BuildMode.profile), true);
      expect(device.supportsRuntimeMode(BuildMode.release), true);
      expect(device.supportsRuntimeMode(BuildMode.jitRelease), false);
46 47 48 49 50 51 52
    });

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

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    testUsingContext('WindowsDevices: devices', () async {
      expect(await WindowsDevices().devices, hasLength(1));
    }, overrides: <Type, Generator>{
      Platform: () => mockWindowsPlatform,
      FeatureFlags: () => TestFeatureFlags(isWindowsEnabled: true),
    });

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

70
    testUsingContext('isSupportedForProject is true with editable host app', () async {
71 72 73
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
      globals.fs.directory('windows').createSync();
74
      globals.fs.file(globals.fs.path.join('windows', 'CMakeLists.txt')).createSync();
75
      final FlutterProject flutterProject = FlutterProject.current();
76 77 78 79

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

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

      expect(WindowsDevice().isSupportedForProject(flutterProject), false);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
91
      ProcessManager: () => FakeProcessManager.any(),
92
    });
93

94
    testUsingContext('isSupportedForProject is false with no build file', () async {
95 96 97 98 99 100 101 102 103 104 105
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
      globals.fs.directory('windows').createSync();
      final FlutterProject flutterProject = FlutterProject.current();

      expect(WindowsDevice().isSupportedForProject(flutterProject), false);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
      ProcessManager: () => FakeProcessManager.any(),
    });

106 107 108 109 110 111 112 113 114 115 116 117
    testUsingContext('executablePathForDevice uses the correct package executable', () async {
      final MockWindowsApp mockApp = MockWindowsApp();
      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(WindowsDevice().executablePathForDevice(mockApp, BuildMode.debug), debugPath);
      expect(WindowsDevice().executablePathForDevice(mockApp, BuildMode.profile), profilePath);
      expect(WindowsDevice().executablePathForDevice(mockApp, BuildMode.release), releasePath);
118
    }, overrides: <Type, Generator>{
119
      FileSystem: () => MemoryFileSystem(),
120
      ProcessManager: () => FakeProcessManager.any(),
121
    });
122 123 124 125
  });
}

class MockPlatform extends Mock implements Platform {}
126

127
class MockWindowsApp extends Mock implements WindowsApp {}