Switch many Device methods to be async (#9587)

`adb` can sometimes hang, which will in turn hang the Dart isolate if
we're using `Process.runSync()`. This changes many of the `Device` methods
to return `Future<T>` in order to allow them to use the async process
methods. A future change will add timeouts to the associated calls so
that we can properly alert the user to the hung `adb` process.

This is work towards #7102, #9567
This commit is contained in:
Todd Volkert
2017-04-25 17:23:00 -07:00
committed by GitHub
parent 596eb033c7
commit 60c5ffc1a9
29 changed files with 249 additions and 241 deletions

View File

@@ -134,20 +134,19 @@ class MockDeviceManager implements DeviceManager {
bool get hasSpecifiedDeviceId => specifiedDeviceId != null;
@override
Future<List<Device>> getAllConnectedDevices() => new Future<List<Device>>.value(devices);
Stream<Device> getAllConnectedDevices() => new Stream<Device>.fromIterable(devices);
@override
Future<List<Device>> getDevicesById(String deviceId) async {
return devices.where((Device device) => device.id == deviceId).toList();
Stream<Device> getDevicesById(String deviceId) {
return new Stream<Device>.fromIterable(
devices.where((Device device) => device.id == deviceId));
}
@override
Future<List<Device>> getDevices() async {
if (specifiedDeviceId == null) {
return getAllConnectedDevices();
} else {
return getDevicesById(specifiedDeviceId);
}
Stream<Device> getDevices() {
return hasSpecifiedDeviceId
? getDevicesById(specifiedDeviceId)
: getAllConnectedDevices();
}
void addDevice(Device device) => devices.add(device);

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:io' show ProcessResult;
import 'package:file/file.dart';
@@ -38,23 +39,23 @@ void main() {
// Let everything else return exit code 0 so process.dart doesn't crash.
// The matcher order is important.
when(
mockProcessManager.runSync(any, environment: null, workingDirectory: null)
mockProcessManager.run(any, environment: null, workingDirectory: null)
).thenReturn(
new ProcessResult(2, 0, '', null)
new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
);
// Let `which idevicescreenshot` fail with exit code 1.
when(
mockProcessManager.runSync(
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
).thenReturn(
new ProcessResult(1, 1, '', null)
new ProcessResult(1, 1, '', '')
);
iosDeviceUnderTest = new IOSDevice('1234');
iosDeviceUnderTest.takeScreenshot(mockOutputFile);
await iosDeviceUnderTest.takeScreenshot(mockOutputFile);
verify(mockProcessManager.runSync(
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
verifyNever(mockProcessManager.runSync(
verifyNever(mockProcessManager.run(
<String>['idevicescreenshot', fs.path.join('some', 'test', 'path', 'image.png')],
environment: null,
workingDirectory: null
@@ -74,23 +75,23 @@ void main() {
// Let everything else return exit code 0.
// The matcher order is important.
when(
mockProcessManager.runSync(any, environment: null, workingDirectory: null)
mockProcessManager.run(any, environment: null, workingDirectory: null)
).thenReturn(
new ProcessResult(4, 0, '', null)
new Future<ProcessResult>.value(new ProcessResult(4, 0, '', ''))
);
// Let there be idevicescreenshot in the PATH.
when(
mockProcessManager.runSync(
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
).thenReturn(
new ProcessResult(3, 0, fs.path.join('some', 'path', 'to', 'iscreenshot'), null)
new ProcessResult(3, 0, fs.path.join('some', 'path', 'to', 'iscreenshot'), '')
);
iosDeviceUnderTest = new IOSDevice('1234');
iosDeviceUnderTest.takeScreenshot(mockOutputFile);
await iosDeviceUnderTest.takeScreenshot(mockOutputFile);
verify(mockProcessManager.runSync(
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
verify(mockProcessManager.runSync(
verify(mockProcessManager.run(
<String>[
fs.path.join('some', 'path', 'to', 'iscreenshot'),
fs.path.join('some', 'test', 'path', 'image.png')

View File

@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io' show ProcessResult;
import 'package:file/file.dart';
@@ -128,9 +129,9 @@ void main() {
mockProcessManager = new MockProcessManager();
// Let everything else return exit code 0 so process.dart doesn't crash.
when(
mockProcessManager.runSync(any, environment: null, workingDirectory: null)
mockProcessManager.run(any, environment: null, workingDirectory: null)
).thenReturn(
new ProcessResult(2, 0, '', null)
new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
);
// Doesn't matter what the device is.
deviceUnderTest = new IOSSimulator('x', name: 'iPhone SE');
@@ -148,14 +149,14 @@ void main() {
testUsingContext(
'Xcode 8.2+ supports screenshots',
() {
() async {
when(mockXcode.xcodeMajorVersion).thenReturn(8);
when(mockXcode.xcodeMinorVersion).thenReturn(2);
expect(deviceUnderTest.supportsScreenshot, true);
final MockFile mockFile = new MockFile();
when(mockFile.path).thenReturn(fs.path.join('some', 'path', 'to', 'screenshot.png'));
deviceUnderTest.takeScreenshot(mockFile);
verify(mockProcessManager.runSync(
await deviceUnderTest.takeScreenshot(mockFile);
verify(mockProcessManager.run(
<String>[
'/usr/bin/xcrun',
'simctl',

View File

@@ -31,7 +31,7 @@ class MockApplicationPackageStore extends ApplicationPackageStore {
class MockAndroidDevice extends Mock implements AndroidDevice {
@override
TargetPlatform get targetPlatform => TargetPlatform.android_arm;
Future<TargetPlatform> get targetPlatform async => TargetPlatform.android_arm;
@override
bool isSupported() => true;
@@ -39,7 +39,7 @@ class MockAndroidDevice extends Mock implements AndroidDevice {
class MockIOSDevice extends Mock implements IOSDevice {
@override
TargetPlatform get targetPlatform => TargetPlatform.ios;
Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
@override
bool isSupported() => true;
@@ -47,7 +47,7 @@ class MockIOSDevice extends Mock implements IOSDevice {
class MockIOSSimulator extends Mock implements IOSSimulator {
@override
TargetPlatform get targetPlatform => TargetPlatform.ios;
Future<TargetPlatform> get targetPlatform async => TargetPlatform.ios;
@override
bool isSupported() => true;