Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/integration_test/e2e_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'firebase_app_installations/firebase_app_installations_e2e_test.dart'
import 'firebase_analytics/firebase_analytics_e2e_test.dart'
as firebase_analytics;
import 'firebase_core/firebase_core_e2e_test.dart' as firebase_core;
import 'firebase_messaging/firebase_messaging_e2e_test.dart'
as firebase_messaging;
import 'firebase_database/firebase_database_e2e_test.dart' as firebase_database;
import 'firebase_dynamic_links/firebase_dynamic_links_e2e_test.dart'
as firebase_dynamic_links;
Expand All @@ -28,6 +30,7 @@ void main() {
firebase_app_installations.main();
firebase_analytics.main();
firebase_core.main();
firebase_messaging.main();
firebase_database.main();
firebase_dynamic_links.main();
firebase_in_app_messaging.main();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright 2019, the Chromium project authors. Please see the AUTHORS file
// for details. 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:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tests/firebase_options.dart';

// ignore: do_not_use_environment
const bool skipManualTests = bool.fromEnvironment('CI');

void main() {
group(
'firebase_messaging',
() {
late FirebaseApp app;
late FirebaseMessaging messaging;

setUpAll(() async {
app = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
messaging = FirebaseMessaging.instance;
});

test('instance', () {
expect(messaging, isA<FirebaseMessaging>());
expect(messaging.app, isA<FirebaseApp>());
expect(messaging.app.name, defaultFirebaseAppName);
});

test('.app accessible from messaging.app', () {
expect(messaging.app, isA<FirebaseApp>());
expect(messaging.app.name, app.name);
});

group('onMessage', () {
test('can listen multiple times', () async {
// regression test for https://github.com/firebase/flutterfire/issues/6009

StreamSubscription<RemoteMessage> _onMessageSubscription;
StreamSubscription<RemoteMessage> _onMessageOpenedAppSubscription;

_onMessageSubscription = FirebaseMessaging.onMessage.listen((_) {});
_onMessageOpenedAppSubscription =
FirebaseMessaging.onMessageOpenedApp.listen((_) {});

await _onMessageSubscription.cancel();
await _onMessageOpenedAppSubscription.cancel();

_onMessageSubscription = FirebaseMessaging.onMessage.listen((_) {});
_onMessageOpenedAppSubscription =
FirebaseMessaging.onMessageOpenedApp.listen((_) {});

await _onMessageSubscription.cancel();
await _onMessageOpenedAppSubscription.cancel();
});
});

group('setAutoInitEnabled()', () {
test(
'sets the value',
() async {
expect(messaging.isAutoInitEnabled, isTrue);
await messaging.setAutoInitEnabled(false);
expect(messaging.isAutoInitEnabled, isFalse);
},
skip: kIsWeb,
);
});

group('isSupported()', () {
test('returns "true" value', () {
expect(messaging.isSupported(), isTrue);
});
});

group('requestPermission', () {
test(
'authorizationStatus returns AuthorizationStatus.authorized on Android',
() async {
final result = await messaging.requestPermission();
expect(result, isA<NotificationSettings>());
expect(result.authorizationStatus, AuthorizationStatus.authorized);
},
skip: defaultTargetPlatform != TargetPlatform.android || kIsWeb,
);
});

group('requestPermission', () {
test(
'authorizationStatus returns AuthorizationStatus.notDetermined on Web',
() async {
final result = await messaging.requestPermission();
expect(result, isA<NotificationSettings>());
expect(
result.authorizationStatus,
AuthorizationStatus.notDetermined,
);
},
skip: !kIsWeb,
);
});

group('getAPNSToken', () {
test(
'resolves null on android',
() async {
expect(await messaging.getAPNSToken(), null);
},
skip: defaultTargetPlatform != TargetPlatform.android,
);

test(
'resolves null on ios if using simulator',
() async {
expect(await messaging.getAPNSToken(), null);
},
skip: !(defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform != TargetPlatform.macOS),
);
});

group('getInitialMessage', () {
test('returns null when no initial message', () async {
expect(await messaging.getInitialMessage(), null);
});
});

group(
'getToken()',
() {
test('returns a token', () async {
final result = await messaging.getToken();
expect(result, isA<String>());
});
},
skip: skipManualTests,
); // only run for manual testing

group('deleteToken()', () {
test(
'generate a new token after deleting',
() async {
final token1 = await messaging.getToken();
await Future.delayed(const Duration(seconds: 3));
await messaging.deleteToken();
await Future.delayed(const Duration(seconds: 3));
final token2 = await messaging.getToken();
expect(token1, isA<String>());
expect(token2, isA<String>());
expect(token1, isNot(token2));
},
skip: skipManualTests,
); // only run for manual testing
});

group('subscribeToTopic()', () {
test(
'successfully subscribes from topic',
() async {
const topic = 'test-topic';
await messaging.subscribeToTopic(topic);
},
skip: kIsWeb,
);
});

group('unsubscribeFromTopic()', () {
test(
'successfully unsubscribes from topic',
() async {
const topic = 'test-topic';
await messaging.unsubscribeFromTopic(topic);
},
skip: kIsWeb,
);
});
},
);
}