Close platform when tests are complete (dispose compiler and delete font files) (#34685)

diff --git a/packages/flutter_tools/lib/src/test/flutter_platform.dart b/packages/flutter_tools/lib/src/test/flutter_platform.dart
index 348217e..83f2bef 100644
--- a/packages/flutter_tools/lib/src/test/flutter_platform.dart
+++ b/packages/flutter_tools/lib/src/test/flutter_platform.dart
@@ -68,12 +68,14 @@
   InternetAddressType.IPv6: InternetAddress.loopbackIPv6,
 };
 
+typedef PlatformPluginRegistration = void Function(FlutterPlatform platform);
+
 /// Configure the `test` package to work with Flutter.
 ///
 /// On systems where each [FlutterPlatform] is only used to run one test suite
 /// (that is, one Dart file with a `*_test.dart` file name and a single `void
 /// main()`), you can set an observatory port explicitly.
-void installHook({
+FlutterPlatform installHook({
   @required String shellPath,
   TestWatcher watcher,
   bool enableObservatory = false,
@@ -91,32 +93,40 @@
   Uri projectRootDirectory,
   FlutterProject flutterProject,
   String icudtlPath,
+  PlatformPluginRegistration platformPluginRegistration
 }) {
   assert(enableObservatory || (!startPaused && observatoryPort == null));
-  hack.registerPlatformPlugin(
-    <Runtime>[Runtime.vm],
-    () {
-      return FlutterPlatform(
-        shellPath: shellPath,
-        watcher: watcher,
-        machine: machine,
-        enableObservatory: enableObservatory,
-        startPaused: startPaused,
-        disableServiceAuthCodes: disableServiceAuthCodes,
-        explicitObservatoryPort: observatoryPort,
-        host: _kHosts[serverType],
-        port: port,
-        precompiledDillPath: precompiledDillPath,
-        precompiledDillFiles: precompiledDillFiles,
-        trackWidgetCreation: trackWidgetCreation,
-        updateGoldens: updateGoldens,
-        buildTestAssets: buildTestAssets,
-        projectRootDirectory: projectRootDirectory,
-        flutterProject: flutterProject,
-        icudtlPath: icudtlPath,
-      );
-    }
+
+  // registerPlatformPlugin can be injected for testing since it's not very mock-friendly.
+  platformPluginRegistration ??= (FlutterPlatform platform) {
+    hack.registerPlatformPlugin(
+      <Runtime>[Runtime.vm],
+        () {
+        return platform;
+      }
+    );
+  };
+  final FlutterPlatform platform = FlutterPlatform(
+    shellPath: shellPath,
+    watcher: watcher,
+    machine: machine,
+    enableObservatory: enableObservatory,
+    startPaused: startPaused,
+    disableServiceAuthCodes: disableServiceAuthCodes,
+    explicitObservatoryPort: observatoryPort,
+    host: _kHosts[serverType],
+    port: port,
+    precompiledDillPath: precompiledDillPath,
+    precompiledDillFiles: precompiledDillFiles,
+    trackWidgetCreation: trackWidgetCreation,
+    updateGoldens: updateGoldens,
+    buildTestAssets: buildTestAssets,
+    projectRootDirectory: projectRootDirectory,
+    flutterProject: flutterProject,
+    icudtlPath: icudtlPath,
   );
+  platformPluginRegistration(platform);
+  return platform;
 }
 
 /// Generates the bootstrap entry point script that will be used to launch an
diff --git a/packages/flutter_tools/lib/src/test/runner.dart b/packages/flutter_tools/lib/src/test/runner.dart
index 925bc27..9f814c4 100644
--- a/packages/flutter_tools/lib/src/test/runner.dart
+++ b/packages/flutter_tools/lib/src/test/runner.dart
@@ -105,7 +105,7 @@
   final InternetAddressType serverType =
       ipv6 ? InternetAddressType.IPv6 : InternetAddressType.IPv4;
 
-  loader.installHook(
+  final loader.FlutterPlatform platform = loader.installHook(
     shellPath: shellPath,
     watcher: watcher,
     enableObservatory: enableObservatory,
@@ -145,5 +145,6 @@
     return exitCode;
   } finally {
     fs.currentDirectory = saved;
+    await platform.close();
   }
 }
diff --git a/packages/flutter_tools/test/flutter_platform_test.dart b/packages/flutter_tools/test/flutter_platform_test.dart
index 1007194..83f5c92 100644
--- a/packages/flutter_tools/test/flutter_platform_test.dart
+++ b/packages/flutter_tools/test/flutter_platform_test.dart
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 import 'package:flutter_tools/src/base/common.dart';
+import 'package:flutter_tools/src/base/io.dart';
 import 'package:flutter_tools/src/test/flutter_platform.dart';
 
 import 'package:mockito/mockito.dart';
@@ -24,6 +25,58 @@
       flutterPlatfrom.loadChannel('test1.dart', MockPlatform());
       expect(() => flutterPlatfrom.loadChannel('test2.dart', MockPlatform()), throwsA(isA<ToolExit>()));
     });
+
+    testUsingContext('installHook creates a FlutterPlatform', () {
+      expect(() => installHook(
+        shellPath: 'abc',
+        enableObservatory: false,
+        startPaused: true
+      ), throwsA(isA<AssertionError>()));
+
+      expect(() => installHook(
+        shellPath: 'abc',
+        enableObservatory: false,
+        startPaused: false,
+        observatoryPort: 123
+      ), throwsA(isA<AssertionError>()));
+
+      FlutterPlatform capturedPlatform;
+      final Map<String, String> expectedPrecompiledDillFiles = <String, String>{'Key': 'Value'};
+      final FlutterPlatform flutterPlatform = installHook(
+        shellPath: 'abc',
+        enableObservatory: true,
+        machine: true,
+        startPaused: true,
+        disableServiceAuthCodes: true,
+        port: 100,
+        precompiledDillPath: 'def',
+        precompiledDillFiles: expectedPrecompiledDillFiles,
+        trackWidgetCreation: true,
+        updateGoldens: true,
+        buildTestAssets: true,
+        observatoryPort: 200,
+        serverType: InternetAddressType.IPv6,
+        icudtlPath: 'ghi',
+        platformPluginRegistration: (FlutterPlatform platform) {
+          capturedPlatform = platform;
+        });
+
+      expect(identical(capturedPlatform, flutterPlatform), equals(true));
+      expect(flutterPlatform.shellPath, equals('abc'));
+      expect(flutterPlatform.enableObservatory, equals(true));
+      expect(flutterPlatform.machine, equals(true));
+      expect(flutterPlatform.startPaused, equals(true));
+      expect(flutterPlatform.disableServiceAuthCodes, equals(true));
+      expect(flutterPlatform.port, equals(100));
+      expect(flutterPlatform.host, InternetAddress.loopbackIPv6);
+      expect(flutterPlatform.explicitObservatoryPort, equals(200));
+      expect(flutterPlatform.precompiledDillPath, equals('def'));
+      expect(flutterPlatform.precompiledDillFiles, expectedPrecompiledDillFiles);
+      expect(flutterPlatform.trackWidgetCreation, equals(true));
+      expect(flutterPlatform.updateGoldens, equals(true));
+      expect(flutterPlatform.buildTestAssets, equals(true));
+      expect(flutterPlatform.icudtlPath, equals('ghi'));
+    });
   });
 }