Polina Cherkasova 01fe4e262a
Create helper method to dispatch object creation and disposal. (#163637)
Contributes to https://github.com/flutter/flutter/issues/137435.

Will test cover and convert to 'ready for review' after getting
agreement about the refactoring.
2025-02-20 01:57:31 +00:00

104 lines
2.8 KiB
Dart

// Copyright 2014 The Flutter Authors. 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:flutter_test/flutter_test.dart';
void main() {
group('debugInstrumentAction', () {
late DebugPrintCallback originalDebugPrintCallback;
late StringBuffer printBuffer;
setUp(() {
debugInstrumentationEnabled = true;
printBuffer = StringBuffer();
originalDebugPrintCallback = debugPrint;
debugPrint = (String? message, {int? wrapWidth}) {
printBuffer.writeln(message);
};
});
tearDown(() {
debugInstrumentationEnabled = false;
debugPrint = originalDebugPrintCallback;
});
test('works with non-failing actions', () async {
final int result = await debugInstrumentAction<int>('no-op', () async {
debugPrint('action()');
return 1;
});
expect(result, 1);
expect(
printBuffer.toString(),
matches(RegExp('^action\\(\\)\nAction "no-op" took .+\$', multiLine: true)),
);
});
test('returns failing future if action throws', () async {
await expectLater(
() => debugInstrumentAction<void>('throws', () async {
await Future<void>.delayed(Duration.zero);
throw 'Error';
}),
throwsA('Error'),
);
expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
});
});
group('Memory allocations', () {
ObjectEvent? dispatchedEvent;
final Object object = List<int>.filled(1, 0);
void listener(ObjectEvent event) {
expect(dispatchedEvent, null);
dispatchedEvent = event;
}
setUp(() {
dispatchedEvent = null;
FlutterMemoryAllocations.instance.addListener(listener);
});
tearDown(() {
FlutterMemoryAllocations.instance.removeListener(listener);
});
test('debugMaybeDispatchObjectCreated', () async {
debugMaybeDispatchObjectCreated('library', 'class', object);
if (kFlutterMemoryAllocationsEnabled) {
final ObjectEvent? theEvent = dispatchedEvent;
if (theEvent is! ObjectCreated) {
fail('Expected ObjectCreated event');
}
expect(theEvent.object, object);
expect(theEvent.library, 'library');
expect(theEvent.className, 'class');
} else {
expect(dispatchedEvent, isNull);
}
});
test('debugMaybeDispatchObjectDisposed', () async {
debugMaybeDispatchObjectDisposed(object);
if (kFlutterMemoryAllocationsEnabled) {
final ObjectEvent? theEvent = dispatchedEvent;
if (theEvent is! ObjectDisposed) {
fail('Expected ObjectDisposed event');
}
expect(theEvent.object, object);
} else {
expect(dispatchedEvent, isNull);
}
});
});
}