tests: subscribe_to_call_decline_events

This commit is contained in:
Valere
2025-09-03 11:06:48 +02:00
committed by Damir Jelić
parent ab58c376dd
commit e83af1aae2
5 changed files with 105 additions and 6 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ pub enum CallError {
#[error("Couldn't fetch the remote event: {0}")]
Fetch(Box<crate::Error>),
/// We tried to decline an event wich is not of type m.rtc.notification.
/// We tried to decline an event which is not of type m.rtc.notification.
#[error("You cannot decline this event type.")]
BadEventType,
@@ -82,7 +82,8 @@ struct ElementCallParams {
/// Defines if a call is encrypted and which encryption system should be used.
///
/// This controls the url parameters: `perParticipantE2EE`, `password`.
#[derive(Debug, PartialEq, Default, uniffi::Enum, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[derive(Debug, PartialEq, Default, Clone)]
pub enum EncryptionSystem {
/// Equivalent to the element call url parameter: `perParticipantE2EE=false`
/// and no password.
@@ -102,7 +103,8 @@ pub enum EncryptionSystem {
/// Defines the intent of showing the call.
///
/// This controls whether to show or skip the lobby.
#[derive(Debug, PartialEq, Serialize, Default, uniffi::Enum, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[derive(Debug, PartialEq, Serialize, Default, Clone)]
#[serde(rename_all = "snake_case")]
pub enum Intent {
#[default]
@@ -113,7 +115,8 @@ pub enum Intent {
}
/// Defines how (if) element-call renders a header.
#[derive(Debug, PartialEq, Serialize, Default, uniffi::Enum, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[derive(Debug, PartialEq, Serialize, Default, Clone)]
#[serde(rename_all = "snake_case")]
pub enum HeaderStyle {
/// The normal header with branding.
@@ -126,7 +129,8 @@ pub enum HeaderStyle {
}
/// Types of call notifications.
#[derive(Debug, PartialEq, Serialize, uniffi::Enum, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
#[derive(Debug, PartialEq, Serialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum NotificationType {
/// The receiving client should display a visual notification.
@@ -136,7 +140,8 @@ pub enum NotificationType {
}
/// Properties to create a new virtual Element Call widget.
#[derive(Debug, Default, uniffi::Record, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
#[derive(Debug, Default, Clone)]
pub struct VirtualElementCallWidgetOptions {
/// The url to the app.
///
@@ -0,0 +1,84 @@
use std::sync::{Arc, Mutex};
use matrix_sdk::test_utils::mocks::MatrixMockServer;
use matrix_sdk_test::{async_test, event_factory::EventFactory, JoinedRoomBuilder};
use ruma::{owned_event_id, room_id, user_id, OwnedUserId};
use tokio::spawn;
#[async_test]
async fn test_subscribe_to_decline_call_events() {
let server = MatrixMockServer::new().await;
let client = server.client_builder().build().await;
let decliners_sequences: Arc<Mutex<Vec<OwnedUserId>>> = Arc::new(Mutex::new(Vec::new()));
let asserted_decliners = vec![user_id!("@bob:matrix.org"), user_id!("@carl:example.com")];
let room_id = room_id!("!test:example.org");
let notification_event_id = owned_event_id!("$00000:localhost");
// Initial sync with our test room.
// let room = server.sync_joined_room(&client, room_id).await;
let f = EventFactory::new();
server
.sync_room(
&client,
JoinedRoomBuilder::new(room_id).add_timeline_event(
f.call_notify(
"call_id".to_owned(),
ruma::events::call::notify::ApplicationType::Call,
ruma::events::call::notify::NotifyType::Ring,
ruma::events::Mentions::new(),
)
.sender(&user_id!("@alice:matrix.org"))
.event_id(&notification_event_id),
),
)
.await;
let room = server.sync_joined_room(&client, room_id).await;
let to_subscribe_to = notification_event_id.clone();
let join_handle = spawn({
let decliners_sequences = Arc::clone(&decliners_sequences);
async move {
let (_drop_guard, mut subscriber) =
room.subscribe_to_call_decline_events(&to_subscribe_to);
while let Ok(user_id) = subscriber.recv().await {
let mut decliners_sequences = decliners_sequences.lock().unwrap();
decliners_sequences.push(user_id);
// When we have received 2 typing notifications, we can stop listening.
if decliners_sequences.len() == 2 {
break;
}
}
}
});
let other_notification_event_id = owned_event_id!("$11111:localhost");
server
.sync_room(
&client,
JoinedRoomBuilder::new(room_id)
.add_timeline_event(
// declines another call
f.call_decline(&other_notification_event_id)
.sender(&user_id!("@valere:matrix.org")),
)
.add_timeline_event(
// declines another call
f.call_decline(&notification_event_id).sender(&user_id!("@bob:matrix.org")),
)
.add_timeline_event(
// declines another call
f.call_decline(&notification_event_id).sender(&user_id!("@carl:example.com")),
),
)
.await;
join_handle.await.unwrap();
assert_eq!(decliners_sequences.lock().unwrap().to_vec(), asserted_decliners);
}
@@ -1,6 +1,7 @@
mod attachment;
mod beacon;
mod beacon_info;
mod calls;
mod common;
mod joined;
mod left;
@@ -73,6 +73,7 @@ use ruma::{
tombstone::RoomTombstoneEventContent,
topic::RoomTopicEventContent,
},
rtc::decline::RtcDeclineEventContent,
space::{child::SpaceChildEventContent, parent::SpaceParentEventContent},
sticker::StickerEventContent,
typing::TypingEventContent,
@@ -1013,6 +1014,14 @@ impl EventFactory {
self.event(CallNotifyEventContent::new(call_id, application, notify_type, mentions))
}
// Creates a new `org.matrix.msc4310.rtc.decline` event.
pub fn call_decline(
&self,
notification_event_id: &EventId,
) -> EventBuilder<RtcDeclineEventContent> {
self.event(RtcDeclineEventContent::new(notification_event_id))
}
/// Create a new `m.direct` global account data event.
pub fn direct(&self) -> EventBuilder<DirectEventContent> {
let mut builder = self.event(DirectEventContent::default());