Implement hot reload using the DDC library bundle format (#162498)

https://github.com/dart-lang/webdev/issues/2516

- Updates restart/reload code to accept a resetCompiler boolean to
disambiguate between whether this is a full restart and whether to reset
the resident compiler.
- Adds code to call reloadSources in DWDS and handle the response
(including any errors).
- Adds code to invoke reassemble.
- Adds code to emit a script that DWDS can later consume that contains
the changed sources and their associated libraries. This is used to hot
reload. The bootstrapper puts this in the global window. DWDS should be
updated to accept it in the provider itself. See
https://github.com/dart-lang/webdev/issues/2584.
- Adds code to parse module metadata from the frontend server. This is
identical to the implementation in DWDS % addressing type-related lints.
- Adds tests that run the existing hot reload tests but with web. Some
modifications are mode, including waiting for Flutter runs to finish
executing, and skipping a test that's not possible on the web.

Needs DWDS 24.3.4 to be published first and used before we can land.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.
This commit is contained in:
Srujan Gaddam
2025-02-02 22:10:24 -08:00
committed by GitHub
parent 4d08217f8d
commit 8e2a6fc3fd
21 changed files with 866 additions and 360 deletions

View File

@@ -488,6 +488,7 @@ class FakeDevFS extends Fake implements DevFS {
AssetBundle? bundle,
bool bundleFirstUpload = false,
bool fullRestart = false,
bool resetCompiler = false,
String? projectRootPath,
File? dartPluginRegistrant,
}) async {

View File

@@ -1663,6 +1663,7 @@ class FakeWebDevFS extends Fake implements WebDevFS {
AssetBundle? bundle,
bool bundleFirstUpload = false,
bool fullRestart = false,
bool resetCompiler = false,
String? projectRootPath,
File? dartPluginRegistrant,
}) async {

View File

@@ -5,204 +5,9 @@
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:async';
import 'package:file/file.dart';
import 'package:vm_service/vm_service.dart';
import '../src/common.dart';
import 'test_data/hot_reload_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';
import 'test_data/hot_reload_test_common.dart';
void main() {
late Directory tempDir;
final HotReloadProject project = HotReloadProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('hot reload works without error', () async {
await flutter.run();
await flutter.hotReload();
});
testWithoutContext('multiple overlapping hot reload are debounced and queued', () async {
await flutter.run();
// Capture how many *real* hot reloads occur.
int numReloads = 0;
final StreamSubscription<void> subscription = flutter.stdout
.map(parseFlutterResponse)
.where(_isHotReloadCompletionEvent)
.listen((_) => numReloads++);
// To reduce tests flaking, override the debounce timer to something higher than
// the default to ensure the hot reloads that are supposed to arrive within the
// debounce period will even on slower CI machines.
const int hotReloadDebounceOverrideMs = 250;
const Duration delay = Duration(milliseconds: hotReloadDebounceOverrideMs * 2);
Future<void> doReload([void _]) =>
flutter.hotReload(debounce: true, debounceDurationOverrideMs: hotReloadDebounceOverrideMs);
try {
await Future.wait<void>(<Future<void>>[
doReload(),
doReload(),
Future<void>.delayed(delay).then(doReload),
Future<void>.delayed(delay).then(doReload),
]);
// We should only get two reloads, as the first two will have been
// merged together by the debounce, and the second two also.
expect(numReloads, equals(2));
} finally {
await subscription.cancel();
}
});
testWithoutContext('newly added code executes during hot reload', () async {
final StringBuffer stdout = StringBuffer();
final Completer<void> completer = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String e) {
stdout.writeln(e);
// If hot reload properly executes newly added code, the 'RELOAD WORKED' message should
// be printed before 'TICK 2'. If we don't wait for some signal that the build method
// has executed after the reload, this test can encounter a race.
if (e.contains('((((TICK 2))))')) {
completer.complete();
}
});
await flutter.run();
project.uncommentHotReloadPrint();
try {
await flutter.hotReload();
await completer.future;
expect(stdout.toString(), contains('(((((RELOAD WORKED)))))'));
} finally {
await subscription.cancel();
}
});
testWithoutContext('hot restart works without error', () async {
await flutter.run(verbose: true);
await flutter.hotRestart();
});
testWithoutContext('breakpoints are hit after hot reload', () async {
Isolate isolate;
final Completer<void> sawTick1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
if (line.contains('((((TICK 1))))')) {
expect(sawTick1.isCompleted, isFalse);
sawTick1.complete();
}
if (line.contains('The application is paused in the debugger on a breakpoint.')) {
expect(sawDebuggerPausedMessage.isCompleted, isFalse);
sawDebuggerPausedMessage.complete();
}
});
await flutter.run(withDebugger: true, startPaused: true);
await flutter
.resume(); // we start paused so we can set up our TICK 1 listener before the app starts
unawaited(
sawTick1.future.timeout(
const Duration(seconds: 5),
onTimeout: () {
// This print is useful for people debugging this test. Normally we would avoid printing in
// a test but this is an exception because it's useful ambient information.
// ignore: avoid_print
print('The test app is taking longer than expected to print its synchronization line...');
},
),
);
printOnFailure('waiting for synchronization line...');
await sawTick1.future; // after this, app is in steady state
await flutter.addBreakpoint(project.scheduledBreakpointUri, project.scheduledBreakpointLine);
await Future<void>.delayed(const Duration(seconds: 2));
await flutter.hotReload(); // reload triggers code which eventually hits the breakpoint
isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
await flutter.resume();
await flutter.addBreakpoint(project.buildBreakpointUri, project.buildBreakpointLine);
bool reloaded = false;
final Future<void> reloadFuture = flutter.hotReload().then((void value) {
reloaded = true;
});
printOnFailure('waiting for pause...');
isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
printOnFailure('waiting for debugger message...');
await sawDebuggerPausedMessage.future;
expect(reloaded, isFalse);
printOnFailure('waiting for resume...');
await flutter.resume();
printOnFailure('waiting for reload future...');
await reloadFuture;
expect(reloaded, isTrue);
reloaded = false;
printOnFailure('subscription cancel...');
await subscription.cancel();
});
testWithoutContext("hot reload doesn't reassemble if paused", () async {
final Completer<void> sawTick1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage2 = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
printOnFailure('[LOG]:"$line"');
if (line.contains('(((TICK 1)))')) {
expect(sawTick1.isCompleted, isFalse);
sawTick1.complete();
}
if (line.contains('The application is paused in the debugger on a breakpoint.')) {
expect(sawDebuggerPausedMessage1.isCompleted, isFalse);
sawDebuggerPausedMessage1.complete();
}
if (line.contains(
'The application is paused in the debugger on a breakpoint; interface might not update.',
)) {
expect(sawDebuggerPausedMessage2.isCompleted, isFalse);
sawDebuggerPausedMessage2.complete();
}
});
await flutter.run(withDebugger: true);
await Future<void>.delayed(const Duration(seconds: 1));
await sawTick1.future;
await flutter.addBreakpoint(project.buildBreakpointUri, project.buildBreakpointLine);
bool reloaded = false;
await Future<void>.delayed(const Duration(seconds: 1));
final Future<void> reloadFuture = flutter.hotReload().then((void value) {
reloaded = true;
});
final Isolate isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
expect(reloaded, isFalse);
await sawDebuggerPausedMessage1
.future; // this is the one where it say "uh, you broke into the debugger while reloading"
await reloadFuture; // this is the one where it times out because you're in the debugger
expect(reloaded, isTrue);
await flutter.hotReload(); // now we're already paused
await sawDebuggerPausedMessage2.future; // so we just get told that nothing is going to happen
await flutter.resume();
await subscription.cancel();
});
}
bool _isHotReloadCompletionEvent(Map<String, Object?>? event) {
return event != null &&
event['event'] == 'app.progress' &&
event['params'] != null &&
(event['params']! as Map<String, Object?>)['progressId'] == 'hot.reload' &&
(event['params']! as Map<String, Object?>)['finished'] == true;
testAll();
}

