-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
flutter_error_integration_test.dart
232 lines (179 loc) · 7.59 KB
/
flutter_error_integration_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:sentry_flutter/src/integrations/flutter_error_integration.dart';
import '../mocks.dart';
import '../mocks.mocks.dart';
void main() {
group(FlutterErrorIntegration, () {
late Fixture fixture;
setUp(() {
fixture = Fixture();
});
void _reportError({
bool silent = false,
FlutterExceptionHandler? handler,
dynamic exception,
FlutterErrorDetails? optionalDetails,
}) {
// replace default error otherwise it fails on testing
FlutterError.onError =
handler ?? (FlutterErrorDetails errorDetails) async {};
when(fixture.hub.captureEvent(captureAny))
.thenAnswer((_) => Future.value(SentryId.empty()));
FlutterErrorIntegration()(fixture.hub, fixture.options);
final throwable = exception ?? StateError('error');
final details = FlutterErrorDetails(
exception: throwable,
silent: silent,
context: DiagnosticsNode.message('while handling a gesture'),
library: 'sentry',
informationCollector: () => [DiagnosticsNode.message('foo bar')],
);
FlutterError.reportError(optionalDetails ?? details);
}
test('FlutterError capture errors', () async {
final exception = StateError('error');
_reportError(exception: exception);
final event = verify(
await fixture.hub.captureEvent(captureAny),
).captured.first as SentryEvent;
expect(event.level, SentryLevel.fatal);
final throwableMechanism = event.throwableMechanism as ThrowableMechanism;
expect(throwableMechanism.mechanism.type, 'FlutterError');
expect(throwableMechanism.mechanism.handled, true);
expect(throwableMechanism.throwable, exception);
expect(event.contexts['flutter_error_details']['library'], 'sentry');
expect(event.contexts['flutter_error_details']['context'],
'thrown while handling a gesture');
expect(event.contexts['flutter_error_details']['information'], 'foo bar');
});
test(
'FlutterError capture errors with long FlutterErrorDetails.information',
() async {
final details = FlutterErrorDetails(
exception: StateError('error'),
silent: false,
context: DiagnosticsNode.message('while handling a gesture'),
library: 'sentry',
informationCollector: () => [
DiagnosticsNode.message('foo bar'),
DiagnosticsNode.message('Hello World!')
],
);
// exception is ignored in this case
_reportError(exception: StateError('error'), optionalDetails: details);
final event = verify(
await fixture.hub.captureEvent(captureAny),
).captured.first as SentryEvent;
expect(event.level, SentryLevel.fatal);
final throwableMechanism = event.throwableMechanism as ThrowableMechanism;
expect(throwableMechanism.mechanism.type, 'FlutterError');
expect(throwableMechanism.mechanism.handled, true);
expect(event.contexts['flutter_error_details']['library'], 'sentry');
expect(event.contexts['flutter_error_details']['context'],
'thrown while handling a gesture');
expect(event.contexts['flutter_error_details']['information'],
'foo bar\nHello World!');
});
test('FlutterError capture errors with no FlutterErrorDetails', () async {
final details = FlutterErrorDetails(
exception: StateError('error'), silent: false, library: null);
// exception is ignored in this case
_reportError(exception: StateError('error'), optionalDetails: details);
final event = verify(
await fixture.hub.captureEvent(captureAny),
).captured.first as SentryEvent;
expect(event.level, SentryLevel.fatal);
final throwableMechanism = event.throwableMechanism as ThrowableMechanism;
expect(throwableMechanism.mechanism.type, 'FlutterError');
expect(throwableMechanism.mechanism.handled, true);
expect(throwableMechanism.mechanism.data['hint'], isNull);
expect(event.contexts['flutter_error_details'], isNull);
});
test('FlutterError calls default error', () async {
var called = false;
final defaultError = (FlutterErrorDetails errorDetails) async {
called = true;
};
_reportError(handler: defaultError);
verify(await fixture.hub.captureEvent(captureAny));
expect(called, true);
});
test('FlutterErrorIntegration captureEvent only called once', () async {
var numberOfDefaultCalls = 0;
final defaultError = (FlutterErrorDetails errorDetails) async {
numberOfDefaultCalls++;
};
FlutterError.onError = defaultError;
when(fixture.hub.captureEvent(captureAny))
.thenAnswer((_) => Future.value(SentryId.empty()));
final details = FlutterErrorDetails(exception: StateError('error'));
final integrationA = FlutterErrorIntegration();
integrationA.call(fixture.hub, fixture.options);
await integrationA.close();
final integrationB = FlutterErrorIntegration();
integrationB.call(fixture.hub, fixture.options);
FlutterError.reportError(details);
verify(await fixture.hub.captureEvent(captureAny)).called(1);
expect(numberOfDefaultCalls, 1);
});
test('FlutterErrorIntegration close restored default onError', () async {
final defaultOnError = (FlutterErrorDetails errorDetails) async {};
FlutterError.onError = defaultOnError;
final integration = FlutterErrorIntegration();
integration.call(fixture.hub, fixture.options);
expect(false, defaultOnError == FlutterError.onError);
await integration.close();
expect(FlutterError.onError, defaultOnError);
});
test(
'FlutterErrorIntegration default not restored if set after integration',
() async {
final defaultOnError = (FlutterErrorDetails errorDetails) async {};
FlutterError.onError = defaultOnError;
final integration = FlutterErrorIntegration();
integration.call(fixture.hub, fixture.options);
expect(defaultOnError == FlutterError.onError, false);
final afterIntegrationOnError =
(FlutterErrorDetails errorDetails) async {};
FlutterError.onError = afterIntegrationOnError;
await integration.close();
expect(FlutterError.onError, afterIntegrationOnError);
});
test('FlutterError do not capture if silent error', () async {
_reportError(silent: true);
verifyNever(await fixture.hub.captureEvent(captureAny));
});
test('FlutterError captures if silent error but reportSilentFlutterErrors',
() async {
fixture.options.reportSilentFlutterErrors = true;
_reportError(silent: true);
verify(await fixture.hub.captureEvent(captureAny));
});
test('FlutterError adds integration', () {
FlutterErrorIntegration()(fixture.hub, fixture.options);
expect(
fixture.options.sdk.integrations.contains('flutterErrorIntegration'),
true);
});
});
}
class Fixture {
final hub = MockHub();
final options = SentryFlutterOptions(dsn: fakeDsn);
LoadReleaseIntegration getIntegration({PackageLoader? loader}) {
return LoadReleaseIntegration(loader ?? loadRelease);
}
Future<PackageInfo> loadRelease() {
return Future.value(PackageInfo(
appName: 'sentry_flutter',
packageName: 'foo.bar',
version: '1.2.3',
buildNumber: '789',
buildSignature: '',
));
}
}