[ Widget Preview ] Add experimental support for web-based widget preview environment (#163154)

`flutter widget-preview start --web` will cause the widget preview
scaffold to be run as a Flutter Web application using experimental hot
reload support. This will eventually be the default, with the desktop
environment being put behind a flag for use as a fallback under the
assumption that the desktop environment will be removed in the future.
This commit is contained in:
Ben Konyi
2025-02-14 15:16:02 -05:00
committed by GitHub
parent 3e219d1cd8
commit 6e7e36fdd3
5 changed files with 110 additions and 32 deletions

View File

@@ -7,6 +7,7 @@ import 'dart:convert';
import 'package:file/file.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/commands/widget_preview.dart';
import 'package:process/process.dart';
import '../src/common.dart';
@@ -26,6 +27,12 @@ const List<String> subsequentLaunchMessages = <String>[
'Done loading previews.',
];
const List<String> firstLaunchMessagesWeb = <String>[
'Creating widget preview scaffolding at:',
'Loading previews into the Widget Preview Scaffold...',
'Done loading previews.',
];
void main() {
late Directory tempDir;
Process? process;
@@ -43,13 +50,21 @@ void main() {
tryToDelete(tempDir);
});
Future<void> runWidgetPreview({required List<String> expectedMessages}) async {
Future<void> runWidgetPreview({
required List<String> expectedMessages,
bool useWeb = false,
}) async {
expect(expectedMessages, isNotEmpty);
int i = 0;
process = await processManager.start(<String>[
flutterBin,
'widget-preview',
'start',
'--verbose',
if (useWeb) ...<String>[
'--${WidgetPreviewStartCommand.kUseFlutterWeb}',
'--${WidgetPreviewStartCommand.kHeadlessWeb}',
],
], workingDirectory: tempDir.path);
final Completer<void> completer = Completer<void>();
@@ -70,6 +85,16 @@ void main() {
printOnFailure('STDERR: $msg');
});
unawaited(
process!.exitCode.then((int exitCode) {
if (completer.isCompleted) {
return;
}
completer.completeError(
TestFailure('The widget previewer exited unexpectedly (exit code: $exitCode)'),
);
}),
);
await completer.future;
process!.kill();
process = null;
@@ -80,6 +105,10 @@ void main() {
await runWidgetPreview(expectedMessages: firstLaunchMessages);
});
testWithoutContext('web smoke test', () async {
await runWidgetPreview(expectedMessages: firstLaunchMessagesWeb, useWeb: true);
});
testWithoutContext('does not rebuild project on subsequent runs', () async {
// The first run of 'flutter widget-preview start' should generate a new preview scaffold and
// pre-build the application.
@@ -88,5 +117,13 @@ void main() {
// We shouldn't regenerate the scaffold after the initial run.
await runWidgetPreview(expectedMessages: subsequentLaunchMessages);
});
testWithoutContext('does not recreate project on subsequent --web runs', () async {
// The first run of 'flutter widget-preview start --web' should generate a new preview scaffold
await runWidgetPreview(expectedMessages: firstLaunchMessagesWeb, useWeb: true);
// We shouldn't regenerate the scaffold after the initial run.
await runWidgetPreview(expectedMessages: subsequentLaunchMessages, useWeb: true);
});
});
}