|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:zulip/api/model/model.dart'; |
| 5 | +import 'package:zulip/api/route/messages.dart'; |
| 6 | +import 'package:zulip/model/compose.dart'; |
| 7 | +import 'package:zulip/model/narrow.dart'; |
| 8 | +import 'package:zulip/model/store.dart'; |
| 9 | +import 'package:zulip/widgets/compose_box.dart'; |
| 10 | +import 'package:zulip/widgets/content.dart'; |
| 11 | +import 'package:zulip/widgets/message_list.dart'; |
| 12 | +import 'package:zulip/widgets/store.dart'; |
| 13 | +import '../api/fake_api.dart'; |
| 14 | + |
| 15 | +import '../example_data.dart' as eg; |
| 16 | +import '../model/binding.dart'; |
| 17 | +import '../model/test_store.dart'; |
| 18 | +import 'compose_box_checks.dart'; |
| 19 | +import 'dialog_checks.dart'; |
| 20 | + |
| 21 | +void main() { |
| 22 | + group('QuoteAndReplyButton', () { |
| 23 | + TestDataBinding.ensureInitialized(); |
| 24 | + |
| 25 | + /// Simulates loading a [MessageListPage] and long-pressing on [message]. |
| 26 | + Future<void> setupToMessageActionSheet(WidgetTester tester, { |
| 27 | + required Message message, |
| 28 | + required Narrow narrow, |
| 29 | + }) async { |
| 30 | + addTearDown(TestDataBinding.instance.reset); |
| 31 | + |
| 32 | + await TestDataBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot); |
| 33 | + final store = await TestDataBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 34 | + store.addUser(eg.user(userId: message.senderId)); |
| 35 | + if (message is StreamMessage) { |
| 36 | + store.addStream(eg.stream(streamId: message.streamId)); |
| 37 | + } |
| 38 | + final connection = store.connection as FakeApiConnection; |
| 39 | + |
| 40 | + // prepare message list data |
| 41 | + connection.prepare(json: GetMessagesResult( |
| 42 | + anchor: message.id, |
| 43 | + foundNewest: true, |
| 44 | + foundOldest: true, |
| 45 | + foundAnchor: true, |
| 46 | + historyLimited: false, |
| 47 | + messages: [message], |
| 48 | + ).toJson()); |
| 49 | + |
| 50 | + await tester.pumpWidget( |
| 51 | + MaterialApp( |
| 52 | + home: GlobalStoreWidget( |
| 53 | + child: PerAccountStoreWidget( |
| 54 | + accountId: eg.selfAccount.id, |
| 55 | + child: MessageListPage(narrow: narrow))))); |
| 56 | + |
| 57 | + // global store, per-account store, and message list get loaded |
| 58 | + await tester.pumpAndSettle(); |
| 59 | + |
| 60 | + // request the message action sheet |
| 61 | + final messageContent = tester.widget<MessageContent>( |
| 62 | + find.byWidgetPredicate((Widget widget) => widget is MessageContent && widget.message.id == message.id)); |
| 63 | + await tester.longPress(find.byWidget(messageContent)); |
| 64 | + // sheet appears onscreen; default duration of bottom-sheet enter animation |
| 65 | + await tester.pump(const Duration(milliseconds: 250)); |
| 66 | + } |
| 67 | + |
| 68 | + ComposeBoxController? findComposeBoxController(WidgetTester tester) { |
| 69 | + return tester.widgetList<ComposeBox>(find.byType(ComposeBox)) |
| 70 | + .single.controllerKey?.currentState; |
| 71 | + } |
| 72 | + |
| 73 | + Widget? findQuoteAndReplyButton(WidgetTester tester) { |
| 74 | + return tester.widgetList(find.byIcon(Icons.format_quote_outlined)).singleOrNull; |
| 75 | + } |
| 76 | + |
| 77 | + void prepareRawContentResponseSuccess(PerAccountStore store, { |
| 78 | + required Message message, |
| 79 | + required String rawContent, |
| 80 | + }) { |
| 81 | + // Prepare fetch-raw-Markdown response |
| 82 | + // TODO: Message should really only differ from `message` |
| 83 | + // in its content / content_type, not in `id` or anything else. |
| 84 | + (store.connection as FakeApiConnection).prepare(json: |
| 85 | + GetMessageResult(message: eg.streamMessage(contentMarkdown: rawContent)).toJson()); |
| 86 | + } |
| 87 | + |
| 88 | + void prepareRawContentResponseError(PerAccountStore store) { |
| 89 | + final fakeResponseJson = { |
| 90 | + 'code': 'BAD_REQUEST', |
| 91 | + 'msg': 'Invalid message(s)', |
| 92 | + 'result': 'error', |
| 93 | + }; |
| 94 | + (store.connection as FakeApiConnection).prepare(httpStatus: 400, json: fakeResponseJson); |
| 95 | + } |
| 96 | + |
| 97 | + /// Simulates tapping the quote-and-reply button in the message action sheet. |
| 98 | + /// |
| 99 | + /// Checks that there is a quote-and-reply button. |
| 100 | + Future<void> tapQuoteAndReplyButton(WidgetTester tester) async { |
| 101 | + final quoteAndReplyButton = findQuoteAndReplyButton(tester); |
| 102 | + check(quoteAndReplyButton).isNotNull(); |
| 103 | + await tester.tap(find.byWidget(quoteAndReplyButton!)); |
| 104 | + } |
| 105 | + |
| 106 | + void checkLoadingState(PerAccountStore store, ComposeContentController contentController, { |
| 107 | + required TextEditingValue valueBefore, |
| 108 | + required Message message, |
| 109 | + }) { |
| 110 | + check(contentController).value.equals((ComposeContentController() |
| 111 | + ..value = valueBefore |
| 112 | + ..insertPadded(quoteAndReplyPlaceholder(store, message: message)) |
| 113 | + ).value); |
| 114 | + check(contentController).validationErrors.contains(ContentValidationError.quoteAndReplyInProgress); |
| 115 | + } |
| 116 | + |
| 117 | + void checkSuccessState(PerAccountStore store, ComposeContentController contentController, { |
| 118 | + required TextEditingValue valueBefore, |
| 119 | + required Message message, |
| 120 | + required String rawContent, |
| 121 | + }) { |
| 122 | + final c = ComposeContentController() |
| 123 | + ..value = valueBefore |
| 124 | + ..insertPadded(quoteAndReply(store, message: message, rawContent: rawContent)); |
| 125 | + if (!valueBefore.selection.isValid) { |
| 126 | + // (At the end of the process, we focus the input, which puts a cursor |
| 127 | + // at the end if there wasn't a cursor before.) |
| 128 | + c.selection = TextSelection.collapsed(offset: c.text.length); |
| 129 | + } |
| 130 | + check(contentController).value.equals(c.value); |
| 131 | + check(contentController).not(it()..validationErrors.contains(ContentValidationError.quoteAndReplyInProgress)); |
| 132 | + } |
| 133 | + |
| 134 | + testWidgets('in stream narrow', (WidgetTester tester) async { |
| 135 | + final message = eg.streamMessage(); |
| 136 | + await setupToMessageActionSheet(tester, message: message, narrow: StreamNarrow(message.streamId)); |
| 137 | + final store = await TestDataBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 138 | + |
| 139 | + final composeBoxController = findComposeBoxController(tester); |
| 140 | + check(composeBoxController).isNotNull(); // should exist; this is a stream narrow |
| 141 | + final contentController = composeBoxController!.contentController; |
| 142 | + |
| 143 | + final valueBefore = contentController.value; |
| 144 | + prepareRawContentResponseSuccess(store, message: message, rawContent: 'Hello world'); |
| 145 | + await tapQuoteAndReplyButton(tester); // should exist; this is a stream narrow |
| 146 | + checkLoadingState(store, contentController, valueBefore: valueBefore, message: message); |
| 147 | + await tester.pump(Duration.zero); // message is fetched; compose box updates |
| 148 | + check(composeBoxController.contentFocusNode.hasFocus).isTrue(); |
| 149 | + checkSuccessState(store, contentController, |
| 150 | + valueBefore: valueBefore, message: message, rawContent: 'Hello world'); |
| 151 | + }); |
| 152 | + |
| 153 | + testWidgets('in topic narrow', (WidgetTester tester) async { |
| 154 | + final message = eg.streamMessage(); |
| 155 | + await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message)); |
| 156 | + final store = await TestDataBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 157 | + |
| 158 | + final composeBoxController = findComposeBoxController(tester); |
| 159 | + check(composeBoxController).isNotNull(); // should exist; this is a topic narrow |
| 160 | + final contentController = composeBoxController!.contentController; |
| 161 | + |
| 162 | + final valueBefore = contentController.value; |
| 163 | + prepareRawContentResponseSuccess(store, message: message, rawContent: 'Hello world'); |
| 164 | + await tapQuoteAndReplyButton(tester); // should exist; this is a topic narrow |
| 165 | + checkLoadingState(store, contentController, valueBefore: valueBefore, message: message); |
| 166 | + await tester.pump(Duration.zero); // message is fetched; compose box updates |
| 167 | + check(composeBoxController.contentFocusNode.hasFocus).isTrue(); |
| 168 | + checkSuccessState(store, contentController, |
| 169 | + valueBefore: valueBefore, message: message, rawContent: 'Hello world'); |
| 170 | + }); |
| 171 | + |
| 172 | + testWidgets('in DM narrow', (WidgetTester tester) async { |
| 173 | + final message = eg.dmMessage(from: eg.selfUser, to: [eg.otherUser]); |
| 174 | + await setupToMessageActionSheet(tester, |
| 175 | + message: message, narrow: DmNarrow.ofMessage(message, selfUserId: eg.selfUser.userId)); |
| 176 | + final store = await TestDataBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 177 | + |
| 178 | + final composeBoxController = findComposeBoxController(tester); |
| 179 | + check(composeBoxController).isNotNull(); // should exist; this is a DM narrow |
| 180 | + final contentController = composeBoxController!.contentController; |
| 181 | + |
| 182 | + final valueBefore = contentController.value; |
| 183 | + prepareRawContentResponseSuccess(store, message: message, rawContent: 'Hello world'); |
| 184 | + await tapQuoteAndReplyButton(tester); // should exist; this is a DM narrow |
| 185 | + checkLoadingState(store, contentController, valueBefore: valueBefore, message: message); |
| 186 | + await tester.pump(Duration.zero); // message is fetched; compose box updates |
| 187 | + check(composeBoxController.contentFocusNode.hasFocus).isTrue(); |
| 188 | + checkSuccessState(store, contentController, |
| 189 | + valueBefore: valueBefore, message: message, rawContent: 'Hello world'); |
| 190 | + }); |
| 191 | + |
| 192 | + testWidgets('request has an error', (WidgetTester tester) async { |
| 193 | + final message = eg.streamMessage(); |
| 194 | + await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message)); |
| 195 | + final store = await TestDataBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 196 | + |
| 197 | + final composeBoxController = findComposeBoxController(tester); |
| 198 | + check(composeBoxController).isNotNull(); // should exist; this is a topic narrow |
| 199 | + final contentController = composeBoxController!.contentController; |
| 200 | + |
| 201 | + final valueBefore = contentController.value = TextEditingValue.empty; |
| 202 | + prepareRawContentResponseError(store); |
| 203 | + await tapQuoteAndReplyButton(tester); // should exist; this is a topic narrow |
| 204 | + checkLoadingState(store, contentController, valueBefore: valueBefore, message: message); |
| 205 | + await tester.pump(Duration.zero); // error arrives; error dialog shows |
| 206 | + |
| 207 | + await tester.tap(find.byWidget(checkErrorDialog(tester, |
| 208 | + expectedTitle: 'Quotation failed', |
| 209 | + expectedMessage: 'That message does not seem to exist.', |
| 210 | + ))); |
| 211 | + |
| 212 | + check(contentController.value).equals(const TextEditingValue( |
| 213 | + // The placeholder was removed. (A newline from the placeholder's |
| 214 | + // insertPadded remains; I guess ideally we'd try to prevent that.) |
| 215 | + text: '\n', |
| 216 | + |
| 217 | + // (At the end of the process, we focus the input.) |
| 218 | + selection: TextSelection.collapsed(offset: 1), // |
| 219 | + )); |
| 220 | + }); |
| 221 | + |
| 222 | + testWidgets('not offered in AllMessagesNarrow (composing to reply is not yet supported)', (WidgetTester tester) async { |
| 223 | + final message = eg.streamMessage(); |
| 224 | + await setupToMessageActionSheet(tester, message: message, narrow: const AllMessagesNarrow()); |
| 225 | + check(findQuoteAndReplyButton(tester)).isNull(); |
| 226 | + check(findComposeBoxController(tester)).isNull(); |
| 227 | + }); |
| 228 | + }); |
| 229 | +} |
0 commit comments