@@ -26,9 +26,9 @@ import 'package:zulip/widgets/message_list.dart';
2626import 'package:zulip/widgets/page.dart' ;
2727import 'package:zulip/widgets/theme.dart' ;
2828
29+ import '../example_data.dart' as eg;
2930import '../fake_async.dart' ;
3031import '../model/binding.dart' ;
31- import '../example_data.dart' as eg;
3232import '../model/narrow_checks.dart' ;
3333import '../stdlib_checks.dart' ;
3434import '../test_images.dart' ;
@@ -114,7 +114,10 @@ void main() {
114114 return http.runWithClient (callback, httpClientFactory ?? () => fakeHttpClientGivingSuccess);
115115 }
116116
117- Future <void > init () async {
117+ Future <void > init ({bool addSelfAccount = true }) async {
118+ if (addSelfAccount) {
119+ await testBinding.globalStore.add (eg.selfAccount, eg.initialSnapshot ());
120+ }
118121 addTearDown (testBinding.reset);
119122 testBinding.firebaseMessagingInitialToken = '012abc' ;
120123 addTearDown (NotificationService .debugReset);
@@ -872,7 +875,8 @@ void main() {
872875 })));
873876
874877 test ('remove: different realm URLs but same user-ids and same message-ids' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
875- await init ();
878+ await init (addSelfAccount: false );
879+
876880 final stream = eg.stream ();
877881 const topic = 'Some Topic' ;
878882 final conversationKey = 'stream:${stream .streamId }:some topic' ;
@@ -881,6 +885,7 @@ void main() {
881885 realmUrl: Uri .parse ('https://1.chat.example' ),
882886 id: 1001 ,
883887 user: eg.user (userId: 1001 ));
888+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
884889 final message1 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
885890 final data1 =
886891 messageFcmMessage (message1, account: account1, streamName: stream.name);
@@ -890,6 +895,7 @@ void main() {
890895 realmUrl: Uri .parse ('https://2.chat.example' ),
891896 id: 1002 ,
892897 user: eg.user (userId: 1001 ));
898+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
893899 final message2 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
894900 final data2 =
895901 messageFcmMessage (message2, account: account2, streamName: stream.name);
@@ -917,19 +923,21 @@ void main() {
917923 })));
918924
919925 test ('remove: different user-ids but same realm URL and same message-ids' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
920- await init ();
926+ await init (addSelfAccount : false );
921927 final realmUrl = eg.realmUrl;
922928 final stream = eg.stream ();
923929 const topic = 'Some Topic' ;
924930 final conversationKey = 'stream:${stream .streamId }:some topic' ;
925931
926932 final account1 = eg.account (id: 1001 , user: eg.user (userId: 1001 ), realmUrl: realmUrl);
933+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
927934 final message1 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
928935 final data1 =
929936 messageFcmMessage (message1, account: account1, streamName: stream.name);
930937 final groupKey1 = '${account1 .realmUrl }|${account1 .userId }' ;
931938
932939 final account2 = eg.account (id: 1002 , user: eg.user (userId: 1002 ), realmUrl: realmUrl);
940+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
933941 final message2 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
934942 final data2 =
935943 messageFcmMessage (message2, account: account2, streamName: stream.name);
@@ -955,6 +963,76 @@ void main() {
955963 receiveFcmMessage (async , removeFcmMessage ([message2], account: account2));
956964 check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
957965 })));
966+
967+ test ('removeNotificationsForAccount: removes notifications' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
968+ await init ();
969+ final message = eg.dmMessage (from: eg.otherUser, to: [eg.selfUser]);
970+ receiveFcmMessage (async , messageFcmMessage (message));
971+ check (testBinding.androidNotificationHost.activeNotifications).isNotEmpty ();
972+
973+ await NotificationDisplayManager .removeNotificationsForAccount (
974+ eg.selfAccount.realmUrl, eg.selfAccount.userId);
975+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
976+ })));
977+
978+ test ('removeNotificationsForAccount: leaves notifications for other accounts (same realm URL)' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
979+ await init (addSelfAccount: false );
980+
981+ final realmUrl = eg.realmUrl;
982+ final account1 = eg.account (id: 1001 , user: eg.user (userId: 1001 ), realmUrl: realmUrl);
983+ final account2 = eg.account (id: 1002 , user: eg.user (userId: 1002 ), realmUrl: realmUrl);
984+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
985+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
986+
987+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
988+
989+ final message1 = eg.streamMessage ();
990+ final message2 = eg.streamMessage ();
991+ receiveFcmMessage (async , messageFcmMessage (message1, account: account1));
992+ receiveFcmMessage (async , messageFcmMessage (message2, account: account2));
993+ check (testBinding.androidNotificationHost.activeNotifications)
994+ .length.equals (4 );
995+
996+ await NotificationDisplayManager .removeNotificationsForAccount (
997+ realmUrl, account1.userId);
998+ check (testBinding.androidNotificationHost.activeNotifications)
999+ ..length.equals (2 )
1000+ ..first.notification.group.equals ('$realmUrl |${account2 .userId }' );
1001+ })));
1002+
1003+ test ('removeNotificationsForAccount leaves notifications for other accounts (same user-ids)' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
1004+ await init (addSelfAccount: false );
1005+
1006+ final userId = 1001 ;
1007+ final account1 = eg.account (
1008+ id: 1001 , user: eg.user (userId: userId),
1009+ realmUrl: Uri .parse ('https://realm1.example' ));
1010+ final account2 = eg.account (
1011+ id: 1002 , user: eg.user (userId: userId),
1012+ realmUrl: Uri .parse ('https://realm2.example' ));
1013+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
1014+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
1015+
1016+ final message1 = eg.streamMessage ();
1017+ final message2 = eg.streamMessage ();
1018+ receiveFcmMessage (async , messageFcmMessage (message1, account: account1));
1019+ receiveFcmMessage (async , messageFcmMessage (message2, account: account2));
1020+ check (testBinding.androidNotificationHost.activeNotifications)
1021+ .length.equals (4 );
1022+
1023+ await NotificationDisplayManager .removeNotificationsForAccount (account1.realmUrl, userId);
1024+ check (testBinding.androidNotificationHost.activeNotifications)
1025+ ..length.equals (2 )
1026+ ..first.notification.group.equals ('${account2 .realmUrl }|$userId ' );
1027+ })));
1028+
1029+ test ('removeNotificationsForAccount does nothing if there are no notifications' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
1030+ await init ();
1031+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
1032+
1033+ await NotificationDisplayManager .removeNotificationsForAccount (eg.selfAccount.realmUrl, eg.selfAccount.userId);
1034+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
1035+ })));
9581036 });
9591037
9601038 group ('NotificationDisplayManager open' , () {
@@ -976,7 +1054,7 @@ void main() {
9761054
9771055 Future <void > prepare (WidgetTester tester,
9781056 {bool early = false , bool withAccount = true }) async {
979- await init ();
1057+ await init (addSelfAccount : false );
9801058 pushedRoutes = [];
9811059 final testNavObserver = TestNavigatorObserver ()
9821060 ..onPushed = (route, prevRoute) => pushedRoutes.add (route);
0 commit comments