-
Notifications
You must be signed in to change notification settings - Fork 222
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 (#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 #118.
- Loading branch information
Showing
6 changed files
with
278 additions
and
6 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,200 @@ | ||
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; | ||
const int streamId = 2; | ||
|
||
Future<PerAccountStore> setupStore(ZulipStream stream) async { | ||
await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot()); | ||
PerAccountStore 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 { | ||
MessageListView 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(); | ||
const narrow = StreamNarrow(streamId); | ||
|
||
final ZulipStream stream = eg.stream(streamId: streamId); | ||
PerAccountStore store = await setupStore(stream); | ||
|
||
group('update message tests', () { | ||
|
||
test('find message in message list returns index of message', () async { | ||
Message m1 = eg.streamMessage(id: 792, stream: stream); | ||
Message m2 = eg.streamMessage(id: 793, stream: stream); | ||
Message m3 = eg.streamMessage(id: 794, stream: stream); | ||
|
||
MessageListView messageList = await messageListViewWithMessages([m1, m2, m3], store, narrow); | ||
|
||
int idx = messageList.findMessageWithId(793); | ||
check(idx).equals(1); | ||
|
||
idx = messageList.findMessageWithId(999); | ||
check(idx).equals(-1); | ||
}); | ||
|
||
test('update events are correctly applied to message when it is in the stream', () async { | ||
String oldContent = "<p>Hello, world</p>"; | ||
String newContent = "<p>Hello, edited</p>"; | ||
int newTimestamp = 99999; | ||
|
||
List<String> oldFlags = []; | ||
List<String> newFlags = ["starred"]; | ||
|
||
Message mockMessage = eg.streamMessage(id: 243, stream: stream, content: oldContent, flags: oldFlags); | ||
MessageListView messageList = await messageListViewWithMessages([mockMessage], store, narrow); | ||
|
||
UpdateMessageEvent updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: mockMessage.id, | ||
messageIds: [mockMessage.id], | ||
flags: newFlags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
isMeMessage: true, | ||
userId: userId | ||
); | ||
|
||
Message message = messageList.messages[0]; | ||
check(message) | ||
..content.equals(oldContent) | ||
..flags.deepEquals(oldFlags) | ||
..isMeMessage.equals(false); | ||
|
||
bool listenersNotified = false; | ||
|
||
messageList.addListener(() { listenersNotified = true; }); | ||
messageList.maybeUpdateMessage(updateEvent); | ||
|
||
Message updatedMessage = messageList.messages[0]; | ||
check(updatedMessage).identicalTo(message); | ||
check(listenersNotified).equals(true); | ||
|
||
check(message) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.equals(newTimestamp) | ||
..flags.equals(newFlags) | ||
..isMeMessage.equals(true); | ||
}); | ||
|
||
test('update event is ignored when message is not in the message list', () async { | ||
String oldContent = "<p>Hello, world</p>"; | ||
String newContent = "<p>Hello, edited</p>"; | ||
int newTimestamp = 99999; | ||
|
||
Message mockMessage = eg.streamMessage(id: 243, stream: stream, content: oldContent); | ||
MessageListView messageList = await messageListViewWithMessages([mockMessage], store, narrow); | ||
|
||
UpdateMessageEvent updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: 972, | ||
messageIds: [972], | ||
flags: mockMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
userId: userId, | ||
); | ||
|
||
Message message = messageList.messages[0]; | ||
check(message).content.equals(oldContent); | ||
|
||
bool listenersNotified = false; | ||
|
||
messageList.addListener(() { listenersNotified = true; }); | ||
messageList.maybeUpdateMessage(updateEvent); | ||
|
||
Message updatedMessage = messageList.messages[0]; | ||
|
||
check(listenersNotified).equals(false); | ||
check(updatedMessage).identicalTo(message); | ||
check(message).content.equals(oldContent); | ||
|
||
}); | ||
test('rendering-only update does not change timestamp', () async { | ||
String oldContent = "<p>Hello, world</p>"; | ||
String newContent = "<p>Hello, world</p> <div>Some link preview</div>"; | ||
int newTimestamp = 99999; | ||
|
||
Message mockMessage = eg.streamMessage(id: 972, stream: stream, content: oldContent); | ||
MessageListView messageList = await messageListViewWithMessages([mockMessage], store, narrow); | ||
|
||
UpdateMessageEvent updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: 972, | ||
messageIds: [972], | ||
flags: mockMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
renderingOnly: true, | ||
userId: null, | ||
); | ||
|
||
Message message = messageList.messages[0]; | ||
messageList.maybeUpdateMessage(updateEvent); | ||
check(message) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.isNull(); | ||
}); | ||
|
||
test('rendering-only update does not change timestamp (for old server versions)', () async { | ||
String oldContent = "<p>Hello, world</p>"; | ||
String newContent = "<p>Hello, world</p> <div>Some link preview</div>"; | ||
int newTimestamp = 99999; | ||
|
||
Message mockMessage = eg.streamMessage(id: 972, stream: stream, content: oldContent); | ||
MessageListView messageList = await messageListViewWithMessages([mockMessage], store, narrow); | ||
|
||
UpdateMessageEvent updateEvent = UpdateMessageEvent( | ||
id: 1, | ||
messageId: 972, | ||
messageIds: [972], | ||
flags: mockMessage.flags, | ||
renderedContent: newContent, | ||
editTimestamp: newTimestamp, | ||
userId: null, | ||
); | ||
|
||
Message message = messageList.messages[0]; | ||
messageList.maybeUpdateMessage(updateEvent); | ||
check(message) | ||
..content.equals(newContent) | ||
..lastEditTimestamp.isNull(); | ||
}); | ||
|
||
|
||
}); | ||
} |