View File

@@ -5,82 +5,9 @@
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:async';
import 'package:file/file.dart';
import '../src/common.dart';
import 'test_data/hot_reload_with_asset.dart';
import 'test_driver.dart';
import 'test_utils.dart';
import 'test_data/hot_reload_with_asset_test_common.dart';
void main() {
late Directory tempDir;
final HotReloadWithAssetProject project = HotReloadWithAssetProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('hot reload does not need to sync assets on the first reload', () async {
final Completer<void> onFirstLoad = Completer<void>();
final Completer<void> onSecondLoad = Completer<void>();
flutter.stdout.listen((String line) {
// If the asset fails to load, this message will be printed instead.
// this indicates that the devFS was not able to locate the asset
// after the hot reload.
if (line.contains('FAILED TO LOAD')) {
fail('Did not load asset: $line');
}
if (line.contains('LOADED DATA')) {
onFirstLoad.complete();
}
if (line.contains('SECOND DATA')) {
onSecondLoad.complete();
}
});
flutter.stdout.listen(printOnFailure);
await flutter.run();
await onFirstLoad.future;
project.uncommentHotReloadPrint();
await flutter.hotReload();
await onSecondLoad.future;
});
testWithoutContext('hot restart does not need to sync assets on the first reload', () async {
final Completer<void> onFirstLoad = Completer<void>();
final Completer<void> onSecondLoad = Completer<void>();
flutter.stdout.listen((String line) {
// If the asset fails to load, this message will be printed instead.
// this indicates that the devFS was not able to locate the asset
// after the hot reload.
if (line.contains('FAILED TO LOAD')) {
fail('Did not load asset: $line');
}
if (line.contains('LOADED DATA')) {
onFirstLoad.complete();
}
if (line.contains('SECOND DATA')) {
onSecondLoad.complete();
}
});
flutter.stdout.listen(printOnFailure);
await flutter.run();
await onFirstLoad.future;
project.uncommentHotReloadPrint();
await flutter.hotRestart();
await onSecondLoad.future;
});
testAll();
}

