linux_device_test.dart 4.35 KB
Newer Older
1 2 3 4
// Copyright 2018 The Chromium Authors. All rights reserved.
// 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
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
8 9
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
10
import 'package:flutter_tools/src/linux/application_package.dart';
11 12
import 'package:flutter_tools/src/linux/linux_device.dart';
import 'package:flutter_tools/src/device.dart';
13
import 'package:flutter_tools/src/project.dart';
14
import 'package:mockito/mockito.dart';
15
import 'package:process/process.dart';
16

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

void main() {
  group(LinuxDevice, () {
    final LinuxDevice device = LinuxDevice();
    final MockPlatform notLinux = MockPlatform();
24
    final MockProcessManager mockProcessManager = MockProcessManager();
25
    const String flutterToolCommand = 'flutter --someoption somevalue';
26

27
    when(notLinux.isLinux).thenReturn(false);
28 29 30
    when(mockProcessManager.run(<String>[
      'ps', 'aux',
    ])).thenAnswer((Invocation invocation) async {
31
      // The flutter tool process is returned as output to the ps aux command
32 33
      final MockProcessResult result = MockProcessResult();
      when(result.exitCode).thenReturn(0);
34
      when<String>(result.stdout).thenReturn('username  $pid  $flutterToolCommand');
35 36
      return result;
    });
37 38 39
    when(mockProcessManager.run(<String>[
      'kill', '$pid',
    ])).thenThrow(Exception('Flutter tool process has been killed'));
40

41 42
    testUsingContext('defaults', () async {
      final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'foo');
43 44
      expect(await device.targetPlatform, TargetPlatform.linux_x64);
      expect(device.name, 'Linux');
45 46 47 48 49
      expect(await device.installApp(linuxApp), true);
      expect(await device.uninstallApp(linuxApp), true);
      expect(await device.isLatestBuildInstalled(linuxApp), true);
      expect(await device.isAppInstalled(linuxApp), true);
      expect(await device.stopApp(linuxApp), true);
50
      expect(device.category, Category.desktop);
51 52
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
53 54 55 56 57 58 59 60 61 62
    });

    test('noop port forwarding', () async {
      final LinuxDevice device = LinuxDevice();
      final DevicePortForwarder portForwarder = device.portForwarder;
      final int result = await portForwarder.forward(2);
      expect(result, 2);
      expect(portForwarder.forwardedPorts.isEmpty, true);
    });

63 64 65 66 67 68 69 70
    testUsingContext('The current running process is not killed when stopping the app', () async {
      // The name of the executable is the same as a command line argument to the flutter tool
      final PrebuiltLinuxApp linuxApp = PrebuiltLinuxApp(executable: 'somevalue');
      expect(await device.stopApp(linuxApp), true);
    }, overrides: <Type, Generator>{
      ProcessManager: () => mockProcessManager,
    });

71 72 73 74 75 76
    testUsingContext('No devices listed if platform unsupported', () async {
      expect(await LinuxDevices().devices, <Device>[]);
    }, overrides: <Type, Generator>{
      Platform: () => notLinux,
    });
  });
77 78 79 80 81

  testUsingContext('LinuxDevice.isSupportedForProject is true with editable host app', () async {
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
    fs.directory('linux').createSync();
82
    final FlutterProject flutterProject = FlutterProject.current();
83 84 85 86 87 88 89 90 91

    expect(LinuxDevice().isSupportedForProject(flutterProject), true);
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
  });

  testUsingContext('LinuxDevice.isSupportedForProject is false with no host app', () async {
    fs.file('pubspec.yaml').createSync();
    fs.file('.packages').createSync();
92
    final FlutterProject flutterProject = FlutterProject.current();
93 94 95 96 97

    expect(LinuxDevice().isSupportedForProject(flutterProject), false);
  }, overrides: <Type, Generator>{
    FileSystem: () => MemoryFileSystem(),
  });
98 99 100
}

class MockPlatform extends Mock implements Platform {}
101 102 103 104 105 106 107 108 109

class MockFileSystem extends Mock implements FileSystem {}

class MockFile extends Mock implements File {}

class MockProcessManager extends Mock implements ProcessManager {}

class MockProcess extends Mock implements Process {}

Dan Field's avatar
Dan Field committed
110
class MockProcessResult extends Mock implements ProcessResult {}