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