View File

@@ -5,46 +5,9 @@
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:async';
import 'package:file/file.dart';
import '../src/common.dart';
import 'test_data/stateless_stateful_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';
import 'test_data/stateless_stateful_hot_reload_test_common.dart';
// This test verifies that we can hot reload a stateless widget into a
// stateful one and back.
void main() {
late Directory tempDir;
final HotReloadProject project = HotReloadProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('Can switch from stateless to stateful', () async {
await flutter.run();
await flutter.hotReload();
final StringBuffer stdout = StringBuffer();
final StreamSubscription<String> subscription = flutter.stdout.listen(stdout.writeln);
// switch to stateful.
project.toggleState();
await flutter.hotReload();
final String logs = stdout.toString();
expect(logs, contains('STATEFUL'));
await subscription.cancel();
});
testAll();
}

View File

@@ -0,0 +1,247 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import 'package:vm_service/vm_service.dart';
import '../../src/common.dart';
import '../test_driver.dart';
import '../test_utils.dart';
import 'hot_reload_project.dart';
void testAll({
bool chrome = false,
List<String> additionalCommandArgs = const <String>[],
Object? skip = false,
}) {
group('chrome: $chrome'
'${additionalCommandArgs.isEmpty ? '' : ' with args: $additionalCommandArgs'}', () {
late Directory tempDir;
final HotReloadProject project = HotReloadProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('hot reload works without error', () async {
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
await flutter.hotReload();
});
testWithoutContext('multiple overlapping hot reload are debounced and queued', () async {
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
// Capture how many *real* hot reloads occur.
int numReloads = 0;
final StreamSubscription<void> subscription = flutter.stdout
.map(parseFlutterResponse)
.where(_isHotReloadCompletionEvent)
.listen((_) => numReloads++);
// To reduce tests flaking, override the debounce timer to something higher than
// the default to ensure the hot reloads that are supposed to arrive within the
// debounce period will even on slower CI machines.
const int hotReloadDebounceOverrideMs = 250;
const Duration delay = Duration(milliseconds: hotReloadDebounceOverrideMs * 2);
Future<void> doReload([void _]) => flutter.hotReload(
debounce: true,
debounceDurationOverrideMs: hotReloadDebounceOverrideMs,
);
try {
await Future.wait<void>(<Future<void>>[
doReload(),
doReload(),
Future<void>.delayed(delay).then(doReload),
Future<void>.delayed(delay).then(doReload),
]);
// We should only get two reloads, as the first two will have been
// merged together by the debounce, and the second two also.
expect(numReloads, equals(2));
} finally {
await subscription.cancel();
}
});
testWithoutContext('newly added code executes during hot reload', () async {
final StringBuffer stdout = StringBuffer();
final Completer<void> sawTick1 = Completer<void>();
final Completer<void> sawTick2 = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String e) {
stdout.writeln(e);
// Initial run should run the build method before we try and hot reload.
if (e.contains('(((TICK 1)))')) {
sawTick1.complete();
}
// If hot reload properly executes newly added code, the 'RELOAD WORKED' message should
// be printed before 'TICK 2'. If we don't wait for some signal that the build method
// has executed after the reload, this test can encounter a race.
if (e.contains('((((TICK 2))))')) {
sawTick2.complete();
}
});
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
await sawTick1.future;
project.uncommentHotReloadPrint();
try {
await flutter.hotReload();
await sawTick2.future;
expect(stdout.toString(), contains('(((((RELOAD WORKED)))))'));
} finally {
await subscription.cancel();
}
});
testWithoutContext('hot restart works without error', () async {
await flutter.run(
verbose: true,
chrome: chrome,
additionalCommandArgs: additionalCommandArgs,
);
await flutter.hotRestart();
});
testWithoutContext('breakpoints are hit after hot reload', () async {
Isolate isolate;
final Completer<void> sawTick1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
if (line.contains('((((TICK 1))))')) {
expect(sawTick1.isCompleted, isFalse);
sawTick1.complete();
}
if (line.contains('The application is paused in the debugger on a breakpoint.')) {
expect(sawDebuggerPausedMessage.isCompleted, isFalse);
sawDebuggerPausedMessage.complete();
}
});
await flutter.run(
withDebugger: true,
startPaused: true,
chrome: chrome,
additionalCommandArgs: additionalCommandArgs,
);
await flutter
.resume(); // we start paused so we can set up our TICK 1 listener before the app starts
unawaited(
sawTick1.future.timeout(
const Duration(seconds: 5),
onTimeout: () {
// This print is useful for people debugging this test. Normally we would avoid printing
// in a test but this is an exception because it's useful ambient information.
// ignore: avoid_print
print(
'The test app is taking longer than expected to print its synchronization line...',
);
},
),
);
printOnFailure('waiting for synchronization line...');
await sawTick1.future; // after this, app is in steady state
await flutter.addBreakpoint(project.scheduledBreakpointUri, project.scheduledBreakpointLine);
await Future<void>.delayed(const Duration(seconds: 2));
await flutter.hotReload(); // reload triggers code which eventually hits the breakpoint
isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
await flutter.resume();
await flutter.addBreakpoint(project.buildBreakpointUri, project.buildBreakpointLine);
bool reloaded = false;
final Future<void> reloadFuture = flutter.hotReload().then((void value) {
reloaded = true;
});
printOnFailure('waiting for pause...');
isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
if (!chrome) {
// TODO(srujzs): Implement paused event messages for the web.
// https://github.com/flutter/flutter/issues/162500
printOnFailure('waiting for debugger message...');
await sawDebuggerPausedMessage.future;
}
expect(reloaded, isFalse);
printOnFailure('waiting for resume...');
await flutter.resume();
printOnFailure('waiting for reload future...');
await reloadFuture;
expect(reloaded, isTrue);
reloaded = false;
printOnFailure('subscription cancel...');
await subscription.cancel();
});
testWithoutContext(
"hot reload doesn't reassemble if paused",
() async {
final Completer<void> sawTick1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage1 = Completer<void>();
final Completer<void> sawDebuggerPausedMessage2 = Completer<void>();
final StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
printOnFailure('[LOG]:"$line"');
if (line.contains('(((TICK 1)))')) {
expect(sawTick1.isCompleted, isFalse);
sawTick1.complete();
}
if (line.contains('The application is paused in the debugger on a breakpoint.')) {
expect(sawDebuggerPausedMessage1.isCompleted, isFalse);
sawDebuggerPausedMessage1.complete();
}
if (line.contains(
'The application is paused in the debugger on a breakpoint; interface might not '
'update.',
)) {
expect(sawDebuggerPausedMessage2.isCompleted, isFalse);
sawDebuggerPausedMessage2.complete();
}
});
await flutter.run(
withDebugger: true,
chrome: chrome,
additionalCommandArgs: additionalCommandArgs,
);
await Future<void>.delayed(const Duration(seconds: 1));
await sawTick1.future;
await flutter.addBreakpoint(project.buildBreakpointUri, project.buildBreakpointLine);
bool reloaded = false;
await Future<void>.delayed(const Duration(seconds: 1));
final Future<void> reloadFuture = flutter.hotReload().then((void value) {
reloaded = true;
});
final Isolate isolate = await flutter.waitForPause();
expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
expect(reloaded, isFalse);
// this is the one where it say "uh, you broke into the debugger while reloading"
await sawDebuggerPausedMessage1.future;
await reloadFuture; // this is the one where it times out because you're in the debugger
expect(reloaded, isTrue);
await flutter.hotReload(); // now we're already paused
await sawDebuggerPausedMessage2
.future; // so we just get told that nothing is going to happen
await flutter.resume();
await subscription.cancel();
},
// On the web, hot reload cannot continue as the browser is paused and there are no multiple
// isolates, so this test will wait forever.
skip: chrome,
);
}, skip: skip);
}
bool _isHotReloadCompletionEvent(Map<String, Object?>? event) {
return event != null &&
event['event'] == 'app.progress' &&
event['params'] != null &&
(event['params']! as Map<String, Object?>)['progressId'] == 'hot.reload' &&
(event['params']! as Map<String, Object?>)['finished'] == true;
}

