Introduce the concept of asynchronicity to the service extensions. (#7823)
This allows us, for example, to wait for the slow mode banner to have been removed from the screen before triggering a screen shot.
This commit is contained in:
@@ -432,14 +432,12 @@ class AndroidDevice extends Device {
|
||||
bool get supportsScreenshot => true;
|
||||
|
||||
@override
|
||||
Future<bool> takeScreenshot(File outputFile) {
|
||||
Future<Null> takeScreenshot(File outputFile) {
|
||||
const String remotePath = '/data/local/tmp/flutter_screenshot.png';
|
||||
|
||||
runCheckedSync(adbCommandForDevice(<String>['shell', 'screencap', '-p', remotePath]));
|
||||
runCheckedSync(adbCommandForDevice(<String>['pull', remotePath, outputFile.path]));
|
||||
runCheckedSync(adbCommandForDevice(<String>['shell', 'rm', remotePath]));
|
||||
|
||||
return new Future<bool>.value(true);
|
||||
return new Future<Null>.value();
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -85,8 +85,7 @@ class ScreenshotCommand extends FlutterCommand {
|
||||
Future<Null> runScreenshot(File outputFile) async {
|
||||
outputFile ??= getUniqueFile(fs.currentDirectory, 'flutter', 'png');
|
||||
try {
|
||||
if (!await device.takeScreenshot(outputFile))
|
||||
throwToolExit('Screenshot failed');
|
||||
await device.takeScreenshot(outputFile);
|
||||
} catch (error) {
|
||||
throwToolExit('Error taking screenshot: $error');
|
||||
}
|
||||
|
||||
@@ -181,8 +181,10 @@ class ServiceProtocolDevFSOperations implements DevFSOperations {
|
||||
|
||||
@override
|
||||
Future<dynamic> destroy(String fsName) async {
|
||||
await vmService.vm.invokeRpcRaw('_deleteDevFS',
|
||||
<String, dynamic> { 'fsName': fsName });
|
||||
await vmService.vm.invokeRpcRaw(
|
||||
'_deleteDevFS',
|
||||
params: <String, dynamic> { 'fsName': fsName },
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -195,14 +197,16 @@ class ServiceProtocolDevFSOperations implements DevFSOperations {
|
||||
}
|
||||
String fileContents = BASE64.encode(bytes);
|
||||
try {
|
||||
return await vmService.vm.invokeRpcRaw('_writeDevFSFile',
|
||||
<String, dynamic> {
|
||||
'fsName': fsName,
|
||||
'path': devicePath,
|
||||
'fileContents': fileContents
|
||||
});
|
||||
} catch (e) {
|
||||
printTrace('DevFS: Failed to write $devicePath: $e');
|
||||
return await vmService.vm.invokeRpcRaw(
|
||||
'_writeDevFSFile',
|
||||
params: <String, dynamic> {
|
||||
'fsName': fsName,
|
||||
'path': devicePath,
|
||||
'fileContents': fileContents
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
printTrace('DevFS: Failed to write $devicePath: $error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -219,7 +219,7 @@ abstract class Device {
|
||||
|
||||
bool get supportsScreenshot => false;
|
||||
|
||||
Future<bool> takeScreenshot(File outputFile) => new Future<bool>.error('unimplemented');
|
||||
Future<Null> takeScreenshot(File outputFile) => new Future<Null>.error('unimplemented');
|
||||
|
||||
/// Find the apps that are currently running on this device.
|
||||
Future<List<DiscoveredApp>> discoverApps() =>
|
||||
|
||||
@@ -365,12 +365,11 @@ class IOSDevice extends Device {
|
||||
bool get supportsScreenshot => false;
|
||||
|
||||
@override
|
||||
Future<bool> takeScreenshot(File outputFile) {
|
||||
Future<Null> takeScreenshot(File outputFile) {
|
||||
// We could use idevicescreenshot here (installed along with the brew
|
||||
// ideviceinstaller tools). It however requires a developer disk image on
|
||||
// the device.
|
||||
|
||||
return new Future<bool>.value(false);
|
||||
return new Future<Null>.error('Taking screenshots is not supported on iOS devices. Consider using a simulator instead.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -587,7 +587,7 @@ class IOSSimulator extends Device {
|
||||
bool get supportsScreenshot => true;
|
||||
|
||||
@override
|
||||
Future<bool> takeScreenshot(File outputFile) async {
|
||||
Future<Null> takeScreenshot(File outputFile) async {
|
||||
Directory desktopDir = fs.directory(path.join(homeDirPath, 'Desktop'));
|
||||
|
||||
// 'Simulator Screen Shot Mar 25, 2016, 2.59.43 PM.png'
|
||||
@@ -621,8 +621,6 @@ class IOSSimulator extends Device {
|
||||
File shot = shots.first;
|
||||
outputFile.writeAsBytesSync(shot.readAsBytesSync());
|
||||
shot.delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -108,19 +108,34 @@ abstract class ResidentRunner {
|
||||
}
|
||||
|
||||
Future<Null> _screenshot() async {
|
||||
Status status = logger.startProgress('Taking screenshot...');
|
||||
File outputFile = getUniqueFile(fs.currentDirectory, 'flutter', 'png');
|
||||
try {
|
||||
if (vmService != null)
|
||||
await vmService.vm.refreshViews();
|
||||
if (isRunningDebug)
|
||||
await currentView.uiIsolate.flutterDebugAllowBanner(false);
|
||||
if (!await device.takeScreenshot(outputFile))
|
||||
printError('Error taking screenshot.');
|
||||
if (isRunningDebug)
|
||||
await currentView.uiIsolate.flutterDebugAllowBanner(true);
|
||||
try {
|
||||
if (isRunningDebug)
|
||||
await currentView.uiIsolate.flutterDebugAllowBanner(false);
|
||||
} catch (error) {
|
||||
status.stop();
|
||||
printError(error);
|
||||
}
|
||||
try {
|
||||
await device.takeScreenshot(outputFile);
|
||||
} finally {
|
||||
try {
|
||||
if (isRunningDebug)
|
||||
await currentView.uiIsolate.flutterDebugAllowBanner(true);
|
||||
} catch (error) {
|
||||
status.stop();
|
||||
printError(error);
|
||||
}
|
||||
}
|
||||
int sizeKB = (await outputFile.length()) ~/ 1024;
|
||||
status.stop();
|
||||
printStatus('Screenshot written to ${path.relative(outputFile.path)} (${sizeKB}kB).');
|
||||
} catch (error) {
|
||||
status.stop();
|
||||
printError('Error taking screenshot: $error');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ class VMService {
|
||||
///
|
||||
/// Requests made via the returns [VMService] time out after [requestTimeout]
|
||||
/// amount of time, which is [kDefaultRequestTimeout] by default.
|
||||
static Future<VMService> connect(Uri httpUri, {Duration requestTimeout: kDefaultRequestTimeout}) async {
|
||||
static Future<VMService> connect(Uri httpUri, { Duration requestTimeout: kDefaultRequestTimeout }) async {
|
||||
Uri wsUri = httpUri.replace(scheme: 'ws', path: path.join(httpUri.path, 'ws'));
|
||||
WebSocket ws;
|
||||
try {
|
||||
@@ -264,7 +264,7 @@ abstract class ServiceObject {
|
||||
Map<String, dynamic> params = <String, dynamic>{
|
||||
'objectId': id,
|
||||
};
|
||||
return _owner.isolate.invokeRpcRaw('getObject', params);
|
||||
return _owner.isolate.invokeRpcRaw('getObject', params: params);
|
||||
}
|
||||
|
||||
Future<ServiceObject> _inProgressReload;
|
||||
@@ -437,7 +437,7 @@ class VM extends ServiceObjectOwner {
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> _fetchDirect() async {
|
||||
return invokeRpcRaw('getVM', <String, dynamic> {});
|
||||
return invokeRpcRaw('getVM');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -567,27 +567,39 @@ class VM extends ServiceObjectOwner {
|
||||
}
|
||||
|
||||
/// Invoke the RPC and return the raw response.
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(
|
||||
String method, [Map<String, dynamic> params, Duration timeout]) async {
|
||||
if (params == null) {
|
||||
params = <String, dynamic>{};
|
||||
}
|
||||
|
||||
///
|
||||
/// If `timeoutFatal` is false, then a timeout will result in a null return
|
||||
/// value. Otherwise, it results in an exception.
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(String method, {
|
||||
Map<String, dynamic> params: const <String, dynamic>{},
|
||||
Duration timeout,
|
||||
bool timeoutFatal: true,
|
||||
}) async {
|
||||
assert(params != null);
|
||||
timeout ??= _vmService._requestTimeout;
|
||||
try {
|
||||
Map<String, dynamic> result = await _vmService.peer
|
||||
.sendRequest(method, params)
|
||||
.timeout(timeout ?? _vmService._requestTimeout);
|
||||
.timeout(timeout);
|
||||
return result;
|
||||
} on TimeoutException catch(_) {
|
||||
printError('Request to Dart VM Service timed out: $method($params)');
|
||||
rethrow;
|
||||
} on TimeoutException {
|
||||
printTrace('Request to Dart VM Service timed out: $method($params)');
|
||||
if (timeoutFatal)
|
||||
throw new TimeoutException('Request to Dart VM Service timed out: $method($params)');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// Invoke the RPC and return a ServiceObject response.
|
||||
Future<ServiceObject> invokeRpc(
|
||||
String method, { Map<String, dynamic> params, Duration timeout }) async {
|
||||
Map<String, dynamic> response = await invokeRpcRaw(method, params, timeout);
|
||||
/// Invoke the RPC and return a [ServiceObject] response.
|
||||
Future<ServiceObject> invokeRpc(String method, {
|
||||
Map<String, dynamic> params: const <String, dynamic>{},
|
||||
Duration timeout,
|
||||
}) async {
|
||||
Map<String, dynamic> response = await invokeRpcRaw(
|
||||
method,
|
||||
params: params,
|
||||
timeout: timeout,
|
||||
);
|
||||
ServiceObject serviceObject = new ServiceObject._fromMap(this, response);
|
||||
if ((serviceObject != null) && (serviceObject._canCache)) {
|
||||
String serviceObjectId = serviceObject.id;
|
||||
@@ -597,19 +609,13 @@ class VM extends ServiceObjectOwner {
|
||||
}
|
||||
|
||||
/// Create a new development file system on the device.
|
||||
Future<Map<String, dynamic>> createDevFS(String fsName) async {
|
||||
Map<String, dynamic> response =
|
||||
await invokeRpcRaw('_createDevFS', <String, dynamic> {
|
||||
'fsName': fsName
|
||||
});
|
||||
return response;
|
||||
Future<Map<String, dynamic>> createDevFS(String fsName) {
|
||||
return invokeRpcRaw('_createDevFS', params: <String, dynamic> { 'fsName': fsName });
|
||||
}
|
||||
|
||||
/// List the development file system son the device.
|
||||
Future<List<String>> listDevFS() async {
|
||||
Map<String, dynamic> response =
|
||||
await invokeRpcRaw('_listDevFS', <String, dynamic>{});
|
||||
return response['fsNames'];
|
||||
return (await invokeRpcRaw('_listDevFS'))['fsNames'];
|
||||
}
|
||||
|
||||
// Write one file into a file system.
|
||||
@@ -619,36 +625,36 @@ class VM extends ServiceObjectOwner {
|
||||
}) {
|
||||
assert(path != null);
|
||||
assert(fileContents != null);
|
||||
|
||||
return invokeRpcRaw('_writeDevFSFile', <String, dynamic> {
|
||||
'fsName': fsName,
|
||||
'path': path,
|
||||
'fileContents': BASE64.encode(fileContents)
|
||||
});
|
||||
return invokeRpcRaw(
|
||||
'_writeDevFSFile',
|
||||
params: <String, dynamic>{
|
||||
'fsName': fsName,
|
||||
'path': path,
|
||||
'fileContents': BASE64.encode(fileContents),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
// Read one file from a file system.
|
||||
Future<List<int>> readDevFSFile(String fsName, String path) {
|
||||
return invokeRpcRaw('_readDevFSFile', <String, dynamic> {
|
||||
'fsName': fsName,
|
||||
'path': path
|
||||
}).then((Map<String, dynamic> response) {
|
||||
return BASE64.decode(response['fileContents']);
|
||||
});
|
||||
Future<List<int>> readDevFSFile(String fsName, String path) async {
|
||||
Map<String, dynamic> response = await invokeRpcRaw(
|
||||
'_readDevFSFile',
|
||||
params: <String, dynamic>{
|
||||
'fsName': fsName,
|
||||
'path': path,
|
||||
},
|
||||
);
|
||||
return BASE64.decode(response['fileContents']);
|
||||
}
|
||||
|
||||
/// The complete list of a file system.
|
||||
Future<List<String>> listDevFSFiles(String fsName) {
|
||||
return invokeRpcRaw('_listDevFSFiles', <String, dynamic> {
|
||||
'fsName': fsName
|
||||
}).then((Map<String, dynamic> response) {
|
||||
return response['files'];
|
||||
});
|
||||
Future<List<String>> listDevFSFiles(String fsName) async {
|
||||
return (await invokeRpcRaw('_listDevFSFiles', params: <String, dynamic>{ 'fsName': fsName }))['files'];
|
||||
}
|
||||
|
||||
/// Delete an existing file system.
|
||||
Future<Map<String, dynamic>> deleteDevFS(String fsName) {
|
||||
return invokeRpcRaw('_deleteDevFS', <String, dynamic> { 'fsName': fsName });
|
||||
return invokeRpcRaw('_deleteDevFS', params: <String, dynamic>{ 'fsName': fsName });
|
||||
}
|
||||
|
||||
Future<ServiceMap> runInView(String viewId,
|
||||
@@ -665,20 +671,21 @@ class VM extends ServiceObjectOwner {
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> clearVMTimeline() {
|
||||
return invokeRpcRaw('_clearVMTimeline', <String, dynamic>{});
|
||||
return invokeRpcRaw('_clearVMTimeline');
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> setVMTimelineFlags(
|
||||
List<String> recordedStreams) {
|
||||
Future<Map<String, dynamic>> setVMTimelineFlags(List<String> recordedStreams) {
|
||||
assert(recordedStreams != null);
|
||||
|
||||
return invokeRpcRaw('_setVMTimelineFlags', <String, dynamic> {
|
||||
'recordedStreams': recordedStreams
|
||||
});
|
||||
return invokeRpcRaw(
|
||||
'_setVMTimelineFlags',
|
||||
params: <String, dynamic>{
|
||||
'recordedStreams': recordedStreams,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> getVMTimeline() {
|
||||
return invokeRpcRaw('_getVMTimeline', <String, dynamic> {}, kLongRequestTimeout);
|
||||
return invokeRpcRaw('_getVMTimeline', timeout: kLongRequestTimeout);
|
||||
}
|
||||
|
||||
Future<Null> refreshViews() async {
|
||||
@@ -736,12 +743,15 @@ class Isolate extends ServiceObjectOwner {
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> _fetchDirect() {
|
||||
return invokeRpcRaw('getIsolate', <String, dynamic>{});
|
||||
return invokeRpcRaw('getIsolate');
|
||||
}
|
||||
|
||||
/// Invoke the RPC and return the raw response.
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(
|
||||
String method, [Map<String, dynamic> params, Duration timeout]) {
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(String method, {
|
||||
Map<String, dynamic> params,
|
||||
Duration timeout,
|
||||
bool timeoutFatal: true,
|
||||
}) {
|
||||
// Inject the 'isolateId' parameter.
|
||||
if (params == null) {
|
||||
params = <String, dynamic>{
|
||||
@@ -750,21 +760,18 @@ class Isolate extends ServiceObjectOwner {
|
||||
} else {
|
||||
params['isolateId'] = id;
|
||||
}
|
||||
return vm.invokeRpcRaw(method, params, timeout);
|
||||
return vm.invokeRpcRaw(method, params: params, timeout: timeout, timeoutFatal: timeoutFatal);
|
||||
}
|
||||
|
||||
/// Invoke the RPC and return a ServiceObject response.
|
||||
Future<ServiceObject> invokeRpc(
|
||||
String method, Map<String, dynamic> params) async {
|
||||
Map<String, dynamic> response = await invokeRpcRaw(method, params);
|
||||
return getFromMap(response);
|
||||
Future<ServiceObject> invokeRpc(String method, Map<String, dynamic> params) async {
|
||||
return getFromMap(await invokeRpcRaw(method, params: params));
|
||||
}
|
||||
|
||||
@override
|
||||
void _update(Map<String, dynamic> map, bool mapIsRef) {
|
||||
if (mapIsRef) {
|
||||
if (mapIsRef)
|
||||
return;
|
||||
}
|
||||
_loaded = true;
|
||||
|
||||
int startTimeMillis = map['startTime'];
|
||||
@@ -791,7 +798,7 @@ class Isolate extends ServiceObjectOwner {
|
||||
if (packagesPath != null) {
|
||||
arguments['packagesUri'] = packagesPath;
|
||||
}
|
||||
Map<String, dynamic> response = await invokeRpcRaw('_reloadSources', arguments);
|
||||
Map<String, dynamic> response = await invokeRpcRaw('_reloadSources', params: arguments);
|
||||
return response;
|
||||
} on rpc.RpcException catch(e) {
|
||||
return new Future<Map<String, dynamic>>.error(<String, dynamic>{
|
||||
@@ -807,9 +814,14 @@ class Isolate extends ServiceObjectOwner {
|
||||
// Invoke a flutter extension method, if the flutter extension is not
|
||||
// available, returns null.
|
||||
Future<Map<String, dynamic>> invokeFlutterExtensionRpcRaw(
|
||||
String method, { Map<String, dynamic> params, Duration timeout }) async {
|
||||
String method, {
|
||||
Map<String, dynamic> params,
|
||||
Duration timeout,
|
||||
bool timeoutFatal: true,
|
||||
}
|
||||
) async {
|
||||
try {
|
||||
return await invokeRpcRaw(method, params, timeout);
|
||||
return await invokeRpcRaw(method, params: params, timeout: timeout, timeoutFatal: timeoutFatal);
|
||||
} on rpc.RpcException catch (e) {
|
||||
// If an application is not using the framework
|
||||
if (e.code == rpc_error_code.METHOD_NOT_FOUND)
|
||||
@@ -821,29 +833,42 @@ class Isolate extends ServiceObjectOwner {
|
||||
// Debug dump extension methods.
|
||||
|
||||
Future<Map<String, dynamic>> flutterDebugDumpApp() {
|
||||
return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpApp');
|
||||
return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpApp', timeout: kLongRequestTimeout);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> flutterDebugDumpRenderTree() {
|
||||
return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpRenderTree');
|
||||
return invokeFlutterExtensionRpcRaw('ext.flutter.debugDumpRenderTree', timeout: kLongRequestTimeout);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> flutterToggleDebugPaintSizeEnabled() async {
|
||||
Map<String, dynamic> state = await invokeFlutterExtensionRpcRaw('ext.flutter.debugPaint');
|
||||
if (state != null && state.containsKey('enabled') && state['enabled'] is bool)
|
||||
state = await invokeFlutterExtensionRpcRaw('ext.flutter.debugPaint',
|
||||
params: <String, dynamic>{ 'enabled': !state['enabled'] });
|
||||
if (state != null && state.containsKey('enabled') && state['enabled'] is bool) {
|
||||
state = await invokeFlutterExtensionRpcRaw(
|
||||
'ext.flutter.debugPaint',
|
||||
params: <String, dynamic>{ 'enabled': !state['enabled'] },
|
||||
timeout: const Duration(milliseconds: 150),
|
||||
timeoutFatal: false,
|
||||
);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
Future<Null> flutterDebugAllowBanner(bool show) async {
|
||||
await invokeFlutterExtensionRpcRaw('ext.flutter.debugAllowBanner', params: <String, dynamic>{ 'enabled': show });
|
||||
await invokeFlutterExtensionRpcRaw(
|
||||
'ext.flutter.debugAllowBanner',
|
||||
params: <String, dynamic>{ 'enabled': show },
|
||||
timeout: const Duration(milliseconds: 150),
|
||||
timeoutFatal: false,
|
||||
);
|
||||
}
|
||||
|
||||
// Reload related extension methods.
|
||||
Future<Map<String, dynamic>> flutterReassemble() async {
|
||||
return await invokeFlutterExtensionRpcRaw('ext.flutter.reassemble',
|
||||
timeout: kLongRequestTimeout);
|
||||
return await invokeFlutterExtensionRpcRaw(
|
||||
'ext.flutter.reassemble',
|
||||
timeout: kLongRequestTimeout,
|
||||
timeoutFatal: false,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> flutterFrameworkPresent() async {
|
||||
@@ -856,16 +881,19 @@ class Isolate extends ServiceObjectOwner {
|
||||
|
||||
Future<Map<String, dynamic>> flutterEvictAsset(String assetPath) async {
|
||||
return await invokeFlutterExtensionRpcRaw('ext.flutter.evict',
|
||||
params: <String, dynamic>{
|
||||
'value': assetPath
|
||||
}
|
||||
params: <String, dynamic>{
|
||||
'value': assetPath,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// Application control extension methods.
|
||||
Future<Map<String, dynamic>> flutterExit() async {
|
||||
return await invokeFlutterExtensionRpcRaw('ext.flutter.exit').timeout(
|
||||
const Duration(seconds: 2), onTimeout: () => null);
|
||||
return await invokeFlutterExtensionRpcRaw(
|
||||
'ext.flutter.exit',
|
||||
timeout: const Duration(seconds: 2),
|
||||
timeoutFatal: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -297,8 +297,11 @@ class MockVM implements VM {
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(
|
||||
String method, [Map<String, dynamic> params, Duration timeout]) async {
|
||||
Future<Map<String, dynamic>> invokeRpcRaw(String method, {
|
||||
Map<String, dynamic> params: const <String, dynamic>{},
|
||||
Duration timeout,
|
||||
bool timeoutFatal: true,
|
||||
}) async {
|
||||
_service.messages.add('$method $params');
|
||||
return <String, dynamic>{'success': true};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user