From 82830fa1a063e9ef73a912e233e972b085868033 Mon Sep 17 00:00:00 2001 From: Hannes Winkler Date: Fri, 30 Apr 2021 07:39:03 +0200 Subject: [PATCH] [custom-devices] add screenshotting support (#80675) --- .../lib/src/custom_devices/custom_device.dart | 19 ++++++ .../custom_devices/custom_device_config.dart | 23 +++++-- .../static/custom-devices.schema.json | 12 ++++ .../custom_devices/custom_device_test.dart | 61 ++++++++++++++++++- 4 files changed, 108 insertions(+), 7 deletions(-) diff --git a/packages/flutter_tools/lib/src/custom_devices/custom_device.dart b/packages/flutter_tools/lib/src/custom_devices/custom_device.dart index 6d3879988b..5f4a1cd9f5 100644 --- a/packages/flutter_tools/lib/src/custom_devices/custom_device.dart +++ b/packages/flutter_tools/lib/src/custom_devices/custom_device.dart @@ -11,6 +11,7 @@ import 'package:process/process.dart'; import '../application_package.dart'; import '../base/common.dart'; +import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; import '../base/process.dart'; @@ -594,6 +595,24 @@ class CustomDevice extends Device { @override Future get isLocalEmulator async => false; + @override + bool get supportsScreenshot => _config.supportsScreenshotting; + + @override + Future takeScreenshot(File outputFile) async { + if (supportsScreenshot == false) { + throw UnsupportedError('Screenshotting is not supported for this device.'); + } + + final List interpolated = interpolateCommand( + _config.screenshotCommand, + {}, + ); + + final RunResult result = await _processUtils.run(interpolated, throwOnError: true); + await outputFile.writeAsBytes(base64Decode(result.stdout)); + } + @override bool isSupported() { return true; diff --git a/packages/flutter_tools/lib/src/custom_devices/custom_device_config.dart b/packages/flutter_tools/lib/src/custom_devices/custom_device_config.dart index 8052df9934..cfa8416128 100644 --- a/packages/flutter_tools/lib/src/custom_devices/custom_device_config.dart +++ b/packages/flutter_tools/lib/src/custom_devices/custom_device_config.dart @@ -22,7 +22,8 @@ class CustomDeviceConfig { required this.uninstallCommand, required this.runDebugCommand, this.forwardPortCommand, - this.forwardPortSuccessRegex + this.forwardPortSuccessRegex, + this.screenshotCommand }) : assert(forwardPortCommand == null || forwardPortSuccessRegex != null); factory CustomDeviceConfig.fromJson(dynamic json) { @@ -40,7 +41,8 @@ class CustomDeviceConfig { uninstallCommand: _castStringList(typedMap[_kUninstallCommand]!), runDebugCommand: _castStringList(typedMap[_kRunDebugCommand]!), forwardPortCommand: _castStringListOrNull(typedMap[_kForwardPortCommand]), - forwardPortSuccessRegex: _convertToRegexOrNull(typedMap[_kForwardPortSuccessRegex]) + forwardPortSuccessRegex: _convertToRegexOrNull(typedMap[_kForwardPortSuccessRegex]), + screenshotCommand: _castStringListOrNull(typedMap[_kScreenshotCommand]) ); } @@ -56,6 +58,7 @@ class CustomDeviceConfig { static const String _kRunDebugCommand = 'runDebug'; static const String _kForwardPortCommand = 'forwardPort'; static const String _kForwardPortSuccessRegex = 'forwardPortSuccessRegex'; + static const String _kScreenshotCommand = 'screenshot'; /// An example device config used for creating the default config file. static final CustomDeviceConfig example = CustomDeviceConfig( @@ -70,7 +73,8 @@ class CustomDeviceConfig { uninstallCommand: const ['ssh', 'pi@raspberrypi', r'rm -rf "/tmp/${appName}"'], runDebugCommand: const ['ssh', 'pi@raspberrypi', r'flutter-pi "/tmp/${appName}"'], forwardPortCommand: const ['ssh', '-o', 'ExitOnForwardFailure=yes', '-L', r'127.0.0.1:${hostPort}:127.0.0.1:${devicePort}', 'pi@raspberrypi'], - forwardPortSuccessRegex: RegExp('Linux') + forwardPortSuccessRegex: RegExp('Linux'), + screenshotCommand: const ['ssh', 'pi@raspberrypi', r"fbgrab /tmp/screenshot.png && cat /tmp/screenshot.png | base64 | tr -d ' \n\t'"] ); final String id; @@ -85,9 +89,12 @@ class CustomDeviceConfig { final List runDebugCommand; final List? forwardPortCommand; final RegExp? forwardPortSuccessRegex; + final List? screenshotCommand; bool get usesPortForwarding => forwardPortCommand != null; + bool get supportsScreenshotting => screenshotCommand != null; + static List _castStringList(Object object) { return (object as List).cast(); } @@ -113,7 +120,8 @@ class CustomDeviceConfig { _kUninstallCommand: uninstallCommand, _kRunDebugCommand: runDebugCommand, _kForwardPortCommand: forwardPortCommand, - _kForwardPortSuccessRegex: forwardPortSuccessRegex?.pattern + _kForwardPortSuccessRegex: forwardPortSuccessRegex?.pattern, + _kScreenshotCommand: screenshotCommand, }; } @@ -133,7 +141,9 @@ class CustomDeviceConfig { bool explicitForwardPortCommand = false, List? forwardPortCommand, bool explicitForwardPortSuccessRegex = false, - RegExp? forwardPortSuccessRegex + RegExp? forwardPortSuccessRegex, + bool explicitScreenshotCommand = false, + List? screenshotCommand }) { return CustomDeviceConfig( id: id ?? this.id, @@ -147,7 +157,8 @@ class CustomDeviceConfig { uninstallCommand: uninstallCommand ?? this.uninstallCommand, runDebugCommand: runDebugCommand ?? this.runDebugCommand, forwardPortCommand: explicitForwardPortCommand ? forwardPortCommand : (forwardPortCommand ?? this.forwardPortCommand), - forwardPortSuccessRegex: explicitForwardPortSuccessRegex ? forwardPortSuccessRegex : (forwardPortSuccessRegex ?? this.forwardPortSuccessRegex) + forwardPortSuccessRegex: explicitForwardPortSuccessRegex ? forwardPortSuccessRegex : (forwardPortSuccessRegex ?? this.forwardPortSuccessRegex), + screenshotCommand: explicitScreenshotCommand ? screenshotCommand : (screenshotCommand ?? this.screenshotCommand), ); } } diff --git a/packages/flutter_tools/static/custom-devices.schema.json b/packages/flutter_tools/static/custom-devices.schema.json index 1a85f88196..f6362fa837 100644 --- a/packages/flutter_tools/static/custom-devices.schema.json +++ b/packages/flutter_tools/static/custom-devices.schema.json @@ -107,6 +107,18 @@ "format": "regex", "default": "Linux", "required": false + }, + "screenshot": { + "description": "Take a screenshot of the app as a png image. This command should take the screenshot, convert it to png, then base64 encode it and echo to stdout. Any stderr output will be ignored. If this command is not given, screenshotting will be disabled for this device.", + "type": ["array", "null"], + "items": { + "type": "string" + }, + "minItems": 1, + "default": [ + "ssh", "pi@raspberrypi", "fbgrab /tmp/screenshot.png && cat /tmp/screenshot.png | base64 | tr -d ' \\n\\t'" + ], + "required": false } } } diff --git a/packages/flutter_tools/test/general.shard/custom_devices/custom_device_test.dart b/packages/flutter_tools/test/general.shard/custom_devices/custom_device_test.dart index 8953986728..60ee5a0099 100644 --- a/packages/flutter_tools/test/general.shard/custom_devices/custom_device_test.dart +++ b/packages/flutter_tools/test/general.shard/custom_devices/custom_device_test.dart @@ -10,6 +10,7 @@ import 'package:file/file.dart'; import 'package:file/memory.dart'; import 'package:file/src/interface/directory.dart'; import 'package:file/src/interface/file.dart'; +import 'package:file_testing/file_testing.dart'; import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_system/build_system.dart'; @@ -108,7 +109,8 @@ void main() { uninstallCommand: const ['testuninstall'], runDebugCommand: const ['testrundebug'], forwardPortCommand: const ['testforwardport'], - forwardPortSuccessRegex: RegExp('testforwardportsuccess') + forwardPortSuccessRegex: RegExp('testforwardportsuccess'), + screenshotCommand: const ['testscreenshot'] ); const String testConfigPingSuccessOutput = 'testpingsuccess\n'; @@ -520,6 +522,63 @@ void main() { ProcessManager: () => FakeProcessManager.any() } ); + + testWithoutContext('CustomDevice screenshotting', () async { + bool screenshotCommandWasExecuted = false; + + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand( + command: testConfig.screenshotCommand, + onRun: () => screenshotCommandWasExecuted = true, + ) + ]); + + final MemoryFileSystem fs = MemoryFileSystem.test(); + final File screenshotFile = fs.file('screenshot.png'); + + final CustomDevice device = CustomDevice( + config: testConfig, + logger: BufferLogger.test(), + processManager: processManager + ); + + expect(device.supportsScreenshot, true); + + await device.takeScreenshot(screenshotFile); + expect(screenshotCommandWasExecuted, true); + expect(screenshotFile, exists); + }); + + testWithoutContext('CustomDevice without screenshotting support', () async { + bool screenshotCommandWasExecuted = false; + + final FakeProcessManager processManager = FakeProcessManager.list([ + FakeCommand( + command: testConfig.screenshotCommand, + onRun: () => screenshotCommandWasExecuted = true, + ) + ]); + + final MemoryFileSystem fs = MemoryFileSystem.test(); + final File screenshotFile = fs.file('screenshot.png'); + + final CustomDevice device = CustomDevice( + config: testConfig.copyWith( + explicitScreenshotCommand: true, + screenshotCommand: null + ), + logger: BufferLogger.test(), + processManager: processManager + ); + + expect(device.supportsScreenshot, false); + expect( + () => device.takeScreenshot(screenshotFile), + throwsA(const TypeMatcher()), + ); + expect(screenshotCommandWasExecuted, false); + expect(screenshotFile.existsSync(), false); + }); } class FakeBundleBuilder extends Fake implements BundleBuilder {