diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 3127b37d86..b24839cb38 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -36,6 +36,11 @@ class HotRunnerConfig { Future setupHotRestart() async { return true; } + /// A hook for implementations to perform any necessary operations right + /// before the runner is about to be shut down. + Future runPreShutdownOperations() async { + return; + } } HotRunnerConfig get hotRunnerConfig => context[HotRunnerConfig]; @@ -851,6 +856,7 @@ class HotRunner extends ResidentRunner { @override Future cleanupAfterSignal() async { await stopEchoingDeviceLog(); + await hotRunnerConfig.runPreShutdownOperations(); if (_didAttach) { appFinished(); } else { @@ -859,7 +865,10 @@ class HotRunner extends ResidentRunner { } @override - Future preStop() => _cleanupDevFS(); + Future preStop() async { + await _cleanupDevFS(); + await hotRunnerConfig.runPreShutdownOperations(); + } @override Future cleanupAtFinish() async { diff --git a/packages/flutter_tools/test/hot_test.dart b/packages/flutter_tools/test/hot_test.dart index dce058d1b1..c14b4582f6 100644 --- a/packages/flutter_tools/test/hot_test.dart +++ b/packages/flutter_tools/test/hot_test.dart @@ -228,6 +228,47 @@ void main() { Artifacts: () => mockArtifacts, HotRunnerConfig: () => TestHotRunnerConfig(successfulSetup: true, computeDartDependencies: false), }); + + group('shutdown hook tests', () { + TestHotRunnerConfig shutdownTestingConfig; + + setUp(() { + shutdownTestingConfig = TestHotRunnerConfig( + successfulSetup: true, + computeDartDependencies: false, + ); + }); + + testUsingContext('shutdown hook called after signal', () async { + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + when(mockDevice.supportsStopApp).thenReturn(false); + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false) + ]; + await HotRunner(devices).cleanupAfterSignal(); + expect(shutdownTestingConfig.shutdownHookCalled, true); + }, overrides: { + Artifacts: () => mockArtifacts, + HotRunnerConfig: () => shutdownTestingConfig, + }); + + testUsingContext('shutdown hook called after app stop', () async { + final MockDevice mockDevice = MockDevice(); + when(mockDevice.supportsHotReload).thenReturn(true); + when(mockDevice.supportsHotRestart).thenReturn(true); + when(mockDevice.supportsStopApp).thenReturn(false); + final List devices = [ + FlutterDevice(mockDevice, generator: residentCompiler, trackWidgetCreation: false) + ]; + await HotRunner(devices).preStop(); + expect(shutdownTestingConfig.shutdownHookCalled, true); + }, overrides: { + Artifacts: () => mockArtifacts, + HotRunnerConfig: () => shutdownTestingConfig, + }); + }); }); } @@ -247,9 +288,15 @@ class TestHotRunnerConfig extends HotRunnerConfig { } bool successfulSetup; + bool shutdownHookCalled = false; @override Future setupHotRestart() async { return successfulSetup; } + + @override + Future runPreShutdownOperations() async { + shutdownHookCalled = true; + } }