1
1
import 'dart:async' ;
2
2
3
3
import 'package:checks/checks.dart' ;
4
+ import 'package:flutter/foundation.dart' ;
4
5
import 'package:http/http.dart' as http;
5
6
import 'package:test/scaffolding.dart' ;
6
7
import 'package:zulip/api/model/events.dart' ;
@@ -117,14 +118,21 @@ void main() {
117
118
connection = store.connection as FakeApiConnection ;
118
119
}
119
120
120
- void checkLastRequest ({required String token}) {
121
+ void checkLastRequestApns ({required String token, required String appid}) {
122
+ check (connection.lastRequest).isA< http.Request > ()
123
+ ..method.equals ('POST' )
124
+ ..url.path.equals ('/api/v1/users/me/apns_device_token' )
125
+ ..bodyFields.deepEquals ({'token' : token, 'appid' : appid});
126
+ }
127
+
128
+ void checkLastRequestFcm ({required String token}) {
121
129
check (connection.lastRequest).isA< http.Request > ()
122
130
..method.equals ('POST' )
123
131
..url.path.equals ('/api/v1/users/me/android_gcm_reg_id' )
124
132
..bodyFields.deepEquals ({'token' : token});
125
133
}
126
134
127
- test ('token already known' , () async {
135
+ testAndroidIos ('token already known' , () async {
128
136
// This tests the case where [NotificationService.start] has already
129
137
// learned the token before the store is created.
130
138
// (This is probably the common case.)
@@ -137,16 +145,22 @@ void main() {
137
145
prepareStore ();
138
146
connection.prepare (json: {});
139
147
await store.registerNotificationToken ();
140
- checkLastRequest (token: '012abc' );
141
-
142
- // If the token changes, send it again.
143
- testBinding.firebaseMessaging.setToken ('456def' );
144
- connection.prepare (json: {});
145
- await null ; // Run microtasks. TODO use FakeAsync for these tests.
146
- checkLastRequest (token: '456def' );
148
+ if (defaultTargetPlatform == TargetPlatform .android) {
149
+ checkLastRequestFcm (token: '012abc' );
150
+ } else {
151
+ checkLastRequestApns (token: '012abc' , appid: 'com.zulip.flutter' );
152
+ }
153
+
154
+ if (defaultTargetPlatform == TargetPlatform .android) {
155
+ // If the token changes, send it again.
156
+ testBinding.firebaseMessaging.setToken ('456def' );
157
+ connection.prepare (json: {});
158
+ await null ; // Run microtasks. TODO use FakeAsync for these tests.
159
+ checkLastRequestFcm (token: '456def' );
160
+ }
147
161
});
148
162
149
- test ('token initially unknown' , () async {
163
+ testAndroidIos ('token initially unknown' , () async {
150
164
// This tests the case where the store is created while our
151
165
// request for the token is still pending.
152
166
addTearDown (testBinding.reset);
@@ -170,13 +184,19 @@ void main() {
170
184
// When the token later appears, send it.
171
185
connection.prepare (json: {});
172
186
await startFuture;
173
- checkLastRequest (token: '012abc' );
174
-
175
- // If the token subsequently changes, send it again.
176
- testBinding.firebaseMessaging.setToken ('456def' );
177
- connection.prepare (json: {});
178
- await null ; // Run microtasks. TODO use FakeAsync for these tests.
179
- checkLastRequest (token: '456def' );
187
+ if (defaultTargetPlatform == TargetPlatform .android) {
188
+ checkLastRequestFcm (token: '012abc' );
189
+ } else {
190
+ checkLastRequestApns (token: '012abc' , appid: 'com.zulip.flutter' );
191
+ }
192
+
193
+ if (defaultTargetPlatform == TargetPlatform .android) {
194
+ // If the token subsequently changes, send it again.
195
+ testBinding.firebaseMessaging.setToken ('456def' );
196
+ connection.prepare (json: {});
197
+ await null ; // Run microtasks. TODO use FakeAsync for these tests.
198
+ checkLastRequestFcm (token: '456def' );
199
+ }
180
200
});
181
201
});
182
202
@@ -239,3 +259,13 @@ class LoadingTestGlobalStore extends TestGlobalStore {
239
259
return completer.future;
240
260
}
241
261
}
262
+
263
+ void testAndroidIos (String description, FutureOr <void > Function () body) {
264
+ test ('$description (Android)' , body);
265
+ test ('$description (iOS)' , () async {
266
+ final origTargetPlatform = debugDefaultTargetPlatformOverride;
267
+ addTearDown (() => debugDefaultTargetPlatformOverride = origTargetPlatform);
268
+ debugDefaultTargetPlatformOverride = TargetPlatform .iOS;
269
+ await body ();
270
+ });
271
+ }
0 commit comments