Skip to content

Commit 5a6cfa7

Browse files
authored
[tool] Migrate ScreenshotCommand to modular dependency injection (#190767)
## Summary Part 9 of the modular dependency injection migration. * Migrates `ScreenshotCommand` to constructor dependency injection with required non-nullable `ToolContext`: ```dart ScreenshotCommand({ required super.toolContext, VMServiceConnector? vmServiceConnector, }) ``` * Eliminates ambient `globals.dart` usage and `fs` constructor fallback parameter in `screenshot.dart`, resolving `fs`, `fileSystemUtils`, and `logger` directly from `toolContext`. * Conditionalizes `refreshWirelessDevices` on `argResults == null || stringArg(_kType) == _kDeviceType` to preserve hermetic execution for Skia VM service screenshot mode under `testWithoutContext`. * Wires `toolContext: toolDependencies.toolContext` in `executable.dart`. * Migrates unit tests in `packages/flutter_tools/test/commands.shard/hermetic/screenshot_command_test.dart` to hermetic `testWithoutContext`. Part of #188471
1 parent 88adfb4 commit 5a6cfa7

3 files changed

Lines changed: 186 additions & 67 deletions

File tree

‎packages/flutter_tools/lib/executable.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ List<FlutterCommand> generateCommands({
342342
featureFlags: featureFlags,
343343
),
344344
RunCommand(verboseHelp: verboseHelp),
345-
ScreenshotCommand(fs: toolDependencies.toolContext.fs),
345+
ScreenshotCommand(toolContext: toolDependencies.toolContext),
346346
ShellCompletionCommand(),
347347
TestCommand(
348348
verboseHelp: verboseHelp,

‎packages/flutter_tools/lib/src/commands/screenshot.dart‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import 'package:vm_service/vm_service.dart' as vm_service;
77

88
import '../base/common.dart';
99
import '../base/file_system.dart';
10+
import '../base/logger.dart';
11+
import '../context/tool_context.dart';
1012
import '../convert.dart';
1113
import '../device.dart';
12-
import '../globals.dart' as globals;
1314
import '../runner/flutter_command.dart';
1415
import '../vmservice.dart';
1516

@@ -19,8 +20,10 @@ const _kVmServiceUrl = 'vm-service-url';
1920
const _kDeviceType = 'device';
2021
const _kSkiaType = 'skia';
2122

23+
/// The `flutter screenshot` command, which captures a screenshot from a connected device.
2224
class ScreenshotCommand extends FlutterCommand {
23-
ScreenshotCommand({required this.fs}) {
25+
ScreenshotCommand({required super.toolContext, VMServiceConnector? vmServiceConnector})
26+
: _vmServiceConnector = vmServiceConnector ?? connectToVmService {
2427
argParser.addOption(
2528
_kOut,
2629
abbr: 'o',
@@ -54,7 +57,12 @@ class ScreenshotCommand extends FlutterCommand {
5457
usesDeviceConnectionOption();
5558
}
5659

57-
final FileSystem fs;
60+
final VMServiceConnector _vmServiceConnector;
61+
62+
FileSystem get fs => toolContext.fs;
63+
64+
@override
65+
ToolContext get toolContext => super.toolContext!;
5866

5967
@override
6068
String get name => 'screenshot';
@@ -66,7 +74,7 @@ class ScreenshotCommand extends FlutterCommand {
6674
final String category = FlutterCommandCategory.tools;
6775

6876
@override
69-
bool get refreshWirelessDevices => true;
77+
bool get refreshWirelessDevices => argResults == null || stringArg(_kType) == _kDeviceType;
7078

7179
@override
7280
final aliases = <String>['pic'];
@@ -104,6 +112,7 @@ class ScreenshotCommand extends FlutterCommand {
104112

105113
@override
106114
Future<FlutterCommandResult> runCommand() async {
115+
final FileSystem fs = toolContext.fs;
107116
File? outputFile;
108117
if (argResults?.wasParsed(_kOut) ?? false) {
109118
outputFile = fs.file(stringArg(_kOut));
@@ -121,7 +130,9 @@ class ScreenshotCommand extends FlutterCommand {
121130
}
122131

123132
Future<void> runScreenshot(File? outputFile) async {
124-
outputFile ??= globals.fsUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'png');
133+
final FileSystem fs = toolContext.fs;
134+
final FileSystemUtils fileSystemUtils = toolContext.fileSystemUtils;
135+
outputFile ??= fileSystemUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'png');
125136

126137
try {
127138
await device!.takeScreenshot(outputFile);
@@ -144,20 +155,20 @@ class ScreenshotCommand extends FlutterCommand {
144155
}
145156

146157
Future<bool> runSkia(File? outputFile) async {
158+
final FileSystem fs = toolContext.fs;
159+
final FileSystemUtils fileSystemUtils = toolContext.fileSystemUtils;
160+
final Logger logger = toolContext.logger;
147161
final Uri vmServiceUrl = Uri.parse(stringArg(_kVmServiceUrl)!);
148-
final FlutterVmService vmService = await connectToVmService(
149-
vmServiceUrl,
150-
logger: globals.logger,
151-
);
162+
final FlutterVmService vmService = await _vmServiceConnector(vmServiceUrl, logger: logger);
152163
final vm_service.Response? skp = await vmService.screenshotSkp();
153164
if (skp == null) {
154-
globals.printError(
165+
logger.printError(
155166
'The Skia picture request failed, probably because the device was '
156167
'disconnected',
157168
);
158169
return false;
159170
}
160-
outputFile ??= globals.fsUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'skp');
171+
outputFile ??= fileSystemUtils.getUniqueFile(fs.currentDirectory, 'flutter', 'skp');
161172
final IOSink sink = outputFile.openWrite();
162173
sink.add(base64.decode(skp.json?['skp'] as String));
163174
await sink.close();
@@ -189,9 +200,9 @@ class ScreenshotCommand extends FlutterCommand {
189200
}
190201

191202
void _showOutputFileInfo(File outputFile) {
192-
final int sizeKB = (outputFile.lengthSync()) ~/ 1024;
193-
globals.printStatus(
194-
'Screenshot written to ${fs.path.relative(outputFile.path)} (${sizeKB}kB).',
195-
);
203+
final FileSystem fs = toolContext.fs;
204+
final Logger logger = toolContext.logger;
205+
final int sizeKB = outputFile.lengthSync() ~/ 1024;
206+
logger.printStatus('Screenshot written to ${fs.path.relative(outputFile.path)} (${sizeKB}kB).');
196207
}
197208
}

0 commit comments

Comments
 (0)