|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter/foundation.dart'; |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:test/scaffolding.dart'; |
| 5 | +import 'package:zulip/model/push_device.dart'; |
| 6 | +import 'package:zulip/notifications/receive.dart'; |
| 7 | + |
| 8 | +import '../api/fake_api.dart'; |
| 9 | +import '../example_data.dart' as eg; |
| 10 | +import '../fake_async.dart'; |
| 11 | +import 'binding.dart'; |
| 12 | +import 'store_test.dart'; |
| 13 | +import '../stdlib_checks.dart'; |
| 14 | + |
| 15 | +void main() { |
| 16 | + TestZulipBinding.ensureInitialized(); |
| 17 | + |
| 18 | + group('registerToken', () { |
| 19 | + late PushDeviceManager model; |
| 20 | + late FakeApiConnection connection; |
| 21 | + |
| 22 | + void prepareStore() { |
| 23 | + final store = eg.store(); |
| 24 | + model = store.pushDevices; |
| 25 | + connection = store.connection as FakeApiConnection; |
| 26 | + } |
| 27 | + |
| 28 | + void checkLastRequestApns({required String token, required String appid}) { |
| 29 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 30 | + ..method.equals('POST') |
| 31 | + ..url.path.equals('/api/v1/users/me/apns_device_token') |
| 32 | + ..bodyFields.deepEquals({'token': token, 'appid': appid}); |
| 33 | + } |
| 34 | + |
| 35 | + void checkLastRequestFcm({required String token}) { |
| 36 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 37 | + ..method.equals('POST') |
| 38 | + ..url.path.equals('/api/v1/users/me/android_gcm_reg_id') |
| 39 | + ..bodyFields.deepEquals({'token': token}); |
| 40 | + } |
| 41 | + |
| 42 | + testAndroidIos('token already known', () => awaitFakeAsync((async) async { |
| 43 | + // This tests the case where [NotificationService.start] has already |
| 44 | + // learned the token before the store is created. |
| 45 | + // (This is probably the common case.) |
| 46 | + addTearDown(testBinding.reset); |
| 47 | + testBinding.firebaseMessagingInitialToken = '012abc'; |
| 48 | + testBinding.packageInfoResult = eg.packageInfo(packageName: 'com.zulip.flutter'); |
| 49 | + addTearDown(NotificationService.debugReset); |
| 50 | + await NotificationService.instance.start(); |
| 51 | + |
| 52 | + // On store startup, send the token. |
| 53 | + prepareStore(); |
| 54 | + connection.prepare(json: {}); |
| 55 | + await model.registerToken(); |
| 56 | + if (defaultTargetPlatform == TargetPlatform.android) { |
| 57 | + checkLastRequestFcm(token: '012abc'); |
| 58 | + } else { |
| 59 | + checkLastRequestApns(token: '012abc', appid: 'com.zulip.flutter'); |
| 60 | + } |
| 61 | + |
| 62 | + if (defaultTargetPlatform == TargetPlatform.android) { |
| 63 | + // If the token changes, send it again. |
| 64 | + testBinding.firebaseMessaging.setToken('456def'); |
| 65 | + connection.prepare(json: {}); |
| 66 | + async.flushMicrotasks(); |
| 67 | + checkLastRequestFcm(token: '456def'); |
| 68 | + } |
| 69 | + })); |
| 70 | + |
| 71 | + testAndroidIos('token initially unknown', () => awaitFakeAsync((async) async { |
| 72 | + // This tests the case where the store is created while our |
| 73 | + // request for the token is still pending. |
| 74 | + addTearDown(testBinding.reset); |
| 75 | + testBinding.firebaseMessagingInitialToken = '012abc'; |
| 76 | + testBinding.packageInfoResult = eg.packageInfo(packageName: 'com.zulip.flutter'); |
| 77 | + addTearDown(NotificationService.debugReset); |
| 78 | + final startFuture = NotificationService.instance.start(); |
| 79 | + |
| 80 | + // TODO this test is a bit brittle in its interaction with asynchrony; |
| 81 | + // to fix, probably extend TestZulipBinding to control when getToken finishes. |
| 82 | + // |
| 83 | + // The aim here is to first wait for `model.registerToken` |
| 84 | + // to complete whatever it's going to do; then check no request was made; |
| 85 | + // and only after that wait for `NotificationService.start` to finish, |
| 86 | + // including its `getToken` call. |
| 87 | + |
| 88 | + // On store startup, send nothing (because we have nothing to send). |
| 89 | + prepareStore(); |
| 90 | + await model.registerToken(); |
| 91 | + check(connection.lastRequest).isNull(); |
| 92 | + |
| 93 | + // When the token later appears, send it. |
| 94 | + connection.prepare(json: {}); |
| 95 | + await startFuture; |
| 96 | + async.flushMicrotasks(); |
| 97 | + if (defaultTargetPlatform == TargetPlatform.android) { |
| 98 | + checkLastRequestFcm(token: '012abc'); |
| 99 | + } else { |
| 100 | + checkLastRequestApns(token: '012abc', appid: 'com.zulip.flutter'); |
| 101 | + } |
| 102 | + |
| 103 | + if (defaultTargetPlatform == TargetPlatform.android) { |
| 104 | + // If the token subsequently changes, send it again. |
| 105 | + testBinding.firebaseMessaging.setToken('456def'); |
| 106 | + connection.prepare(json: {}); |
| 107 | + async.flushMicrotasks(); |
| 108 | + checkLastRequestFcm(token: '456def'); |
| 109 | + } |
| 110 | + })); |
| 111 | + |
| 112 | + test('on iOS, use provided app ID from packageInfo', () => awaitFakeAsync((async) async { |
| 113 | + final origTargetPlatform = debugDefaultTargetPlatformOverride; |
| 114 | + addTearDown(() => debugDefaultTargetPlatformOverride = origTargetPlatform); |
| 115 | + debugDefaultTargetPlatformOverride = TargetPlatform.iOS; |
| 116 | + addTearDown(testBinding.reset); |
| 117 | + testBinding.firebaseMessagingInitialToken = '012abc'; |
| 118 | + testBinding.packageInfoResult = eg.packageInfo(packageName: 'com.example.test'); |
| 119 | + addTearDown(NotificationService.debugReset); |
| 120 | + await NotificationService.instance.start(); |
| 121 | + |
| 122 | + prepareStore(); |
| 123 | + connection.prepare(json: {}); |
| 124 | + await model.registerToken(); |
| 125 | + checkLastRequestApns(token: '012abc', appid: 'com.example.test'); |
| 126 | + })); |
| 127 | + }); |
| 128 | +} |
0 commit comments