Debian Codegraph @

tests/virtual_device.rs

raw symbols SCIP
1 #![cfg(feature = "tokio")]
2
3 use std::error::Error;
4 use std::thread::sleep;
5 use std::time::Duration;
6
7 use tokio::time::timeout;
8
9 use evdev::{uinput::VirtualDevice, AttributeSet, EventType, InputEvent, KeyCode};
10
11 #[tokio::test]
12 async fn test_virtual_device_actually_emits() -> Result<(), Box<dyn Error>> {
13 let mut keys = AttributeSet::<KeyCode>::new();
14 let virtual_device_name = "fake-keyboard";
15 keys.insert(KeyCode::KEY_ESC);
16
17 let mut device = VirtualDevice::builder()?
18 .name(virtual_device_name)
19 .with_keys(&keys)?
20 .build()
21 .unwrap();
22
23 let mut maybe_device = None;
24 sleep(Duration::from_millis(500));
25 for (_i, d) in evdev::enumerate() {
26 println!("{:?}", d.name());
27 if d.name() == Some(virtual_device_name) {
28 maybe_device = Some(d);
29 break;
30 }
31 }
32 assert!(maybe_device.is_some());
33 let listen_device = maybe_device.unwrap();
34
35 let type_ = EventType::KEY;
36 let code = KeyCode::KEY_ESC.code();
37
38 // listen for events on the listen device
39 let listener = tokio::spawn(async move {
40 // try to read the key code that will be sent through virtual device
41 let mut events = listen_device.into_event_stream()?;
42 events.next_event().await
43 });
44
45 // emit a key code through virtual device
46 let down_event = InputEvent::new(type_.0, code, 10);
47 device.emit(&[down_event]).unwrap();
48
49 let event = timeout(Duration::from_secs(1), listener).await???;
50
51 assert_eq!(down_event.event_type(), event.event_type());
52 assert_eq!(down_event.code(), event.code());
53
54 // wait for listener
55 Ok(())
56 }