From c034f1a1cc7a044d2c6cf98e4d7bf568331231fe Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 2 Sep 2020 17:33:41 -0700 Subject: [PATCH] [flutter_tools] fix screenshot command in release mode and help documentation (#65114) Currently taking a screenshot in release mode crashes and is also not documented as a supported command. Fix both of these and add test cases. --- .../lib/src/resident_runner.dart | 46 +++++++------- packages/flutter_tools/lib/src/run_cold.dart | 6 +- .../general.shard/resident_runner_test.dart | 62 +++++++++++++++++-- 3 files changed, 82 insertions(+), 32 deletions(-) diff --git a/packages/flutter_tools/lib/src/resident_runner.dart b/packages/flutter_tools/lib/src/resident_runner.dart index ec645f39e5..be1bd294e5 100644 --- a/packages/flutter_tools/lib/src/resident_runner.dart +++ b/packages/flutter_tools/lib/src/resident_runner.dart @@ -1102,20 +1102,29 @@ abstract class ResidentRunner { 'flutter', 'png', ); - final List views = await device - .vmService.getFlutterViews(); + List views = []; + Future setDebugBanner(bool value) async { + try { + for (final FlutterView view in views) { + await device.vmService.flutterDebugAllowBanner( + value, + isolateId: view.uiIsolate.id, + ); + } + return true; + } on Exception catch (error) { + status.cancel(); + globals.printError('Error communicating with Flutter on the device: $error'); + return false; + } + } + try { if (supportsServiceProtocol && isRunningDebug) { - try { - for (final FlutterView view in views) { - await device.vmService.flutterDebugAllowBanner( - false, - isolateId: view.uiIsolate.id, - ); - } - } on Exception catch (error) { - status.cancel(); - globals.printError('Error communicating with Flutter on the device: $error'); + // Ensure that the vmService access is guarded by supportsServiceProtocol, it + // will be null in release mode. + views = await device.vmService.getFlutterViews(); + if (!await setDebugBanner(false)) { return; } } @@ -1123,18 +1132,7 @@ abstract class ResidentRunner { await device.device.takeScreenshot(outputFile); } finally { if (supportsServiceProtocol && isRunningDebug) { - try { - for (final FlutterView view in views) { - await device.vmService.flutterDebugAllowBanner( - true, - isolateId: view.uiIsolate.id, - ); - } - } on Exception catch (error) { - status.cancel(); - globals.printError('Error communicating with Flutter on the device: $error'); - return; - } + await setDebugBanner(true); } } final int sizeKB = outputFile.lengthSync() ~/ 1024; diff --git a/packages/flutter_tools/lib/src/run_cold.dart b/packages/flutter_tools/lib/src/run_cold.dart index 576e1db8f1..da1012897a 100644 --- a/packages/flutter_tools/lib/src/run_cold.dart +++ b/packages/flutter_tools/lib/src/run_cold.dart @@ -185,10 +185,8 @@ class ColdRunner extends ResidentRunner { @override void printHelp({ @required bool details }) { globals.printStatus('Flutter run key commands.'); - if (supportsServiceProtocol) { - if (details) { - printHelpDetails(); - } + if (details) { + printHelpDetails(); } commandHelp.h.print(); if (_didAttach) { diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 46fa510ced..6061d4830d 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -188,7 +188,7 @@ void main() { return testUri; }); when(mockFlutterDevice.vmService).thenAnswer((Invocation invocation) { - return fakeVmServiceHost.vmService; + return fakeVmServiceHost?.vmService; }); when(mockFlutterDevice.reloadSources(any, pause: anyNamed('pause'))).thenAnswer((Invocation invocation) async { return >[ @@ -1289,6 +1289,42 @@ void main() { )); })); + testUsingContext('ResidentRunner printHelpDetails cold runner', () => testbed.run(() { + when(mockDevice.supportsHotRestart).thenReturn(true); + when(mockDevice.supportsScreenshot).thenReturn(true); + fakeVmServiceHost = null; + residentRunner = ColdRunner( + [ + mockFlutterDevice, + ], + stayResident: false, + debuggingOptions: DebuggingOptions.disabled(BuildInfo.release), + ); + residentRunner.printHelp(details: true); + + final CommandHelp commandHelp = residentRunner.commandHelp; + + // does not supports service protocol + expect(residentRunner.supportsServiceProtocol, false); + // isRunningDebug + expect(residentRunner.isRunningDebug, false); + // does not support CanvasKit + expect(residentRunner.supportsCanvasKit, false); + // does support SkSL + expect(residentRunner.supportsWriteSkSL, false); + // commands + expect(testLogger.statusText, equals( + [ + 'Flutter run key commands.', + commandHelp.s, + commandHelp.h, + commandHelp.c, + commandHelp.q, + '' + ].join('\n') + )); + })); + testUsingContext('ResidentRunner does support CanvasKit', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: []); @@ -1395,6 +1431,26 @@ void main() { expect(fakeVmServiceHost.hasRemainingExpectations, false); })); + testUsingContext('ResidentRunner can take screenshot on release device', () => testbed.run(() async { + when(mockDevice.supportsScreenshot).thenReturn(true); + when(mockDevice.takeScreenshot(any)) + .thenAnswer((Invocation invocation) async { + final File file = invocation.positionalArguments.first as File; + file.writeAsBytesSync(List.generate(1024, (int i) => i)); + }); + + residentRunner = ColdRunner( + [ + mockFlutterDevice, + ], + stayResident: false, + debuggingOptions: DebuggingOptions.disabled(BuildInfo.release), + ); + await residentRunner.screenshot(mockFlutterDevice); + + expect(testLogger.statusText, contains('1kB')); + })); + testUsingContext('ResidentRunner clears the screen when it should', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: []); const String message = 'This should be cleared'; @@ -1488,9 +1544,7 @@ void main() { })); testUsingContext('ResidentRunner does not toggle banner in non-debug mode', () => testbed.run(() async { - fakeVmServiceHost = FakeVmServiceHost(requests: [ - listViews, - ]); + fakeVmServiceHost = FakeVmServiceHost(requests: []); residentRunner = HotRunner( [ mockFlutterDevice,