MCPcopy Index your code
hub / github.com/endbasic/endbasic / test_sdl_console_poll_key

Function test_sdl_console_poll_key

sdl/src/console.rs:718–783  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

716
717 #[test]
718 fn test_sdl_console_poll_key() {
719 let mut test = SdlTest::default();
720
721 /// Ensure the console doesn't return a key press for a brief period of time.
722 ///
723 /// Given that this is based on timing, the console could still report an event long after
724 /// we finished polling. This is fine: if the console works well, we shouldn't receive
725 /// spurious events here, which means that under normal conditions, this is not flaky.
726 /// However, if there is a bug somewhere, this is not guaranteed to catch it reliably.
727 fn assert_poll_is_none(console: &mut SdlConsole) {
728 let mut millis = 10;
729 while millis > 0 {
730 assert_eq!(None, block_on(console.poll_key()).unwrap());
731 thread::sleep(Duration::from_millis(1));
732 millis -= 1;
733 }
734 }
735
736 /// Waits for `console.poll_key` to return `exp_key` and fails the test if it returns any
737 /// other key.
738 fn assert_poll_is_key(console: &mut SdlConsole, exp_key: Key) {
739 loop {
740 match block_on(console.poll_key()).unwrap() {
741 Some(key) if key == exp_key => break,
742 Some(key) => panic!("Unexpected key {:?}", key),
743 None => (),
744 }
745 thread::sleep(Duration::from_millis(1));
746 }
747 }
748
749 assert_poll_is_none(test.console());
750
751 test.push_event(Event::Quit { timestamp: 0 });
752 assert_poll_is_key(test.console(), Key::EofOrDelete);
753 assert_poll_is_none(test.console());
754
755 test.push_event(key_down(Keycode::C, Mod::LCTRLMOD));
756 assert_poll_is_key(test.console(), Key::Interrupt);
757 assert_eq!(Signal::Break, test.wait_one_signal());
758 assert_poll_is_none(test.console());
759
760 // TODO(jmmv): We aren't testing textual input because push_event doesn't support injecting
761 // Event::TextInput events. At least check that individual key presses are ignored, because
762 // those would otherwise "hide" the follow-up text input events.
763 test.push_event(key_down(Keycode::A, Mod::empty()));
764 assert_poll_is_none(test.console());
765
766 // Check that non-keyboard events are ignored until a key event is received.
767 test.push_event(Event::MouseButtonDown {
768 timestamp: 0,
769 window_id: 0,
770 which: 0,
771 mouse_btn: MouseButton::Left,
772 clicks: 0,
773 x: 0,
774 y: 0,
775 });

Callers

nothing calls this directly

Calls 6

assert_poll_is_noneFunction · 0.85
assert_poll_is_keyFunction · 0.85
consoleMethod · 0.80
verifyMethod · 0.80
key_downFunction · 0.70
push_eventMethod · 0.45

Tested by

no test coverage detected