forked from zulip/zulip-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
msglist: Handle updated events in MessageListView (zulip#118).
Processes an UpdateMessageEvent and hands it off to the MessageListView to update, if the message is visible in the MessageListView. This completes the changes required for issue zulip#118.
- Loading branch information
Showing
6 changed files
with
311 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
import 'package:checks/checks.dart'; | ||
import 'package:test/scaffolding.dart'; | ||
import 'package:zulip/api/model/events.dart'; | ||
import 'package:zulip/api/model/model.dart'; | ||
import 'package:zulip/api/route/messages.dart'; | ||
import 'package:zulip/model/message_list.dart'; | ||
import 'package:zulip/model/narrow.dart'; | ||
import 'package:zulip/model/store.dart'; | ||
|
||
import '../api/fake_api.dart'; | ||
import '../api/model/model_checks.dart'; | ||
import '../model/binding.dart'; | ||
import '../model/test_store.dart'; | ||
import '../example_data.dart' as eg; | ||
|
||
const int userId = 1; | ||
|
||
Future<PerAccountStore> setupStore(ZulipStream stream) async { | ||
addTearDown(TestZulipBinding.instance.reset); | ||
|
||
await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot()); | ||
|
||
final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id); | ||
store.addUser(eg.user(userId: userId)); | ||
store.addStream(stream); | ||
|
||
return store; | ||
} | ||
|
||
Future<MessageListView> messageListViewWithMessages(List<Message> messages, PerAccountStore store, Narrow narrow) async { | ||
final messageList = MessageListView.init(store: store, narrow: narrow); | ||
|
||
final connection = store.connection as FakeApiConnection; | ||
|
||
connection.prepare(json: GetMessagesResult( | ||
anchor: messages.first.id, | ||
foundNewest: true, | ||
foundOldest: true, | ||
foundAnchor: true, | ||
historyLimited: false, | ||
messages: messages, | ||
).toJson()); | ||
|
||
await messageList.fetch(); | ||
|
||
check(messageList.messages.length).equals(messages.length); | ||
|
||
return messageList; | ||
} | ||
|
||
void main() async { | ||
TestZulipBinding.ensureInitialized(); | ||
|
||
final stream = eg.stream(); | ||
final narrow = StreamNarrow(stream.streamId); | ||
|
||
group('update message tests', () { | ||
|
||
test('find message in message list returns index of message', () async { | ||
final store = await setupStore(stream); | ||
|
||
final m1 = eg.streamMessage(id: 2, stream: stream); | ||
final m2 = eg.streamMessage(id: 4, stream: stream); | ||
final m3 = eg.streamMessage(id: 6, stream: stream); | ||
|
||
final messageList = await messageListViewWithMessages([m1, m2, m3], store, narrow); | ||
// The implementation of this uses a binary search, so let's test it | ||
// a bit more exhaustively. | ||
|
||
check(messageList.findMessageWithId(1)).equals(-1); | ||
check(messageList.findMessageWithId(2)).equals(0); | ||
check(messageList.findMessageWithId(3)).equals(-1); | ||
check(messageList.findMessageWithId(4)).equals(1); | ||
check(messageList.findMessageWithId(5)).equals(-1); | ||
check(messageList.findMessageWithId(6)).equals(2); | ||
check(messageList.findMessageWithId(7)).equals(-1); | ||
|
||
// Invalid IDs | ||
check(messageList.findMessageWithId(-8409)).equals(-1); | ||
check(messageList.findMessageWithId(0)).equals(-1); | ||
}); | ||
|
||
test('update events are correctly applied to message when it is in the stream', () async { | ||
final store = await setupStore(stream); | ||
|
||
const oldContent = "<p>Hello, world</p>"; | ||
const newContent = "<p>Hello, edited</p>"; | ||
const newTimestamp = 99999; | ||
|
||
List<String> oldFlags = []; | ||
List<String> newFlags = ["starred"]; | ||
|
||
final originalMessage = eg.streamMessage(id: 243, stream: stream, content: oldContent, flags: oldFlags); | ||
final messageList = await messageListViewWithMessages([originalMessage], store, narrow); | ||
|
||
final updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: originalMessage.id, | ||
messageIds: [originalMessage.id], | ||
flags: newFlags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
isMeMessage: true, | ||
userId: userId, | ||
renderingOnly: false, | ||
); | ||
|
||
final message = messageList.messages.single; | ||
check(message) | ||
..content.equals(oldContent) | ||
..flags.deepEquals(oldFlags) | ||
..isMeMessage.isFalse(); | ||
|
||
var listenersNotified = false; | ||
|
||
messageList.addListener(() { listenersNotified = true; }); | ||
messageList.maybeUpdateMessage(updateEvent); | ||
|
||
check(listenersNotified).isTrue(); | ||
|
||
check(message) | ||
..identicalTo(messageList.messages.single) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.equals(newTimestamp) | ||
..flags.equals(newFlags) | ||
..isMeMessage.isTrue(); | ||
}); | ||
|
||
test('update event is ignored when message is not in the message list', () async { | ||
final store = await setupStore(stream); | ||
|
||
const oldContent = "<p>Hello, world</p>"; | ||
const newContent = "<p>Hello, edited</p>"; | ||
const newTimestamp = 99999; | ||
|
||
final originalMessage = eg.streamMessage(id: 243, stream: stream, content: oldContent); | ||
final messageList = await messageListViewWithMessages([originalMessage], store, narrow); | ||
|
||
final updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: originalMessage.id + 1, | ||
messageIds: [originalMessage.id + 1], | ||
flags: originalMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
userId: userId, | ||
renderingOnly: false, | ||
); | ||
|
||
final message = messageList.messages.single; | ||
check(message).content.equals(oldContent); | ||
|
||
var listenersNotified = false; | ||
|
||
messageList.addListener(() { listenersNotified = true; }); | ||
messageList.maybeUpdateMessage(updateEvent); | ||
|
||
check(listenersNotified).isFalse(); | ||
check(message).content.equals(oldContent); | ||
}); | ||
|
||
test('rendering-only update does not change timestamp', () async { | ||
final store = await setupStore(stream); | ||
|
||
const oldContent = "<p>Hello, world</p>"; | ||
const oldTimestamp = 78492; | ||
const newContent = "<p>Hello, world</p> <div>Some link preview</div>"; | ||
const newTimestamp = 99999; | ||
|
||
final originalMessage = eg.streamMessage(id: 972, stream: stream, content: oldContent); | ||
originalMessage.lastEditTimestamp = oldTimestamp; | ||
|
||
final messageList = await messageListViewWithMessages([originalMessage], store, narrow); | ||
|
||
final updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: originalMessage.id, | ||
messageIds: [originalMessage.id], | ||
flags: originalMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
renderingOnly: true, | ||
userId: null, | ||
); | ||
|
||
final message = messageList.messages[0]; | ||
messageList.maybeUpdateMessage(updateEvent); | ||
check(message) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.equals(oldTimestamp); | ||
}); | ||
|
||
// TODO(server-5): Cut this test; rely on renderingOnly from FL 114 | ||
test('rendering-only update does not change timestamp (for old server versions)', () async { | ||
final store = await setupStore(stream); | ||
|
||
const oldContent = "<p>Hello, world</p>"; | ||
const oldTimestamp = 78492; | ||
const newContent = "<p>Hello, world</p> <div>Some link preview</div>"; | ||
const newTimestamp = 99999; | ||
|
||
final originalMessage = eg.streamMessage(id: 972, stream: stream, content: oldContent); | ||
originalMessage.lastEditTimestamp = oldTimestamp; | ||
|
||
final messageList = await messageListViewWithMessages([originalMessage], store, narrow); | ||
|
||
final updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: originalMessage.id, | ||
messageIds: [originalMessage.id], | ||
flags: originalMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
userId: null, | ||
); | ||
|
||
final message = messageList.messages.single; | ||
messageList.maybeUpdateMessage(updateEvent); | ||
check(message) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.equals(oldTimestamp); | ||
}); | ||
}); | ||
} |