Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Dec 10, 2024
1 parent 5b0ac9b commit b743297
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 28 deletions.
2 changes: 2 additions & 0 deletions crates/tests/bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ workspace = true
workspace = true
features = [
"Foundation",
"Win32_Foundation",
"Win32_Security",
]

[dependencies.windows-sys]
Expand Down
13 changes: 13 additions & 0 deletions crates/tests/bindgen/src/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]

#[inline]
pub unsafe fn EnableMouseInPointer(fenable: bool) -> windows_core::Result<()> {
windows_targets::link!("user32.dll" "system" fn EnableMouseInPointer(fenable : windows::Win32::Foundation:: BOOL) -> windows::Win32::Foundation:: BOOL);
EnableMouseInPointer(fenable.into()).ok()
}
60 changes: 60 additions & 0 deletions crates/tests/bindgen/src/bool_event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#![allow(
non_snake_case,
non_upper_case_globals,
non_camel_case_types,
dead_code,
clippy::all
)]

#[inline]
pub unsafe fn CreateEventW<P3>(
lpeventattributes: Option<*const windows::Win32::Security::SECURITY_ATTRIBUTES>,
bmanualreset: bool,
binitialstate: bool,
lpname: P3,
) -> windows_core::Result<windows::Win32::Foundation::HANDLE>
where
P3: windows_core::Param<windows_core::PCWSTR>,
{
windows_targets::link!("kernel32.dll" "system" fn CreateEventW(lpeventattributes : *const windows::Win32::Security:: SECURITY_ATTRIBUTES, bmanualreset : windows::Win32::Foundation:: BOOL, binitialstate : windows::Win32::Foundation:: BOOL, lpname : windows_core::PCWSTR) -> windows::Win32::Foundation:: HANDLE);
let result__ = CreateEventW(
core::mem::transmute(lpeventattributes.unwrap_or(core::mem::zeroed())),
bmanualreset.into(),
binitialstate.into(),
lpname.param().abi(),
);
(!result__.is_invalid())
.then_some(result__)
.ok_or_else(windows_core::Error::from_win32)
}
#[inline]
pub unsafe fn NtWaitForSingleObject(
handle: windows::Win32::Foundation::HANDLE,
alertable: bool,
timeout: *mut i64,
) -> windows::Win32::Foundation::NTSTATUS {
windows_targets::link!("ntdll.dll" "system" fn NtWaitForSingleObject(handle : windows::Win32::Foundation:: HANDLE, alertable : windows::Win32::Foundation:: BOOLEAN, timeout : *mut i64) -> windows::Win32::Foundation:: NTSTATUS);
NtWaitForSingleObject(
core::mem::transmute(handle),
alertable.into(),
core::mem::transmute(timeout),
)
}
#[inline]
pub unsafe fn SetEvent(hevent: windows::Win32::Foundation::HANDLE) -> windows_core::Result<()> {
windows_targets::link!("kernel32.dll" "system" fn SetEvent(hevent : windows::Win32::Foundation:: HANDLE) -> windows::Win32::Foundation:: BOOL);
SetEvent(core::mem::transmute(hevent)).ok()
}
#[inline]
pub unsafe fn WaitForSingleObjectEx(
hhandle: windows::Win32::Foundation::HANDLE,
dwmilliseconds: u32,
balertable: bool,
) -> windows::Win32::Foundation::WAIT_EVENT {
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObjectEx(hhandle : windows::Win32::Foundation:: HANDLE, dwmilliseconds : u32, balertable : windows::Win32::Foundation:: BOOL) -> windows::Win32::Foundation:: WAIT_EVENT);
WaitForSingleObjectEx(
core::mem::transmute(hhandle),
core::mem::transmute(dwmilliseconds),
balertable.into(),
)
}
2 changes: 2 additions & 0 deletions crates/tests/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// This package was generated by `tool_bindgen`.

