From af5d4c688205c7b348201c702b05c03f1f5eb9ff Mon Sep 17 00:00:00 2001 From: Mark Fielbig Date: Sat, 30 Jun 2018 17:17:35 -0700 Subject: [PATCH] Fix type errors in iOS simulator discovery. (#18960) --- .../flutter_tools/lib/src/ios/simulators.dart | 17 +++-- .../test/ios/simulators_test.dart | 76 +++++++++++++++++++ 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart index 4b7a5280e8..d80fc2a3f4 100644 --- a/packages/flutter_tools/lib/src/ios/simulators.dart +++ b/packages/flutter_tools/lib/src/ios/simulators.dart @@ -58,10 +58,7 @@ class SimControl { /// Runs `simctl list --json` and returns the JSON of the corresponding /// [section]. - /// - /// The return type depends on the [section] being listed but is usually - /// either a [Map] or a [List]. - dynamic _list(SimControlListSection section) { + Map _list(SimControlListSection section) { // Sample output from `simctl list --json`: // // { @@ -97,9 +94,8 @@ class SimControl { final Map devicesSection = _list(SimControlListSection.devices); for (String deviceCategory in devicesSection.keys) { - final List> devicesData = devicesSection[deviceCategory]; - - for (Map data in devicesData) { + final List devicesData = devicesSection[deviceCategory]; + for (Map data in devicesData.map(_castStringKeyedMap)) { devices.add(new SimDevice(deviceCategory, data)); } } @@ -107,6 +103,11 @@ class SimControl { return devices; } + Map _castStringKeyedMap(dynamic untyped) { + final Map map = untyped; + return map.cast(); + } + /// Returns all the connected simulator devices. List getConnectedDevices() { return getDevices().where((SimDevice device) => device.isBooted).toList(); @@ -182,7 +183,7 @@ class SimDevice { SimDevice(this.category, this.data); final String category; - final Map data; + final Map data; String get state => data['state']; String get availability => data['availability']; diff --git a/packages/flutter_tools/test/ios/simulators_test.dart b/packages/flutter_tools/test/ios/simulators_test.dart index 03fc5304e0..190f2c08fc 100644 --- a/packages/flutter_tools/test/ios/simulators_test.dart +++ b/packages/flutter_tools/test/ios/simulators_test.dart @@ -329,4 +329,80 @@ void main() { ProcessManager: () => mockProcessManager, }); }); + + group('SimControl', () { + const int mockPid = 123; + const String validSimControlOutput = ''' +{ + "devices" : { + "watchOS 4.3" : [ + { + "state" : "Shutdown", + "availability" : "(available)", + "name" : "Apple Watch - 38mm", + "udid" : "TEST-WATCH-UDID" + } + ], + "iOS 11.4" : [ + { + "state" : "Booted", + "availability" : "(available)", + "name" : "iPhone 5s", + "udid" : "TEST-PHONE-UDID" + } + ], + "tvOS 11.4" : [ + { + "state" : "Shutdown", + "availability" : "(available)", + "name" : "Apple TV", + "udid" : "TEST-TV-UDID" + } + ] + } +} + '''; + + MockProcessManager mockProcessManager; + SimControl simControl; + + setUp(() { + mockProcessManager = new MockProcessManager(); + when(mockProcessManager.runSync(any)) + .thenReturn(new ProcessResult(mockPid, 0, validSimControlOutput, '')); + + simControl = new SimControl(); + }); + + testUsingContext('getDevices succeeds', () { + final List devices = simControl.getDevices(); + + final SimDevice watch = devices[0]; + expect(watch.category, 'watchOS 4.3'); + expect(watch.state, 'Shutdown'); + expect(watch.availability, '(available)'); + expect(watch.name, 'Apple Watch - 38mm'); + expect(watch.udid, 'TEST-WATCH-UDID'); + expect(watch.isBooted, isFalse); + + final SimDevice phone = devices[1]; + expect(phone.category, 'iOS 11.4'); + expect(phone.state, 'Booted'); + expect(phone.availability, '(available)'); + expect(phone.name, 'iPhone 5s'); + expect(phone.udid, 'TEST-PHONE-UDID'); + expect(phone.isBooted, isTrue); + + final SimDevice tv = devices[2]; + expect(tv.category, 'tvOS 11.4'); + expect(tv.state, 'Shutdown'); + expect(tv.availability, '(available)'); + expect(tv.name, 'Apple TV'); + expect(tv.udid, 'TEST-TV-UDID'); + expect(tv.isBooted, isFalse); + }, overrides: { + ProcessManager: () => mockProcessManager, + SimControl: () => simControl, + }); + }); }