View File

@@ -0,0 +1,90 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import '../../src/common.dart';
import '../test_driver.dart';
import '../test_utils.dart';
import 'hot_reload_with_asset.dart';
void testAll({
bool chrome = false,
List<String> additionalCommandArgs = const <String>[],
Object? skip = false,
}) {
group('chrome: $chrome'
'${additionalCommandArgs.isEmpty ? '' : ' with args: $additionalCommandArgs'}', () {
late Directory tempDir;
final HotReloadWithAssetProject project = HotReloadWithAssetProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('hot reload does not need to sync assets on the first reload', () async {
final Completer<void> onFirstLoad = Completer<void>();
final Completer<void> onSecondLoad = Completer<void>();
flutter.stdout.listen((String line) {
// If the asset fails to load, this message will be printed instead.
// this indicates that the devFS was not able to locate the asset
// after the hot reload.
if (line.contains('FAILED TO LOAD')) {
fail('Did not load asset: $line');
}
if (line.contains('LOADED DATA')) {
onFirstLoad.complete();
}
if (line.contains('SECOND DATA')) {
onSecondLoad.complete();
}
});
flutter.stdout.listen(printOnFailure);
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
await onFirstLoad.future;
project.uncommentHotReloadPrint();
await flutter.hotReload();
await onSecondLoad.future;
});
testWithoutContext('hot restart does not need to sync assets on the first reload', () async {
final Completer<void> onFirstLoad = Completer<void>();
final Completer<void> onSecondLoad = Completer<void>();
flutter.stdout.listen((String line) {
// If the asset fails to load, this message will be printed instead.
// this indicates that the devFS was not able to locate the asset
// after the hot reload.
if (line.contains('FAILED TO LOAD')) {
fail('Did not load asset: $line');
}
if (line.contains('LOADED DATA')) {
onFirstLoad.complete();
}
if (line.contains('SECOND DATA')) {
onSecondLoad.complete();
}
});
flutter.stdout.listen(printOnFailure);
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
await onFirstLoad.future;
project.uncommentHotReloadPrint();
await flutter.hotRestart();
await onSecondLoad.future;
});
}, skip: skip);
}

