Unverified Commit 359daf4f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] reland: remove globals from android device/testing (#57614)

parent 478d4c9c
......@@ -5,19 +5,19 @@
import 'dart:async';
import 'package:async/async.dart';
import '../base/context.dart';
import '../base/io.dart';
import '../convert.dart';
/// Default factory that creates a real Android console connection.
final AndroidConsoleSocketFactory _kAndroidConsoleSocketFactory = (String host, int port) => Socket.connect( host, port);
///
/// The default implementation will create real connections to a device.
/// Override this in tests with an implementation that returns mock responses.
Future<Socket> kAndroidConsoleSocketFactory(String host, int port) => Socket.connect(host, port);
/// Currently active implementation of the AndroidConsoleFactory.
///
/// The default implementation will create real connections to a device.
/// Override this in tests with an implementation that returns mock responses.
AndroidConsoleSocketFactory get androidConsoleSocketFactory => context.get<AndroidConsoleSocketFactory>() ?? _kAndroidConsoleSocketFactory;
typedef AndroidConsoleSocketFactory = Future<Socket> Function(String host, int port);
/// Creates a console connection to an Android emulator that can be used to run
......
......@@ -6,8 +6,10 @@ import 'package:meta/meta.dart';
import 'package:process/process.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/io.dart';
import '../base/logger.dart';
import '../base/platform.dart';
import '../base/process.dart';
import '../device.dart';
import '../globals.dart' as globals;
......@@ -63,7 +65,11 @@ class AndroidDevices extends PollingDeviceDiscovery {
'ANDROID_HOME environment variable: ${exception.executable}');
}
final List<AndroidDevice> devices = <AndroidDevice>[];
parseADBDeviceOutput(text, devices: devices);
parseADBDeviceOutput(
text,
devices: devices,
timeoutConfiguration: timeoutConfiguration,
);
return devices;
}
......@@ -80,7 +86,11 @@ class AndroidDevices extends PollingDeviceDiscovery {
} else {
final String text = result.stdout;
final List<String> diagnostics = <String>[];
parseADBDeviceOutput(text, diagnostics: diagnostics);
parseADBDeviceOutput(
text,
diagnostics: diagnostics,
timeoutConfiguration: timeoutConfiguration,
);
return diagnostics;
}
}
......@@ -96,6 +106,12 @@ class AndroidDevices extends PollingDeviceDiscovery {
String text, {
List<AndroidDevice> devices,
List<String> diagnostics,
AndroidSdk androidSdk,
FileSystem fileSystem,
Logger logger,
Platform platform,
ProcessManager processManager,
@required TimeoutConfiguration timeoutConfiguration,
}) {
// Check for error messages from adb
if (!text.contains('List of devices')) {
......@@ -154,6 +170,12 @@ class AndroidDevices extends PollingDeviceDiscovery {
productID: info['product'],
modelID: info['model'] ?? deviceID,
deviceCodeName: info['device'],
androidSdk: androidSdk ?? globals.androidSdk,
fileSystem: fileSystem ?? globals.fs,
logger: logger ?? globals.logger,
platform: platform ?? globals.platform,
processManager: processManager ?? globals.processManager,
timeoutConfiguration: timeoutConfiguration,
));
}
} else {
......
......@@ -2,11 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_device.dart';
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';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:mockito/mockito.dart';
......@@ -69,7 +71,15 @@ void main() {
List of devices attached
05a02bac device usb:336592896X product:razor model:Nexus_7 device:flo
''', devices: devices);
''',
devices: devices,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 7');
......@@ -84,7 +94,15 @@ localhost:36790 device
0149947A0D01500C device usb:340787200X
emulator-5612 host features:shell_2
''', devices: devices);
''',
devices: devices,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
expect(devices, hasLength(3));
expect(devices.first.name, 'localhost:36790');
......@@ -95,7 +113,15 @@ emulator-5612 host features:shell_2
AndroidDevices.parseADBDeviceOutput('''
List of devices attached
ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu features:cmd,shell_v2
''', devices: devices);
''',
devices: devices,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 6');
......@@ -108,7 +134,10 @@ ZX1G22JJWR device usb:3-3 product:shamu model:Nexus_6 device:shamu f
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'
''', devices: devices, diagnostics: diagnostics);
''', devices: devices,
diagnostics: diagnostics,
timeoutConfiguration: const TimeoutConfiguration(),
);
expect(devices, isEmpty);
expect(diagnostics, hasLength(1));
......
......@@ -7,6 +7,8 @@ import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/application_package.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:mockito/mockito.dart';
......@@ -55,11 +57,16 @@ void main() {
TargetPlatform.android_arm64,
TargetPlatform.android_x64,
]) {
testUsingContext('AndroidDevice.startApp allows release builds on $targetPlatform', () async {
const String deviceId = '1234';
testWithoutContext('AndroidDevice.startApp allows release builds on $targetPlatform', () async {
final String arch = getNameForAndroidArch(
getAndroidArchForName(getNameForTargetPlatform(targetPlatform)));
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel',
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
androidSdk: androidSdk,
);
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -117,16 +124,17 @@ void main() {
expect(launchResult.started, true);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
AndroidSdk: () => androidSdk,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
});
}
testUsingContext('AndroidDevice.startApp does not allow release builds on x86', () async {
const String deviceId = '1234';
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
testWithoutContext('AndroidDevice.startApp does not allow release builds on x86', () async {
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel',
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
androidSdk: androidSdk,
);
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -155,15 +163,16 @@ void main() {
expect(launchResult.started, false);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
AndroidSdk: () => androidSdk,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
});
testUsingContext('AndroidDevice.startApp forwards all supported debugging options', () async {
const String deviceId = '1234';
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
testWithoutContext('AndroidDevice.startApp forwards all supported debugging options', () async {
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel',
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
androidSdk: androidSdk,
);
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -264,10 +273,6 @@ void main() {
// This fails to start due to observatory discovery issues.
expect(launchResult.started, false);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
AndroidSdk: () => androidSdk,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
});
}
......
......@@ -2,14 +2,28 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:mockito/mockito.dart';
import '../../src/common.dart';
import '../../src/context.dart';
void main() {
testWithoutContext('AndroidDevice.stopApp handles a null ApplicationPackage', () async {
final AndroidDevice androidDevice = AndroidDevice('2');
final AndroidDevice androidDevice = AndroidDevice('1234',
androidSdk: MockAndroidSdk(),
fileSystem: MemoryFileSystem.test(),
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
processManager: FakeProcessManager.any(),
);
expect(await androidDevice.stopApp(null), false);
});
}
class MockAndroidSdk extends Mock implements AndroidSdk {}
......@@ -11,7 +11,6 @@ import 'package:flutter_tools/src/android/android_sdk.dart'
import 'package:flutter_tools/src/android/android_emulator.dart';
import 'package:flutter_tools/src/base/common.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:mockito/mockito.dart';
import 'package:quiver/testing/async.dart';
......@@ -131,7 +130,7 @@ void main() {
testUsingContext('succeeds', () async {
final AndroidEmulator emulator = AndroidEmulator(emulatorID);
expect(getEmulatorPath(globals.androidSdk), mockSdk.emulatorPath);
expect(getEmulatorPath(mockSdk), mockSdk.emulatorPath);
final Completer<void> completer = Completer<void>();
FakeAsync().run((FakeAsync time) {
unawaited(emulator.launch().whenComplete(completer.complete));
......
......@@ -7,7 +7,8 @@ import 'package:flutter_tools/src/android/android_device.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/application_package.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:mockito/mockito.dart';
import '../../src/common.dart';
......@@ -28,66 +29,50 @@ const FakeCommand kStoreShaCommand = FakeCommand(
);
void main() {
testUsingContext('Cannot install app on API level below 16', () async {
final File apk = globals.fs.file('app.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = AndroidDevice('1234');
when(globals.androidSdk.adbPath).thenReturn('adb');
final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
expect(await androidDevice.installApp(androidApk), false);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
testWithoutContext('Cannot install app on API level below 16', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
stdout: '[ro.build.version.sdk]: [11]',
),
]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
});
testUsingContext('Cannot install app if APK file is missing', () async {
final File apk = globals.fs.file('app.apk');
]);
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = AndroidDevice('1234');
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
processManager: processManager,
);
expect(await androidDevice.installApp(androidApk), false);
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
expect(processManager.hasRemainingExpectations, false);
});
testUsingContext('Can install app on API level 16 or greater', () async {
final File apk = globals.fs.file('app.apk')..createSync();
testWithoutContext('Cannot install app if APK file is missing', () async {
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk');
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = AndroidDevice('1234');
when(globals.androidSdk.adbPath).thenReturn('adb');
final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
);
expect(await androidDevice.installApp(androidApk), true);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
expect(await androidDevice.installApp(androidApk), false);
});
testWithoutContext('Can install app on API level 16 or greater', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
......@@ -96,27 +81,26 @@ void main() {
),
kInstallCommand,
kStoreShaCommand,
]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
});
testUsingContext('Defaults to API level 16 if adb returns a null response', () async {
final File apk = globals.fs.file('app.apk')..createSync();
]);
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = AndroidDevice('1234');
when(globals.androidSdk.adbPath).thenReturn('adb');
final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
processManager: processManager,
);
expect(await androidDevice.installApp(androidApk), true);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
});
testWithoutContext('Defaults to API level 16 if adb returns a null response', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
......@@ -124,10 +108,39 @@ void main() {
),
kInstallCommand,
kStoreShaCommand,
]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
]);
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk')..createSync();
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
processManager: processManager,
);
expect(await androidDevice.installApp(androidApk), true);
expect(processManager.hasRemainingExpectations, false);
});
}
AndroidDevice setUpAndroidDevice({
AndroidSdk androidSdk,
FileSystem fileSystem,
ProcessManager processManager,
}) {
androidSdk ??= MockAndroidSdk();
when(androidSdk.adbPath).thenReturn('adb');
return AndroidDevice('1234',
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
androidSdk: androidSdk,
fileSystem: fileSystem ?? MemoryFileSystem.test(),
processManager: processManager ?? FakeProcessManager.any(),
);
}
class MockAndroidSdk extends Mock implements AndroidSdk {}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment