diff --git a/pkgs/leak_tracker/lib/src/leak_tracking/model.dart b/pkgs/leak_tracker/lib/src/leak_tracking/model.dart index a85eef40..e7be259a 100644 --- a/pkgs/leak_tracker/lib/src/leak_tracking/model.dart +++ b/pkgs/leak_tracker/lib/src/leak_tracking/model.dart @@ -88,7 +88,7 @@ class LeakTrackingConfig { /// - will not auto check leaks /// - when leak checking is invoked, will not send notifications /// - will assume the methods `dispose` are completed - /// at the moment of leak checking. + /// at the moment of leak checking by setting [disposalTime] to zero. LeakTrackingConfig.passive({ LeakDiagnosticConfig leakDiagnosticConfig = const LeakDiagnosticConfig(), int numberOfGcCycles = defaultNumberOfGcCycles, diff --git a/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart b/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart index 914e5a19..feb37f51 100644 --- a/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart +++ b/pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart @@ -10,7 +10,7 @@ import 'package:leak_tracker_testing/leak_tracker_testing.dart'; import 'test_infra/dart_classes.dart'; import 'test_infra/flutter_classes.dart'; -import 'test_infra/helpers.dart'; +import 'test_infra/leak_tracking_in_flutter.dart'; /// Tests for non-mocked public API of leak tracker. /// diff --git a/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart b/pkgs/leak_tracker_flutter_test/test/test_infra/leak_tracking_in_flutter.dart similarity index 64% rename from pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart rename to pkgs/leak_tracker_flutter_test/test/test_infra/leak_tracking_in_flutter.dart index cb282864..1079d74a 100644 --- a/pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart +++ b/pkgs/leak_tracker_flutter_test/test/test_infra/leak_tracking_in_flutter.dart @@ -2,10 +2,11 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// Content of this file is copied from +// Content of this file is mirror of // https://github.com/flutter/flutter/blob/master/packages/flutter/test/foundation/leak_tracking.dart -// to test that new versions work well for Flutter Framework. -// TODO(polina-c): This code should be removed after `testWidgets` start supporting leak tracking. +// to test that a new versions work well for Flutter Framework, before it upgrades to the version. + +import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -13,6 +14,60 @@ import 'package:leak_tracker/leak_tracker.dart'; import 'package:leak_tracker_testing/leak_tracker_testing.dart'; import 'package:meta/meta.dart'; +typedef TestMain = FutureOr Function(); + +/// Wrapper for testExecutable to configure leak tracking for tests. +/// +/// See https://api.flutter.dev/flutter/flutter_test/flutter_test-library.html. +Future testExecutableWithLeakTracking( + Future Function() testExecutable, +) async { + _startLeakTracking(); + await testExecutable(); + await _stopLeakTracking(); +} + +void _flutterEventToLeakTracker(ObjectEvent event) { + return LeakTracking.dispatchObjectEvent(event.toMap()); + } + +void _startLeakTracking() { + _printPlatformWarningIfNeeded(); + if (!_isPlatformSupported) return; + + LeakTracking.start(config: LeakTrackingConfig.passive()); + + MemoryAllocations.instance.addListener(_flutterEventToLeakTracker); + // Future asyncCodeRunner(DartAsyncCallback action) async => + // tester.runAsync(action); + + // try { + // Leaks leaks = await withLeakTracking( + // callback, + // asyncCodeRunner: asyncCodeRunner, + // leakDiagnosticConfig: config.leakDiagnosticConfig, + // shouldThrowOnLeaks: false, + // ); + + // leaks = LeakCleaner(config).clean(leaks); + + // if (leaks.total > 0) { + // config.onLeaks?.call(leaks); + // if (config.failTestOnLeaks) { + // expect(leaks, isLeakFree); + // } + // } + // } finally { + + // } + +} + +Future _stopLeakTracking() async { + if (!_isPlatformSupported) return Future.value(); + MemoryAllocations.instance.removeListener(_flutterEventToLeakTracker); +} + /// Wrapper for [testWidgets] with memory leak tracking. /// /// The method will fail if instrumented objects in [callback] are @@ -54,7 +109,22 @@ void testWidgetsWithLeakTracking( ); } -bool _webWarningPrinted = false; +bool _notSupportedWarningPrinted = false; +bool get _isPlatformSupported => !kIsWeb; +void _printPlatformWarningIfNeeded() { + if (kIsWeb) { + final bool shouldPrintWarning = !_notSupportedWarningPrinted && + LeakTracking.warnForNotSupportedPlatforms; + if (shouldPrintWarning) { + _notSupportedWarningPrinted = true; + debugPrint( + 'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTracking.warnForNotSupportedPlatforms` to false.', + ); + } + return; + } + assert(_isPlatformSupported); +} /// Runs [callback] with leak tracking. /// @@ -78,10 +148,10 @@ Future withFlutterLeakTracking( ) async { // Leak tracker does not work for web platform. if (kIsWeb) { - final bool shouldPrintWarning = - !_webWarningPrinted && LeakTracking.warnForNotSupportedPlatforms; + final bool shouldPrintWarning = !_notSupportedWarningPrinted && + LeakTracking.warnForNotSupportedPlatforms; if (shouldPrintWarning) { - _webWarningPrinted = true; + _notSupportedWarningPrinted = true; debugPrint( 'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTracking.warnForNotSupportedPlatforms` to false.', );