Skip to content

Commit b9fce3f

Browse files
gnpricechrisbobbe
authored andcommitted
notif test: Add requestPermission, getAPNSToken to bindings
1 parent 0b1e101 commit b9fce3f

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

test/model/binding.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,55 @@ class TestZulipBinding extends ZulipBinding {
213213
}
214214

215215
class FakeFirebaseMessaging extends Fake implements FirebaseMessaging {
216+
////////////////////////////////
217+
// Permissions.
218+
219+
NotificationSettings requestPermissionResult = const NotificationSettings(
220+
alert: AppleNotificationSetting.enabled,
221+
announcement: AppleNotificationSetting.disabled,
222+
authorizationStatus: AuthorizationStatus.authorized,
223+
badge: AppleNotificationSetting.enabled,
224+
carPlay: AppleNotificationSetting.disabled,
225+
lockScreen: AppleNotificationSetting.enabled,
226+
notificationCenter: AppleNotificationSetting.enabled,
227+
showPreviews: AppleShowPreviewSetting.whenAuthenticated,
228+
timeSensitive: AppleNotificationSetting.disabled,
229+
criticalAlert: AppleNotificationSetting.disabled,
230+
sound: AppleNotificationSetting.enabled,
231+
);
232+
233+
List<FirebaseMessagingRequestPermissionCall> takeRequestPermissionCalls() {
234+
final result = _requestPermissionCalls;
235+
_requestPermissionCalls = [];
236+
return result;
237+
}
238+
List<FirebaseMessagingRequestPermissionCall> _requestPermissionCalls = [];
239+
240+
@override
241+
Future<NotificationSettings> requestPermission({
242+
bool alert = true,
243+
bool announcement = false,
244+
bool badge = true,
245+
bool carPlay = false,
246+
bool criticalAlert = false,
247+
bool provisional = false,
248+
bool sound = true,
249+
}) async {
250+
_requestPermissionCalls.add((
251+
alert: alert,
252+
announcement: announcement,
253+
badge: badge,
254+
carPlay: carPlay,
255+
criticalAlert: criticalAlert,
256+
provisional: provisional,
257+
sound: sound,
258+
));
259+
return requestPermissionResult;
260+
}
261+
262+
////////////////////////////////
263+
// Tokens.
264+
216265
String? _initialToken;
217266

218267
/// Set the token to a new value, as if it were newly generated.
@@ -248,6 +297,27 @@ class FakeFirebaseMessaging extends Fake implements FirebaseMessaging {
248297
@override
249298
Stream<String> get onTokenRefresh => _tokenController.stream;
250299

300+
@override
301+
Future<String?> getAPNSToken() async {
302+
switch (defaultTargetPlatform) {
303+
case TargetPlatform.iOS:
304+
case TargetPlatform.macOS:
305+
// In principle the APNs token is unrelated to any FCM token.
306+
// But for tests it's convenient to have just one version of
307+
// [TestBinding.firebaseMessagingInitialToken].
308+
return _initialToken;
309+
310+
case TargetPlatform.android:
311+
case TargetPlatform.linux:
312+
case TargetPlatform.windows:
313+
case TargetPlatform.fuchsia:
314+
return null;
315+
}
316+
}
317+
318+
////////////////////////////////
319+
// Messages.
320+
251321
StreamController<RemoteMessage> onMessage = StreamController.broadcast();
252322

253323
/// Controls [TestZulipBinding.firebaseMessagingOnBackgroundMessage].
@@ -257,6 +327,16 @@ class FakeFirebaseMessaging extends Fake implements FirebaseMessaging {
257327
StreamController<RemoteMessage> onBackgroundMessage = StreamController.broadcast();
258328
}
259329

330+
typedef FirebaseMessagingRequestPermissionCall = ({
331+
bool alert,
332+
bool announcement,
333+
bool badge,
334+
bool carPlay,
335+
bool criticalAlert,
336+
bool provisional,
337+
bool sound,
338+
});
339+
260340
class FakeFlutterLocalNotificationsPlugin extends Fake implements FlutterLocalNotificationsPlugin {
261341
InitializationSettings? initializationSettings;
262342
DidReceiveNotificationResponseCallback? onDidReceiveNotificationResponse;

0 commit comments

Comments
 (0)