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 @@
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.
///
/// 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);
final AndroidConsoleSocketFactory _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,10 +6,8 @@ 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;
......@@ -98,12 +96,6 @@ class AndroidDevices extends PollingDeviceDiscovery {
String text, {
List<AndroidDevice> devices,
List<String> diagnostics,
AndroidSdk androidSdk,
FileSystem fileSystem,
Logger logger,
Platform platform,
ProcessManager processManager,
TimeoutConfiguration timeoutConfiguration,
}) {
// Check for error messages from adb
if (!text.contains('List of devices')) {
......@@ -162,12 +154,6 @@ 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,13 +2,11 @@
// 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';
......@@ -71,15 +69,7 @@ void main() {
List of devices attached
05a02bac device usb:336592896X product:razor model:Nexus_7 device:flo
''',
devices: devices,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
''', devices: devices);
expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 7');
......@@ -94,15 +84,7 @@ localhost:36790 device
0149947A0D01500C device usb:340787200X
emulator-5612 host features:shell_2
''',
devices: devices,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
''', devices: devices);
expect(devices, hasLength(3));
expect(devices.first.name, 'localhost:36790');
......@@ -113,15 +95,7 @@ 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,
androidSdk: MockAndroidSdk(),
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
timeoutConfiguration: const TimeoutConfiguration(),
platform: FakePlatform(),
fileSystem: MemoryFileSystem.test(),
);
''', devices: devices);
expect(devices, hasLength(1));
expect(devices.first.name, 'Nexus 6');
......
......@@ -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/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';
......@@ -57,16 +55,11 @@ void main() {
TargetPlatform.android_arm64,
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(
getAndroidArchForName(getNameForTargetPlatform(targetPlatform)));
final AndroidDevice device = AndroidDevice('1234', modelID: 'TestModel',
fileSystem: fileSystem,
processManager: processManager,
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
androidSdk: androidSdk,
);
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -124,17 +117,16 @@ void main() {
expect(launchResult.started, true);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
AndroidSdk: () => androidSdk,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
});
}
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,
);
testUsingContext('AndroidDevice.startApp does not allow release builds on x86', () async {
const String deviceId = '1234';
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -163,16 +155,15 @@ void main() {
expect(launchResult.started, false);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
AndroidSdk: () => androidSdk,
FileSystem: () => fileSystem,
ProcessManager: () => processManager,
});
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,
);
testUsingContext('AndroidDevice.startApp forwards all supported debugging options', () async {
const String deviceId = '1234';
final AndroidDevice device = AndroidDevice(deviceId, modelID: 'TestModel');
final File apkFile = fileSystem.file('app.apk')..createSync();
final AndroidApk apk = AndroidApk(
id: 'FlutterApp',
......@@ -273,6 +264,10 @@ 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,28 +2,14 @@
// 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('1234',
androidSdk: MockAndroidSdk(),
fileSystem: MemoryFileSystem.test(),
logger: BufferLogger.test(),
platform: FakePlatform(operatingSystem: 'linux'),
processManager: FakeProcessManager.any(),
);
final AndroidDevice androidDevice = AndroidDevice('2');
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'
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';
......@@ -130,7 +131,7 @@ void main() {
testUsingContext('succeeds', () async {
final AndroidEmulator emulator = AndroidEmulator(emulatorID);
expect(getEmulatorPath(mockSdk), mockSdk.emulatorPath);
expect(getEmulatorPath(globals.androidSdk), mockSdk.emulatorPath);
final Completer<void> completer = Completer<void>();
FakeAsync().run((FakeAsync time) {
unawaited(emulator.launch().whenComplete(completer.complete));
......
......@@ -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/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/globals.dart' as globals;
import 'package:mockito/mockito.dart';
import '../../src/common.dart';
......@@ -29,50 +28,66 @@ const FakeCommand kStoreShaCommand = FakeCommand(
);
void main() {
testWithoutContext('Cannot install app on API level below 16', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
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>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
command: <String>['adb', '-s', '1234', 'shell', 'getprop'],
stdout: '[ro.build.version.sdk]: [11]',
),
]);
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk')..createSync();
]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
});
testUsingContext('Cannot install app if APK file is missing', () async {
final File apk = globals.fs.file('app.apk');
final AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
processManager: processManager,
);
final AndroidDevice androidDevice = AndroidDevice('1234');
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 {
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk');
testUsingContext('Can install app on API level 16 or greater', () 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 = setUpAndroidDevice(
fileSystem: fileSystem,
);
final AndroidDevice androidDevice = AndroidDevice('1234');
when(globals.androidSdk.adbPath).thenReturn('adb');
final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
expect(await androidDevice.installApp(androidApk), false);
});
testWithoutContext('Can install app on API level 16 or greater', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
expect(await androidDevice.installApp(androidApk), true);
expect(processManager.hasRemainingExpectations, false);
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
......@@ -81,26 +96,27 @@ void main() {
),
kInstallCommand,
kStoreShaCommand,
]);
final FileSystem fileSystem = MemoryFileSystem.test();
final File apk = fileSystem.file('app.apk')..createSync();
]),
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 AndroidApk androidApk = AndroidApk(
file: apk,
id: 'app',
versionCode: 22,
launchActivity: 'Main',
);
final AndroidDevice androidDevice = setUpAndroidDevice(
fileSystem: fileSystem,
processManager: processManager,
);
final AndroidDevice androidDevice = AndroidDevice('1234');
when(globals.androidSdk.adbPath).thenReturn('adb');
final FakeProcessManager processManager = globals.processManager as FakeProcessManager;
expect(await androidDevice.installApp(androidApk), true);
expect(processManager.hasRemainingExpectations, false);
});
testWithoutContext('Defaults to API level 16 if adb returns a null response', () async {
final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
}, overrides: <Type, Generator>{
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
kAdbVersionCommand,
kAdbStartServerCommand,
const FakeCommand(
......@@ -108,39 +124,10 @@ void main() {
),
kInstallCommand,
kStoreShaCommand,
]);
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);
]),
FileSystem: () => MemoryFileSystem.test(),
AndroidSdk: () => MockAndroidSdk(),
});
}
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