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