Unverified Commit 5e33ecee authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

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

This reverts commit 602d8baf.
parent 602d8baf
...@@ -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,10 +6,8 @@ import 'package:meta/meta.dart'; ...@@ -6,10 +6,8 @@ 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;
...@@ -98,12 +96,6 @@ class AndroidDevices extends PollingDeviceDiscovery { ...@@ -98,12 +96,6 @@ 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')) {
...@@ -162,12 +154,6 @@ class AndroidDevices extends PollingDeviceDiscovery { ...@@ -162,12 +154,6 @@ 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,13 +2,11 @@ ...@@ -2,13 +2,11 @@
// 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';
...@@ -71,15 +69,7 @@ void main() { ...@@ -71,15 +69,7 @@ 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');
...@@ -94,15 +84,7 @@ localhost:36790 device ...@@ -94,15 +84,7 @@ 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');
...@@ -113,15 +95,7 @@ emulator-5612 host features:shell_2 ...@@ -113,15 +95,7 @@ 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,8 +7,6 @@ import 'package:flutter_tools/src/android/android_device.dart'; ...@@ -7,8 +7,6 @@ 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';
...@@ -57,16 +55,11 @@ void main() { ...@@ -57,16 +55,11 @@ void main() {
TargetPlatform.android_arm64, TargetPlatform.android_arm64,
TargetPlatform.android_x64, TargetPlatform.android_x64,
]) { ]) {
testWithoutContext('AndroidDevice.startApp allows release builds on $targetPlatform', () async { testUsingContext('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('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',
...@@ -124,17 +117,16 @@ void main() { ...@@ -124,17 +117,16 @@ 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,
}); });
} }
testWithoutContext('AndroidDevice.startApp does not allow release builds on x86', () async { testUsingContext('AndroidDevice.startApp does not allow release builds on x86', () async {
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel', const String deviceId = '1234';
fileSystem: fileSystem, final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
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',
...@@ -163,16 +155,15 @@ void main() { ...@@ -163,16 +155,15 @@ 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,
}); });
testWithoutContext('AndroidDevice.startApp forwards all supported debugging options', () async { testUsingContext('AndroidDevice.startApp forwards all supported debugging options', () async {
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel', const String deviceId = '1234';
fileSystem: fileSystem, final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
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',
...@@ -273,6 +264,10 @@ void main() { ...@@ -273,6 +264,10 @@ 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,28 +2,14 @@ ...@@ -2,28 +2,14 @@
// 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('1234', final AndroidDevice androidDevice = AndroidDevice('2');
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,6 +11,7 @@ import 'package:flutter_tools/src/android/android_sdk.dart' ...@@ -11,6 +11,7 @@ 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';
...@@ -130,7 +131,7 @@ void main() { ...@@ -130,7 +131,7 @@ void main() {
testUsingContext('succeeds', () async { testUsingContext('succeeds', () async {
final AndroidEmulator emulator = AndroidEmulator(emulatorID); final AndroidEmulator emulator = AndroidEmulator(emulatorID);
expect(getEmulatorPath(mockSdk), mockSdk.emulatorPath); expect(getEmulatorPath(globals.androidSdk), 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,8 +7,7 @@ import 'package:flutter_tools/src/android/android_device.dart'; ...@@ -7,8 +7,7 @@ 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/globals.dart' as globals;
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';
...@@ -29,50 +28,66 @@ const FakeCommand kStoreShaCommand = FakeCommand( ...@@ -29,50 +28,66 @@ const FakeCommand kStoreShaCommand = FakeCommand(
); );
void main() { void main() {
testWithoutContext('Cannot install app on API level below 16', () async { testUsingContext('Cannot install app on API level below 16', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[ 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>[
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]',
), ),
]); ]),
final FileSystem fileSystem = MemoryFileSystem.test(); FileSystem: () => MemoryFileSystem.test(),
final File apk = fileSystem.file('app.apk')..createSync(); AndroidSdk: () => MockAndroidSdk(),
});
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 = setUpAndroidDevice( final AndroidDevice androidDevice = AndroidDevice('1234');
fileSystem: fileSystem,
processManager: processManager,
);
expect(await androidDevice.installApp(androidApk), false); expect(await androidDevice.installApp(androidApk), false);
expect(processManager.hasRemainingExpectations, false); }, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
}); });
testWithoutContext('Cannot install app if APK file is missing', () async { testUsingContext('Can install app on API level 16 or greater', () async {
final FileSystem fileSystem = MemoryFileSystem.test(); final File apk = globals.fs.file('app.apk')..createSync();
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 = setUpAndroidDevice( final AndroidDevice androidDevice = AndroidDevice('1234');
fileSystem: fileSystem, when(globals.androidSdk.adbPath).thenReturn('adb');
); final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
expect(await androidDevice.installApp(androidApk), false); expect(await androidDevice.installApp(androidApk), true);
}); expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
testWithoutContext('Can install app on API level 16 or greater', () async { ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand, kAdbVersionCommand,
kAdbStartServerCommand, kAdbStartServerCommand,
const FakeCommand( const FakeCommand(
...@@ -81,26 +96,27 @@ void main() { ...@@ -81,26 +96,27 @@ void main() {
), ),
kInstallCommand, kInstallCommand,
kStoreShaCommand, kStoreShaCommand,
]); ]),
final FileSystem fileSystem = MemoryFileSystem.test(); FileSystem: () => MemoryFileSystem.test(),
final File apk = fileSystem.file('app.apk')..createSync(); 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 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 = setUpAndroidDevice( final AndroidDevice androidDevice = AndroidDevice('1234');
fileSystem: fileSystem, when(globals.androidSdk.adbPath).thenReturn('adb');
processManager: processManager, final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
);
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(
...@@ -108,39 +124,10 @@ void main() { ...@@ -108,39 +124,10 @@ void main() {
), ),
kInstallCommand, kInstallCommand,
kStoreShaCommand, kStoreShaCommand,
]); ]),
final FileSystem fileSystem = MemoryFileSystem.test(); FileSystem: () => MemoryFileSystem.test(),
final File apk = fileSystem.file('app.apk')..createSync(); AndroidSdk: () => MockAndroidSdk(),
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