| 1 |
#![cfg(feature = "device-test")] |
| 2 |
|
| 3 |
mod common; |
| 4 |
|
| 5 |
use common::{final_dot_state, final_event_state, get_test_device, key_click, key_event}; |
| 6 |
use evdev::{EventType, InputEvent, KeyCode, SwitchCode}; |
| 7 |
|
| 8 |
#[test] |
| 9 |
pub fn test_compensate_keys() -> std::io::Result<()> { |
| 10 |
let (mut input, mut output) = get_test_device()?; |
| 11 |
|
| 12 |
let dot_state: i32 = 0; |
| 13 |
|
| 14 |
|
| 15 |
for _ in 0..30 { |
| 16 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 17 |
} |
| 18 |
|
| 19 |
let dot_state = final_dot_state(dot_state, input.fetch_events()?); |
| 20 |
|
| 21 |
assert_eq!(0, dot_state); |
| 22 |
|
| 23 |
|
| 24 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 25 |
|
| 26 |
let dot_state = final_dot_state(dot_state, input.fetch_events()?); |
| 27 |
|
| 28 |
assert_eq!(0, dot_state); |
| 29 |
|
| 30 |
|
| 31 |
output.emit(&[key_event(KeyCode::KEY_DOT, 1)])?; |
| 32 |
|
| 33 |
let dot_state = final_dot_state(dot_state, input.fetch_events()?); |
| 34 |
|
| 35 |
assert_eq!(1, dot_state); |
| 36 |
|
| 37 |
Ok(()) |
| 38 |
} |
| 39 |
|
| 40 |
#[test] |
| 41 |
pub fn test_compensate_with_key_down() -> std::io::Result<()> { |
| 42 |
let (mut input, mut output) = get_test_device()?; |
| 43 |
|
| 44 |
output.emit(&[InputEvent::new(EventType::KEY.0, KeyCode::KEY_A.0, 1)])?; |
| 45 |
output.emit(&[InputEvent::new(EventType::KEY.0, KeyCode::KEY_B.0, 1)])?; |
| 46 |
|
| 47 |
|
| 48 |
for _ in 0..30 { |
| 49 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 50 |
} |
| 51 |
|
| 52 |
assert_eq!(0, input.fetch_events()?.into_iter().count()); |
| 53 |
|
| 54 |
|
| 55 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 56 |
|
| 57 |
let events = input |
| 58 |
.fetch_events()? |
| 59 |
.into_iter() |
| 60 |
.collect::<Vec<InputEvent>>(); |
| 61 |
|
| 62 |
assert_eq!(Some(1), final_event_state(KeyCode::KEY_A, &events)); |
| 63 |
assert_eq!(Some(1), final_event_state(KeyCode::KEY_B, &events)); |
| 64 |
|
| 65 |
Ok(()) |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
pub fn test_compensate_with_switch_down() -> std::io::Result<()> { |
| 70 |
let (mut input, mut output) = get_test_device()?; |
| 71 |
|
| 72 |
let dot_state: i32 = 0; |
| 73 |
|
| 74 |
output.emit(&[InputEvent::new( |
| 75 |
EventType::SWITCH.0, |
| 76 |
SwitchCode::SW_LID.0, |
| 77 |
1, |
| 78 |
)])?; |
| 79 |
|
| 80 |
|
| 81 |
for _ in 0..30 { |
| 82 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 83 |
} |
| 84 |
|
| 85 |
let dot_state = final_dot_state(dot_state, input.fetch_events()?); |
| 86 |
|
| 87 |
assert_eq!(0, dot_state); |
| 88 |
|
| 89 |
|
| 90 |
output.emit(&key_click(KeyCode::KEY_DOT))?; |
| 91 |
|
| 92 |
let dot_state = final_dot_state(dot_state, input.fetch_events()?); |
| 93 |
|
| 94 |
assert_eq!(0, dot_state); |
| 95 |
|
| 96 |
Ok(()) |
| 97 |
} |