|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter_test/flutter_test.dart'; |
| 3 | +import 'package:zulip/api/model/events.dart'; |
| 4 | +import 'package:zulip/api/model/model.dart'; |
| 5 | +import 'package:zulip/model/narrow.dart'; |
| 6 | +import 'package:zulip/model/typing_status.dart'; |
| 7 | + |
| 8 | +import '../example_data.dart' as eg; |
| 9 | +import '../fake_async.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + late TypingStatus model; |
| 13 | + late int notifiedCount; |
| 14 | + |
| 15 | + void checkNotNotified() { |
| 16 | + check(notifiedCount).equals(0); |
| 17 | + } |
| 18 | + |
| 19 | + void checkNotifiedOnce() { |
| 20 | + check(notifiedCount).equals(1); |
| 21 | + notifiedCount = 0; |
| 22 | + } |
| 23 | + |
| 24 | + void handleTypingEvent(SendableNarrow narrow, TypingOp op, User sender) { |
| 25 | + assert(sender != eg.selfUser); |
| 26 | + model.handleTypingEvent(eg.typingEvent(narrow, op, sender.userId)); |
| 27 | + } |
| 28 | + |
| 29 | + void checkTypists(Map<SendableNarrow, List<User>> typistsByNarrow) { |
| 30 | + final actualTypistsByNarrow = <SendableNarrow, Iterable<int>>{}; |
| 31 | + for (final narrow in model.debugActiveNarrows) { |
| 32 | + actualTypistsByNarrow[narrow] = model.typistIdsInNarrow(narrow); |
| 33 | + } |
| 34 | + check(actualTypistsByNarrow).deepEquals( |
| 35 | + typistsByNarrow.map((k, v) => MapEntry(k, v.map((e) => e.userId)))); |
| 36 | + } |
| 37 | + |
| 38 | + void prepareModel({ |
| 39 | + int? selfUserId, |
| 40 | + Map<SendableNarrow, List<User>> typistsByNarrow = const {}, |
| 41 | + }) { |
| 42 | + model = TypingStatus( |
| 43 | + selfUserId: selfUserId ?? eg.selfUser.userId, |
| 44 | + typingStartedExpiryPeriod: const Duration(milliseconds: 15000)); |
| 45 | + check(model.debugActiveNarrows).isEmpty(); |
| 46 | + notifiedCount = 0; |
| 47 | + model.addListener(() => notifiedCount += 1); |
| 48 | + |
| 49 | + typistsByNarrow.forEach((narrow, typists) { |
| 50 | + for (final typist in typists) { |
| 51 | + handleTypingEvent(narrow, TypingOp.start, typist); |
| 52 | + checkNotifiedOnce(); |
| 53 | + } |
| 54 | + }); |
| 55 | + checkTypists(typistsByNarrow); |
| 56 | + } |
| 57 | + |
| 58 | + final stream = eg.stream(); |
| 59 | + final topicNarrow = TopicNarrow(stream.streamId, 'foo'); |
| 60 | + |
| 61 | + final dmNarrow = DmNarrow.withUser(eg.otherUser.userId, selfUserId: eg.selfUser.userId); |
| 62 | + final groupNarrow = DmNarrow.withOtherUsers( |
| 63 | + [eg.otherUser.userId, eg.thirdUser.userId], selfUserId: eg.selfUser.userId); |
| 64 | + |
| 65 | + group('handle typing start events', () { |
| 66 | + test('add typists in separate narrows', () { |
| 67 | + prepareModel(); |
| 68 | + |
| 69 | + handleTypingEvent(dmNarrow, TypingOp.start, eg.otherUser); |
| 70 | + checkTypists({dmNarrow: [eg.otherUser]}); |
| 71 | + checkNotifiedOnce(); |
| 72 | + |
| 73 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.thirdUser); |
| 74 | + checkTypists({dmNarrow: [eg.otherUser], groupNarrow: [eg.thirdUser]}); |
| 75 | + checkNotifiedOnce(); |
| 76 | + }); |
| 77 | + |
| 78 | + test('add a typist in the same narrow', () { |
| 79 | + prepareModel(); |
| 80 | + |
| 81 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.otherUser); |
| 82 | + checkTypists({groupNarrow: [eg.otherUser]}); |
| 83 | + checkNotifiedOnce(); |
| 84 | + |
| 85 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.thirdUser); |
| 86 | + checkTypists({groupNarrow: [eg.otherUser, eg.thirdUser]}); |
| 87 | + checkNotifiedOnce(); |
| 88 | + }); |
| 89 | + |
| 90 | + test('sort dm recipients', () { |
| 91 | + prepareModel(selfUserId: 5); |
| 92 | + final recipientIds = [5, 4, 10, 8, 2, 1]; |
| 93 | + |
| 94 | + final eventUnsorted = TypingEvent(id: 1, op: TypingOp.start, |
| 95 | + senderId: 1, |
| 96 | + messageType: MessageType.direct, |
| 97 | + recipientIds: recipientIds, |
| 98 | + streamId: null, topic: null); |
| 99 | + // DmNarrow's constructor expects the recipient IDs to be sorted. |
| 100 | + model.handleTypingEvent(eventUnsorted); |
| 101 | + check(model.typistIdsInNarrow( |
| 102 | + DmNarrow(allRecipientIds: recipientIds..sort(), selfUserId: 5), |
| 103 | + )).single.equals(1); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + group('handle typing stop events', () { |
| 108 | + test('remove a typist from an unknown narrow', () { |
| 109 | + prepareModel(); |
| 110 | + |
| 111 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.otherUser); |
| 112 | + checkTypists({groupNarrow: [eg.otherUser]}); |
| 113 | + checkNotifiedOnce(); |
| 114 | + |
| 115 | + handleTypingEvent(dmNarrow, TypingOp.stop, eg.otherUser); |
| 116 | + checkTypists({groupNarrow: [eg.otherUser]}); |
| 117 | + checkNotNotified(); |
| 118 | + }); |
| 119 | + |
| 120 | + test('remove one of two typists in the same narrow', () { |
| 121 | + prepareModel(typistsByNarrow: { |
| 122 | + groupNarrow: [eg.otherUser, eg.thirdUser], |
| 123 | + }); |
| 124 | + |
| 125 | + handleTypingEvent(groupNarrow, TypingOp.stop, eg.otherUser); |
| 126 | + checkTypists({groupNarrow: [eg.thirdUser]}); |
| 127 | + checkNotifiedOnce(); |
| 128 | + }); |
| 129 | + |
| 130 | + test('remove all typists in a narrow', () { |
| 131 | + prepareModel(typistsByNarrow: {dmNarrow: [eg.otherUser]}); |
| 132 | + |
| 133 | + handleTypingEvent(dmNarrow, TypingOp.stop, eg.otherUser); |
| 134 | + checkTypists({}); |
| 135 | + checkNotifiedOnce(); |
| 136 | + }); |
| 137 | + |
| 138 | + test('remove typists from different narrows', () { |
| 139 | + prepareModel(typistsByNarrow: { |
| 140 | + dmNarrow: [eg.otherUser], |
| 141 | + groupNarrow: [eg.thirdUser], |
| 142 | + topicNarrow: [eg.fourthUser], |
| 143 | + }); |
| 144 | + |
| 145 | + handleTypingEvent(groupNarrow, TypingOp.stop, eg.thirdUser); |
| 146 | + checkTypists({dmNarrow: [eg.otherUser], topicNarrow: [eg.fourthUser]}); |
| 147 | + checkNotifiedOnce(); |
| 148 | + |
| 149 | + handleTypingEvent(dmNarrow, TypingOp.stop, eg.otherUser); |
| 150 | + checkTypists({topicNarrow: [eg.fourthUser]}); |
| 151 | + checkNotifiedOnce(); |
| 152 | + |
| 153 | + handleTypingEvent(topicNarrow, TypingOp.stop, eg.fourthUser); |
| 154 | + checkTypists({}); |
| 155 | + checkNotifiedOnce(); |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + group('cancelling old timer', () { |
| 160 | + test('when typing stopped early', () => awaitFakeAsync((async) async { |
| 161 | + prepareModel(); |
| 162 | + |
| 163 | + handleTypingEvent(dmNarrow, TypingOp.start, eg.otherUser); |
| 164 | + checkTypists({dmNarrow: [eg.otherUser]}); |
| 165 | + checkNotifiedOnce(); |
| 166 | + check(async.pendingTimers).length.equals(1); |
| 167 | + |
| 168 | + handleTypingEvent(dmNarrow, TypingOp.stop, eg.otherUser); |
| 169 | + checkTypists({}); |
| 170 | + checkNotifiedOnce(); |
| 171 | + check(async.pendingTimers).isEmpty(); |
| 172 | + })); |
| 173 | + |
| 174 | + test('when typing repeatedly started', () => awaitFakeAsync((async) async { |
| 175 | + prepareModel(); |
| 176 | + |
| 177 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.otherUser); |
| 178 | + checkTypists({groupNarrow: [eg.otherUser]}); |
| 179 | + checkNotifiedOnce(); |
| 180 | + check(async.pendingTimers).length.equals(1); |
| 181 | + |
| 182 | + // The new timer should be active and the old timer should be cancelled. |
| 183 | + handleTypingEvent(groupNarrow, TypingOp.start, eg.otherUser); |
| 184 | + checkTypists({groupNarrow: [eg.otherUser]}); |
| 185 | + check(async.pendingTimers).length.equals(1); |
| 186 | + checkNotNotified(); |
| 187 | + })); |
| 188 | + }); |
| 189 | + |
| 190 | + group('typing start expiry period', () { |
| 191 | + test('typist is removed when the expiry period ends', () => awaitFakeAsync((async) async { |
| 192 | + prepareModel(); |
| 193 | + |
| 194 | + handleTypingEvent(dmNarrow, TypingOp.start, eg.otherUser); |
| 195 | + async.elapse(const Duration(seconds: 5)); |
| 196 | + checkTypists({dmNarrow: [eg.otherUser]}); |
| 197 | + async.elapse(const Duration(seconds: 10)); |
| 198 | + checkTypists({}); |
| 199 | + })); |
| 200 | + |
| 201 | + test('repeated typing start event resets the timer', () => awaitFakeAsync((async) async { |
| 202 | + prepareModel(); |
| 203 | + |
| 204 | + handleTypingEvent(dmNarrow, TypingOp.start, eg.otherUser); |
| 205 | + checkNotifiedOnce(); |
| 206 | + |
| 207 | + async.elapse(const Duration(seconds: 10)); |
| 208 | + checkTypists({dmNarrow: [eg.otherUser]}); |
| 209 | + // We expect the timer to restart from the event. |
| 210 | + handleTypingEvent(dmNarrow, TypingOp.start, eg.otherUser); |
| 211 | + checkNotNotified(); |
| 212 | + |
| 213 | + async.elapse(const Duration(seconds: 10)); |
| 214 | + checkTypists({dmNarrow: [eg.otherUser]}); |
| 215 | + checkNotNotified(); |
| 216 | + |
| 217 | + async.elapse(const Duration(seconds: 5)); |
| 218 | + checkTypists({}); |
| 219 | + checkNotifiedOnce(); |
| 220 | + })); |
| 221 | + }); |
| 222 | +} |
0 commit comments