1// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter/foundation.dart';
6import 'package:flutter_test/flutter_test.dart';
7
8void main() {
9 group('debugInstrumentAction', () {
10 late DebugPrintCallback originalDebugPrintCallback;
11 late StringBuffer printBuffer;
12
13 setUp(() {
14 debugInstrumentationEnabled = true;
15 printBuffer = StringBuffer();
16 originalDebugPrintCallback = debugPrint;
17 debugPrint = (String? message, {int? wrapWidth}) {
18 printBuffer.writeln(message);
19 };
20 });
21
22 tearDown(() {
23 debugInstrumentationEnabled = false;
24 debugPrint = originalDebugPrintCallback;
25 });
26
27 test('works with non-failing actions', () async {
28 final int result = await debugInstrumentAction<int>('no-op', () async {
29 debugPrint('action()');
30 return 1;
31 });
32 expect(result, 1);
33 expect(
34 printBuffer.toString(),
35 matches(RegExp('^action\\(\\)\nAction "no-op" took .+\$', multiLine: true)),
36 );
37 });
38
39 test('returns failing future if action throws', () async {
40 await expectLater(
41 () => debugInstrumentAction<void>('throws', () async {
42 await Future<void>.delayed(Duration.zero);
43 throw 'Error';
44 }),
45 throwsA('Error'),
46 );
47 expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
48 });
49 });
50
51 group('Memory allocations', () {
52 ObjectEvent? dispatchedEvent;
53 final Object object = List<int>.filled(1, 0);
54
55 void listener(ObjectEvent event) {
56 expect(dispatchedEvent, null);
57 dispatchedEvent = event;
58 }
59
60 setUp(() {
61 dispatchedEvent = null;
62 FlutterMemoryAllocations.instance.addListener(listener);
63 });
64
65 tearDown(() {
66 FlutterMemoryAllocations.instance.removeListener(listener);
67 });
68
69 test('debugMaybeDispatchCreated', () async {
70 debugMaybeDispatchCreated('library', 'class', object);
71
72 if (kFlutterMemoryAllocationsEnabled) {
73 final ObjectEvent? theEvent = dispatchedEvent;
74
75 if (theEvent is! ObjectCreated) {
76 fail('Expected ObjectCreated event');
77 }
78
79 expect(theEvent.object, object);
80 expect(theEvent.library, 'package:flutter/library.dart');
81 expect(theEvent.className, 'class');
82 } else {
83 expect(dispatchedEvent, isNull);
84 }
85 });
86
87 test('debugMaybeDispatchDisposed', () async {
88 debugMaybeDispatchDisposed(object);
89
90 if (kFlutterMemoryAllocationsEnabled) {
91 final ObjectEvent? theEvent = dispatchedEvent;
92
93 if (theEvent is! ObjectDisposed) {
94 fail('Expected ObjectDisposed event');
95 }
96
97 expect(theEvent.object, object);
98 } else {
99 expect(dispatchedEvent, isNull);
100 }
101 });
102 });
103}
104

Provided by KDAB

Privacy Policy
Learn more about Flutter for embedded and desktop on industrialflutter.com