Skip to content

Edit-message (5/n): Fix bug where compose progress can be lost on new event queue #1482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/widgets/compose_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1523,14 +1523,26 @@ class _ComposeBoxState extends State<ComposeBox> with PerAccountStoreAwareStateM

@override
void onNewStore() {
final newStore = PerAccountStoreWidget.of(context);

final controller = _controller;
if (controller == null) {
_setNewController(newStore);
return;
}

switch (controller) {
case StreamComposeBoxController():
controller.topic.store = newStore;
case FixedDestinationComposeBoxController():
// no reference to the store that needs updating
}
}

void _setNewController(PerAccountStore store) {
switch (widget.narrow) {
case ChannelNarrow():
final store = PerAccountStoreWidget.of(context);
if (_controller == null) {
_controller = StreamComposeBoxController(store: store);
} else {
(controller as StreamComposeBoxController).topic.store = store;
}
_controller = StreamComposeBoxController(store: store);
case TopicNarrow():
case DmNarrow():
_controller = FixedDestinationComposeBoxController();
Expand Down
45 changes: 43 additions & 2 deletions test/widgets/compose_box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import '../api/fake_api.dart';
import '../example_data.dart' as eg;
import '../flutter_checks.dart';
import '../model/binding.dart';
import '../model/store_checks.dart';
import '../model/test_store.dart';
import '../model/typing_status_test.dart';
import '../stdlib_checks.dart';
Expand Down Expand Up @@ -58,14 +59,14 @@ void main() {
zulipFeatureLevel ??= eg.futureZulipFeatureLevel;
final selfAccount = eg.account(user: selfUser, zulipFeatureLevel: zulipFeatureLevel);
await testBinding.globalStore.add(selfAccount, eg.initialSnapshot(
realmUsers: [selfUser, ...otherUsers],
streams: streams,
zulipFeatureLevel: zulipFeatureLevel,
realmMandatoryTopics: mandatoryTopics,
));

store = await testBinding.globalStore.perAccount(selfAccount.id);

await store.addUsers([selfUser, ...otherUsers]);
await store.addStreams(streams);
connection = store.connection as FakeApiConnection;

await tester.pumpWidget(TestZulipApp(accountId: selfAccount.id,
Expand Down Expand Up @@ -111,6 +112,11 @@ void main() {
await tester.enterText(contentInputFinder, content);
}

void checkContentInputValue(WidgetTester tester, String expected) {
check(tester.widget<TextField>(contentInputFinder))
.controller.isNotNull().value.text.equals(expected);
}

Future<void> tapSendButton(WidgetTester tester) async {
connection.prepare(json: SendMessageResult(id: 123).toJson());
await tester.tap(find.byIcon(ZulipIcons.send));
Expand Down Expand Up @@ -1239,4 +1245,39 @@ void main() {
maxHeight: verticalPadding + 170 * 1.5, maxVisibleLines: 6);
});
});

group('ComposeBoxState new-event-queue transition', () {
testWidgets('content input not cleared when store changes', (tester) async {
// Regression test for: https://github.com/zulip/zulip-flutter/issues/1470

TypingNotifier.debugEnable = false;
addTearDown(TypingNotifier.debugReset);

final channel = eg.stream();
await prepareComposeBox(tester,
narrow: eg.topicNarrow(channel.streamId, 'topic'), streams: [channel]);

await enterContent(tester, 'some content');
checkContentInputValue(tester, 'some content');

store.updateMachine!
..debugPauseLoop()
..poll()
..debugPrepareLoopError(
eg.apiExceptionBadEventQueueId(queueId: store.queueId))
..debugAdvanceLoop();
await tester.pump();

final newStore = testBinding.globalStore.perAccountSync(store.accountId)!;
check(newStore)
// a new store has replaced the old one
..not((it) => it.identicalTo(store))
// new store has the same boring data, in order to present a compose box
// that allows composing, instead of a no-posting-permission banner
..accountId.equals(store.accountId)
..streams.containsKey(channel.streamId);

checkContentInputValue(tester, 'some content');
});
});
}