Unverified Commit 602d8baf authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove globals/context for android testing (#57445)

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