android_device_discovery_test.dart 9.08 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter 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 8 9
import 'package:flutter_tools/src/android/android_device_discovery.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/android/android_workflow.dart';
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/base/user_messages.dart';
12
import 'package:flutter_tools/src/device.dart';
13
import 'package:test/fake.dart';
14 15

import '../../src/common.dart';
16
import '../../src/fake_process_manager.dart';
17
import '../../src/fakes.dart';
18 19

void main() {
20
  late AndroidWorkflow androidWorkflow;
21 22 23 24 25 26 27 28

  setUp(() {
    androidWorkflow = AndroidWorkflow(
      androidSdk: FakeAndroidSdk(),
      featureFlags: TestFeatureFlags(),
    );
  });

29
  testWithoutContext('AndroidDevices returns empty device list and diagnostics on null adb', () async {
30
    final AndroidDevices androidDevices = AndroidDevices(
31
      androidSdk: FakeAndroidSdk(null),
32
      logger: BufferLogger.test(),
33
      androidWorkflow: AndroidWorkflow(
34
        androidSdk: FakeAndroidSdk(null),
35
        featureFlags: TestFeatureFlags(),
36
      ),
37
      processManager: FakeProcessManager.empty(),
38 39
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
40
      userMessages: UserMessages(),
41 42 43
    );

    expect(await androidDevices.pollingGetDevices(), isEmpty);
44 45 46
    expect(await androidDevices.getDiagnostics(), isEmpty);
  });

47
  testWithoutContext('AndroidDevices returns empty device list and diagnostics when adb cannot be run', () async {
48
    final FakeProcessManager fakeProcessManager = FakeProcessManager.empty();
49
    fakeProcessManager.excludedExecutables.add('adb');
50
    final AndroidDevices androidDevices = AndroidDevices(
51
      androidSdk: FakeAndroidSdk(),
52 53
      logger: BufferLogger.test(),
      androidWorkflow: AndroidWorkflow(
54
        androidSdk: FakeAndroidSdk(),
55 56
        featureFlags: TestFeatureFlags(),
      ),
57
      processManager: fakeProcessManager,
58 59 60 61 62 63 64
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
      userMessages: UserMessages(),
    );

    expect(await androidDevices.pollingGetDevices(), isEmpty);
    expect(await androidDevices.getDiagnostics(), isEmpty);
65
    expect(fakeProcessManager, hasNoRemainingExpectations);
66 67
  });

68 69 70 71
  testWithoutContext('AndroidDevices returns empty device list and diagnostics on null Android SDK', () async {
    final AndroidDevices androidDevices = AndroidDevices(
      logger: BufferLogger.test(),
      androidWorkflow: AndroidWorkflow(
72
        androidSdk: FakeAndroidSdk(null),
73 74
        featureFlags: TestFeatureFlags(),
      ),
75
      processManager: FakeProcessManager.empty(),
76 77
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
78
      userMessages: UserMessages(),
79 80 81 82 83
    );

    expect(await androidDevices.pollingGetDevices(), isEmpty);
    expect(await androidDevices.getDiagnostics(), isEmpty);
  });
84 85 86 87 88 89

  testWithoutContext('AndroidDevices throwsToolExit on failing adb', () {
    final ProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      const FakeCommand(
        command: <String>['adb', 'devices', '-l'],
        exitCode: 1,
90
      ),
91 92
    ]);
    final AndroidDevices androidDevices = AndroidDevices(
93
      androidSdk: FakeAndroidSdk(),
94
      logger: BufferLogger.test(),
95
      androidWorkflow: androidWorkflow,
96
      processManager: processManager,
97 98
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
99
      userMessages: UserMessages(),
100 101 102 103 104 105
    );

    expect(androidDevices.pollingGetDevices(),
      throwsToolExit(message: RegExp('Unable to run "adb"')));
  });

106 107
  testWithoutContext('AndroidDevices is disabled if feature is disabled', () {
    final AndroidDevices androidDevices = AndroidDevices(
108
      androidSdk: FakeAndroidSdk(),
109 110
      logger: BufferLogger.test(),
      androidWorkflow: AndroidWorkflow(
111
        androidSdk: FakeAndroidSdk(),
112 113 114 115 116
        featureFlags: TestFeatureFlags(
          isAndroidEnabled: false,
        ),
      ),
      processManager: FakeProcessManager.any(),
117 118
      fileSystem: MemoryFileSystem.test(),
      platform: FakePlatform(),
119
      userMessages: UserMessages(),
120 121 122 123 124
    );

    expect(androidDevices.supportsPlatform, false);
  });