pub mod bool;
pub mod bool_event;
pub mod class;
pub mod class_dep;
pub mod class_static;
Expand Down
6 changes: 0 additions & 6 deletions crates/tests/bindgen/src/window_long_get_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ pub use GetWindowLongA as GetWindowLongPtrA;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HANDLE(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HANDLE {
type TypeKind = windows_core::CopyType;
}
impl HANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 as _ || self.0 == 0 as _
Expand All @@ -51,9 +48,6 @@ impl Default for HANDLE {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HWND(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HWND {
type TypeKind = windows_core::CopyType;
}
impl HWND {
pub fn is_invalid(&self) -> bool {
self.0.is_null()
Expand Down
6 changes: 0 additions & 6 deletions crates/tests/bindgen/src/window_long_get_w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ pub unsafe fn GetWindowLongW(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX) -> i32 {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HANDLE(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HANDLE {
type TypeKind = windows_core::CopyType;
}
impl HANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 as _ || self.0 == 0 as _
Expand All @@ -51,9 +48,6 @@ impl Default for HANDLE {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HWND(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HWND {
type TypeKind = windows_core::CopyType;
}
impl HWND {
pub fn is_invalid(&self) -> bool {
self.0.is_null()
Expand Down
6 changes: 0 additions & 6 deletions crates/tests/bindgen/src/window_long_set_a.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ pub use SetWindowLongA as SetWindowLongPtrA;
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HANDLE(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HANDLE {
type TypeKind = windows_core::CopyType;
}
impl HANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 as _ || self.0 == 0 as _
Expand All @@ -63,9 +60,6 @@ impl Default for HANDLE {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HWND(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HWND {
type TypeKind = windows_core::CopyType;
}
impl HWND {
pub fn is_invalid(&self) -> bool {
self.0.is_null()
Expand Down
6 changes: 0 additions & 6 deletions crates/tests/bindgen/src/window_long_set_w.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ pub unsafe fn SetWindowLongW(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX, dwnewlon
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HANDLE(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HANDLE {
type TypeKind = windows_core::CopyType;
}
impl HANDLE {
pub fn is_invalid(&self) -> bool {
self.0 == -1 as _ || self.0 == 0 as _
Expand All @@ -63,9 +60,6 @@ impl Default for HANDLE {
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct HWND(pub *mut core::ffi::c_void);
impl windows_core::TypeKind for HWND {
type TypeKind = windows_core::CopyType;
}
impl HWND {
pub fn is_invalid(&self) -> bool {
self.0.is_null()
Expand Down
21 changes: 21 additions & 0 deletions crates/tests/bindgen/tests/bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use test_bindgen::bool_event::*;
use windows::Win32::Foundation::{STATUS_SUCCESS, STATUS_TIMEOUT, WAIT_OBJECT_0, WAIT_TIMEOUT};

#[test]
fn test() {
unsafe {
// These bool parameters are actually BOOL.
let event = CreateEventW(None, true, false, None).unwrap();

// This bool parameter is actually BOOL.
assert_eq!(WAIT_TIMEOUT, WaitForSingleObjectEx(event, 0, false));

// This bool parameter is actually BOOLEAN.
assert_eq!(STATUS_TIMEOUT, NtWaitForSingleObject(event, false, &mut 0));

let event = CreateEventW(None, true, true, None).unwrap();

assert_eq!(WAIT_OBJECT_0, WaitForSingleObjectEx(event, 0, false));
assert_eq!(STATUS_SUCCESS, NtWaitForSingleObject(event, false, &mut 0));
}
}
1 change: 0 additions & 1 deletion crates/tests/misc/extensions/tests/bool32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ fn test() {
fn no_run() {
unsafe {
_ = CreateEventA(None, false, true, None);
_ = CreateEventA(None, BOOL(0), BOOL(1), None);
}
}
2 changes: 1 addition & 1 deletion crates/tests/misc/extensions/tests/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn no_run() {
Default::default(),
None,
None,
BOOLEAN(1),
true,
std::ptr::null_mut(),
);
}
Expand Down
7 changes: 5 additions & 2 deletions crates/tools/bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ fn main() {
test("--out fn_no_return_win.rs --filter FatalExit");
test("--out fn_no_return_sys.rs --filter FatalExit --sys");
test("--out fn_result_void_sys.rs --filter SetComputerNameA --sys");
// TODO: this requires BOOL extensions which are currently only in the `windows` crate
// test("--out fn_result_void_win.rs --filter SetComputerNameA");

// Tests for delegates
test("--out delegate.rs --filter DeferralCompletedHandler");
Expand Down Expand Up @@ -137,6 +135,11 @@ fn main() {
test("--out reference_struct_sys_reference_type.rs --sys --filter GAMING_DEVICE_MODEL_INFORMATION --reference windows_sys,skip-root,GAMING_DEVICE_VENDOR_ID");
test("--out reference_struct_sys_reference_namespace.rs --sys --filter GAMING_DEVICE_MODEL_INFORMATION --reference windows_sys,skip-root,Windows.Win32.Gaming");

// Tests for BOOL and BOOLEAN parameters
// TODO: this requires BOOL extensions which are currently only in the `windows` crate hence the --reference below
test("--out bool.rs --filter EnableMouseInPointer --reference windows,skip-root,Windows");
test("--out bool_event.rs --filter CreateEventW SetEvent NtWaitForSingleObject WaitForSingleObjectEx --reference windows,skip-root,Windows");

// Tests simulating reference dependency and dependent
test_raw(
"--no-comment --out reference_dependency_flat.rs --filter IMemoryBufferReference --flat",
Expand Down

0 comments on commit b743297

Please sign in to comment.