BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / model.rs

model.rs

model.rs 24.6 KiB Download Raw
1use std::sync::Arc;
2use std::time::{Duration, Instant};
3
4use calling::{
5 CallCommand, CallState, CallUpdate, CameraDevice, DeviceChoice, DeviceLists, EndKind, EndReason, Progress, RosterEntry,
6 ShareKind, ShareSource, SpeakingDetector, VideoHub,
7};
8use tokio::sync::mpsc::UnboundedSender;
9
10use super::pictures::CallPictures;
11
12pub const TEST_CALL_TITLE: &str = "Test call";
13pub const ECHO_NAME: &str = "Teams echo";
14pub const ECHO_MRI: &str = calling::signaling::ECHO_BOT_MRI;
15pub const MAX_TILES: usize = 9;
16const ORGID_PREFIX: &str = "8:orgid:";
17const WIDE_TILES: usize = 2;
18const MEDIUM_TILES: usize = 4;
19
20pub struct ActiveCall {
21 pub id: u64,
22 pub model: CallModel,
23 pub commands: UnboundedSender<CallCommand>,
24 pub viewing: bool,
25 pub conversation_id: Option<String>,
26 pub video: Arc<VideoHub>,
27 pub pictures: CallPictures,
28 pub stage_fullscreen: bool,
29}
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum CallKind {
33 Test,
34 Direct,
35 Group,
36 Meeting,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40pub enum TileState {
41 Invited,
42 Present,
43 InLobby,
44 Left,
45}
46
47#[derive(Debug, Clone, PartialEq, Eq)]
48pub struct Tile {
49 pub mri: String,
50 pub user_id: Option<String>,
51 pub name: String,
52 pub muted: bool,
53 pub state: TileState,
54 pub invited: bool,
55 pub has_video: bool,
56}
57
58impl Tile {
59 pub fn invited(mri: &str, name: &str) -> Tile {
60 Tile {
61 mri: mri.to_owned(),
62 user_id: mri.strip_prefix(ORGID_PREFIX).map(str::to_owned),
63 name: name.to_owned(),
64 muted: false,
65 state: TileState::Invited,
66 invited: true,
67 has_video: false,
68 }
69 }
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub struct GridLayout {
74 pub shown_others: usize,
75 pub hidden: usize,
76}
77
78pub fn grid_layout(others: usize) -> GridLayout {
79 let total = others + 1;
80 if total <= MAX_TILES {
81 return GridLayout {
82 shown_others: others,
83 hidden: 0,
84 };
85 }
86 let shown_others = MAX_TILES - 2;
87 GridLayout {
88 shown_others,
89 hidden: others - shown_others,
90 }
91}
92
93#[derive(Debug, Clone, Copy, PartialEq)]
94pub struct TileSize {
95 pub width: f32,
96 pub height: f32,
97 pub avatar: f32,
98}
99
100pub fn tile_size(cells: usize) -> TileSize {
101 if cells <= WIDE_TILES {
102 TileSize {
103 width: 260.,
104 height: 210.,
105 avatar: 96.,
106 }
107 } else if cells <= MEDIUM_TILES {
108 TileSize {
109 width: 220.,
110 height: 180.,
111 avatar: 72.,
112 }
113 } else {
114 TileSize {
115 width: 190.,
116 height: 150.,
117 avatar: 56.,
118 }
119 }
120}
121
122#[derive(Debug, Clone)]
123pub struct CallModel {
124 pub title: String,
125 pub kind: CallKind,
126 pub peer_name: String,
127 pub state: CallState,
128 pub progress: Option<Progress>,
129 pub muted: bool,
130 pub listen_only: bool,
131 pub lobby: bool,
132 pub can_end_meeting: bool,
133 pub incoming: bool,
134 pub input: DeviceChoice,
135 pub output: DeviceChoice,
136 pub devices: DeviceLists,
137 pub local_speaking: bool,
138 pub remote_speaking: bool,
139 pub speaking_mris: Vec<String>,
140 pub connected_since: Option<Instant>,
141 pub own_mri: Option<String>,
142 pub tiles: Vec<Tile>,
143 pub screen_sharer: Option<String>,
144 pub camera_on: bool,
145 pub cameras: Vec<CameraDevice>,
146 pub camera: DeviceChoice,
147 pub local_share: Option<String>,
148 pub share_sources: Vec<ShareSource>,
149 local_detector: SpeakingDetector,
150 remote_detector: SpeakingDetector,
151}
152
153impl CallModel {
154 fn base(kind: CallKind, title: &str, peer_name: &str, tiles: Vec<Tile>) -> Self {
155 CallModel {
156 title: title.to_owned(),
157 kind,
158 peer_name: peer_name.to_owned(),
159 state: CallState::Connecting,
160 progress: None,
161 muted: false,
162 listen_only: false,
163 lobby: false,
164 can_end_meeting: false,
165 incoming: false,
166 input: DeviceChoice::SystemDefault,
167 output: DeviceChoice::SystemDefault,
168 devices: DeviceLists::default(),
169 local_speaking: false,
170 remote_speaking: false,
171 speaking_mris: Vec::new(),
172 connected_since: None,
173 own_mri: None,
174 tiles,
175 screen_sharer: None,
176 camera_on: false,
177 cameras: Vec::new(),
178 camera: DeviceChoice::SystemDefault,
179 local_share: None,
180 share_sources: Vec::new(),
181 local_detector: SpeakingDetector::default(),
182 remote_detector: SpeakingDetector::default(),
183 }
184 }
185
186 pub fn test() -> Self {
187 Self::base(
188 CallKind::Test,
189 TEST_CALL_TITLE,
190 ECHO_NAME,
191 vec![Tile::invited(ECHO_MRI, ECHO_NAME)],
192 )
193 }
194
195 pub fn people(title: &str, callees: &[(String, String)]) -> Self {
196 let kind = if callees.len() == 1 { CallKind::Direct } else { CallKind::Group };
197 let peer_name = match callees {
198 [(_, name)] => name.clone(),
199 _ => title.to_owned(),
200 };
201 let tiles = callees.iter().map(|(mri, name)| Tile::invited(mri, name)).collect();
202 Self::base(kind, title, &peer_name, tiles)
203 }
204
205 pub fn meeting(title: &str, can_end_meeting: bool) -> Self {
206 let mut model = Self::base(CallKind::Meeting, title, title, Vec::new());
207 model.can_end_meeting = can_end_meeting;
208 model
209 }
210
211 pub fn incoming(title: &str, caller_mri: &str, caller_name: &str) -> Self {
212 let mut model = Self::base(
213 CallKind::Direct,
214 title,
215 caller_name,
216 vec![Tile {
217 state: TileState::Present,
218 ..Tile::invited(caller_mri, caller_name)
219 }],
220 );
221 model.incoming = true;
222 model
223 }
224
225 pub fn apply(&mut self, update: CallUpdate) {
226 match update {
227 CallUpdate::State(state) => {
228 if let Some(since) = state.started_at() {
229 self.connected_since = Some(since);
230 }
231 if !matches!(state, CallState::Connected { .. }) {
232 self.silence();
233 }
234 self.state = state;
235 }
236 CallUpdate::Muted(muted) => {
237 self.muted = muted;
238 if muted {
239 self.local_speaking = false;
240 self.local_detector.reset();
241 }
242 }
243 CallUpdate::Levels { local, remote } => {
244 self.local_speaking = !self.muted && self.local_detector.update(local);
245 self.remote_speaking = self.remote_detector.update(remote);
246 }
247 CallUpdate::Devices(devices) => self.devices = devices,
248 CallUpdate::Selected { input, output } => {
249 self.input = input;
250 self.output = output;
251 }
252 CallUpdate::ListenOnly(listen_only) => self.listen_only = listen_only,
253 CallUpdate::Stats { .. } => {}
254 CallUpdate::Progress(progress) => self.progress = Some(progress),
255 CallUpdate::Roster(entries) => self.apply_roster(&entries),
256 CallUpdate::Speakers(mris) => self.speaking_mris = mris,
257 CallUpdate::Lobby(lobby) => self.lobby = lobby,
258 CallUpdate::OwnIdentity { mri } => self.own_mri = Some(mri),
259 CallUpdate::ScreenShare(sharer) => self.screen_sharer = sharer,
260 CallUpdate::VideoReady | CallUpdate::Notice(_) => {}
261 CallUpdate::Camera(on) => self.camera_on = on,
262 CallUpdate::Cameras(cameras) => self.cameras = cameras,
263 CallUpdate::LocalShare(label) => self.local_share = label,
264 CallUpdate::ShareSources(sources) => self.share_sources = sources,
265 }
266 }
267
268 fn silence(&mut self) {
269 self.local_speaking = false;
270 self.remote_speaking = false;
271 self.speaking_mris.clear();
272 self.local_detector.reset();
273 self.remote_detector.reset();
274 }
275
276 fn apply_roster(&mut self, entries: &[RosterEntry]) {
277 for entry in entries.iter().filter(|entry| Some(&entry.mri) != self.own_mri.as_ref()) {
278 let state = if entry.in_lobby { TileState::InLobby } else { TileState::Present };
279 match self.tiles.iter_mut().find(|tile| tile.mri == entry.mri) {
280 Some(tile) => {
281 tile.state = state;
282 tile.muted = entry.muted;
283 tile.has_video = entry.has_video;
284 if tile.name.is_empty() {
285 tile.name = entry.display_name.clone();
286 }
287 }
288 None => self.tiles.push(Tile {
289 mri: entry.mri.clone(),
290 user_id: entry.mri.strip_prefix(ORGID_PREFIX).map(str::to_owned),
291 name: entry.display_name.clone(),
292 muted: entry.muted,
293 state,
294 invited: false,
295 has_video: entry.has_video,
296 }),
297 }
298 }
299 let own_mri = self.own_mri.clone();
300 let listed = |mri: &str| entries.iter().any(|entry| entry.mri == mri);
301 self.tiles.retain_mut(|tile| {
302 if listed(&tile.mri) || Some(&tile.mri) == own_mri.as_ref() {
303 return true;
304 }
305 match (tile.invited, tile.state) {
306 (true, TileState::Invited) => true,
307 (true, _) => {
308 tile.state = TileState::Left;
309 true
310 }
311 (false, _) => false,
312 }
313 });
314 }
315
316 pub fn visible_tiles(&self) -> Vec<&Tile> {
317 let mut tiles: Vec<&Tile> = self.tiles.iter().filter(|tile| tile.state != TileState::InLobby).collect();
318 tiles.sort_by_key(|tile| match tile.state {
319 TileState::Present => 0,
320 TileState::Invited => 1,
321 _ => 2,
322 });
323 tiles
324 }
325
326 pub fn strip_tiles(&self) -> Vec<&Tile> {
327 let mut tiles = self.visible_tiles();
328 tiles.sort_by_key(|tile| !self.tile_speaking(tile));
329 tiles
330 }
331
332 pub fn sharing_label(&self) -> Option<String> {
333 let sharer = self.screen_sharer.as_ref()?;
334 let name = self
335 .tiles
336 .iter()
337 .find(|tile| &tile.mri == sharer)
338 .map(|tile| tile.name.as_str())
339 .filter(|name| !name.is_empty())
340 .unwrap_or("Someone");
341 Some(format!("{name} is sharing"))
342 }
343
344 pub fn tile_speaking(&self, tile: &Tile) -> bool {
345 if self.speaking_mris.contains(&tile.mri) {
346 return true;
347 }
348 let only_remote = self.tiles.len() == 1;
349 only_remote && tile.state != TileState::Left && self.remote_speaking
350 }
351
352 pub fn tile_caption(&self, tile: &Tile) -> Option<&'static str> {
353 match tile.state {
354 TileState::Invited => Some(match self.progress {
355 Some(Progress::Ringing) => "Ringing",
356 _ => "Calling...",
357 }),
358 TileState::Left => Some("Left"),
359 TileState::Present | TileState::InLobby => None,
360 }
361 }
362
363 pub fn elapsed(&self, now: Instant) -> Option<Duration> {
364 self.connected_since
365 .map(|since| now.saturating_duration_since(since))
366 }
367
368 pub fn connecting_text(&self) -> &'static str {
369 match (self.kind, self.progress) {
370 (CallKind::Meeting, _) => "Joining...",
371 (_, _) if self.incoming => "Connecting...",
372 (_, Some(Progress::Ringing)) => "Ringing",
373 _ => "Calling...",
374 }
375 }
376
377 pub fn is_active(&self) -> bool {
378 matches!(
379 self.state,
380 CallState::Connecting | CallState::Connected { .. } | CallState::Reconnecting { .. }
381 )
382 }
383
384 pub fn is_connecting(&self) -> bool {
385 matches!(self.state, CallState::Connecting)
386 }
387
388 pub fn is_reconnecting(&self) -> bool {
389 matches!(self.state, CallState::Reconnecting { .. })
390 }
391
392 pub fn ended_reason(&self) -> Option<&EndReason> {
393 match &self.state {
394 CallState::Ended { reason } => Some(reason),
395 _ => None,
396 }
397 }
398
399 pub fn timer_text(&self, now: Instant) -> String {
400 self.elapsed(now).map(format_elapsed).unwrap_or_default()
401 }
402
403 pub fn can_use_camera(&self) -> bool {
404 !self.cameras.is_empty()
405 }
406
407 pub fn can_share(&self) -> bool {
408 !self.share_sources.is_empty() && self.is_live()
409 }
410
411 fn is_live(&self) -> bool {
412 matches!(self.state, CallState::Connected { .. } | CallState::Reconnecting { .. })
413 }
414
415 pub fn screens(&self) -> Vec<&ShareSource> {
416 self.share_sources.iter().filter(|source| source.kind == ShareKind::Screen).collect()
417 }
418
419 pub fn windows(&self) -> Vec<&ShareSource> {
420 self.share_sources.iter().filter(|source| source.kind == ShareKind::Window).collect()
421 }
422
423 pub fn can_unmute(&self) -> bool {
424 !self.listen_only
425 }
426}
427
428pub fn format_elapsed(elapsed: Duration) -> String {
429 let seconds = elapsed.as_secs();
430 let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60);
431 if hours > 0 {
432 format!("{hours}:{minutes:02}:{seconds:02}")
433 } else {
434 format!("{minutes:02}:{seconds:02}")
435 }
436}
437
438pub fn ended_notice(reason: &EndReason, elapsed: Option<Duration>, peer_name: &str) -> String {
439 let ended = || match elapsed {
440 Some(elapsed) => format!("Call ended {}", format_elapsed(elapsed)),
441 None => "Call ended".to_owned(),
442 };
443 match reason {
444 EndReason::Cancelled => "Call cancelled".to_owned(),
445 EndReason::Dropped => "Call dropped".to_owned(),
446 EndReason::Failed(message) => format!("Call failed: {message}"),
447 EndReason::LocalHangup => ended(),
448 EndReason::Remote(EndKind::Declined) => format!("{peer_name} declined"),
449 EndReason::Remote(EndKind::NoAnswer) => "No answer".to_owned(),
450 EndReason::Remote(EndKind::Unavailable) => format!("{peer_name} is not available"),
451 EndReason::Remote(_) => ended(),
452 }
453}
454
455#[cfg(test)]
456mod tests {
457 use super::*;
458
459 fn connected_model() -> CallModel {
460 let mut model = CallModel::test();
461 model.apply(CallUpdate::State(CallState::Connected {
462 since: Instant::now(),
463 }));
464 model
465 }
466
467 fn entry(mri: &str, name: &str, muted: bool, in_lobby: bool) -> RosterEntry {
468 RosterEntry {
469 mri: mri.into(),
470 display_name: name.into(),
471 muted,
472 in_lobby,
473 has_video: false,
474 sharing: false,
475 }
476 }
477
478 fn meeting_with_me() -> CallModel {
479 let mut model = CallModel::meeting("Standup", false);
480 model.apply(CallUpdate::OwnIdentity { mri: "8:orgid:me".into() });
481 model
482 }
483
484 #[test]
485 fn elapsed_text_is_minutes_and_seconds() {
486 assert_eq!(format_elapsed(Duration::from_secs(42)), "00:42");
487 assert_eq!(format_elapsed(Duration::from_secs(61)), "01:01");
488 assert_eq!(format_elapsed(Duration::from_secs(3725)), "1:02:05");
489 }
490
491 #[test]
492 fn levels_drive_the_speaking_rings() {
493 let mut model = connected_model();
494 model.apply(CallUpdate::Levels {
495 local: 0.0,
496 remote: 0.4,
497 });
498 assert!(model.remote_speaking);
499 assert!(!model.local_speaking);
500 model.apply(CallUpdate::Levels {
501 local: 0.4,
502 remote: 0.0,
503 });
504 assert!(model.local_speaking);
505 }
506
507 #[test]
508 fn muting_switches_the_local_ring_off_at_once() {
509 let mut model = connected_model();
510 model.apply(CallUpdate::Levels {
511 local: 0.5,
512 remote: 0.0,
513 });
514 assert!(model.local_speaking);
515 model.apply(CallUpdate::Muted(true));
516 assert!(!model.local_speaking);
517 model.apply(CallUpdate::Levels {
518 local: 0.5,
519 remote: 0.0,
520 });
521 assert!(!model.local_speaking);
522 }
523
524 #[test]
525 fn reconnecting_keeps_the_timer_and_clears_the_rings() {
526 let mut model = connected_model();
527 let since = model.connected_since.unwrap();
528 model.apply(CallUpdate::Levels {
529 local: 0.5,
530 remote: 0.5,
531 });
532 model.apply(CallUpdate::State(CallState::Reconnecting { since }));
533 assert!(model.is_reconnecting());
534 assert_eq!(model.connected_since, Some(since));
535 assert!(!model.remote_speaking && !model.local_speaking);
536 }
537
538 #[test]
539 fn notices_for_every_end_reason() {
540 let elapsed = Some(Duration::from_secs(42));
541 let notice = |reason: EndReason, elapsed| ended_notice(&reason, elapsed, "Bea");
542 assert_eq!(notice(EndReason::LocalHangup, elapsed), "Call ended 00:42");
543 assert_eq!(notice(EndReason::Remote(EndKind::Normal), elapsed), "Call ended 00:42");
544 assert_eq!(notice(EndReason::Remote(EndKind::Normal), None), "Call ended");
545 assert_eq!(notice(EndReason::Cancelled, None), "Call cancelled");
546 assert_eq!(notice(EndReason::Dropped, elapsed), "Call dropped");
547 assert_eq!(notice(EndReason::Failed("no route".into()), None), "Call failed: no route");
548 assert_eq!(notice(EndReason::Remote(EndKind::Declined), None), "Bea declined");
549 assert_eq!(notice(EndReason::Remote(EndKind::NoAnswer), None), "No answer");
550 assert_eq!(notice(EndReason::Remote(EndKind::Unavailable), None), "Bea is not available");
551 assert_eq!(notice(EndReason::Remote(EndKind::Cancelled), None), "Call ended");
552 assert_eq!(notice(EndReason::Remote(EndKind::Other), None), "Call ended");
553 assert_eq!(notice(EndReason::Remote(EndKind::AnsweredElsewhere), None), "Call ended");
554 }
555
556 #[test]
557 fn the_connecting_text_follows_the_kind_of_call() {
558 let mut direct = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]);
559 assert_eq!(direct.connecting_text(), "Calling...");
560 direct.apply(CallUpdate::Progress(Progress::Ringing));
561 assert_eq!(direct.connecting_text(), "Ringing");
562 assert_eq!(CallModel::meeting("Standup", false).connecting_text(), "Joining...");
563 assert_eq!(CallModel::incoming("Bea", "8:orgid:b", "Bea").connecting_text(), "Connecting...");
564 assert_eq!(CallModel::test().connecting_text(), "Calling...");
565 }
566
567 #[test]
568 fn listen_only_blocks_unmuting() {
569 let mut model = connected_model();
570 assert!(model.can_unmute());
571 model.apply(CallUpdate::ListenOnly(true));
572 assert!(!model.can_unmute());
573 }
574
575 #[test]
576 fn callees_start_invited_and_follow_the_progress() {
577 let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]);
578 assert_eq!(model.kind, CallKind::Direct);
579 assert_eq!(model.peer_name, "Bea");
580 assert_eq!(model.tile_caption(&model.tiles[0]), Some("Calling..."));
581 model.apply(CallUpdate::Progress(Progress::Ringing));
582 assert_eq!(model.tile_caption(&model.tiles[0]), Some("Ringing"));
583 model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)]));
584 assert_eq!(model.tiles[0].state, TileState::Present);
585 assert_eq!(model.tile_caption(&model.tiles[0]), None);
586 }
587
588 #[test]
589 fn several_callees_make_a_group_call_named_after_the_chat() {
590 let model = CallModel::people(
591 "Retro team",
592 &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())],
593 );
594 assert_eq!(model.kind, CallKind::Group);
595 assert_eq!(model.peer_name, "Retro team");
596 assert_eq!(model.tiles.len(), 2);
597 }
598
599 #[test]
600 fn roster_adds_guests_updates_mute_and_drops_leavers() {
601 let mut model = meeting_with_me();
602 model.apply(CallUpdate::Roster(vec![
603 entry("8:orgid:me", "Me", false, false),
604 entry("8:orgid:a", "Ana", true, false),
605 entry("8:orgid:b", "Bo", false, false),
606 ]));
607 assert_eq!(model.tiles.len(), 2);
608 assert!(model.tiles.iter().find(|tile| tile.name == "Ana").unwrap().muted);
609 model.apply(CallUpdate::Roster(vec![entry("8:orgid:me", "Me", false, false), entry("8:orgid:b", "Bo", false, false)]));
610 assert_eq!(model.tiles.len(), 1);
611 assert_eq!(model.tiles[0].name, "Bo");
612 }
613
614 #[test]
615 fn an_invited_callee_who_leaves_stays_as_left() {
616 let mut model = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]);
617 model.apply(CallUpdate::Roster(vec![entry("8:orgid:b", "Bea", false, false)]));
618 model.apply(CallUpdate::Roster(Vec::new()));
619 assert_eq!(model.tiles[0].state, TileState::Left);
620 assert_eq!(model.tile_caption(&model.tiles[0]), Some("Left"));
621 }
622
623 #[test]
624 fn lobby_guests_get_no_tile() {
625 let mut model = meeting_with_me();
626 model.apply(CallUpdate::Roster(vec![
627 entry("8:orgid:a", "Ana", false, false),
628 entry("8:orgid:g", "Guest", false, true),
629 ]));
630 let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect();
631 assert_eq!(names, vec!["Ana"]);
632 }
633
634 #[test]
635 fn the_lobby_flag_follows_the_call() {
636 let mut model = meeting_with_me();
637 model.apply(CallUpdate::Lobby(true));
638 assert!(model.lobby);
639 model.apply(CallUpdate::Lobby(false));
640 assert!(!model.lobby);
641 }
642
643 #[test]
644 fn speakers_light_their_own_tile_and_a_lone_remote_follows_the_level() {
645 let mut model = meeting_with_me();
646 model.apply(CallUpdate::Roster(vec![
647 entry("8:orgid:a", "Ana", false, false),
648 entry("8:orgid:b", "Bo", false, false),
649 ]));
650 model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() }));
651 model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()]));
652 let tiles: Vec<Tile> = model.tiles.clone();
653 assert!(!model.tile_speaking(&tiles[0]));
654 assert!(model.tile_speaking(&tiles[1]));
655 let mut lone = CallModel::people("Bea", &[("8:orgid:b".into(), "Bea".into())]);
656 lone.apply(CallUpdate::State(CallState::Connected { since: Instant::now() }));
657 lone.apply(CallUpdate::Levels { local: 0.0, remote: 0.5 });
658 assert!(lone.tile_speaking(&lone.tiles[0].clone()));
659 }
660
661 #[test]
662 fn more_than_nine_cells_collapse_into_a_plus_n_tile() {
663 assert_eq!(grid_layout(0), GridLayout { shown_others: 0, hidden: 0 });
664 assert_eq!(grid_layout(8), GridLayout { shown_others: 8, hidden: 0 });
665 assert_eq!(grid_layout(9), GridLayout { shown_others: 7, hidden: 2 });
666 assert_eq!(grid_layout(30), GridLayout { shown_others: 7, hidden: 23 });
667 }
668
669 #[test]
670 fn crowded_calls_get_smaller_tiles() {
671 assert_eq!(tile_size(2).avatar, 96.);
672 assert_eq!(tile_size(4).avatar, 72.);
673 assert_eq!(tile_size(9).avatar, 56.);
674 }
675
676 #[test]
677 fn the_stage_label_names_the_sharer_and_the_strip_puts_the_speaker_first() {
678 let mut model = meeting_with_me();
679 model.apply(CallUpdate::Roster(vec![
680 entry("8:orgid:a", "Ana", false, false),
681 entry("8:orgid:b", "Bo", false, false),
682 ]));
683 assert_eq!(model.sharing_label(), None);
684 model.apply(CallUpdate::ScreenShare(Some("8:orgid:b".into())));
685 assert_eq!(model.sharing_label().as_deref(), Some("Bo is sharing"));
686 model.apply(CallUpdate::State(CallState::Connected { since: Instant::now() }));
687 model.apply(CallUpdate::Speakers(vec!["8:orgid:b".into()]));
688 let names: Vec<&str> = model.strip_tiles().iter().map(|tile| tile.name.as_str()).collect();
689 assert_eq!(names, vec!["Bo", "Ana"]);
690 model.apply(CallUpdate::ScreenShare(None));
691 assert_eq!(model.sharing_label(), None);
692 }
693
694 #[test]
695 fn roster_entries_carry_the_camera_flag_to_the_tile() {
696 let mut model = meeting_with_me();
697 let mut with_camera = entry("8:orgid:a", "Ana", false, false);
698 with_camera.has_video = true;
699 model.apply(CallUpdate::Roster(vec![with_camera]));
700 assert!(model.tiles[0].has_video);
701 model.apply(CallUpdate::Roster(vec![entry("8:orgid:a", "Ana", false, false)]));
702 assert!(!model.tiles[0].has_video);
703 }
704
705 #[test]
706 fn present_guests_sort_before_waiting_ones() {
707 let mut model = CallModel::people(
708 "Group",
709 &[("8:orgid:b".into(), "Bea".into()), ("8:orgid:c".into(), "Cy".into())],
710 );
711 model.apply(CallUpdate::Roster(vec![entry("8:orgid:c", "Cy", false, false)]));
712 let names: Vec<&str> = model.visible_tiles().iter().map(|tile| tile.name.as_str()).collect();
713 assert_eq!(names, vec!["Cy", "Bea"]);
714 }
715}