View File

@@ -0,0 +1,64 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/file.dart';
import '../../src/common.dart';
import '../test_data/stateless_stateful_project.dart';
import '../test_driver.dart';
import '../test_utils.dart';
// This test verifies that we can hot reload a stateless widget into a
// stateful one and back.
void testAll({
bool chrome = false,
List<String> additionalCommandArgs = const <String>[],
Object? skip = false,
}) {
group('chrome: $chrome'
'${additionalCommandArgs.isEmpty ? '' : ' with args: $additionalCommandArgs'}', () {
late Directory tempDir;
final HotReloadProject project = HotReloadProject();
late FlutterRunTestDriver flutter;
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_reload_test.');
await project.setUpIn(tempDir);
flutter = FlutterRunTestDriver(tempDir);
});
tearDown(() async {
await flutter.stop();
tryToDelete(tempDir);
});
testWithoutContext('Can switch from stateless to stateful', () async {
final Completer<void> completer = Completer<void>();
StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
if (line.contains('STATELESS')) {
completer.complete();
}
});
await flutter.run(chrome: chrome, additionalCommandArgs: additionalCommandArgs);
// Wait for run to finish.
await completer.future;
await subscription.cancel();
await flutter.hotReload();
final StringBuffer stdout = StringBuffer();
subscription = flutter.stdout.listen(stdout.writeln);
// switch to stateful.
project.toggleState();
await flutter.hotReload();
final String logs = stdout.toString();
expect(logs, contains('STATEFUL'));
await subscription.cancel();
});
}, skip: skip);
}

