Add tests in espanso-detect win32
This commit is contained in:
		
							parent
							
								
									b16d1a04ae
								
							
						
					
					
						commit
						b5e2d42ec4
					
				
							
								
								
									
										63
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										63
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| 
						 | 
				
			
			@ -10,6 +10,17 @@ name = "cfg-if"
 | 
			
		|||
version = "1.0.0"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "enum-as-inner"
 | 
			
		||||
version = "0.3.3"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "heck 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "syn 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "espanso"
 | 
			
		||||
version = "1.0.0"
 | 
			
		||||
| 
						 | 
				
			
			@ -22,10 +33,19 @@ name = "espanso-detect"
 | 
			
		|||
version = "0.1.0"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "cc 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "enum-as-inner 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "widestring 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "heck"
 | 
			
		||||
version = "0.3.2"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "unicode-segmentation 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "log"
 | 
			
		||||
version = "0.4.14"
 | 
			
		||||
| 
						 | 
				
			
			@ -34,6 +54,42 @@ dependencies = [
 | 
			
		|||
 "cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "proc-macro2"
 | 
			
		||||
version = "1.0.24"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "quote"
 | 
			
		||||
version = "1.0.8"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "syn"
 | 
			
		||||
version = "1.0.60"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
dependencies = [
 | 
			
		||||
 "proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
 "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "unicode-segmentation"
 | 
			
		||||
version = "1.7.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "unicode-xid"
 | 
			
		||||
version = "0.2.1"
 | 
			
		||||
source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		||||
 | 
			
		||||
[[package]]
 | 
			
		||||
name = "widestring"
 | 
			
		||||
version = "0.4.3"
 | 
			
		||||
| 
						 | 
				
			
			@ -42,5 +98,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 | 
			
		|||
[metadata]
 | 
			
		||||
"checksum cc 1.0.66 (registry+https://github.com/rust-lang/crates.io-index)" = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48"
 | 
			
		||||
"checksum cfg-if 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 | 
			
		||||
"checksum enum-as-inner 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "7c5f0096a91d210159eceb2ff5e1c4da18388a170e1e3ce948aac9c8fdbbf595"
 | 
			
		||||
"checksum heck 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
 | 
			
		||||
"checksum log 0.4.14 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
 | 
			
		||||
"checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
 | 
			
		||||
"checksum quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df"
 | 
			
		||||
"checksum syn 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)" = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
 | 
			
		||||
"checksum unicode-segmentation 1.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
 | 
			
		||||
"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
 | 
			
		||||
"checksum widestring 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c168940144dd21fd8046987c16a46a33d5fc84eec29ef9dcddc2ac9e31526b7c"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,4 +12,7 @@ log = "0.4.14"
 | 
			
		|||
widestring = "0.4.3"
 | 
			
		||||
 | 
			
		||||
[build-dependencies]
 | 
			
		||||
cc = "1.0.66"
 | 
			
		||||
cc = "1.0.66"
 | 
			
		||||
 | 
			
		||||
[dev-dependencies]
 | 
			
		||||
enum-as-inner = "0.3.3"
 | 
			
		||||
| 
						 | 
				
			
			@ -16,14 +16,17 @@
 | 
			
		|||
 * You should have received a copy of the GNU General Public License
 | 
			
		||||
 * along with espanso.  If not, see <https://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
use enum_as_inner::EnumAsInner;
 | 
			
		||||
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
#[cfg_attr(test, derive(EnumAsInner))]
 | 
			
		||||
pub enum InputEvent {
 | 
			
		||||
  Mouse(MouseEvent),
 | 
			
		||||
  Keyboard(KeyboardEvent),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub enum MouseButton {
 | 
			
		||||
  Left,
 | 
			
		||||
  Right,
 | 
			
		||||
| 
						 | 
				
			
			@ -35,25 +38,25 @@ pub enum MouseButton {
 | 
			
		|||
  Button5,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub struct MouseEvent {
 | 
			
		||||
  pub button: MouseButton,
 | 
			
		||||
  pub status: Status,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub enum Status {
 | 
			
		||||
  Pressed,
 | 
			
		||||
  Released,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub enum Variant {
 | 
			
		||||
  Left,
 | 
			
		||||
  Right,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub struct KeyboardEvent {
 | 
			
		||||
  pub key: Key,
 | 
			
		||||
  pub value: Option<String>,
 | 
			
		||||
| 
						 | 
				
			
			@ -62,7 +65,7 @@ pub struct KeyboardEvent {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// A subset of the Web's key values: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
 | 
			
		||||
#[derive(Debug)]
 | 
			
		||||
#[derive(Debug, PartialEq)]
 | 
			
		||||
pub enum Key {
 | 
			
		||||
  // Modifiers
 | 
			
		||||
  Alt,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,12 +20,4 @@
 | 
			
		|||
pub mod event;
 | 
			
		||||
 | 
			
		||||
#[cfg(target_os = "windows")]
 | 
			
		||||
pub mod win32;
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
  #[test]
 | 
			
		||||
  fn it_works() {
 | 
			
		||||
    assert_eq!(2 + 2, 5);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
pub mod win32;
 | 
			
		||||
| 
						 | 
				
			
			@ -25,8 +25,8 @@ use crate::event::Variant::*;
 | 
			
		|||
use crate::event::{InputEvent, Key, KeyboardEvent, Variant};
 | 
			
		||||
use crate::event::{Key::*, MouseButton, MouseEvent};
 | 
			
		||||
 | 
			
		||||
const LEFT_VARIANT: i32 = 1;
 | 
			
		||||
const RIGHT_VARIANT: i32 = 2;
 | 
			
		||||
const INPUT_LEFT_VARIANT: i32 = 1;
 | 
			
		||||
const INPUT_RIGHT_VARIANT: i32 = 2;
 | 
			
		||||
 | 
			
		||||
const INPUT_EVENT_TYPE_KEYBOARD: i32 = 1;
 | 
			
		||||
const INPUT_EVENT_TYPE_MOUSE: i32 = 2;
 | 
			
		||||
| 
						 | 
				
			
			@ -72,9 +72,7 @@ pub struct Win32Source {
 | 
			
		|||
 | 
			
		||||
impl Win32Source {
 | 
			
		||||
  pub fn new(callback: Win32SourceCallback) -> Win32Source {
 | 
			
		||||
    Self {
 | 
			
		||||
      callback
 | 
			
		||||
    }
 | 
			
		||||
    Self { callback }
 | 
			
		||||
  }
 | 
			
		||||
  pub fn eventloop(&self) {
 | 
			
		||||
    unsafe {
 | 
			
		||||
| 
						 | 
				
			
			@ -87,10 +85,7 @@ impl Win32Source {
 | 
			
		|||
        }
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      raw_eventloop(
 | 
			
		||||
        self as *const Win32Source,
 | 
			
		||||
        callback,
 | 
			
		||||
      );
 | 
			
		||||
      raw_eventloop(self as *const Win32Source, callback);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -110,8 +105,8 @@ impl From<RawInputEvent> for Option<InputEvent> {
 | 
			
		|||
 | 
			
		||||
        // If the raw event does not include an explicit variant, use the hint provided by the key code
 | 
			
		||||
        let variant = match raw.variant {
 | 
			
		||||
          LEFT_VARIANT => Some(Left),
 | 
			
		||||
          RIGHT_VARIANT => Some(Right),
 | 
			
		||||
          INPUT_LEFT_VARIANT => Some(Left),
 | 
			
		||||
          INPUT_RIGHT_VARIANT => Some(Right),
 | 
			
		||||
          _ => variant_hint,
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -235,3 +230,82 @@ fn raw_to_mouse_button(raw: i32) -> Option<MouseButton> {
 | 
			
		|||
    _ => None,
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[cfg(test)]
 | 
			
		||||
mod tests {
 | 
			
		||||
  use super::*;
 | 
			
		||||
 | 
			
		||||
  fn default_raw_input_event() -> RawInputEvent {
 | 
			
		||||
    RawInputEvent {
 | 
			
		||||
      event_type: INPUT_EVENT_TYPE_KEYBOARD,
 | 
			
		||||
      buffer: [0; 24],
 | 
			
		||||
      buffer_len: 0,
 | 
			
		||||
      key_code: 0,
 | 
			
		||||
      variant: INPUT_LEFT_VARIANT,
 | 
			
		||||
      status: INPUT_STATUS_PRESSED,
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #[test]
 | 
			
		||||
  fn raw_to_input_event_keyboard_works_correctly() {
 | 
			
		||||
    let wide_string = widestring::WideString::from("k".to_string());
 | 
			
		||||
    let mut buffer: [u16; 24] = [0; 24];
 | 
			
		||||
    buffer[..1].copy_from_slice(wide_string.as_slice());
 | 
			
		||||
 | 
			
		||||
    let mut raw = default_raw_input_event();
 | 
			
		||||
    raw.buffer = buffer;
 | 
			
		||||
    raw.buffer_len = 1;
 | 
			
		||||
    raw.status = INPUT_STATUS_RELEASED;
 | 
			
		||||
    raw.variant = 0;
 | 
			
		||||
    raw.key_code = 0x4B;
 | 
			
		||||
 | 
			
		||||
    let result: Option<InputEvent> = raw.into();
 | 
			
		||||
    assert_eq!(result.unwrap(), InputEvent::Keyboard(KeyboardEvent {
 | 
			
		||||
      key: Other(0x4B),
 | 
			
		||||
      status: Released,
 | 
			
		||||
      value: Some("k".to_string()),
 | 
			
		||||
      variant: None,
 | 
			
		||||
    }));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #[test]
 | 
			
		||||
  fn raw_to_input_event_mouse_works_correctly() {
 | 
			
		||||
 | 
			
		||||
    let mut raw = default_raw_input_event();
 | 
			
		||||
    raw.event_type = INPUT_EVENT_TYPE_MOUSE;
 | 
			
		||||
    raw.status = INPUT_STATUS_RELEASED;
 | 
			
		||||
    raw.variant = 0;
 | 
			
		||||
    raw.key_code = INPUT_MOUSE_RIGHT_BUTTON;
 | 
			
		||||
 | 
			
		||||
    let result: Option<InputEvent> = raw.into();
 | 
			
		||||
    assert_eq!(result.unwrap(), InputEvent::Mouse(MouseEvent {
 | 
			
		||||
      status: Released,
 | 
			
		||||
      button: MouseButton::Right,
 | 
			
		||||
    }));
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #[test]
 | 
			
		||||
  fn raw_to_input_invalid_buffer() {
 | 
			
		||||
    let buffer: [u16; 24] = [123; 24];
 | 
			
		||||
 | 
			
		||||
    let mut raw = default_raw_input_event();
 | 
			
		||||
    raw.buffer = buffer;
 | 
			
		||||
    raw.buffer_len = 5;
 | 
			
		||||
 | 
			
		||||
    let result: Option<InputEvent> = raw.into();
 | 
			
		||||
    assert!(result.unwrap().into_keyboard().unwrap().value.is_none());
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  #[test]
 | 
			
		||||
  fn raw_to_input_event_returns_none_when_missing_type() {
 | 
			
		||||
    let result: Option<InputEvent> = RawInputEvent {
 | 
			
		||||
      event_type: 0,  // Missing type
 | 
			
		||||
      buffer: [0; 24],
 | 
			
		||||
      buffer_len: 0,
 | 
			
		||||
      key_code: 123,
 | 
			
		||||
      variant: INPUT_LEFT_VARIANT,
 | 
			
		||||
      status: INPUT_STATUS_PRESSED,
 | 
			
		||||
    }.into();
 | 
			
		||||
    assert!(result.is_none());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user