@@ -26,9 +26,9 @@ import 'package:zulip/widgets/message_list.dart';
26
26
import 'package:zulip/widgets/page.dart' ;
27
27
import 'package:zulip/widgets/theme.dart' ;
28
28
29
+ import '../example_data.dart' as eg;
29
30
import '../fake_async.dart' ;
30
31
import '../model/binding.dart' ;
31
- import '../example_data.dart' as eg;
32
32
import '../model/narrow_checks.dart' ;
33
33
import '../stdlib_checks.dart' ;
34
34
import '../test_images.dart' ;
@@ -114,7 +114,10 @@ void main() {
114
114
return http.runWithClient (callback, httpClientFactory ?? () => fakeHttpClientGivingSuccess);
115
115
}
116
116
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
+ }
118
121
addTearDown (testBinding.reset);
119
122
testBinding.firebaseMessagingInitialToken = '012abc' ;
120
123
addTearDown (NotificationService .debugReset);
@@ -872,7 +875,8 @@ void main() {
872
875
})));
873
876
874
877
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
+
876
880
final stream = eg.stream ();
877
881
const topic = 'Some Topic' ;
878
882
final conversationKey = 'stream:${stream .streamId }:some topic' ;
@@ -881,6 +885,7 @@ void main() {
881
885
realmUrl: Uri .parse ('https://1.chat.example' ),
882
886
id: 1001 ,
883
887
user: eg.user (userId: 1001 ));
888
+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
884
889
final message1 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
885
890
final data1 =
886
891
messageFcmMessage (message1, account: account1, streamName: stream.name);
@@ -890,6 +895,7 @@ void main() {
890
895
realmUrl: Uri .parse ('https://2.chat.example' ),
891
896
id: 1002 ,
892
897
user: eg.user (userId: 1001 ));
898
+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
893
899
final message2 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
894
900
final data2 =
895
901
messageFcmMessage (message2, account: account2, streamName: stream.name);
@@ -917,19 +923,21 @@ void main() {
917
923
})));
918
924
919
925
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 );
921
927
final realmUrl = eg.realmUrl;
922
928
final stream = eg.stream ();
923
929
const topic = 'Some Topic' ;
924
930
final conversationKey = 'stream:${stream .streamId }:some topic' ;
925
931
926
932
final account1 = eg.account (id: 1001 , user: eg.user (userId: 1001 ), realmUrl: realmUrl);
933
+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
927
934
final message1 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
928
935
final data1 =
929
936
messageFcmMessage (message1, account: account1, streamName: stream.name);
930
937
final groupKey1 = '${account1 .realmUrl }|${account1 .userId }' ;
931
938
932
939
final account2 = eg.account (id: 1002 , user: eg.user (userId: 1002 ), realmUrl: realmUrl);
940
+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
933
941
final message2 = eg.streamMessage (id: 1000 , stream: stream, topic: topic);
934
942
final data2 =
935
943
messageFcmMessage (message2, account: account2, streamName: stream.name);
@@ -955,6 +963,68 @@ void main() {
955
963
receiveFcmMessage (async , removeFcmMessage ([message2], account: account2));
956
964
check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
957
965
})));
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
+
971
+ receiveFcmMessage (async , messageFcmMessage (message));
972
+ check (testBinding.androidNotificationHost.activeNotifications).isNotEmpty ();
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
+ final message1 = eg.streamMessage ();
989
+ final message2 = eg.streamMessage ();
990
+ receiveFcmMessage (async , messageFcmMessage (message1, account: account1));
991
+ receiveFcmMessage (async , messageFcmMessage (message2, account: account2));
992
+
993
+ check (testBinding.androidNotificationHost.activeNotifications).length.equals (4 );
994
+ await NotificationDisplayManager .removeNotificationsForAccount (realmUrl, account1.userId);
995
+ check (testBinding.androidNotificationHost.activeNotifications).length.equals (2 );
996
+ check (testBinding.androidNotificationHost.activeNotifications.first.notification.group)
997
+ .equals ('$realmUrl |${account2 .userId }' );
998
+ })));
999
+
1000
+ test ('removeNotificationsForAccount leaves notifications for other accounts (same user-ids)' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
1001
+ await init (addSelfAccount: false );
1002
+
1003
+ final userId = 1001 ;
1004
+ final account1 = eg.account (id: 1001 , user: eg.user (userId: userId), realmUrl: Uri .parse ('https://realm1.example' ));
1005
+ final account2 = eg.account (id: 1002 , user: eg.user (userId: userId), realmUrl: Uri .parse ('https://realm2.example' ));
1006
+ await testBinding.globalStore.add (account1, eg.initialSnapshot ());
1007
+ await testBinding.globalStore.add (account2, eg.initialSnapshot ());
1008
+
1009
+ final message1 = eg.streamMessage ();
1010
+ final message2 = eg.streamMessage ();
1011
+ receiveFcmMessage (async , messageFcmMessage (message1, account: account1));
1012
+ receiveFcmMessage (async , messageFcmMessage (message2, account: account2));
1013
+
1014
+ check (testBinding.androidNotificationHost.activeNotifications).length.equals (4 );
1015
+ await NotificationDisplayManager .removeNotificationsForAccount (account1.realmUrl, userId);
1016
+ check (testBinding.androidNotificationHost.activeNotifications).length.equals (2 );
1017
+ check (testBinding.androidNotificationHost.activeNotifications.first.notification.group)
1018
+ .equals ('${account2 .realmUrl }|$userId ' );
1019
+ })));
1020
+
1021
+ test ('removeNotificationsForAccount does nothing if there are no notifications' , () => runWithHttpClient (() => awaitFakeAsync ((async ) async {
1022
+ await init ();
1023
+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
1024
+ await NotificationDisplayManager .removeNotificationsForAccount (eg.selfAccount.realmUrl, eg.selfAccount.userId);
1025
+ check (testBinding.androidNotificationHost.activeNotifications).isEmpty ();
1026
+ })));
1027
+
958
1028
});
959
1029
960
1030
group ('NotificationDisplayManager open' , () {
@@ -976,7 +1046,7 @@ void main() {
976
1046
977
1047
Future <void > prepare (WidgetTester tester,
978
1048
{bool early = false , bool withAccount = true }) async {
979
- await init ();
1049
+ await init (addSelfAccount : false );
980
1050
pushedRoutes = [];
981
1051
final testNavObserver = TestNavigatorObserver ()
982
1052
..onPushed = (route, prevRoute) => pushedRoutes.add (route);
0 commit comments