Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to integrate with testWidgets. #188

Merged
merged 17 commits into from
Dec 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
export 'src/matchers.dart';
export 'src/model.dart';
export 'src/test_widgets.dart';
export 'src/testing.dart';
export 'package:leak_tracker/leak_tracker.dart'
show LeakTesting, Leaks, LeakTracking, IgnoredLeaks, LeakType, LeakReport;
export 'package:leak_tracker_testing/leak_tracker_testing.dart' show isLeakFree;
99 changes: 99 additions & 0 deletions pkgs/leak_tracker_flutter_testing/lib/src/testing.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:flutter/foundation.dart';
import 'package:leak_tracker/leak_tracker.dart';
import 'package:leak_tracker_testing/leak_tracker_testing.dart';
import 'package:matcher/expect.dart';

void mayBeSetupLeakTrackingForTest(
LeakTesting settings,
String testDescription,
) {
if (settings.ignore) return;

if (!_checkPlatformAndMayBePrintWarning(
platformName: defaultTargetPlatform.name, isBrowser: kIsWeb)) {
return;
}

_maybeStartLeakTracking();

final PhaseSettings phase = PhaseSettings(
name: testDescription,
leakDiagnosticConfig: settings.leakDiagnosticConfig,
ignoredLeaks: settings.ignoredLeaks,
baselining: settings.baselining,
ignoreLeaks: settings.ignore,
);

LeakTracking.phase = phase;
}

void ignoreAllLeaks() {
LeakTracking.phase = const PhaseSettings.ignored();
}

/// Should be invoked after execution of all tests to report found leaks.
Future<void> maybeTearDownLeakTracking() async {
if (!LeakTracking.isStarted) {
return;
}

MemoryAllocations.instance.removeListener(_dispatchFlutterEventToLeakTracker);
LeakTracking.declareNotDisposedObjectsAsLeaks();
await forceGC(fullGcCycles: defaultNumberOfGcCycles);
final Leaks leaks = await LeakTracking.collectLeaks();
LeakTracking.stop();

collectedLeaksReporter(leaks);
}

/// Handler for memory leaks found in tests.
///
/// Set it to analyse the leaks programmatically.
/// The handler is invoked on tear down of the test run.
/// The default reporter fails in case of found leaks.
///
/// Used to test leak tracking functionality.
LeaksCallback collectedLeaksReporter =
(Leaks leaks) => expect(leaks, isLeakFree);

void _dispatchFlutterEventToLeakTracker(ObjectEvent event) {
return LeakTracking.dispatchObjectEvent(event.toMap());
}

bool _notSupportedWarningPrinted = false;
polina-c marked this conversation as resolved.
Show resolved Hide resolved

/// Checks if platform supported and, if no, prints warning if the warning is needed.
///
/// Warning is printed one time if `LeakTracking.warnForNotSupportedPlatforms` is true.
bool _checkPlatformAndMayBePrintWarning(
{required String platformName, required bool isBrowser}) {
final isSupported = !isBrowser;

if (isSupported) return true;

final shouldPrintWarning =
LeakTracking.warnForUnsupportedPlatforms && !_notSupportedWarningPrinted;

if (!shouldPrintWarning) return false;

_notSupportedWarningPrinted = true;
debugPrint(
"Leak tracking is not supported on the platform '$platformName'.\n"
'To turn off this message, set `LeakTracking.warnForNotSupportedPlatforms` to false.',
);

return false;
}

/// Starts leak tracking with all leaks ignored.
void _maybeStartLeakTracking() {
if (LeakTracking.isStarted) return;

LeakTracking.phase = const PhaseSettings.ignored();
LeakTracking.start(config: LeakTrackingConfig.passive());
MemoryAllocations.instance.addListener(_dispatchFlutterEventToLeakTracker);
}
1 change: 1 addition & 0 deletions pkgs/leak_tracker_flutter_testing/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies:
sdk: flutter
leak_tracker: ^9.0.10
leak_tracker_testing: ^1.0.5
matcher: ^0.12.16
meta: ^1.8.0

dev_dependencies:
Expand Down