125
  testWithoutContext('AndroidDevices can parse output for physical attached devices', () async {
126 127 128 129 130 131 132 133 134
    final AndroidDevices androidDevices = AndroidDevices(
      userMessages: UserMessages(),
      androidWorkflow: androidWorkflow,
      androidSdk: FakeAndroidSdk(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['adb', 'devices', '-l'],
          stdout: '''
135 136 137
List of devices attached
05a02bac               device usb:336592896X product:razor model:Nexus_7 device:flo

138
  ''',
139
        ),
140 141 142 143 144 145
      ]),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
    );

    final List<Device> devices = await androidDevices.pollingGetDevices();
146 147 148 149

    expect(devices, hasLength(1));
    expect(devices.first.name, 'Nexus 7');
    expect(devices.first.category, Category.mobile);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    expect(devices.first.connectionInterface, DeviceConnectionInterface.attached);
  });

  testWithoutContext('AndroidDevices can parse output for physical wireless devices', () async {
    final AndroidDevices androidDevices = AndroidDevices(
      userMessages: UserMessages(),
      androidWorkflow: androidWorkflow,
      androidSdk: FakeAndroidSdk(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['adb', 'devices', '-l'],
          stdout: '''
List of devices attached
05a02bac._adb-tls-connect._tcp.               device product:razor model:Nexus_7 device:flo

  ''',
        ),
      ]),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
    );

    final List<Device> devices = await androidDevices.pollingGetDevices();

    expect(devices, hasLength(1));
    expect(devices.first.name, 'Nexus 7');
    expect(devices.first.category, Category.mobile);
    expect(devices.first.connectionInterface, DeviceConnectionInterface.wireless);
179 180
  });

181 182 183 184 185 186 187 188 189 190
  testWithoutContext('AndroidDevices can parse output for emulators and short listings', () async {
    final AndroidDevices androidDevices = AndroidDevices(
      userMessages: UserMessages(),
      androidWorkflow: androidWorkflow,
      androidSdk: FakeAndroidSdk(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['adb', 'devices', '-l'],
          stdout: '''
191 192 193 194 195
List of devices attached
localhost:36790        device
0149947A0D01500C       device usb:340787200X
emulator-5612          host features:shell_2

196
  ''',
197
        ),
198 199 200 201 202 203
      ]),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
    );

    final List<Device> devices = await androidDevices.pollingGetDevices();
204 205

    expect(devices, hasLength(3));
206 207 208
    expect(devices[0].name, 'localhost:36790');
    expect(devices[1].name, '0149947A0D01500C');
    expect(devices[2].name, 'emulator-5612');
209 210
  });

211 212 213 214 215 216 217 218 219 220
  testWithoutContext('AndroidDevices can parse output from android n', () async {
    final AndroidDevices androidDevices = AndroidDevices(
      userMessages: UserMessages(),
      androidWorkflow: androidWorkflow,
      androidSdk: FakeAndroidSdk(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['adb', 'devices', '-l'],
          stdout: '''
221 222
List of devices attached
ZX1G22JJWR             device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2
223

224
''',
225
        ),
226 227 228 229 230 231
      ]),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
    );

    final List<Device> devices = await androidDevices.pollingGetDevices();
232 233 234 235 236

    expect(devices, hasLength(1));
    expect(devices.first.name, 'Nexus 6');
  });

237 238 239 240 241 242 243 244 245 246
  testWithoutContext('AndroidDevices provides adb error message as diagnostics', () async {
    final AndroidDevices androidDevices = AndroidDevices(
      userMessages: UserMessages(),
      androidWorkflow: androidWorkflow,
      androidSdk: FakeAndroidSdk(),
      logger: BufferLogger.test(),
      processManager: FakeProcessManager.list(<FakeCommand>[
        const FakeCommand(
          command: <String>['adb', 'devices', '-l'],
          stdout: '''
247 248 249
It appears you do not have 'Android SDK Platform-tools' installed.
Use the 'android' tool to install them:
  android update sdk --no-ui --filter 'platform-tools'
250
''',
251
        ),
252 253 254
      ]),
      platform: FakePlatform(),
      fileSystem: MemoryFileSystem.test(),
255
    );
256

257 258
    final List<String> diagnostics = await androidDevices.getDiagnostics();

259 260 261 262 263
    expect(diagnostics, hasLength(1));
    expect(diagnostics.first, contains('you do not have'));
  });
}

264 265
class FakeAndroidSdk extends Fake implements AndroidSdk {
  FakeAndroidSdk([this.adbPath = 'adb']);
266 267

  @override
268
  final String? adbPath;
269
}