leejunyoung5819wayland-android-remote

공개

Ubuntu(Wayland/X11)에서 Android 기기 화면 미러링 및 원격 제어 (Rust)

639줄18 KB
1//! 마우스 조작 시뮬레이션: 아이콘 클릭과 빈 공간 드래그(창 이동)가 서로 가로채지 않는지,
2//! 휴대폰 화면 위 입력이 올바른 제어 메시지로 바뀌는지 확인한다.
3
4use std::sync::Arc;
5
6use egui::{Event, Key, Modifiers, Pos2, Rect, pos2, vec2};
7use egui_kittest::Harness;
8use wayland_android_remote::protocol::{
9 AuthGrant, KeyAction, Message, ScrollEvent, SystemAction, TextInput, TouchAction, TouchEvent,
10 VideoCodec, VideoConfig,
11};
12
13use super::conn::ConnEvent;
14use super::layout;
15use super::video_render::YuvFrame;
16use super::view::{Actions, View};
17
18#[derive(Default)]
19struct Recorded {
20 screen: Option<Rect>,
21 drag: bool,
22 open_settings: bool,
23 system: Option<SystemAction>,
24 input: Vec<Message>,
25 resize: Option<layout::ResizeDir>,
26}
27
28struct State {
29 view: View,
30 rec: Recorded,
31}
32
33/// 영상 프레임 크기. 기본 화면(360x800)과 종횡비가 같아 영상이 휴대폰 화면 영역을 꽉 채운다.
34const FRAME_W: u16 = 864;
35const FRAME_H: u16 = 1920;
36
37fn harness(connected: bool) -> Harness<'static, State> {
38 let mut view = View::for_test();
39 if connected {
40 view.start_connecting_for_test("10.0.0.2:27200".parse().unwrap());
41 view.on_conn_event(ConnEvent::Connected {
42 grant: AuthGrant::Always,
43 });
44 }
45 build(view)
46}
47
48/// 연결되고 영상 설정과 프레임까지 받은 상태. 프레임 내용은 그리지 않으므로 비워 둔다.
49fn harness_with_video() -> Harness<'static, State> {
50 let mut view = View::for_test();
51 view.start_connecting_for_test("10.0.0.2:27200".parse().unwrap());
52 view.on_conn_event(ConnEvent::Connected {
53 grant: AuthGrant::Always,
54 });
55 view.on_conn_event(ConnEvent::VideoConfig(VideoConfig {
56 codec: VideoCodec::H264,
57 config_gen: 1,
58 width: FRAME_W,
59 height: FRAME_H,
60 fps: 60,
61 bitrate: 8_000_000,
62 display_width: 1080,
63 display_height: 2400,
64 }));
65 view.set_video_frame(Arc::new(YuvFrame {
66 width: u32::from(FRAME_W),
67 height: u32::from(FRAME_H),
68 nv12: true,
69 planes: Vec::new(),
70 bt709: true,
71 full_range: false,
72 }));
73 build(view)
74}
75
76fn build(view: View) -> Harness<'static, State> {
77 let size = view.window_size();
78 let state = State {
79 view,
80 rec: Recorded::default(),
81 };
82 let mut h = Harness::builder()
83 .with_size(size)
84 .with_accessibility_check(false)
85 .with_context_setup(super::fonts::install_korean_font_owned)
86 .build_ui_state(
87 |ui, s: &mut State| {
88 s.rec.screen = Some(ui.max_rect());
89 let mut a = Actions::default();
90 s.view.ui(ui, &mut a);
91 s.rec.drag |= a.start_drag;
92 s.rec.open_settings |= a.open_settings;
93 s.rec.system = s.rec.system.or(a.system_action);
94 s.rec.input.extend(a.input);
95 s.rec.resize = s.rec.resize.or(a.start_resize);
96 },
97 state,
98 );
99 h.run();
100 h
101}
102
103fn layout_of(h: &Harness<'_, State>) -> layout::Layout {
104 layout::compute(h.state().rec.screen.unwrap())
105}
106
107fn click(h: &mut Harness<'_, State>, p: Pos2) {
108 // 누르고 바로 떼는 짧은 클릭. run()은 여러 프레임을 돌려 길게 누른 것처럼 될 수 있어 step()을 쓴다.
109 h.hover_at(p);
110 h.step();
111 h.drag_at(p);
112 h.step();
113 h.drop_at(p);
114 h.step();
115}
116
117fn drag(h: &mut Harness<'_, State>, from: Pos2) {
118 h.hover_at(from);
119 h.drag_at(from);
120 h.run();
121 h.hover_at(from + vec2(25.0, 25.0));
122 h.run();
123 h.drop_at(from + vec2(25.0, 25.0));
124 h.run();
125}
126
127#[test]
128fn clicking_settings_icon_opens_settings_without_moving_window() {
129 let mut h = harness(false);
130 let l = layout_of(&h);
131 click(&mut h, layout::rail_slot(l.rail, 0, false).center());
132 assert!(h.state().rec.open_settings);
133 assert!(!h.state().rec.drag);
134}
135
136#[test]
137fn dragging_empty_rail_moves_window() {
138 let mut h = harness(false);
139 let l = layout_of(&h);
140 drag(&mut h, pos2(l.rail.center().x, l.rail.center().y));
141 assert!(h.state().rec.drag);
142 assert!(!h.state().rec.open_settings);
143}
144
145#[test]
146fn dragging_empty_nav_bar_moves_window() {
147 let mut h = harness(true);
148 let l = layout_of(&h);
149 drag(&mut h, pos2(l.nav_bar.min.x + 8.0, l.nav_bar.center().y));
150 assert!(h.state().rec.drag);
151 assert_eq!(h.state().rec.system, None);
152}
153
154#[test]
155fn clicking_nav_button_sends_system_action() {
156 let mut h = harness(true);
157 let l = layout_of(&h);
158 let [back, home, _recents] = layout::nav_slots(l.nav_bar);
159 click(&mut h, back.center());
160 assert_eq!(h.state().rec.system, Some(SystemAction::Back));
161 assert!(!h.state().rec.drag);
162
163 let mut h = harness(true);
164 click(&mut h, home.center());
165 assert_eq!(h.state().rec.system, Some(SystemAction::Home));
166}
167
168#[test]
169fn dragging_title_bar_still_moves_window() {
170 let mut h = harness(false);
171 let l = layout_of(&h);
172 drag(&mut h, l.title_bar.left_center() + vec2(40.0, 0.0));
173 assert!(h.state().rec.drag);
174}
175
176fn side_button(h: &mut Harness<'_, State>, p: Pos2, button: egui::PointerButton) {
177 h.hover_at(p);
178 h.step();
179 for pressed in [true, false] {
180 h.event(egui::Event::PointerButton {
181 pos: p,
182 button,
183 pressed,
184 modifiers: egui::Modifiers::NONE,
185 });
186 h.step();
187 }
188}
189
190#[test]
191fn mouse_back_side_button_sends_back_when_connected() {
192 let mut h = harness(true);
193 let l = layout_of(&h);
194 side_button(&mut h, l.viewport.center(), egui::PointerButton::Extra1);
195 assert_eq!(h.state().rec.system, Some(SystemAction::Back));
196 assert!(!h.state().rec.drag);
197}
198
199#[test]
200fn other_mouse_buttons_send_nothing() {
201 let mut h = harness(true);
202 let l = layout_of(&h);
203 for button in [
204 egui::PointerButton::Secondary,
205 egui::PointerButton::Middle,
206 egui::PointerButton::Extra2,
207 ] {
208 side_button(&mut h, l.viewport.center(), button);
209 }
210 assert_eq!(h.state().rec.system, None);
211}
212
213#[test]
214fn mouse_back_side_button_ignored_when_disconnected() {
215 let mut h = harness(false);
216 let l = layout_of(&h);
217 side_button(&mut h, l.viewport.center(), egui::PointerButton::Extra1);
218 assert_eq!(h.state().rec.system, None);
219}
220
221// ---------------------------------------------------------------------------
222// 휴대폰 화면 입력
223// ---------------------------------------------------------------------------
224
225fn touches(h: &Harness<'_, State>) -> Vec<TouchEvent> {
226 h.state()
227 .rec
228 .input
229 .iter()
230 .filter_map(|m| match m {
231 Message::TouchEvent(t) => Some(*t),
232 _ => None,
233 })
234 .collect()
235}
236
237fn keys(h: &Harness<'_, State>) -> Vec<(KeyAction, u16, u32)> {
238 h.state()
239 .rec
240 .input
241 .iter()
242 .filter_map(|m| match m {
243 Message::KeyEvent(k) => Some((k.action, k.keycode, k.meta_state)),
244 _ => None,
245 })
246 .collect()
247}
248
249fn text(s: &str) -> Message {
250 Message::TextInput(TextInput { text: s.to_owned() })
251}
252
253/// 같은 프레임에 이벤트를 한꺼번에 넣는다. egui-winit은 키와 그 글자를 한 번에 넘긴다.
254fn frame_events(h: &mut Harness<'_, State>, events: impl IntoIterator<Item = Event>) {
255 h.input_mut().events.extend(events);
256 h.step();
257}
258
259fn key_event(key: Key, pressed: bool, modifiers: Modifiers) -> Event {
260 Event::Key {
261 key,
262 physical_key: None,
263 pressed,
264 repeat: false,
265 modifiers,
266 }
267}
268
269#[test]
270fn click_on_video_sends_touch_down_and_up() {
271 let mut h = harness_with_video();
272 let l = layout_of(&h);
273 click(&mut h, l.viewport.center());
274 let t = touches(&h);
275 assert_eq!(t.len(), 2, "{t:?}");
276 let (down, up) = (t[0], t[1]);
277 assert_eq!(down.action, TouchAction::Down);
278 assert_eq!(up.action, TouchAction::Up);
279 assert_eq!(
280 (down.x, down.y),
281 (u32::from(FRAME_W) / 2, u32::from(FRAME_H) / 2)
282 );
283 assert_eq!((up.x, up.y), (down.x, down.y));
284 assert_eq!((down.frame_width, down.frame_height), (FRAME_W, FRAME_H));
285 assert_eq!(down.pointer_id, 0);
286 assert_eq!(down.pressure, 0xFFFF);
287 assert_eq!(up.pressure, 0);
288 assert_eq!(h.state().rec.input.len(), 2);
289 assert!(!h.state().rec.drag);
290}
291
292#[test]
293fn dragging_on_video_sends_moves_in_order() {
294 let mut h = harness_with_video();
295 let l = layout_of(&h);
296 let start = l.viewport.center();
297 h.hover_at(start);
298 h.drag_at(start);
299 h.step();
300 for dx in [10.0, 20.0, 30.0] {
301 h.hover_at(start + vec2(dx, 0.0));
302 h.step();
303 }
304 h.drop_at(start + vec2(30.0, 0.0));
305 h.step();
306
307 let t = touches(&h);
308 let actions: Vec<_> = t.iter().map(|e| e.action).collect();
309 assert_eq!(actions.first(), Some(&TouchAction::Down));
310 assert_eq!(actions.last(), Some(&TouchAction::Up));
311 assert!(
312 actions[1..actions.len() - 1]
313 .iter()
314 .all(|a| *a == TouchAction::Move)
315 );
316 assert_eq!(actions.len(), 5, "{actions:?}");
317 assert!(t.windows(2).all(|w| w[0].x <= w[1].x));
318 assert!(t[1].x > t[0].x && t[3].x > t[1].x);
319 assert!(t.iter().all(|e| e.y == t[0].y));
320}
321
322#[test]
323fn moves_are_coalesced_per_frame() {
324 let mut h = harness_with_video();
325 let l = layout_of(&h);
326 let start = l.viewport.center();
327 frame_events(
328 &mut h,
329 [
330 Event::PointerMoved(start),
331 Event::PointerButton {
332 pos: start,
333 button: egui::PointerButton::Primary,
334 pressed: true,
335 modifiers: Modifiers::NONE,
336 },
337 ],
338 );
339 frame_events(
340 &mut h,
341 (1..=5).map(|i| Event::PointerMoved(start + vec2(i as f32 * 4.0, 0.0))),
342 );
343 // 움직이지 않은 프레임은 이동을 보내지 않는다.
344 frame_events(&mut h, [Event::PointerMoved(start + vec2(20.0, 0.0))]);
345 let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect();
346 assert_eq!(actions, [TouchAction::Down, TouchAction::Move]);
347}
348
349#[test]
350fn release_outside_video_is_clamped() {
351 let mut h = harness_with_video();
352 let l = layout_of(&h);
353 let start = l.viewport.center();
354 h.hover_at(start);
355 h.drag_at(start);
356 h.step();
357 let outside = pos2(l.viewport.max.x + 200.0, l.viewport.max.y + 300.0);
358 h.hover_at(outside);
359 h.step();
360 h.drop_at(outside);
361 h.step();
362 let up = *touches(&h).last().unwrap();
363 assert_eq!(up.action, TouchAction::Up);
364 assert_eq!(
365 (up.x, up.y),
366 (u32::from(FRAME_W) - 1, u32::from(FRAME_H) - 1)
367 );
368}
369
370#[test]
371fn press_outside_video_sends_no_touch() {
372 let mut h = harness_with_video();
373 let l = layout_of(&h);
374 click(&mut h, l.nav_bar.min + vec2(8.0, 8.0));
375 assert!(touches(&h).is_empty());
376}
377
378#[test]
379fn right_and_middle_click_send_nothing() {
380 let mut h = harness_with_video();
381 let l = layout_of(&h);
382 for button in [
383 egui::PointerButton::Secondary,
384 egui::PointerButton::Middle,
385 egui::PointerButton::Extra2,
386 ] {
387 side_button(&mut h, l.viewport.center(), button);
388 }
389 assert!(h.state().rec.input.is_empty(), "{:?}", h.state().rec.input);
390 assert_eq!(h.state().rec.system, None);
391}
392
393#[test]
394fn wheel_up_scrolls_positive() {
395 let mut h = harness_with_video();
396 let l = layout_of(&h);
397 h.hover_at(l.viewport.center());
398 h.step();
399 h.event(Event::MouseWheel {
400 unit: egui::MouseWheelUnit::Line,
401 delta: vec2(0.0, 1.0),
402 phase: egui::TouchPhase::Move,
403 source: egui::MouseWheelSource::Wheel,
404 modifiers: Modifiers::NONE,
405 });
406 h.step();
407 assert_eq!(
408 h.state().rec.input,
409 [Message::ScrollEvent(ScrollEvent {
410 x: u32::from(FRAME_W) / 2,
411 y: u32::from(FRAME_H) / 2,
412 frame_width: FRAME_W,
413 frame_height: FRAME_H,
414 h_scroll: 0,
415 v_scroll: 120,
416 })]
417 );
418}
419
420#[test]
421fn wheel_outside_video_sends_nothing() {
422 let mut h = harness_with_video();
423 let l = layout_of(&h);
424 h.hover_at(l.rail.center());
425 h.step();
426 h.event(Event::MouseWheel {
427 unit: egui::MouseWheelUnit::Line,
428 delta: vec2(0.0, 1.0),
429 phase: egui::TouchPhase::Move,
430 source: egui::MouseWheelSource::Wheel,
431 modifiers: Modifiers::NONE,
432 });
433 h.step();
434 assert!(h.state().rec.input.is_empty());
435}
436
437#[test]
438fn typing_letter_sends_text_only() {
439 let mut h = harness_with_video();
440 frame_events(
441 &mut h,
442 [
443 key_event(Key::A, true, Modifiers::NONE),
444 Event::Text("a".to_owned()),
445 ],
446 );
447 frame_events(&mut h, [key_event(Key::A, false, Modifiers::NONE)]);
448 assert_eq!(h.state().rec.input, [text("a")]);
449}
450
451#[test]
452fn enter_sends_key_down_and_up() {
453 let mut h = harness(true);
454 h.key_press(Key::Enter);
455 h.step();
456 assert_eq!(keys(&h), [(KeyAction::Down, 66, 0), (KeyAction::Up, 66, 0)]);
457 assert_eq!(h.state().rec.input.len(), 2);
458}
459
460#[test]
461fn ctrl_c_sends_copy_action_only() {
462 let mut h = harness(true);
463 h.key_press_modifiers(Modifiers::CTRL, Key::C);
464 h.step();
465 assert_eq!(
466 h.state().rec.input,
467 [Message::SystemAction(SystemAction::Copy)]
468 );
469}
470
471#[test]
472fn ctrl_z_sends_key_with_ctrl_meta() {
473 let mut h = harness(true);
474 h.key_press_modifiers(Modifiers::CTRL, Key::Z);
475 h.step();
476 assert_eq!(
477 keys(&h),
478 [(KeyAction::Down, 54, 0x3000), (KeyAction::Up, 54, 0x3000)]
479 );
480 assert_eq!(h.state().rec.input.len(), 2);
481}
482
483#[test]
484fn tab_goes_to_phone_instead_of_moving_focus() {
485 let mut h = harness(true);
486 // egui 기본 동작이면 Tab 두 번에 아이콘 열 빈 곳 → 설정 버튼 순서로 포커스가 간다.
487 for _ in 0..2 {
488 h.key_press(Key::Tab);
489 h.step();
490 }
491 assert_eq!(h.ctx.memory(|m| m.focused()), None);
492 h.key_press(Key::Enter);
493 h.key_press(Key::Space);
494 h.step();
495 // 설정 버튼이 포커스를 받았다면 Enter·Space가 버튼을 눌렀을 것이다.
496 assert!(!h.state().rec.open_settings);
497 assert_eq!(h.state().rec.system, None);
498 let codes: Vec<_> = keys(&h)
499 .into_iter()
500 .filter(|(a, ..)| *a == KeyAction::Down)
501 .map(|(_, code, _)| code)
502 .collect();
503 assert_eq!(codes, [61, 61, 66, 62]);
504}
505
506#[test]
507fn ime_commit_sends_text_and_preedit_is_local() {
508 let mut h = harness_with_video();
509 assert!(
510 h.output().platform_output.ime.is_some(),
511 "연결 중에는 IME를 켜 달라고 요청해야 한다"
512 );
513 h.event(Event::Ime(egui::ImeEvent::Preedit {
514 text: "하".to_owned(),
515 active_range_chars: None,
516 }));
517 h.event(Event::Ime(egui::ImeEvent::Preedit {
518 text: "한".to_owned(),
519 active_range_chars: None,
520 }));
521 h.step();
522 assert!(h.state().rec.input.is_empty());
523 h.event(Event::Ime(egui::ImeEvent::Commit("한글".to_owned())));
524 h.step();
525 assert_eq!(h.state().rec.input, [text("한글")]);
526}
527
528#[test]
529fn nothing_is_sent_when_disconnected() {
530 let mut h = harness(false);
531 let l = layout_of(&h);
532 click(&mut h, l.viewport.center() + vec2(0.0, 120.0));
533 h.hover_at(l.viewport.center());
534 h.event(Event::MouseWheel {
535 unit: egui::MouseWheelUnit::Line,
536 delta: vec2(0.0, 1.0),
537 phase: egui::TouchPhase::Move,
538 source: egui::MouseWheelSource::Wheel,
539 modifiers: Modifiers::NONE,
540 });
541 h.step();
542 frame_events(
543 &mut h,
544 [
545 key_event(Key::A, true, Modifiers::NONE),
546 Event::Text("a".to_owned()),
547 key_event(Key::A, false, Modifiers::NONE),
548 ],
549 );
550 h.key_press(Key::Enter);
551 h.key_press_modifiers(Modifiers::CTRL, Key::C);
552 h.event(Event::Ime(egui::ImeEvent::Commit("한".to_owned())));
553 h.step();
554 assert!(h.state().rec.input.is_empty(), "{:?}", h.state().rec.input);
555}
556
557#[test]
558fn connected_without_video_sends_keys_but_no_touch() {
559 let mut h = harness(true);
560 let l = layout_of(&h);
561 click(&mut h, l.viewport.center());
562 assert!(touches(&h).is_empty());
563 h.event(Event::Text("x".to_owned()));
564 h.step();
565 assert_eq!(h.state().rec.input, [text("x")]);
566}
567
568#[test]
569fn ended_connection_cancels_held_touch() {
570 let mut h = harness_with_video();
571 let l = layout_of(&h);
572 h.hover_at(l.viewport.center());
573 h.drag_at(l.viewport.center());
574 h.step();
575 h.state_mut()
576 .view
577 .on_conn_event(ConnEvent::Ended("연결이 끊겼습니다".to_owned()));
578 h.step();
579 let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect();
580 assert_eq!(actions, [TouchAction::Down, TouchAction::Cancel]);
581}
582
583#[test]
584fn window_focus_loss_cancels_touch() {
585 let mut h = harness_with_video();
586 let l = layout_of(&h);
587 h.hover_at(l.viewport.center());
588 h.drag_at(l.viewport.center());
589 h.step();
590 h.event(Event::WindowFocused(false));
591 h.step();
592 let actions: Vec<_> = touches(&h).iter().map(|e| e.action).collect();
593 assert_eq!(actions, [TouchAction::Down, TouchAction::Cancel]);
594}
595
596#[test]
597fn pressing_window_edges_starts_resize_not_touch() {
598 type Case = (fn(Rect) -> Pos2, layout::ResizeDir);
599 let cases: [Case; 4] = [
600 (
601 |r: Rect| pos2(r.max.x - 2.0, r.center().y),
602 layout::ResizeDir::East,
603 ),
604 (
605 |r: Rect| pos2(r.min.x + 2.0, r.center().y),
606 layout::ResizeDir::West,
607 ),
608 (
609 |r: Rect| pos2(r.center().x, r.max.y - 2.0),
610 layout::ResizeDir::South,
611 ),
612 (
613 |r: Rect| pos2(r.max.x - 2.0, r.max.y - 2.0),
614 layout::ResizeDir::SouthEast,
615 ),
616 ];
617 for (at, dir) in cases {
618 let mut h = harness_with_video();
619 let screen = h.state().rec.screen.unwrap();
620 click(&mut h, at(screen));
621 assert_eq!(h.state().rec.resize, Some(dir), "{dir:?}");
622 assert!(
623 !h.state()
624 .rec
625 .input
626 .iter()
627 .any(|m| matches!(m, Message::TouchEvent(_))),
628 "가장자리에서 터치가 나가면 안 된다: {dir:?}"
629 );
630 }
631}
632
633#[test]
634fn pressing_inside_phone_screen_does_not_resize() {
635 let mut h = harness_with_video();
636 let l = layout_of(&h);
637 click(&mut h, l.viewport.center());
638 assert_eq!(h.state().rec.resize, None);
639}