Skip to content

Commit

Permalink
test: reuse the internal EventFactory of the TestTimeline in more…
Browse files Browse the repository at this point in the history
… places

Also rename a few `factory` to `f`, for consistency with the rest of the
testing code.
  • Loading branch information
bnjbvr committed Aug 20, 2024
1 parent b00f58a commit bbefad3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 52 deletions.
31 changes: 15 additions & 16 deletions crates/matrix-sdk-ui/src/timeline/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_test::{async_test, sync_timeline_event, ALICE, BOB, CAROL};
use ruma::{
events::{
Expand Down Expand Up @@ -45,7 +44,7 @@ async fn test_initial_events() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline
.inner
.add_events_at(
Expand Down Expand Up @@ -87,8 +86,8 @@ async fn test_replace_with_initial_events_and_read_marker() {
)
.with_settings(TimelineInnerSettings { track_read_receipts: true, ..Default::default() });

let factory = EventFactory::new();
let ev = factory.text_msg("hey").sender(*ALICE).into_sync();
let f = &timeline.factory;
let ev = f.text_msg("hey").sender(*ALICE).into_sync();

timeline.inner.add_events_at(vec![ev], TimelineEnd::Back, RemoteEventOrigin::Sync).await;

Expand All @@ -97,7 +96,7 @@ async fn test_replace_with_initial_events_and_read_marker() {
assert!(items[0].is_day_divider());
assert_eq!(items[1].as_event().unwrap().content().as_message().unwrap().body(), "hey");

let ev = factory.text_msg("yo").sender(*BOB).into_sync();
let ev = f.text_msg("yo").sender(*BOB).into_sync();
timeline.inner.replace_with_initial_remote_events(vec![ev], RemoteEventOrigin::Sync).await;

let items = timeline.inner.items().await;
Expand Down Expand Up @@ -298,10 +297,10 @@ async fn test_dedup_pagination() {
async fn test_dedup_initial() {
let timeline = TestTimeline::new();

let factory = EventFactory::new();
let event_a = factory.text_msg("A").sender(*ALICE).into_sync();
let event_b = factory.text_msg("B").sender(*BOB).into_sync();
let event_c = factory.text_msg("C").sender(*CAROL).into_sync();
let f = &timeline.factory;
let event_a = f.text_msg("A").sender(*ALICE).into_sync();
let event_b = f.text_msg("B").sender(*BOB).into_sync();
let event_c = f.text_msg("C").sender(*CAROL).into_sync();

timeline
.inner
Expand Down Expand Up @@ -346,10 +345,10 @@ async fn test_dedup_initial() {
async fn test_internal_id_prefix() {
let timeline = TestTimeline::with_internal_id_prefix("le_prefix_".to_owned());

let factory = EventFactory::new();
let ev_a = factory.text_msg("A").sender(*ALICE).into_sync();
let ev_b = factory.text_msg("B").sender(*BOB).into_sync();
let ev_c = factory.text_msg("C").sender(*CAROL).into_sync();
let f = &timeline.factory;
let ev_a = f.text_msg("A").sender(*ALICE).into_sync();
let ev_b = f.text_msg("B").sender(*BOB).into_sync();
let ev_c = f.text_msg("C").sender(*CAROL).into_sync();

timeline
.inner
Expand Down Expand Up @@ -380,7 +379,7 @@ async fn test_sanitized() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;

timeline
.handle_live_event(
Expand Down Expand Up @@ -424,7 +423,7 @@ async fn test_reply() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("I want you to reply").sender(&ALICE)).await;

let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand Down Expand Up @@ -476,7 +475,7 @@ async fn test_thread() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("I want you to reply").sender(&ALICE)).await;

let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand Down
8 changes: 4 additions & 4 deletions crates/matrix-sdk-ui/src/timeline/tests/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async fn test_remote_echo_full_trip() {
async fn test_remote_echo_new_position() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;
let f = EventFactory::new();
let f = &timeline.factory;

// Given a local event…
let txn_id = timeline
Expand Down Expand Up @@ -234,7 +234,7 @@ async fn test_day_divider_duplication() {
async fn test_day_divider_removed_after_local_echo_disappeared() {
let timeline = TestTimeline::new();

let f = EventFactory::new();
let f = &timeline.factory;

timeline
.handle_live_event(
Expand Down Expand Up @@ -282,15 +282,15 @@ async fn test_day_divider_removed_after_local_echo_disappeared() {

#[async_test]
async fn test_read_marker_removed_after_local_echo_disappeared() {
let f = EventFactory::new();

let event_id = event_id!("$1");

let timeline = TestTimeline::with_room_data_provider(
TestRoomDataProvider::default().with_fully_read_marker(event_id.to_owned()),
)
.with_settings(TimelineInnerSettings { track_read_receipts: true, ..Default::default() });

let f = &timeline.factory;

// Use `replace_with_initial_remote_events` which initializes the read marker;
// other methods don't, by default.
timeline
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-ui/src/timeline/tests/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_test::{async_test, sync_timeline_event, ALICE};
use ruma::{
events::room::message::{MessageType, RedactedRoomMessageEventContent},
Expand All @@ -30,7 +29,8 @@ async fn test_live_redacted() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;

timeline
.handle_live_redacted_message_event(*ALICE, RedactedRoomMessageEventContent::new())
.await;
Expand All @@ -57,7 +57,7 @@ async fn test_live_sanitized() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline
.handle_live_event(
f.text_html("**original** message", "<strong>original</strong> message").sender(&ALICE),
Expand Down
10 changes: 5 additions & 5 deletions crates/matrix-sdk-ui/src/timeline/tests/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::{
crypto::{decrypt_room_key_export, types::events::UtdCause, OlmMachine},
test_utils::{events::EventFactory, test_client_builder},
test_utils::test_client_builder,
};
use matrix_sdk_test::{async_test, BOB};
use ruma::{
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn test_retry_message_decryption() {
let timeline = TestTimeline::with_unable_to_decrypt_hook(utd_hook.clone());
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline
.handle_live_event(
f.event(RoomEncryptedEventContent::new(
Expand Down Expand Up @@ -200,7 +200,7 @@ async fn test_retry_edit_decryption() {
-----END MEGOLM SESSION DATA-----";

let timeline = TestTimeline::new();
let f = EventFactory::new();
let f = &timeline.factory;

let encrypted = EncryptedEventScheme::MegolmV1AesSha2(
MegolmV1AesSha2ContentInit {
Expand Down Expand Up @@ -313,7 +313,7 @@ async fn test_retry_edit_and_more() {
}

let timeline = TestTimeline::new();
let f = EventFactory::new();
let f = &timeline.factory;

timeline
.handle_live_event(
Expand Down Expand Up @@ -401,7 +401,7 @@ async fn test_retry_message_decryption_highlighted() {
-----END MEGOLM SESSION DATA-----";

let timeline = TestTimeline::new();
let f = EventFactory::new();
let f = &timeline.factory;
let mut stream = timeline.subscribe().await;

timeline
Expand Down
11 changes: 5 additions & 6 deletions crates/matrix-sdk-ui/src/timeline/tests/event_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use std::sync::Arc;
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_test::{async_test, sync_timeline_event, ALICE, BOB};
use ruma::events::{
room::{
Expand All @@ -41,7 +40,7 @@ async fn test_default_filter() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;

// Test edits work.
timeline.handle_live_event(f.text_msg("The first message").sender(&ALICE)).await;
Expand Down Expand Up @@ -99,7 +98,7 @@ async fn test_filter_always_false() {
..Default::default()
});

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("The first message").sender(&ALICE)).await;

timeline
Expand Down Expand Up @@ -131,7 +130,7 @@ async fn test_custom_filter() {
});
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("The first message").sender(&ALICE)).await;
let _item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
let _day_divider = assert_next_matches!(stream, VectorDiff::PushFront { value } => value);
Expand Down Expand Up @@ -199,7 +198,7 @@ async fn test_event_type_filter_include_only_room_names() {
event_filter: Arc::new(move |event, _| event_filter.filter(event)),
..Default::default()
});
let f = EventFactory::new();
let f = &timeline.factory;

// Add a non-encrypted message event
timeline.handle_live_event(f.text_msg("The first message").sender(&ALICE)).await;
Expand Down Expand Up @@ -247,7 +246,7 @@ async fn test_event_type_filter_exclude_messages() {
event_filter: Arc::new(move |event, _| event_filter.filter(event)),
..Default::default()
});
let f = EventFactory::new();
let f = &timeline.factory;

// Add a message event
timeline.handle_live_event(f.text_msg("The first message").sender(&ALICE)).await;
Expand Down
3 changes: 1 addition & 2 deletions crates/matrix-sdk-ui/src/timeline/tests/invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_test::{async_test, sync_timeline_event, ALICE, BOB};
use ruma::{
events::{room::message::MessageType, MessageLikeEventType, StateEventType},
Expand All @@ -30,7 +29,7 @@ async fn invalid_edit() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe_events().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("test").sender(&ALICE)).await;
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
let msg = item.content().as_message().unwrap();
Expand Down
1 change: 0 additions & 1 deletion crates/matrix-sdk-ui/src/timeline/tests/polls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ impl TestTimeline {
}

async fn send_poll_start(&self, sender: &UserId, content: UnstablePollStartContentBlock) {
// TODO: consider putting it in the `TestTimeline` itself?
let event_content = AnyMessageLikeEventContent::UnstablePollStart(
NewUnstablePollStartEventContent::new(content).into(),
);
Expand Down
6 changes: 3 additions & 3 deletions crates/matrix-sdk-ui/src/timeline/tests/reactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ async fn test_add_reaction_success() {

#[async_test]
async fn test_redact_reaction_success() {
let f = EventFactory::new();

let timeline = TestTimeline::new();
let f = &timeline.factory;

let mut stream = timeline.subscribe().await;
let (msg_id, msg_pos) = send_first_message(&timeline, &mut stream).await;
let reaction = create_reaction(&msg_id);
Expand Down Expand Up @@ -127,7 +127,7 @@ async fn test_redact_reaction_failure() {
let mut stream = timeline.subscribe().await;
let (msg_id, msg_pos) = send_first_message(&timeline, &mut stream).await;

let f = EventFactory::new();
let f = &timeline.factory;

let event_id = event_id!("$1");
timeline
Expand Down
11 changes: 5 additions & 6 deletions crates/matrix-sdk-ui/src/timeline/tests/redaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use assert_matches::assert_matches;
use assert_matches2::assert_let;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_base::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_test::{async_test, ALICE, BOB};
use ruma::events::{
Expand All @@ -39,7 +38,7 @@ async fn test_redact_state_event() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe_events().await;

let f = EventFactory::new();
let f = &timeline.factory;

timeline
.handle_live_state_event(
Expand Down Expand Up @@ -71,7 +70,7 @@ async fn test_redact_replied_to_event() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe_events().await;

let f = EventFactory::new();
let f = &timeline.factory;

timeline.handle_live_event(f.text_msg("Hello, world!").sender(&ALICE)).await;

Expand Down Expand Up @@ -110,7 +109,7 @@ async fn test_reaction_redaction() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe_events().await;

let f = EventFactory::new();
let f = &timeline.factory;

timeline.handle_live_event(f.text_msg("hi!").sender(&ALICE)).await;
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand All @@ -136,7 +135,7 @@ async fn test_reaction_redaction_timeline_filter() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe_events().await;

let f = EventFactory::new();
let f = &timeline.factory;

// Initialise a timeline with a redacted reaction.
timeline
Expand Down Expand Up @@ -183,7 +182,7 @@ async fn test_reaction_redaction_timeline_filter() {
async fn test_receive_unredacted() {
let timeline = TestTimeline::new();

let f = EventFactory::new();
let f = &timeline.factory;

// send two events, second one redacted
timeline.handle_live_event(f.text_msg("about to be redacted").sender(&ALICE)).await;
Expand Down
5 changes: 2 additions & 3 deletions crates/matrix-sdk-ui/src/timeline/tests/shields.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use assert_matches::assert_matches;
use eyeball_im::VectorDiff;
use matrix_sdk::test_utils::events::EventFactory;
use matrix_sdk_base::deserialized_responses::{ShieldState, ShieldStateCode};
use matrix_sdk_test::{async_test, sync_timeline_event, ALICE};
use ruma::{
Expand All @@ -15,7 +14,7 @@ use crate::timeline::{tests::TestTimeline, EventSendState};
async fn test_no_shield_in_unencrypted_room() {
let timeline = TestTimeline::new();
let mut stream = timeline.subscribe().await;
let f = EventFactory::new();
let f = &timeline.factory;

timeline.handle_live_event(f.text_msg("Unencrypted message.").sender(&ALICE)).await;

Expand All @@ -29,7 +28,7 @@ async fn test_sent_in_clear_shield() {
let timeline = TestTimeline::with_is_room_encrypted(true);
let mut stream = timeline.subscribe().await;

let f = EventFactory::new();
let f = &timeline.factory;
timeline.handle_live_event(f.text_msg("Unencrypted message.").sender(&ALICE)).await;

let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand Down
Loading

0 comments on commit bbefad3

Please sign in to comment.