[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.
This commit is contained in:
@@ -1102,20 +1102,29 @@ abstract class ResidentRunner {
|
||||
'flutter',
|
||||
'png',
|
||||
);
|
||||
final List<FlutterView> views = await device
|
||||
.vmService.getFlutterViews();
|
||||
List<FlutterView> views = <FlutterView>[];
|
||||
Future<bool> 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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 <Future<vm_service.ReloadReport>>[
|
||||
@@ -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 {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
|
||||
|
||||
@@ -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<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 {
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
|
||||
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: <VmServiceExpectation>[
|
||||
listViews,
|
||||
]);
|
||||
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[]);
|
||||
residentRunner = HotRunner(
|
||||
<FlutterDevice>[
|
||||
mockFlutterDevice,
|
||||
|
||||
Reference in New Issue
Block a user