@@ -5,17 +5,19 @@ import 'package:flutter_test/flutter_test.dart';
5
5
import 'package:sentry_flutter/sentry_flutter.dart' ;
6
6
import 'package:sentry_flutter/src/sentry_flutter_options.dart' ;
7
7
8
- import 'mocks.dart' ;
8
+ import 'mocks.mocks. dart' ;
9
9
10
10
void main () {
11
- const _channel = MethodChannel ('sentry_flutter' );
12
-
13
11
TestWidgetsFlutterBinding .ensureInitialized ();
14
12
15
13
var called = false ;
16
14
15
+ late Fixture fixture;
16
+
17
17
setUp (() {
18
- _channel.setMockMethodCallHandler ((MethodCall methodCall) async {
18
+ fixture = Fixture ();
19
+
20
+ fixture.channel.setMockMethodCallHandler ((MethodCall methodCall) async {
19
21
called = true ;
20
22
return {
21
23
'integrations' : ['NativeIntegration' ],
@@ -34,20 +36,36 @@ void main() {
34
36
});
35
37
36
38
tearDown (() {
37
- _channel .setMockMethodCallHandler (null );
39
+ fixture.channel .setMockMethodCallHandler (null );
38
40
});
39
41
40
- test ('should apply the loadContextsIntegration eventProcessor' , () async {
41
- final options = SentryFlutterOptions ()..dsn = fakeDsn;
42
- final hub = Hub (options);
42
+ SdkVersion getSdkVersion ({String name = 'sentry.dart' }) {
43
+ return SdkVersion (
44
+ name: name,
45
+ version: '1.0' ,
46
+ integrations: const ['EventIntegration' ],
47
+ packages: const [SentryPackage ('event-package' , '2.0' )]);
48
+ }
49
+
50
+ SentryEvent getEvent ({
51
+ SdkVersion ? sdk,
52
+ Map <String , String >? tags,
53
+ }) {
54
+ return SentryEvent (
55
+ sdk: sdk ?? getSdkVersion (),
56
+ tags: tags,
57
+ );
58
+ }
43
59
44
- LoadContextsIntegration (_channel)(hub, options);
60
+ test ('should apply the loadContextsIntegration eventProcessor' , () async {
61
+ final integration = fixture.getSut ();
62
+ integration (fixture.hub, fixture.options);
45
63
46
- expect (options.eventProcessors.length, 1 );
64
+ expect (fixture. options.eventProcessors.length, 1 );
47
65
48
66
final e = SentryEvent ();
49
- final event =
50
- await (options.eventProcessors. first (e) as FutureOr <SentryEvent >);
67
+ final event = await (fixture.options.eventProcessors. first (e)
68
+ as FutureOr <SentryEvent >);
51
69
52
70
expect (called, true );
53
71
expect (event.contexts.device! .name, 'Device1' );
@@ -68,12 +86,10 @@ void main() {
68
86
test (
69
87
'should not override event contexts with the loadContextsIntegration infos' ,
70
88
() async {
71
- final options = SentryFlutterOptions ()..dsn = fakeDsn ;
72
- final hub = Hub ( options);
89
+ final integration = fixture. getSut () ;
90
+ integration (fixture. hub, fixture. options);
73
91
74
- LoadContextsIntegration (_channel)(hub, options);
75
-
76
- expect (options.eventProcessors.length, 1 );
92
+ expect (fixture.options.eventProcessors.length, 1 );
77
93
78
94
final eventContexts = Contexts (
79
95
device: const SentryDevice (name: 'eDevice' ),
@@ -84,8 +100,8 @@ void main() {
84
100
runtimes: [const SentryRuntime (name: 'eRT' )])
85
101
..['theme' ] = 'cuppertino' ;
86
102
final e = SentryEvent (contexts: eventContexts);
87
- final event =
88
- await (options.eventProcessors. first (e) as FutureOr <SentryEvent >);
103
+ final event = await (fixture.options.eventProcessors. first (e)
104
+ as FutureOr <SentryEvent >);
89
105
90
106
expect (called, true );
91
107
expect (event.contexts.device! .name, 'eDevice' );
@@ -103,20 +119,12 @@ void main() {
103
119
test (
104
120
'should merge event and loadContextsIntegration sdk packages and integration' ,
105
121
() async {
106
- final options = SentryFlutterOptions ()..dsn = fakeDsn;
107
- final hub = Hub (options);
108
-
109
- LoadContextsIntegration (_channel)(hub, options);
122
+ final integration = fixture.getSut ();
123
+ integration (fixture.hub, fixture.options);
110
124
111
- final eventSdk = SdkVersion (
112
- name: 'sdk1' ,
113
- version: '1.0' ,
114
- integrations: const ['EventIntegration' ],
115
- packages: const [SentryPackage ('event-package' , '2.0' )],
116
- );
117
- final e = SentryEvent (sdk: eventSdk);
118
- final event =
119
- await (options.eventProcessors.first (e) as FutureOr <SentryEvent >);
125
+ final e = getEvent ();
126
+ final event = await (fixture.options.eventProcessors.first (e)
127
+ as FutureOr <SentryEvent >);
120
128
121
129
expect (
122
130
event.sdk! .packages.any ((element) => element.name == 'native-package' ),
@@ -132,17 +140,77 @@ void main() {
132
140
);
133
141
134
142
test ('should not throw on loadContextsIntegration exception' , () async {
135
- _channel .setMockMethodCallHandler ((MethodCall methodCall) async {
143
+ fixture.channel .setMockMethodCallHandler ((MethodCall methodCall) async {
136
144
throw Exception ();
137
145
});
138
- final options = SentryFlutterOptions ()..dsn = fakeDsn;
139
- final hub = Hub (options);
140
-
141
- LoadContextsIntegration (_channel)(hub, options);
146
+ final integration = fixture.getSut ();
147
+ integration (fixture.hub, fixture.options);
142
148
143
149
final e = SentryEvent ();
144
- final event = await options.eventProcessors.first (e);
150
+ final event = await fixture. options.eventProcessors.first (e);
145
151
146
152
expect (event, isNotNull);
147
153
});
154
+
155
+ test (
156
+ 'should add origin and environment tags if tags is null' ,
157
+ () async {
158
+ final integration = fixture.getSut ();
159
+ integration (fixture.hub, fixture.options);
160
+
161
+ final eventSdk = getSdkVersion (name: 'sentry.dart.flutter' );
162
+ final e = getEvent (sdk: eventSdk);
163
+ final event = await (fixture.options.eventProcessors.first (e)
164
+ as FutureOr <SentryEvent >);
165
+
166
+ expect (event.tags! ['event.origin' ], 'flutter' );
167
+ expect (event.tags! ['event.environment' ], 'dart' );
168
+ },
169
+ );
170
+
171
+ test (
172
+ 'should merge origin and environment tags' ,
173
+ () async {
174
+ final integration = fixture.getSut ();
175
+ integration (fixture.hub, fixture.options);
176
+
177
+ final eventSdk = getSdkVersion (name: 'sentry.dart.flutter' );
178
+ final e = getEvent (
179
+ sdk: eventSdk,
180
+ tags: {'a' : 'b' },
181
+ );
182
+ final event = await (fixture.options.eventProcessors.first (e)
183
+ as FutureOr <SentryEvent >);
184
+
185
+ expect (event.tags! ['event.origin' ], 'flutter' );
186
+ expect (event.tags! ['event.environment' ], 'dart' );
187
+ expect (event.tags! ['a' ], 'b' );
188
+ },
189
+ );
190
+
191
+ test (
192
+ 'should not add origin and environment tags if not flutter sdk' ,
193
+ () async {
194
+ final integration = fixture.getSut ();
195
+ integration (fixture.hub, fixture.options);
196
+
197
+ final e = getEvent (tags: {});
198
+ final event = await (fixture.options.eventProcessors.first (e)
199
+ as FutureOr <SentryEvent >);
200
+
201
+ expect (event.tags! .containsKey ('event.origin' ), false );
202
+ expect (event.tags! .containsKey ('event.environment' ), false );
203
+ },
204
+ );
205
+ }
206
+
207
+ class Fixture {
208
+ final channel = MethodChannel ('sentry_flutter' );
209
+
210
+ final hub = MockHub ();
211
+ final options = SentryFlutterOptions ();
212
+
213
+ LoadContextsIntegration getSut () {
214
+ return LoadContextsIntegration (channel);
215
+ }
148
216
}
0 commit comments