-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firebase_crashlytics_test.dart
297 lines (266 loc) 路 10.5 KB
/
firebase_crashlytics_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// ignore_for_file: require_trailing_commas
// Copyright 2020 The Chromium 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:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_crashlytics/src/utils.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import './mock.dart';
void main() {
setupFirebaseCrashlyticsMocks();
FirebaseCrashlytics? crashlytics;
group('$FirebaseCrashlytics', () {
setUpAll(() async {
await Firebase.initializeApp();
crashlytics = FirebaseCrashlytics.instance;
});
setUp(() async {
methodCallLog.clear();
});
tearDown(methodCallLog.clear);
test('checkForUnsentReports', () async {
await crashlytics!.setCrashlyticsCollectionEnabled(false);
await crashlytics!.checkForUnsentReports();
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#setCrashlyticsCollectionEnabled',
arguments: {'enabled': false}),
isMethodCall('Crashlytics#checkForUnsentReports', arguments: null)
]);
});
test('crash', () async {
crashlytics!.crash();
expect(methodCallLog,
<Matcher>[isMethodCall('Crashlytics#crash', arguments: null)]);
});
test('deleteUnsentReports', () async {
await crashlytics!.deleteUnsentReports();
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#deleteUnsentReports', arguments: null)
]);
});
test('didCrashOnPreviousExecution', () async {
await crashlytics!.didCrashOnPreviousExecution();
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#didCrashOnPreviousExecution', arguments: null)
]);
});
group('recordError', () {
test('with stack', () async {
final stack = StackTrace.current;
const exception = 'foo exception';
const exceptionReason = 'bar reason';
await crashlytics!
.recordError(exception, stack, reason: exceptionReason);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#recordError', arguments: {
'exception': exception,
'reason': exceptionReason,
'information': '',
'fatal': false,
'stackTraceElements': getStackTraceElements(stack),
'buildId': '',
})
]);
// Confirm that the stack trace contains current stack.
expect(
methodCallLog[0].arguments['stackTraceElements'],
contains(
containsPair('file', contains('firebase_crashlytics_test.dart'))),
);
});
test('without stack', () async {
const exception = 'foo exception';
const exceptionReason = 'bar reason';
await crashlytics!
.recordError(exception, null, reason: exceptionReason);
expect(methodCallLog[0].method, 'Crashlytics#recordError');
expect(methodCallLog[0].arguments['exception'], exception);
expect(methodCallLog[0].arguments['reason'], exceptionReason);
// Confirm that the stack trace contains current stack.
expect(
methodCallLog[0].arguments['stackTraceElements'],
contains(
containsPair('file', contains('firebase_crashlytics_test.dart'))),
);
});
});
test('recordFlutterError', () async {
const exception = 'foo exception';
const exceptionReason = 'bar reason';
const exceptionLibrary = 'baz library';
const exceptionFirstMessage = 'first message';
const exceptionSecondMessage = 'second message';
final stack = StackTrace.current;
final FlutterErrorDetails details = FlutterErrorDetails(
exception: exception,
stack: stack,
library: exceptionLibrary,
informationCollector: () => <DiagnosticsNode>[
DiagnosticsNode.message(exceptionFirstMessage),
DiagnosticsNode.message(exceptionSecondMessage),
],
context: ErrorDescription(exceptionReason),
);
final oldPresentError = FlutterError.presentError;
var presentedError = false;
FlutterError.presentError = (details) {
presentedError = true;
};
try {
await crashlytics!.recordFlutterError(details);
expect(presentedError, true);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#recordError', arguments: {
'exception': exception,
'reason': exceptionReason,
'fatal': false,
'information': '$exceptionFirstMessage\n$exceptionSecondMessage',
'stackTraceElements': getStackTraceElements(stack),
'buildId': '',
})
]);
} finally {
FlutterError.presentError = oldPresentError;
}
});
group('log', () {
test('should call delegate method', () async {
const msg = 'foo';
await crashlytics!.log(msg);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#log', arguments: {
'message': msg,
})
]);
});
});
group('sendUnsentReports', () {
test('should call delegate method', () async {
await crashlytics!.sendUnsentReports();
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#sendUnsentReports', arguments: null)
]);
});
});
group('setCrashlyticsCollectionEnabled', () {
test('should call delegate method', () async {
await crashlytics!.setCrashlyticsCollectionEnabled(false);
expect(crashlytics!.isCrashlyticsCollectionEnabled, isFalse);
await crashlytics!.setCrashlyticsCollectionEnabled(true);
expect(crashlytics!.isCrashlyticsCollectionEnabled, isTrue);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#setCrashlyticsCollectionEnabled',
arguments: {
'enabled': false,
}),
isMethodCall('Crashlytics#setCrashlyticsCollectionEnabled',
arguments: {
'enabled': true,
})
]);
});
});
group('setUserIdentifier', () {
test('should call delegate method', () async {
const id = 'foo';
await crashlytics!.setUserIdentifier(id);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#setUserIdentifier', arguments: {
'identifier': id,
})
]);
});
});
group('setCustomKey', () {
test('should throw if null', () async {
expect(
() => crashlytics!.setCustomKey('foo', []), throwsAssertionError);
expect(
() => crashlytics!.setCustomKey('foo', {}), throwsAssertionError);
});
test('should call delegate method', () async {
const key = 'foo';
const value = 'bar';
await crashlytics!.setCustomKey(key, value);
expect(methodCallLog, <Matcher>[
isMethodCall('Crashlytics#setCustomKey', arguments: {
'key': key,
'value': value,
})
]);
});
});
group('getStackTraceElements', () {
test('with symbolic stack trace', () async {
final List<String> lines = <String>[
'#0 StatefulElement.build (package:flutter/src/widgets/framework.dart:3825:27)'
];
final StackTrace trace = StackTrace.fromString(lines.join('\n'));
final List<Map<String, String>> elements = getStackTraceElements(trace);
expect(elements.length, 1);
expect(elements.first, <String, String>{
'class': 'StatefulElement',
'method': 'build',
'file': 'package:flutter/src/widgets/framework.dart',
'line': '3825',
});
});
test('with symbolic stack trace and without class', () async {
final List<String> lines = <String>[
'#0 main (package:firebase_crashlytics/test/main.dart:12)'
];
final StackTrace trace = StackTrace.fromString(lines.join('\n'));
final List<Map<String, String>> elements = getStackTraceElements(trace);
expect(elements.length, 1);
expect(elements.first, <String, String>{
'method': 'main',
'file': 'package:firebase_crashlytics/test/main.dart',
'line': '12',
});
});
test('with android obfuscated stack trace', () async {
final List<String> lines = <String>[
'Warning: This VM has been configured to produce stack traces that violate the Dart standard.',
'*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***',
'pid: 1357, tid: 1415, name 1.ui',
"build_id: '77d1b910140a2d66b93594aea59469cf'",
'isolate_dso_base: 75f178181000, vm_dso_base: 75f178181000',
'isolate_instructions: 75f17818f000, vm_instructions: 75f178183000',
' #00 abs 000075f17833027b virt 00000000001af27b _kDartIsolateSnapshotInstructions+0x1a127b',
];
final StackTrace trace = StackTrace.fromString(lines.join('\n'));
final List<Map<String, String>> elements = getStackTraceElements(trace);
expect(elements.length, 1);
expect(elements.first, <String, String>{
'method':
' #00 abs 000075f17833027b virt 00000000001af27b _kDartIsolateSnapshotInstructions+0x1a127b',
'file': '',
'line': '0',
});
});
test('with ios obfuscated stack trace', () async {
final List<String> lines = <String>[
'Warning: This VM has been configured to produce stack traces that violate the Dart standard.',
'*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***',
'pid: 1357, tid: 1415, name 1.ui',
"build_id: '77d1b910140a2d66b93594aea59469cf'",
'isolate_dso_base: 75f178181000, vm_dso_base: 75f178181000',
'isolate_instructions: 75f17818f000, vm_instructions: 75f178183000',
' #00 abs 000075f17833027b _kDartIsolateSnapshotInstructions+0x1a127b',
];
final StackTrace trace = StackTrace.fromString(lines.join('\n'));
final List<Map<String, String>> elements = getStackTraceElements(trace);
expect(elements.length, 1);
expect(elements.first, <String, String>{
'method':
' #00 abs 000075f17833027b _kDartIsolateSnapshotInstructions+0x1a127b',
'file': '',
'line': '0',
});
});
});
});
}