Unverified Commit ca5411e3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Fix type error hidden by implicit downcasts (#38102)

parent 2003432c
......@@ -90,7 +90,12 @@ class SimControl {
return <String, Map<String, dynamic>>{};
}
try {
return json.decode(results.stdout)[section.name];
final Object decodeResult = json.decode(results.stdout?.toString())[section.name];
if (decodeResult is Map<String, dynamic>) {
return decodeResult;
}
printError('simctl returned unexpected JSON response: ${results.stdout}');
return <String, dynamic>{};
} on FormatException {
// We failed to parse the simctl output, or it returned junk.
// One known message is "Install Started" isn't valid JSON but is
......@@ -107,11 +112,13 @@ class SimControl {
final Map<String, dynamic> devicesSection = await _list(SimControlListSection.devices);
for (String deviceCategory in devicesSection.keys) {
final List<dynamic> devicesData = devicesSection[deviceCategory];
final Object devicesData = devicesSection[deviceCategory];
if (devicesData != null && devicesData is List<dynamic>) {
for (Map<String, dynamic> data in devicesData.map<Map<String, dynamic>>(castStringKeyedMap)) {
devices.add(SimDevice(deviceCategory, data));
}
}
}
return devices;
}
......@@ -220,10 +227,10 @@ class SimDevice {
final String category;
final Map<String, dynamic> data;
String get state => data['state'];
String get availability => data['availability'];
String get name => data['name'];
String get udid => data['udid'];
String get state => data['state']?.toString();
String get availability => data['availability']?.toString();
String get name => data['name']?.toString();
String get udid => data['udid']?.toString();
bool get isBooted => state == 'Booted';
}
......@@ -267,7 +274,7 @@ class IOSSimulator extends Device {
Future<bool> isLatestBuildInstalled(ApplicationPackage app) async => false;
@override
Future<bool> installApp(ApplicationPackage app) async {
Future<bool> installApp(covariant IOSApp app) async {
try {
final IOSApp iosApp = app;
await SimControl.instance.install(id, iosApp.simulatorBundlePath);
......@@ -316,7 +323,7 @@ class IOSSimulator extends Device {
@override
Future<LaunchResult> startApp(
ApplicationPackage package, {
covariant IOSApp package, {
String mainPath,
String route,
DebuggingOptions debuggingOptions,
......@@ -325,7 +332,7 @@ class IOSSimulator extends Device {
bool usesTerminalUi = true,
bool ipv6 = false,
}) async {
if (!prebuiltApplication) {
if (!prebuiltApplication && package is BuildableIOSApp) {
printTrace('Building ${package.name} for $id.');
try {
......@@ -371,8 +378,7 @@ class IOSSimulator extends Device {
// which should always yield the correct value and does not require
// parsing the xcodeproj or configuration files.
// See https://github.com/flutter/flutter/issues/31037 for more information.
final IOSApp iosApp = package;
final String plistPath = fs.path.join(iosApp.simulatorBundlePath, 'Info.plist');
final String plistPath = fs.path.join(package.simulatorBundlePath, 'Info.plist');
final String bundleIdentifier = iosWorkflow.getPlistValueFromFile(plistPath, kCFBundleIdentifierKey);
await SimControl.instance.launch(id, bundleIdentifier, args);
......@@ -400,7 +406,7 @@ class IOSSimulator extends Device {
}
}
Future<void> _setupUpdatedApplicationBundle(ApplicationPackage app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async {
Future<void> _setupUpdatedApplicationBundle(covariant BuildableIOSApp app, BuildInfo buildInfo, String mainPath, bool usesTerminalUi) async {
await _sideloadUpdatedAssetsForInstalledApplicationBundle(app, buildInfo, mainPath);
// Step 1: Build the Xcode project.
......@@ -422,8 +428,7 @@ class IOSSimulator extends Device {
throwToolExit('Could not build the application for the simulator.');
// Step 2: Assert that the Xcode project was successfully built.
final IOSApp iosApp = app;
final Directory bundle = fs.directory(iosApp.simulatorBundlePath);
final Directory bundle = fs.directory(app.simulatorBundlePath);
final bool bundleExists = bundle.existsSync();
if (!bundleExists)
throwToolExit('Could not find the built application bundle at ${bundle.path}.');
......@@ -463,11 +468,11 @@ class IOSSimulator extends Device {
Future<int> get sdkMajorVersion async {
final Match sdkMatch = _iosSdkRegExp.firstMatch(await sdkNameAndVersion);
return int.parse(sdkMatch?.group(2) ?? 11);
return int.parse(sdkMatch?.group(2) ?? '11');
}
@override
DeviceLogReader getLogReader({ ApplicationPackage app }) {
DeviceLogReader getLogReader({ covariant IOSApp app }) {
assert(app is IOSApp);
_logReaders ??= <ApplicationPackage, _IOSSimulatorLogReader>{};
return _logReaders.putIfAbsent(app, () => _IOSSimulatorLogReader(this, app));
......
......@@ -438,6 +438,12 @@ void main() {
ProcessManager: () => mockProcessManager,
SimControl: () => simControl,
});
testUsingContext('sdkMajorVersion defaults to 11 when sdkNameAndVersion is junk', () async {
final IOSSimulator iosSimulatorA = IOSSimulator('x', name: 'Testo', simulatorCategory: 'NaN');
expect(await iosSimulatorA.sdkMajorVersion, 11);
});
});
group('startApp', () {
......
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