diff --git a/src/message.rs b/src/message.rs index 4cfa5c05c..f4c284020 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1136,4 +1136,121 @@ mod tests { let _msg2 = Message::load_from_db(ctx, msg_id).unwrap(); assert_eq!(_msg2.get_filemime(), None); } + + #[test] + pub fn test_get_summarytext_by_raw() { + let d = test::dummy_context(); + let ctx = &d.ctx; + + let some_text = Some("bla bla".to_string()); + let empty_text = Some("".to_string()); + let no_text: Option = None; + + let mut some_file = Params::new(); + some_file.set(Param::File, "foo.bar"); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Text, + some_text.as_ref(), + &mut Params::new(), + 50, + &ctx + ), + "bla bla" // for simple text, the type is not added to the summary + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::Image, no_text.as_ref(), &mut some_file, 50, &ctx,), + "Image" // file names are not added for images + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::Video, no_text.as_ref(), &mut some_file, 50, &ctx,), + "Video" // file names are not added for videos + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::Gif, no_text.as_ref(), &mut some_file, 50, &ctx,), + "GIF" // file names are not added for GIFs + ); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Sticker, + no_text.as_ref(), + &mut some_file, + 50, + &ctx, + ), + "Sticker" // file names are not added for stickers + ); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Voice, + empty_text.as_ref(), + &mut some_file, + 50, + &ctx, + ), + "Voice message" // file names are not added for voice messages, empty text is skipped + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::Voice, no_text.as_ref(), &mut some_file, 50, &ctx), + "Voice message" // file names are not added for voice messages + ); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Voice, + some_text.as_ref(), + &mut some_file, + 50, + &ctx + ), + "Voice message \u{2013} bla bla" // `\u{2013}` explicitly checks for "EN DASH" + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::Audio, no_text.as_ref(), &mut some_file, 50, &ctx), + "Audio \u{2013} foo.bar" // file name is added for audio + ); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Audio, + empty_text.as_ref(), + &mut some_file, + 50, + &ctx, + ), + "Audio \u{2013} foo.bar" // file name is added for audio, empty text is not added + ); + + assert_eq!( + get_summarytext_by_raw( + Viewtype::Audio, + some_text.as_ref(), + &mut some_file, + 50, + &ctx + ), + "Audio \u{2013} foo.bar \u{2013} bla bla" // file name and text added for audio + ); + + assert_eq!( + get_summarytext_by_raw(Viewtype::File, some_text.as_ref(), &mut some_file, 50, &ctx), + "File \u{2013} foo.bar \u{2013} bla bla" // file name is added for files + ); + + let mut asm_file = Params::new(); + asm_file.set(Param::File, "foo.bar"); + asm_file.set_cmd(SystemMessage::AutocryptSetupMessage); + assert_eq!( + get_summarytext_by_raw(Viewtype::File, no_text.as_ref(), &mut asm_file, 50, &ctx), + "Autocrypt Setup Message" // file name is not added for autocrypt setup messages + ); + } }