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

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 35
      expect(await device.targetPlatform, TargetPlatform.windows_x64);
      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 46 47
    });

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

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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),
    });

65
    testUsingContext('isSupportedForProject is true with editable host app', () async {
66 67 68
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
      globals.fs.directory('windows').createSync();
69
      final FlutterProject flutterProject = FlutterProject.current();
70 71 72 73

      expect(WindowsDevice().isSupportedForProject(flutterProject), true);
    }, overrides: <Type, Generator>{
      FileSystem: () => MemoryFileSystem(),
74
      ProcessManager: () => FakeProcessManager.any(),
75 76 77
    });

    testUsingContext('isSupportedForProject is false with no host app', () async {
78 79
      globals.fs.file('pubspec.yaml').createSync();
      globals.fs.file('.packages').createSync();
80
      final FlutterProject flutterProject = FlutterProject.current();
81 82 83 84

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

88 89 90 91 92 93 94 95 96 97 98 99
    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);
100
    }, overrides: <Type, Generator>{
101
      FileSystem: () => MemoryFileSystem(),
102
      ProcessManager: () => FakeProcessManager.any(),
103
    });
104 105 106 107
  });
}

class MockPlatform extends Mock implements Platform {}
108

109
class MockWindowsApp extends Mock implements WindowsApp {}