Revert "Migrate core devicelab framework to null safety. (#85993)" (#86269)

This reverts commit 2175e64e4f.
This commit is contained in:
Darren Austin
2021-07-11 23:25:25 -07:00
committed by GitHub
parent 28cb43e601
commit 08ef7752d5
28 changed files with 463 additions and 404 deletions

View File

@@ -2,12 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart = 2.8
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math' as math;
import 'package:flutter_devicelab/common.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'utils.dart';
@@ -34,8 +37,8 @@ String getArtifactPath() {
}
/// Return the item is in idList if find a match, otherwise return null
String? _findMatchId(List<String> idList, String idPattern) {
String? candidate;
String _findMatchId(List<String> idList, String idPattern) {
String candidate;
idPattern = idPattern.toLowerCase();
for(final String id in idList) {
if (id.toLowerCase() == idPattern) {
@@ -181,46 +184,47 @@ enum AndroidCPU {
}
class AndroidDeviceDiscovery implements DeviceDiscovery {
factory AndroidDeviceDiscovery({AndroidCPU? cpu}) {
factory AndroidDeviceDiscovery({AndroidCPU cpu}) {
return _instance ??= AndroidDeviceDiscovery._(cpu);
}
AndroidDeviceDiscovery._(this.cpu);
final AndroidCPU? cpu;
final AndroidCPU cpu;
// Parses information about a device. Example:
//
// 015d172c98400a03 device usb:340787200X product:nakasi model:Nexus_7 device:grouper
static final RegExp _kDeviceRegex = RegExp(r'^(\S+)\s+(\S+)(.*)');
static AndroidDeviceDiscovery? _instance;
static AndroidDeviceDiscovery _instance;
AndroidDevice? _workingDevice;
AndroidDevice _workingDevice;
@override
Future<AndroidDevice> get workingDevice async {
if (_workingDevice == null) {
if (Platform.environment.containsKey(DeviceIdEnvName)) {
final String deviceId = Platform.environment[DeviceIdEnvName]!;
final String deviceId = Platform.environment[DeviceIdEnvName];
await chooseWorkingDeviceById(deviceId);
return _workingDevice!;
return _workingDevice;
}
await chooseWorkingDevice();
}
return _workingDevice!;
return _workingDevice;
}
Future<bool> _matchesCPURequirement(AndroidDevice device) async {
if (cpu == null)
return true;
switch (cpu) {
case null:
return true;
case AndroidCPU.arm64:
return device.isArm64();
case AndroidCPU.arm:
return device.isArm();
}
return true;
}
/// Picks a random Android device out of connected devices and sets it as
@@ -255,11 +259,11 @@ class AndroidDeviceDiscovery implements DeviceDiscovery {
@override
Future<void> chooseWorkingDeviceById(String deviceId) async {
final String? matchedId = _findMatchId(await discoverDevices(), deviceId);
final String matchedId = _findMatchId(await discoverDevices(), deviceId);
if (matchedId != null) {
_workingDevice = AndroidDevice(deviceId: matchedId);
if (cpu != null) {
if (!await _matchesCPURequirement(_workingDevice!)) {
if (!await _matchesCPURequirement(_workingDevice)) {
throw DeviceException('The selected device $matchedId does not match the cpu requirement');
}
}
@@ -286,10 +290,10 @@ class AndroidDeviceDiscovery implements DeviceDiscovery {
continue;
if (_kDeviceRegex.hasMatch(line)) {
final Match match = _kDeviceRegex.firstMatch(line)!;
final Match match = _kDeviceRegex.firstMatch(line);
final String deviceID = match[1]!;
final String deviceState = match[2]!;
final String deviceID = match[1];
final String deviceState = match[2];
if (!const <String>['unauthorized', 'offline'].contains(deviceState)) {
results.add(deviceID);
@@ -338,9 +342,9 @@ class FuchsiaDeviceDiscovery implements DeviceDiscovery {
FuchsiaDeviceDiscovery._();
static FuchsiaDeviceDiscovery? _instance;
static FuchsiaDeviceDiscovery _instance;
FuchsiaDevice? _workingDevice;
FuchsiaDevice _workingDevice;
String get _ffx {
final String ffx = path.join(getArtifactPath(), 'fuchsia', 'tools','x64', 'ffx');
@@ -354,13 +358,13 @@ class FuchsiaDeviceDiscovery implements DeviceDiscovery {
Future<FuchsiaDevice> get workingDevice async {
if (_workingDevice == null) {
if (Platform.environment.containsKey(DeviceIdEnvName)) {
final String deviceId = Platform.environment[DeviceIdEnvName]!;
final String deviceId = Platform.environment[DeviceIdEnvName];
await chooseWorkingDeviceById(deviceId);
return _workingDevice!;
return _workingDevice;
}
await chooseWorkingDevice();
}
return _workingDevice!;
return _workingDevice;
}
/// Picks the first connected Fuchsia device.
@@ -379,8 +383,8 @@ class FuchsiaDeviceDiscovery implements DeviceDiscovery {
@override
Future<void> chooseWorkingDeviceById(String deviceId) async {
final String? matchedId = _findMatchId(await discoverDevices(), deviceId);
if (matchedId != null) {
final String matchedId = _findMatchId(await discoverDevices(), deviceId);
if (deviceId != null) {
_workingDevice = FuchsiaDevice(deviceId: matchedId);
print('Choose device by ID: $matchedId');
return;
@@ -438,7 +442,7 @@ class FuchsiaDeviceDiscovery implements DeviceDiscovery {
}
class AndroidDevice extends Device {
AndroidDevice({required this.deviceId}) {
AndroidDevice({@required this.deviceId}) {
_updateDeviceInfo();
}
@@ -536,19 +540,19 @@ class AndroidDevice extends Device {
}
/// Executes [command] on `adb shell` and returns its exit code.
Future<void> shellExec(String command, List<String> arguments, { Map<String, String>? environment, bool silent = false }) async {
Future<void> shellExec(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) async {
await adb(<String>['shell', command, ...arguments], environment: environment, silent: silent);
}
/// Executes [command] on `adb shell` and returns its standard output as a [String].
Future<String> shellEval(String command, List<String> arguments, { Map<String, String>? environment, bool silent = false }) {
Future<String> shellEval(String command, List<String> arguments, { Map<String, String> environment, bool silent = false }) {
return adb(<String>['shell', command, ...arguments], environment: environment, silent: silent);
}
/// Runs `adb` with the given [arguments], selecting this device.
Future<String> adb(
List<String> arguments, {
Map<String, String>? environment,
Map<String, String> environment,
bool silent = false,
}) {
return eval(
@@ -564,10 +568,10 @@ class AndroidDevice extends Device {
@override
Future<Map<String, dynamic>> getMemoryStats(String packageName) async {
final String meminfo = await shellEval('dumpsys', <String>['meminfo', packageName]);
final Match? match = RegExp(r'TOTAL\s+(\d+)').firstMatch(meminfo);
final Match match = RegExp(r'TOTAL\s+(\d+)').firstMatch(meminfo);
assert(match != null, 'could not parse dumpsys meminfo output');
return <String, dynamic>{
'total_kb': int.parse(match!.group(1)!),
'total_kb': int.parse(match.group(1)),
};
}
@@ -575,7 +579,7 @@ class AndroidDevice extends Device {
bool get canStreamLogs => true;
bool _abortedLogging/*!*/ = false;
Process? _loggingProcess;
Process/*?*/ _loggingProcess;
@override
Future<void> startLoggingToSink(IOSink sink, {bool clear = true}) async {
@@ -592,17 +596,17 @@ class AndroidDevice extends Device {
// to view the whole log, or just run logcat alongside this.
<String>['-s', deviceId, 'logcat', 'ActivityManager:I', 'flutter:V', '*:F'],
);
_loggingProcess!.stdout
_loggingProcess.stdout
.transform<String>(const Utf8Decoder(allowMalformed: true))
.listen((String line) {
sink.write(line);
});
_loggingProcess!.stderr
_loggingProcess.stderr
.transform<String>(const Utf8Decoder(allowMalformed: true))
.listen((String line) {
sink.write(line);
});
unawaited(_loggingProcess!.exitCode.then<void>((int exitCode) {
unawaited(_loggingProcess.exitCode.then<void>((int exitCode) {
if (!_abortedLogging) {
sink.writeln('adb logcat failed with exit code $exitCode.\n');
}
@@ -613,8 +617,8 @@ class AndroidDevice extends Device {
Future<void> stopLoggingToSink() async {
if (_loggingProcess != null) {
_abortedLogging = true;
_loggingProcess!.kill();
await _loggingProcess!.exitCode;
_loggingProcess.kill();
await _loggingProcess.exitCode;
}
}
@@ -625,7 +629,7 @@ class AndroidDevice extends Device {
final Completer<void> processDone = Completer<void>();
final Completer<void> abort = Completer<void>();
bool aborted = false;
late final StreamController<String> stream;
StreamController<String> stream;
stream = StreamController<String>(
onListen: () async {
await adb(<String>['logcat', '--clear']);
@@ -709,22 +713,22 @@ class IosDeviceDiscovery implements DeviceDiscovery {
IosDeviceDiscovery._();
static IosDeviceDiscovery? _instance;
static IosDeviceDiscovery _instance;
IosDevice? _workingDevice;
IosDevice _workingDevice;
@override
Future<IosDevice> get workingDevice async {
if (_workingDevice == null) {
if (Platform.environment.containsKey(DeviceIdEnvName)) {
final String deviceId = Platform.environment[DeviceIdEnvName]!;
final String deviceId = Platform.environment[DeviceIdEnvName];
await chooseWorkingDeviceById(deviceId);
return _workingDevice!;
return _workingDevice;
}
await chooseWorkingDevice();
}
return _workingDevice!;
return _workingDevice;
}
/// Picks a random iOS device out of connected devices and sets it as
@@ -745,7 +749,7 @@ class IosDeviceDiscovery implements DeviceDiscovery {
@override
Future<void> chooseWorkingDeviceById(String deviceId) async {
final String? matchedId = _findMatchId(await discoverDevices(), deviceId);
final String matchedId = _findMatchId(await discoverDevices(), deviceId);
if (matchedId != null) {
_workingDevice = IosDevice(deviceId: matchedId);
print('Choose device by ID: $matchedId');
@@ -820,7 +824,7 @@ class IosDeviceDiscovery implements DeviceDiscovery {
/// iOS device.
class IosDevice extends Device {
IosDevice({ required this.deviceId });
IosDevice({ @required this.deviceId });
@override
final String deviceId;
@@ -842,8 +846,8 @@ class IosDevice extends Device {
@override
bool get canStreamLogs => true;
bool _abortedLogging = false;
Process? _loggingProcess;
bool _abortedLogging/*!*/ = false;
Process/*?*/ _loggingProcess;
@override
Future<void> startLoggingToSink(IOSink sink, {bool clear = true}) async {
@@ -855,17 +859,17 @@ class IosDevice extends Device {
'DYLD_LIBRARY_PATH': dyldLibraryPath,
},
);
_loggingProcess!.stdout
_loggingProcess.stdout
.transform<String>(const Utf8Decoder(allowMalformed: true))
.listen((String line) {
sink.write(line);
});
_loggingProcess!.stderr
_loggingProcess.stderr
.transform<String>(const Utf8Decoder(allowMalformed: true))
.listen((String line) {
sink.write(line);
});
unawaited(_loggingProcess!.exitCode.then<void>((int exitCode) {
unawaited(_loggingProcess.exitCode.then<void>((int exitCode) {
if (!_abortedLogging) {
sink.writeln('idevicesyslog failed with exit code $exitCode.\n');
}
@@ -876,8 +880,8 @@ class IosDevice extends Device {
Future<void> stopLoggingToSink() async {
if (_loggingProcess != null) {
_abortedLogging = true;
_loggingProcess!.kill();
await _loggingProcess!.exitCode;
_loggingProcess.kill();
await _loggingProcess.exitCode;
}
}
@@ -930,7 +934,7 @@ class IosDevice extends Device {
/// Fuchsia device.
class FuchsiaDevice extends Device {
const FuchsiaDevice({ required this.deviceId });
const FuchsiaDevice({ @required this.deviceId });
@override
final String deviceId;
@@ -978,7 +982,7 @@ class FuchsiaDevice extends Device {
/// Path to the `adb` executable.
String get adbPath {
final String? androidHome = Platform.environment['ANDROID_HOME'] ?? Platform.environment['ANDROID_SDK_ROOT'];
final String androidHome = Platform.environment['ANDROID_HOME'] ?? Platform.environment['ANDROID_SDK_ROOT'];
if (androidHome == null) {
throw const DeviceException(
@@ -997,7 +1001,7 @@ String get adbPath {
}
class FakeDevice extends Device {
const FakeDevice({ required this.deviceId });
const FakeDevice({ @required this.deviceId });
@override
final String deviceId;
@@ -1051,22 +1055,22 @@ class FakeDeviceDiscovery implements DeviceDiscovery {
FakeDeviceDiscovery._();
static FakeDeviceDiscovery? _instance;
static FakeDeviceDiscovery _instance;
FakeDevice? _workingDevice;
FakeDevice _workingDevice;
@override
Future<FakeDevice> get workingDevice async {
if (_workingDevice == null) {
if (Platform.environment.containsKey(DeviceIdEnvName)) {
final String deviceId = Platform.environment[DeviceIdEnvName]!;
final String deviceId = Platform.environment[DeviceIdEnvName];
await chooseWorkingDeviceById(deviceId);
return _workingDevice!;
return _workingDevice;
}
await chooseWorkingDevice();
}
return _workingDevice!;
return _workingDevice;
}
/// The Fake is only available for by ID device discovery.
@@ -1077,7 +1081,7 @@ class FakeDeviceDiscovery implements DeviceDiscovery {
@override
Future<void> chooseWorkingDeviceById(String deviceId) async {
final String? matchedId = _findMatchId(await discoverDevices(), deviceId);
final String matchedId = _findMatchId(await discoverDevices(), deviceId);
if (matchedId != null) {
_workingDevice = FakeDevice(deviceId: matchedId);
print('Choose device by ID: $matchedId');