devices_test.dart 3.97 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 'dart:async';
6 7 8
import 'dart:convert';
import 'dart:io';

9
import 'package:flutter_tools/src/android/android_sdk.dart';
10
import 'package:flutter_tools/src/cache.dart';
11
import 'package:flutter_tools/src/commands/devices.dart';
12
import 'package:flutter_tools/src/device.dart';
13 14
import 'package:mockito/mockito.dart';
import 'package:process/process.dart';
15

16 17
import '../../src/common.dart';
import '../../src/context.dart';
18
import '../../src/fake_devices.dart';
19

20
void main() {
21
  group('devices', () {
22 23 24 25
    setUpAll(() {
      Cache.disableLocking();
    });

26
    testUsingContext('returns 0 when called', () async {
27
      final DevicesCommand command = DevicesCommand();
28
      await createTestCommandRunner(command).run(<String>['devices']);
29
    });
30

31
    testUsingContext('no error when no connected devices', () async {
32
      final DevicesCommand command = DevicesCommand();
33
      await createTestCommandRunner(command).run(<String>['devices']);
34
      expect(testLogger.statusText, containsIgnoringWhitespace('No devices detected'));
35 36
    }, overrides: <Type, Generator>{
      AndroidSdk: () => null,
37 38
      DeviceManager: () => DeviceManager(),
      ProcessManager: () => MockProcessManager(),
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

    testUsingContext('Outputs parsable JSON with --machine flag', () async {
      final DevicesCommand command = DevicesCommand();
      await createTestCommandRunner(command).run(<String>['devices', '--machine']);
      expect(
        json.decode(testLogger.statusText),
        <Map<String,Object>>[
          <String, Object>{
            'name': 'ephemeral',
            'id': 'ephemeral',
            'isSupported': true,
            'targetPlatform': 'android-arm',
            'emulator': true,
            'sdk': 'Test SDK (1.2.3)',
            'capabilities': <String, Object>{
              'hotReload': true,
              'hotRestart': true,
              'screenshot': false,
              'fastStart': false,
              'flutterExit': true,
              'hardwareRendering': true,
              'startPaused': true
            }
          },
          <String,Object>{
            'name': 'webby',
            'id': 'webby',
            'isSupported': true,
            'targetPlatform': 'web-javascript',
            'emulator': true,
            'sdk': 'Web SDK (1.2.4)',
            'capabilities': <String, Object>{
              'hotReload': true,
              'hotRestart': true,
              'screenshot': false,
              'fastStart': false,
              'flutterExit': true,
              'hardwareRendering': false,
              'startPaused': true
            }
          }
        ]
      );
    }, overrides: <Type, Generator>{
      DeviceManager: () => _FakeDeviceManager(),
      ProcessManager: () => MockProcessManager(),
    });
87 88
  });
}
89 90

class MockProcessManager extends Mock implements ProcessManager {
91 92
  @override
  Future<ProcessResult> run(
93 94 95 96 97 98 99 100
    List<dynamic> command, {
    String workingDirectory,
    Map<String, String> environment,
    bool includeParentEnvironment = true,
    bool runInShell = false,
    Encoding stdoutEncoding = systemEncoding,
    Encoding stderrEncoding = systemEncoding,
  }) async {
101
    return ProcessResult(0, 0, '', '');
102 103
  }

104 105
  @override
  ProcessResult runSync(
106 107 108 109 110 111 112 113
    List<dynamic> command, {
    String workingDirectory,
    Map<String, String> environment,
    bool includeParentEnvironment = true,
    bool runInShell = false,
    Encoding stdoutEncoding = systemEncoding,
    Encoding stderrEncoding = systemEncoding,
  }) {
114
    return ProcessResult(0, 0, '', '');
115 116
  }
}
117 118 119 120 121 122 123 124 125 126 127 128 129

class _FakeDeviceManager extends DeviceManager {
  _FakeDeviceManager();

  @override
  Future<List<Device>> getAllConnectedDevices() =>
    Future<List<Device>>.value(fakeDevices.map((FakeDeviceJsonData d) => d.dev).toList());

  @override
  Future<List<Device>> refreshAllConnectedDevices({Duration timeout}) =>
    getAllConnectedDevices();

}