Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

test: add mocks for `wm-platform` structs (#1366)

T. Alexander Popiel2a5f984cc1a6
diff --git a/.gitignore b/.gitignore
index 63c13a6e..0c98b18e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,3 +13,7 @@ out/
# IDE files
.vs/
.idea/
+
+# vi files
+.*.swp
+.*.swo
diff --git a/packages/wm-platform/Cargo.toml b/packages/wm-platform/Cargo.toml
index 9fb6798d..57b92055 100644
--- a/packages/wm-platform/Cargo.toml
+++ b/packages/wm-platform/Cargo.toml
@@ -20,6 +20,9 @@ tracing = { workspace = true }
serde = { workspace = true }
regex = { workspace = true }
+[features]
+test_utils = []
+
[dev-dependencies]
libtest-mimic-collect = "0.3.2"
diff --git a/packages/wm-platform/src/lib.rs b/packages/wm-platform/src/lib.rs
index 6f235c4f..70470941 100644
--- a/packages/wm-platform/src/lib.rs
+++ b/packages/wm-platform/src/lib.rs
@@ -17,6 +17,9 @@ mod single_instance;
mod thread_bound;
mod window_listener;
+#[cfg(feature = "test_utils")]
+pub mod test_utils;
+
pub use dispatcher::*;
pub use display::*;
pub use display_listener::*;
diff --git a/packages/wm-platform/src/test_utils.rs b/packages/wm-platform/src/test_utils.rs
new file mode 100644
index 00000000..8fd7c4b9
--- /dev/null
+++ b/packages/wm-platform/src/test_utils.rs
@@ -0,0 +1,58 @@
+//! Utilities for testing.
+//!
+//! Available via the `test_utils` Cargo feature.
+use std::sync::{atomic::AtomicBool, Arc};
+
+use crate::platform_impl;
+#[cfg(target_os = "macos")]
+pub use crate::WindowId;
+pub use crate::{Dispatcher, Display, NativeWindow};
+
+impl Dispatcher {
+ /// Creates a mock `Dispatcher` for use in tests.
+ ///
+ /// Calling any methods on the mock is undefined behavior and may panic.
+ #[must_use]
+ pub fn mock() -> Self {
+ Self::new(None, Arc::new(AtomicBool::new(false)))
+ }
+}
+
+impl NativeWindow {
+ /// Creates a mock `NativeWindow` for use in tests.
+ ///
+ /// Calling any methods on the mock is undefined behavior and may panic.
+ #[must_use]
+ pub fn mock() -> Self {
+ #[cfg(target_os = "windows")]
+ {
+ platform_impl::NativeWindow::new(0).into()
+ }
+ #[cfg(target_os = "macos")]
+ {
+ #[allow(invalid_value)]
+ platform_impl::NativeWindow::new(
+ WindowId(0),
+ unsafe { std::mem::zeroed() },
+ unsafe { std::mem::zeroed() },
+ )
+ .into()
+ }
+ }
+}
+
+impl Display {
+ /// Creates a mock `Display` for use in tests.
+ ///
+ /// Calling any methods on the mock is undefined behavior and may panic.
+ #[must_use]
+ pub fn mock() -> Self {
+ Self {
+ #[cfg(target_os = "windows")]
+ inner: platform_impl::Display::new(0),
+ #[cfg(target_os = "macos")]
+ #[allow(invalid_value)]
+ inner: unsafe { std::mem::zeroed() },
+ }
+ }
+}