desktop_device_test.dart 7.25 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';

import 'package:flutter_tools/src/application_package.dart';
9
import 'package:flutter_tools/src/base/context.dart';
10 11 12 13 14
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/desktop_device.dart';
import 'package:flutter_tools/src/device.dart';
15
import 'package:flutter_tools/src/globals.dart' as globals;
16 17 18
import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

import '../src/common.dart';
import '../src/context.dart';
import '../src/mocks.dart';

/// A trivial subclass of DesktopDevice for testing the shared functionality.
class FakeDesktopDevice extends DesktopDevice {
  FakeDesktopDevice() : super(
      'dummy',
      platformType: PlatformType.linux,
      ephemeral: false,
  );

  /// The [mainPath] last passed to [buildForDevice].
  String lastBuiltMainPath;

  /// The [buildInfo] last passed to [buildForDevice].
  BuildInfo lastBuildInfo;

  @override
  String get name => 'dummy';

  @override
  Future<TargetPlatform> get targetPlatform async => TargetPlatform.tester;

  @override
  bool isSupported() => true;

  @override
  bool isSupportedForProject(FlutterProject flutterProject) => true;

  @override
  Future<void> buildForDevice(
    ApplicationPackage package, {
    String mainPath,
    BuildInfo buildInfo,
  }) async {
    lastBuiltMainPath = mainPath;
    lastBuildInfo = buildInfo;
  }

  // Dummy implementation that just returns the build mode name.
  @override
  String executablePathForDevice(ApplicationPackage package, BuildMode buildMode) {
    return buildMode == null ? 'null' : getNameForBuildMode(buildMode);
  }
}

/// A desktop device that returns a null executable path, for failure testing.
class NullExecutableDesktopDevice extends FakeDesktopDevice {
  @override
  String executablePathForDevice(ApplicationPackage package, BuildMode buildMode) {
    return null;
  }
}

class MockAppplicationPackage extends Mock implements ApplicationPackage {}

class MockFileSystem extends Mock implements FileSystem {}

class MockFile extends Mock implements File {}

class MockProcessManager extends Mock implements ProcessManager {}

void main() {
  group('Basic info', () {
    test('Category is desktop', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      expect(device.category, Category.desktop);
    });

    test('Not an emulator', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      expect(await device.isLocalEmulator, false);
      expect(await device.emulatorId, null);
    });

    testUsingContext('Uses OS name as SDK name', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
98
      expect(await device.sdkNameAndVersion, globals.os.name);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    });
  });

  group('Install', () {
    test('Install checks always return true', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      expect(await device.isAppInstalled(null), true);
      expect(await device.isLatestBuildInstalled(null), true);
      expect(device.category, Category.desktop);
    });

    test('Install and uninstall are no-ops that report success', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      final MockAppplicationPackage package = MockAppplicationPackage();
      expect(await device.uninstallApp(package), true);
      expect(await device.isAppInstalled(package), true);
      expect(await device.isLatestBuildInstalled(package), true);

      expect(await device.installApp(package), true);
      expect(await device.isAppInstalled(package), true);
      expect(await device.isLatestBuildInstalled(package), true);
      expect(device.category, Category.desktop);
    });
  });

  group('Starting and stopping application', () {
    final MockFileSystem mockFileSystem = MockFileSystem();
    final MockProcessManager mockProcessManager = MockProcessManager();

    // Configures mock environment so that startApp will be able to find and
    // run an FakeDesktopDevice exectuable with for the given mode.
    void setUpMockExecutable(FakeDesktopDevice device, BuildMode mode, {Future<int> exitFuture}) {
      final String executableName = device.executablePathForDevice(null, mode);
      final MockFile mockFile = MockFile();
      when(mockFileSystem.file(executableName)).thenReturn(mockFile);
      when(mockFile.existsSync()).thenReturn(true);
      when(mockProcessManager.start(<String>[executableName])).thenAnswer((Invocation invocation) async {
        return FakeProcess(
          exitCode: Completer<int>().future,
          stdout: Stream<List<int>>.fromIterable(<List<int>>[
            utf8.encode('Observatory listening on http://127.0.0.1/0\n'),
          ]),
          stderr: const Stream<List<int>>.empty(),
        );
      });
      when(mockProcessManager.run(any)).thenAnswer((Invocation invocation) async {
        return ProcessResult(0, 1, '', '');
      });
    }

    test('Stop without start is a successful no-op', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
    final MockAppplicationPackage package = MockAppplicationPackage();
      expect(await device.stopApp(package), true);
    });

    testUsingContext('Can run from prebuilt application', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      final MockAppplicationPackage package = MockAppplicationPackage();
      setUpMockExecutable(device, null);
      final LaunchResult result = await device.startApp(package, prebuiltApplication: true);
      expect(result.started, true);
      expect(result.observatoryUri, Uri.parse('http://127.0.0.1/0'));
    }, overrides: <Type, Generator>{
      FileSystem: () => mockFileSystem,
      ProcessManager: () => mockProcessManager,
    });

    testUsingContext('Null executable path fails gracefully', () async {
      final NullExecutableDesktopDevice device = NullExecutableDesktopDevice();
      final MockAppplicationPackage package = MockAppplicationPackage();
      final LaunchResult result = await device.startApp(package, prebuiltApplication: true);
      expect(result.started, false);
172
      expect(testLogger.errorText, contains('Unable to find executable to run'));
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    });

    testUsingContext('stopApp kills process started by startApp', () async {
      final FakeDesktopDevice device = FakeDesktopDevice();
      final MockAppplicationPackage package = MockAppplicationPackage();
      setUpMockExecutable(device, null);
      final LaunchResult result = await device.startApp(package, prebuiltApplication: true);
      expect(result.started, true);
      expect(await device.stopApp(package), true);
    }, overrides: <Type, Generator>{
      FileSystem: () => mockFileSystem,
      ProcessManager: () => mockProcessManager,
    });
  });

  test('Port forwarder is a no-op', () async {
    final FakeDesktopDevice device = FakeDesktopDevice();
    final DevicePortForwarder portForwarder = device.portForwarder;
    final int result = await portForwarder.forward(2);
    expect(result, 2);
    expect(portForwarder.forwardedPorts.isEmpty, true);
  });
}