View File

@@ -0,0 +1,22 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:io';
import '../integration.shard/test_data/hot_reload_test_common.dart';
import '../src/common.dart';
void main() {
testAll(
chrome: true,
additionalCommandArgs: <String>[
'--extra-front-end-options=--dartdevc-canary,--dartdevc-module-format=ddc',
],
// https://github.com/flutter/flutter/issues/162567
skip: Platform.isWindows,
);
}

View File

@@ -0,0 +1,22 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:io';
import '../integration.shard/test_data/hot_reload_with_asset_test_common.dart';
import '../src/common.dart';
void main() {
testAll(
chrome: true,
additionalCommandArgs: <String>[
'--extra-front-end-options=--dartdevc-canary,--dartdevc-module-format=ddc',
],
// https://github.com/flutter/flutter/issues/162567
skip: Platform.isWindows,
);
}

View File

@@ -7,7 +7,7 @@ library;
import '../src/common.dart';
import 'test_data/hot_restart_web_utils.dart';
import 'test_data/hot_restart_web_test_common.dart';
void main() async {
await testAll(useDDCLibraryBundleFormat: false);

View File

@@ -7,7 +7,7 @@ library;
import '../src/common.dart';
import 'test_data/hot_restart_web_utils.dart';
import 'test_data/hot_restart_web_test_common.dart';
void main() async {
await testAll(useDDCLibraryBundleFormat: true);

View File

@@ -0,0 +1,22 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@Tags(<String>['flutter-test-driver'])
library;
import 'dart:io';
import '../integration.shard/test_data/stateless_stateful_hot_reload_test_common.dart';
import '../src/common.dart';
void main() {
testAll(
chrome: true,
additionalCommandArgs: <String>[
'--extra-front-end-options=--dartdevc-canary,--dartdevc-module-format=ddc',
],
// https://github.com/flutter/flutter/issues/162567
skip: Platform.isWindows,
);
}

View File

@@ -72,11 +72,13 @@ Future<void> _testProject(
late Directory tempDir;
late FlutterRunTestDriver flutter;
final String testName = 'Hot restart (index.html: $name)';
final List<String> additionalCommandArgs =
useDDCLibraryBundleFormat
? <String>['--extra-front-end-options=--dartdevc-canary,--dartdevc-module-format=ddc']
: <String>[];
final String testName =
'Hot restart (index.html: $name)'
'${additionalCommandArgs.isEmpty ? '' : ' with args: $additionalCommandArgs'}';
setUp(() async {
tempDir = createResolvedTempDirectorySync('hot_restart_test.');