From 2a0c6c6474a130c55ed9d6cbbcde26ef20e42f6b Mon Sep 17 00:00:00 2001 From: Denis Kasak Date: Wed, 1 Jul 2020 14:50:42 +0200 Subject: [PATCH] Add test and example event to ensure display name changes work correctly. --- matrix_sdk_base/src/models/room_member.rs | 44 +++++++++++++++++++++++ matrix_sdk_test/src/lib.rs | 2 ++ matrix_sdk_test/src/test_json/events.rs | 26 ++++++++++++++ matrix_sdk_test/src/test_json/mod.rs | 4 +-- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/matrix_sdk_base/src/models/room_member.rs b/matrix_sdk_base/src/models/room_member.rs index 3800fda7..0db41cb3 100644 --- a/matrix_sdk_base/src/models/room_member.rs +++ b/matrix_sdk_base/src/models/room_member.rs @@ -253,6 +253,50 @@ mod test { assert_eq!(member.power_level, Int::new(100)); } + #[async_test] + async fn room_member_display_name_change() { + let client = get_client().await; + let room_id = test_room_id(); + + let mut builder = EventBuilder::default(); + let mut initial_response = builder + .add_room_event(EventsJson::Member, RoomEvent::RoomMember) + .build_sync_response(); + let mut name_change_response = builder + .add_room_event(EventsJson::MemberNameChange, RoomEvent::RoomMember) + .build_sync_response(); + + client.receive_sync_response(&mut initial_response).await.unwrap(); + + let room = client.get_joined_room(&room_id).await.unwrap(); + + // Initially, the display name is "example". + { + let room = room.read().await; + + let member = room + .joined_members + .get(&UserId::try_from("@example:localhost").unwrap()) + .unwrap(); + + assert_eq!(member.display_name.as_ref().unwrap(), "example"); + } + + client.receive_sync_response(&mut name_change_response).await.unwrap(); + + // Afterwards, the display name is "changed". + { + let room = room.read().await; + + let member = room + .joined_members + .get(&UserId::try_from("@example:localhost").unwrap()) + .unwrap(); + + assert_eq!(member.display_name.as_ref().unwrap(), "changed"); + } + } + #[async_test] async fn member_presence_events() { let client = get_client().await; diff --git a/matrix_sdk_test/src/lib.rs b/matrix_sdk_test/src/lib.rs index c07c1d33..2cfa88c7 100644 --- a/matrix_sdk_test/src/lib.rs +++ b/matrix_sdk_test/src/lib.rs @@ -31,6 +31,7 @@ pub enum EventsJson { HistoryVisibility, JoinRules, Member, + MemberNameChange, MessageEmote, MessageNotice, MessageText, @@ -154,6 +155,7 @@ impl EventBuilder { ) -> &mut Self { let val: &JsonValue = match json { EventsJson::Member => &test_json::MEMBER, + EventsJson::MemberNameChange => &test_json::MEMBER_NAME_CHANGE, EventsJson::PowerLevels => &test_json::POWER_LEVELS, _ => panic!("unknown room event json {:?}", json), }; diff --git a/matrix_sdk_test/src/test_json/events.rs b/matrix_sdk_test/src/test_json/events.rs index c3bcea49..da3bc5a4 100644 --- a/matrix_sdk_test/src/test_json/events.rs +++ b/matrix_sdk_test/src/test_json/events.rs @@ -234,6 +234,32 @@ lazy_static! { }); } +// TODO: Move `prev_content` into `unsigned` once ruma supports it +lazy_static! { + pub static ref MEMBER_NAME_CHANGE: JsonValue = json!({ + "content": { + "avatar_url": null, + "displayname": "changed", + "membership": "join" + }, + "event_id": "$151800234427abgho:localhost", + "membership": "join", + "origin_server_ts": 151800152, + "sender": "@example:localhost", + "state_key": "@example:localhost", + "type": "m.room.member", + "prev_content": { + "avatar_url": null, + "displayname": "example", + "membership": "join" + }, + "unsigned": { + "age": 297032, + "replaces_state": "$151800140517rfvjc:localhost" + } + }); +} + lazy_static! { pub static ref MESSAGE_EDIT: JsonValue = json!({ "content": { diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index 406f2023..d17f13bd 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -9,7 +9,7 @@ pub mod sync; pub use events::{ ALIAS, ALIASES, EVENT_ID, KEYS_QUERY, KEYS_UPLOAD, LOGIN, LOGIN_RESPONSE_ERR, LOGOUT, MEMBER, - MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, PUBLIC_ROOMS, REACTION, - REGISTRATION_RESPONSE_ERR, ROOM_ID, ROOM_MESSAGES, TYPING, + MEMBER_NAME_CHANGE, MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, PUBLIC_ROOMS, + REACTION, REGISTRATION_RESPONSE_ERR, ROOM_ID, ROOM_MESSAGES, TYPING, }; pub use sync::{DEFAULT_SYNC_SUMMARY, INVITE_SYNC, LEAVE_SYNC, LEAVE_SYNC_EVENT, MORE_SYNC, SYNC};