Unverified Commit c034f1a1 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[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.
parent 6d46ff7e
...@@ -1102,20 +1102,29 @@ abstract class ResidentRunner { ...@@ -1102,20 +1102,29 @@ abstract class ResidentRunner {
'flutter', 'flutter',
'png', 'png',
); );
final List<FlutterView> views = await device List<FlutterView> views = <FlutterView>[];
.vmService.getFlutterViews(); Future<bool> setDebugBanner(bool value) async {
try {
if (supportsServiceProtocol && isRunningDebug) {
try { try {
for (final FlutterView view in views) { for (final FlutterView view in views) {
await device.vmService.flutterDebugAllowBanner( await device.vmService.flutterDebugAllowBanner(
false, value,
isolateId: view.uiIsolate.id, isolateId: view.uiIsolate.id,
); );
} }
return true;
} on Exception catch (error) { } on Exception catch (error) {
status.cancel(); status.cancel();
globals.printError('Error communicating with Flutter on the device: $error'); globals.printError('Error communicating with Flutter on the device: $error');
return false;
}
}
try {
if (supportsServiceProtocol && isRunningDebug) {
// 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; return;
} }
} }
...@@ -1123,18 +1132,7 @@ abstract class ResidentRunner { ...@@ -1123,18 +1132,7 @@ abstract class ResidentRunner {
await device.device.takeScreenshot(outputFile); await device.device.takeScreenshot(outputFile);
} finally { } finally {
if (supportsServiceProtocol && isRunningDebug) { if (supportsServiceProtocol && isRunningDebug) {
try { await setDebugBanner(true);
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;
}
} }
} }
final int sizeKB = outputFile.lengthSync() ~/ 1024; final int sizeKB = outputFile.lengthSync() ~/ 1024;
......
...@@ -185,11 +185,9 @@ class ColdRunner extends ResidentRunner { ...@@ -185,11 +185,9 @@ class ColdRunner extends ResidentRunner {
@override @override
void printHelp({ @required bool details }) { void printHelp({ @required bool details }) {
globals.printStatus('Flutter run key commands.'); globals.printStatus('Flutter run key commands.');
if (supportsServiceProtocol) {
if (details) { if (details) {
printHelpDetails(); printHelpDetails();
} }
}
commandHelp.h.print(); commandHelp.h.print();
if (_didAttach) { if (_didAttach) {
commandHelp.d.print(); commandHelp.d.print();
......
...@@ -188,7 +188,7 @@ void main() { ...@@ -188,7 +188,7 @@ void main() {
return testUri; return testUri;
}); });
when(mockFlutterDevice.vmService).thenAnswer((Invocation invocation) { when(mockFlutterDevice.vmService).thenAnswer((Invocation invocation) {
return fakeVmServiceHost.vmService; return fakeVmServiceHost?.vmService;
}); });
when(mockFlutterDevice.reloadSources(any, pause: anyNamed('pause'))).thenAnswer((Invocation invocation) async { when(mockFlutterDevice.reloadSources(any, pause: anyNamed('pause'))).thenAnswer((Invocation invocation) async {
return <Future<vm_service.ReloadReport>>[ return <Future<vm_service.ReloadReport>>[
...@@ -1289,6 +1289,42 @@ void main() { ...@@ -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(
<FlutterDevice>[
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(
<dynamic>[
'Flutter run key commands.',
commandHelp.s,
commandHelp.h,
commandHelp.c,
commandHelp.q,
''
].join('\n')
));
}));
testUsingContext('ResidentRunner does support CanvasKit', () => testbed.run(() async { testUsingContext('ResidentRunner does support CanvasKit', () => testbed.run(() async {
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]); fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
...@@ -1395,6 +1431,26 @@ void main() { ...@@ -1395,6 +1431,26 @@ void main() {
expect(fakeVmServiceHost.hasRemainingExpectations, false); 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<int>.generate(1024, (int i) => i));
});
residentRunner = ColdRunner(
<FlutterDevice>[
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 { testUsingContext('ResidentRunner clears the screen when it should', () => testbed.run(() async {
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]); fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
const String message = 'This should be cleared'; const String message = 'This should be cleared';
...@@ -1488,9 +1544,7 @@ void main() { ...@@ -1488,9 +1544,7 @@ void main() {
})); }));
testUsingContext('ResidentRunner does not toggle banner in non-debug mode', () => testbed.run(() async { testUsingContext('ResidentRunner does not toggle banner in non-debug mode', () => testbed.run(() async {
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[ fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
listViews,
]);
residentRunner = HotRunner( residentRunner = HotRunner(
<FlutterDevice>[ <FlutterDevice>[
mockFlutterDevice, mockFlutterDevice,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment