maybeTearDownLeakTrackingForAll function

Future<void> maybeTearDownLeakTrackingForAll()

Should be invoked after execution of all tests to report found leaks.

Is noop if leak tracking is not started.

Implementation

Future<void> maybeTearDownLeakTrackingForAll() async {
  if (!LeakTesting.enabled || !LeakTracking.isStarted) {
    // Reporter is invoked so that tests can verify the number of
    // collected leaks is as expected.
    LeakTesting.collectedLeaksReporter(Leaks({}));
    return;
  }

  // The listener is not added/removed for each test,
  // because GC may happen after test is complete.
  FlutterMemoryAllocations.instance
      .removeListener(_dispatchFlutterEventToLeakTracker);
  await forceGC(fullGcCycles: defaultNumberOfGcCycles);
  LeakTracking.declareNotDisposedObjectsAsLeaks();
  final leaks = await LeakTracking.collectLeaks();
  LeakTracking.stop();

  LeakTesting.collectedLeaksReporter(leaks);
}