forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfirebase_messaging_test.dart
141 lines (113 loc) · 5.21 KB
/
firebase_messaging_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
// Copyright 2017 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 'dart:async';
import 'package:flutter/services.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:test/test.dart';
void main() {
MockMethodChannel mockChannel;
FirebaseMessaging firebaseMessaging;
setUp(() {
mockChannel = new MockMethodChannel();
firebaseMessaging = new FirebaseMessaging.private(
mockChannel, new FakePlatform(operatingSystem: 'ios'));
});
test('requestNotificationPermissions on ios with default permissions', () {
firebaseMessaging.requestNotificationPermissions();
verify(mockChannel.invokeMethod('requestNotificationPermissions',
<String, bool>{'sound': true, 'badge': true, 'alert': true}));
});
test('requestNotificationPermissions on ios with custom permissions', () {
firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: false));
verify(mockChannel.invokeMethod('requestNotificationPermissions',
<String, bool>{'sound': false, 'badge': true, 'alert': true}));
});
test('requestNotificationPermissions on android', () {
firebaseMessaging = new FirebaseMessaging.private(
mockChannel, new FakePlatform(operatingSystem: 'android'));
firebaseMessaging.requestNotificationPermissions();
verifyZeroInteractions(mockChannel);
});
test('requestNotificationPermissions on android', () {
firebaseMessaging = new FirebaseMessaging.private(
mockChannel, new FakePlatform(operatingSystem: 'android'));
firebaseMessaging.requestNotificationPermissions();
verifyZeroInteractions(mockChannel);
});
test('configure', () {
firebaseMessaging.configure();
verify(mockChannel.setMethodCallHandler(any));
verify(mockChannel.invokeMethod('configure'));
});
test('incoming token', () async {
firebaseMessaging.configure();
final dynamic handler =
verify(mockChannel.setMethodCallHandler(captureAny)).captured.single;
final String token1 = 'I am a super secret token';
final String token2 = 'I am the new token in town';
Future<String> tokenFromStream = firebaseMessaging.onTokenRefresh.first;
await handler(new MethodCall('onToken', token1));
expect(await firebaseMessaging.getToken(), token1);
expect(await tokenFromStream, token1);
tokenFromStream = firebaseMessaging.onTokenRefresh.first;
await handler(new MethodCall('onToken', token2));
expect(await firebaseMessaging.getToken(), token2);
expect(await tokenFromStream, token2);
});
test('incoming iOS settings', () async {
firebaseMessaging.configure();
final dynamic handler =
verify(mockChannel.setMethodCallHandler(captureAny)).captured.single;
IosNotificationSettings iosSettings = const IosNotificationSettings();
Future<IosNotificationSettings> iosSettingsFromStream =
firebaseMessaging.onIosSettingsRegistered.first;
await handler(
new MethodCall('onIosSettingsRegistered', iosSettings.toMap()));
expect((await iosSettingsFromStream).toMap(), iosSettings.toMap());
iosSettings = const IosNotificationSettings(sound: false);
iosSettingsFromStream = firebaseMessaging.onIosSettingsRegistered.first;
await handler(
new MethodCall('onIosSettingsRegistered', iosSettings.toMap()));
expect((await iosSettingsFromStream).toMap(), iosSettings.toMap());
});
test('incoming messages', () async {
final Completer<dynamic> onMessage = new Completer<dynamic>();
final Completer<dynamic> onLaunch = new Completer<dynamic>();
final Completer<dynamic> onResume = new Completer<dynamic>();
firebaseMessaging.configure(onMessage: (dynamic m) async {
onMessage.complete(m);
}, onLaunch: (dynamic m) async {
onLaunch.complete(m);
}, onResume: (dynamic m) async {
onResume.complete(m);
});
final dynamic handler =
verify(mockChannel.setMethodCallHandler(captureAny)).captured.single;
final Map<String, dynamic> onMessageMessage = <String, dynamic>{};
final Map<String, dynamic> onLaunchMessage = <String, dynamic>{};
final Map<String, dynamic> onResumeMessage = <String, dynamic>{};
await handler(new MethodCall('onMessage', onMessageMessage));
expect(await onMessage.future, onMessageMessage);
expect(onLaunch.isCompleted, isFalse);
expect(onResume.isCompleted, isFalse);
await handler(new MethodCall('onLaunch', onLaunchMessage));
expect(await onLaunch.future, onLaunchMessage);
expect(onResume.isCompleted, isFalse);
await handler(new MethodCall('onResume', onResumeMessage));
expect(await onResume.future, onResumeMessage);
});
const String myTopic = 'Flutter';
test('subscribe to topic', () {
firebaseMessaging.subscribeToTopic(myTopic);
verify(mockChannel.invokeMethod('subscribeToTopic', myTopic));
});
test('unsubscribe from topic', () {
firebaseMessaging.unsubscribeFromTopic(myTopic);
verify(mockChannel.invokeMethod('unsubscribeFromTopic', myTopic));
});
}
class MockMethodChannel extends Mock implements MethodChannel {}