From 037ae204113ee8506f256f87051e6bf551fa3f3e Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sun, 14 May 2023 13:43:39 -0700 Subject: [PATCH 1/4] 100% coverage for dpi module --- tests/dpi.rs | 417 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 417 insertions(+) create mode 100644 tests/dpi.rs diff --git a/tests/dpi.rs b/tests/dpi.rs new file mode 100644 index 0000000000..589bfb7e6e --- /dev/null +++ b/tests/dpi.rs @@ -0,0 +1,417 @@ +// Tests for the DPI functionality of the library. + +use std::collections::HashSet; + +use winit::dpi; + +macro_rules! test_pixel_int_impl { + ($($name:ident => $ty:ty),*) => {$( + #[test] + fn $name() { + use dpi::Pixel; + + assert_eq!( + <$ty as Pixel>::from_f64(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.5), + 38, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.9), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + } + )*}; +} + +test_pixel_int_impl! { + test_pixel_int_u8 => u8, + test_pixel_int_u16 => u16, + test_pixel_int_u32 => u32, + test_pixel_int_i8 => i8, + test_pixel_int_i16 => i16 +} + +macro_rules! assert_approx_eq { + ($a:expr, $b:expr $(,)?) => { + assert!( + ($a - $b).abs() < 0.001, + "{} is not approximately equal to {}", + $a, + $b + ); + }; +} + +macro_rules! test_pixel_float_impl { + ($($name:ident => $ty:ty),*) => {$( + #[test] + fn $name() { + use dpi::Pixel; + + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.0), + 37.0, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.4), + 37.4, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.5), + 37.5, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.9), + 37.9, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + } + )*}; +} + +test_pixel_float_impl! { + test_pixel_float_f32 => f32, + test_pixel_float_f64 => f64 +} + +#[test] +fn test_validate_scale_factor() { + assert!(dpi::validate_scale_factor(1.0)); + assert!(dpi::validate_scale_factor(2.0)); + assert!(dpi::validate_scale_factor(3.0)); + assert!(dpi::validate_scale_factor(1.5)); + assert!(dpi::validate_scale_factor(0.5)); + + assert!(!dpi::validate_scale_factor(0.0)); + assert!(!dpi::validate_scale_factor(-1.0)); + assert!(!dpi::validate_scale_factor(f64::INFINITY)); + assert!(!dpi::validate_scale_factor(f64::NAN)); + assert!(!dpi::validate_scale_factor(f64::NEG_INFINITY)); +} + +#[test] +fn test_logical_position() { + let log_pos = dpi::LogicalPosition::new(1.0, 2.0); + assert_eq!( + log_pos.to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + log_pos.to_physical::(2.0), + dpi::PhysicalPosition::new(2, 4) + ); + assert_eq!(log_pos.cast::(), dpi::LogicalPosition::new(1, 2)); + assert_eq!( + log_pos, + dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(1.0, 2.0), 1.0) + ); + assert_eq!( + log_pos, + dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(2.0, 4.0), 2.0) + ); + assert_eq!( + dpi::LogicalPosition::from((2.0, 2.0)), + dpi::LogicalPosition::new(2.0, 2.0) + ); + assert_eq!( + dpi::LogicalPosition::from([2.0, 3.0]), + dpi::LogicalPosition::new(2.0, 3.0) + ); + + let x: (f64, f64) = log_pos.into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = log_pos.into(); + assert_eq!(x, [1.0, 2.0]); +} + +#[test] +fn test_physical_position() { + assert_eq!( + dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(1.0, 2.0), 1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(2.0, 4.0), 0.5), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::PhysicalPosition::from((2.0, 2.0)), + dpi::PhysicalPosition::new(2.0, 2.0) + ); + assert_eq!( + dpi::PhysicalPosition::from([2.0, 3.0]), + dpi::PhysicalPosition::new(2.0, 3.0) + ); + + let x: (f64, f64) = dpi::PhysicalPosition::new(1, 2).into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = dpi::PhysicalPosition::new(1, 2).into(); + assert_eq!(x, [1.0, 2.0]); +} + +#[test] +fn test_logical_size() { + let log_size = dpi::LogicalSize::new(1.0, 2.0); + assert_eq!( + log_size.to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + log_size.to_physical::(2.0), + dpi::PhysicalSize::new(2, 4) + ); + assert_eq!(log_size.cast::(), dpi::LogicalSize::new(1, 2)); + assert_eq!( + log_size, + dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(1.0, 2.0), 1.0) + ); + assert_eq!( + log_size, + dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(2.0, 4.0), 2.0) + ); + assert_eq!( + dpi::LogicalSize::from((2.0, 2.0)), + dpi::LogicalSize::new(2.0, 2.0) + ); + assert_eq!( + dpi::LogicalSize::from([2.0, 3.0]), + dpi::LogicalSize::new(2.0, 3.0) + ); + + let x: (f64, f64) = log_size.into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = log_size.into(); + assert_eq!(x, [1.0, 2.0]); +} + +#[test] +fn test_physical_size() { + assert_eq!( + dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(1.0, 2.0), 1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(2.0, 4.0), 0.5), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::PhysicalSize::from((2.0, 2.0)), + dpi::PhysicalSize::new(2.0, 2.0) + ); + assert_eq!( + dpi::PhysicalSize::from([2.0, 3.0]), + dpi::PhysicalSize::new(2.0, 3.0) + ); + + let x: (f64, f64) = dpi::PhysicalSize::new(1, 2).into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = dpi::PhysicalSize::new(1, 2).into(); + assert_eq!(x, [1.0, 2.0]); +} + +#[test] +fn test_size() { + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)), + dpi::Size::Physical(dpi::PhysicalSize::new(1, 2)) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)), + dpi::Size::Logical(dpi::LogicalSize::new(1.0, 2.0)) + ); + + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(1.0), + dpi::LogicalSize::new(1.0, 2.0) + ); + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(2.0), + dpi::LogicalSize::new(0.5, 1.0) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_logical::(1.0), + dpi::LogicalSize::new(1.0, 2.0) + ); + + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(2.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(2.0), + dpi::PhysicalSize::new(2, 4) + ); + + let small = dpi::Size::Physical((1, 2).into()); + let medium = dpi::Size::Logical((3, 4).into()); + let medium_physical = dpi::Size::new(medium.to_physical::(1.0)); + let large = dpi::Size::Physical((5, 6).into()); + assert_eq!(dpi::Size::clamp(medium, small, large, 1.0), medium_physical); + assert_eq!(dpi::Size::clamp(small, medium, large, 1.0), medium_physical); + assert_eq!(dpi::Size::clamp(large, small, medium, 1.0), medium_physical); +} + +#[test] +fn test_position() { + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)), + dpi::Position::Physical(dpi::PhysicalPosition::new(1, 2)) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)), + dpi::Position::Logical(dpi::LogicalPosition::new(1.0, 2.0)) + ); + + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(1.0), + dpi::LogicalPosition::new(1.0, 2.0) + ); + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(2.0), + dpi::LogicalPosition::new(0.5, 1.0) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_logical::(1.0), + dpi::LogicalPosition::new(1.0, 2.0) + ); + + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(2.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(2.0), + dpi::PhysicalPosition::new(2, 4) + ); +} + +// Eat coverage for the Debug impls et al +#[test] +fn attr_coverage() { + let _ = format!("{:?}", dpi::LogicalPosition::::default().clone()); + HashSet::new().insert(dpi::LogicalPosition::::default()); + + let _ = format!("{:?}", dpi::PhysicalPosition::::default().clone()); + HashSet::new().insert(dpi::PhysicalPosition::::default()); + + let _ = format!("{:?}", dpi::LogicalSize::::default().clone()); + HashSet::new().insert(dpi::LogicalSize::::default()); + + let _ = format!("{:?}", dpi::PhysicalSize::::default().clone()); + HashSet::new().insert(dpi::PhysicalSize::::default()); + + let _ = format!("{:?}", dpi::Size::Physical((1, 2).into()).clone()); + let _ = format!("{:?}", dpi::Position::Physical((1, 2).into()).clone()); +} From b08780ef316095e3728b4b18502f31b2c74b1165 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sun, 14 May 2023 13:46:33 -0700 Subject: [PATCH 2/4] Eat some coverage in error.rs --- src/error.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/error.rs b/src/error.rs index 7324742d4b..758ac9b611 100644 --- a/src/error.rs +++ b/src/error.rs @@ -109,3 +109,25 @@ impl error::Error for OsError {} impl error::Error for ExternalError {} impl error::Error for NotSupportedError {} impl error::Error for RunLoopError {} + +#[cfg(test)] +mod tests { + #![allow(clippy::redundant_clone)] + + use super::*; + + // Eat attributes for testing + #[test] + fn attr_coverage() { + let _ = format!( + "{:?}, {}", + NotSupportedError::new(), + NotSupportedError::new().clone() + ); + let _ = format!( + "{:?}, {}", + ExternalError::NotSupported(NotSupportedError::new()), + ExternalError::NotSupported(NotSupportedError::new()) + ); + } +} From 1c81719464d392fa47f35ab6368bbc9f1835c2ca Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sun, 14 May 2023 14:28:02 -0700 Subject: [PATCH 3/4] 100% coverage for the event module --- tests/event.rs | 280 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 tests/event.rs diff --git a/tests/event.rs b/tests/event.rs new file mode 100644 index 0000000000..7de8149413 --- /dev/null +++ b/tests/event.rs @@ -0,0 +1,280 @@ +use std::collections::{BTreeSet, HashSet}; + +use winit::event; + +macro_rules! foreach_event { + ($closure:expr) => {{ + #[allow(unused_mut)] + let mut x = $closure; + let did = unsafe { event::DeviceId::dummy() }; + + #[allow(deprecated)] + { + use winit::event::{Event::*, Ime::Enabled, WindowEvent::*}; + use winit::window::WindowId; + + // Mainline events. + let wid = unsafe { WindowId::dummy() }; + x(UserEvent(())); + x(NewEvents(event::StartCause::Init)); + x(MainEventsCleared); + x(RedrawRequested(wid)); + x(RedrawEventsCleared); + x(LoopDestroyed); + x(Suspended); + x(Resumed); + + // Window events. + let with_window_event = |wev| { + x(WindowEvent { + window_id: wid, + event: wev, + }) + }; + + with_window_event(CloseRequested); + with_window_event(Destroyed); + with_window_event(Focused(true)); + with_window_event(Moved((0, 0).into())); + with_window_event(Resized((0, 0).into())); + with_window_event(ReceivedCharacter('a')); + with_window_event(DroppedFile("x.txt".into())); + with_window_event(HoveredFile("x.txt".into())); + with_window_event(HoveredFileCancelled); + with_window_event(KeyboardInput { + device_id: did, + is_synthetic: false, + input: event::KeyboardInput { + scancode: 0, + state: event::ElementState::Pressed, + virtual_keycode: Some(event::VirtualKeyCode::A), + modifiers: event::ModifiersState::default(), + }, + }); + with_window_event(Ime(Enabled)); + with_window_event(CursorMoved { + device_id: did, + position: (0, 0).into(), + modifiers: event::ModifiersState::default(), + }); + with_window_event(ModifiersChanged(event::ModifiersState::default())); + with_window_event(CursorEntered { device_id: did }); + with_window_event(CursorLeft { device_id: did }); + with_window_event(MouseWheel { + device_id: did, + delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), + phase: event::TouchPhase::Started, + modifiers: event::ModifiersState::default(), + }); + with_window_event(MouseInput { + device_id: did, + state: event::ElementState::Pressed, + button: event::MouseButton::Other(0), + modifiers: event::ModifiersState::default(), + }); + with_window_event(TouchpadMagnify { + device_id: did, + delta: 0.0, + phase: event::TouchPhase::Started, + }); + with_window_event(SmartMagnify { device_id: did }); + with_window_event(TouchpadRotate { + device_id: did, + delta: 0.0, + phase: event::TouchPhase::Started, + }); + with_window_event(TouchpadPressure { + device_id: did, + pressure: 0.0, + stage: 0, + }); + with_window_event(AxisMotion { + device_id: did, + axis: 0, + value: 0.0, + }); + with_window_event(Touch(event::Touch { + device_id: did, + phase: event::TouchPhase::Started, + location: (0.0, 0.0).into(), + id: 0, + force: Some(event::Force::Normalized(0.0)), + })); + with_window_event(ThemeChanged(winit::window::Theme::Light)); + with_window_event(Occluded(true)); + } + + #[allow(deprecated)] + { + use event::DeviceEvent::*; + + let with_device_event = |dev_ev| { + x(event::Event::DeviceEvent { + device_id: did, + event: dev_ev, + }) + }; + + with_device_event(Added); + with_device_event(Removed); + with_device_event(MouseMotion { + delta: (0.0, 0.0).into(), + }); + with_device_event(MouseWheel { + delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), + }); + with_device_event(Motion { + axis: 0, + value: 0.0, + }); + with_device_event(Button { + button: 0, + state: event::ElementState::Pressed, + }); + with_device_event(Key(event::KeyboardInput { + scancode: 0, + state: event::ElementState::Pressed, + virtual_keycode: Some(event::VirtualKeyCode::A), + modifiers: event::ModifiersState::default(), + })); + with_device_event(Text { codepoint: 'a' }); + } + }}; +} + +#[test] +fn test_event_clone() { + foreach_event!(|event: event::Event<'static, ()>| { + let event2 = event.clone(); + assert_eq!(event, event2); + }) +} + +#[test] +#[should_panic] +fn test_cant_clone_scale_factor_changed() { + let inner_size = Box::new((0, 0).into()); + let ev: event::Event<'_, ()> = event::Event::WindowEvent { + window_id: unsafe { winit::window::WindowId::dummy() }, + event: event::WindowEvent::ScaleFactorChanged { + scale_factor: 1.0, + new_inner_size: Box::leak(inner_size), + }, + }; + let _ = ev.clone(); +} + +#[test] +fn test_map_nonuser_event() { + foreach_event!(|event: event::Event<'static, ()>| { + let is_user = matches!(event, event::Event::UserEvent(())); + let event2 = event.map_nonuser_event::<()>(); + if is_user { + assert_eq!(event2, Err(event::Event::UserEvent(()))); + } else { + assert!(event2.is_ok()); + } + }) +} + +#[test] +fn test_to_static() { + foreach_event!(|event: event::Event<'static, ()>| { + let event2 = event.clone().to_static(); + assert_eq!(Some(event), event2); + }) +} + +#[test] +fn test_scale_factor_changed_to_static() { + let mut inner_size = (0, 0).into(); + let ev: event::Event<'_, ()> = event::Event::WindowEvent { + window_id: unsafe { winit::window::WindowId::dummy() }, + event: event::WindowEvent::ScaleFactorChanged { + scale_factor: 1.0, + new_inner_size: &mut inner_size, + }, + }; + assert!(ev.to_static().is_none()); +} + +#[test] +fn test_force_normalize() { + let force = event::Force::Normalized(0.0); + assert_eq!(force.normalized(), 0.0); + + let force2 = event::Force::Calibrated { + force: 5.0, + max_possible_force: 2.5, + altitude_angle: None, + }; + assert_eq!(force2.normalized(), 2.0); + + let force3 = event::Force::Calibrated { + force: 5.0, + max_possible_force: 2.5, + altitude_angle: Some(std::f64::consts::PI / 2.0), + }; + assert_eq!(force3.normalized(), 2.0); +} + +#[test] +fn test_modifiers() { + assert!(event::ModifiersState::SHIFT.shift()); + assert!(event::ModifiersState::CTRL.ctrl()); + assert!(event::ModifiersState::ALT.alt()); + assert!(event::ModifiersState::LOGO.logo()); +} + +#[test] +fn attr_coverage() { + foreach_event!(|event: event::Event<'static, ()>| { + let _ = format!("{:?}", event); + }); + let _ = event::StartCause::Init.clone(); + + let did = unsafe { winit::event::DeviceId::dummy() }.clone(); + HashSet::new().insert(did); + let mut set = [did, did, did]; + set.sort_unstable(); + let mut set2 = BTreeSet::new(); + set2.insert(did); + set2.insert(did); + + HashSet::new().insert(event::KeyboardInput { + scancode: 0, + state: event::ElementState::Pressed, + virtual_keycode: Some(event::VirtualKeyCode::A), + #[allow(deprecated)] + modifiers: event::ModifiersState::default(), + }); + HashSet::new().insert(event::TouchPhase::Started.clone()); + HashSet::new().insert(event::MouseButton::Left.clone()); + HashSet::new().insert(event::Ime::Enabled); + + let _ = event::Touch { + device_id: did, + phase: event::TouchPhase::Started, + location: (0.0, 0.0).into(), + id: 0, + force: Some(event::Force::Normalized(0.0)), + } + .clone(); + let _ = event::Force::Calibrated { + force: 0.0, + max_possible_force: 0.0, + altitude_angle: None, + } + .clone(); + + let mut set = [ + event::VirtualKeyCode::A, + event::VirtualKeyCode::C, + event::VirtualKeyCode::B, + ]; + set.sort_unstable(); + let mut set2 = BTreeSet::new(); + set2.insert(event::VirtualKeyCode::A); + set2.insert(event::VirtualKeyCode::C); + set2.insert(event::VirtualKeyCode::B.clone()); +} From 90329683ee1886ba535ae5d59d995c6a60a9a8c3 Mon Sep 17 00:00:00 2001 From: John Nunley Date: Fri, 4 Aug 2023 17:47:37 -0700 Subject: [PATCH 4/4] Fix review comments - Rename attr_coverage functions to be more descriptive - Move tests to the file that they test - Fix build errors caused by changes to the Event enum Signed-off-by: John Nunley --- src/dpi.rs | 418 +++++++++++++++++++++++++++++++++++++++++++++++++ src/error.rs | 2 +- src/event.rs | 203 ++++++++++++++++++++++++ tests/dpi.rs | 417 ------------------------------------------------ tests/event.rs | 280 --------------------------------- 5 files changed, 622 insertions(+), 698 deletions(-) delete mode 100644 tests/dpi.rs delete mode 100644 tests/event.rs diff --git a/src/dpi.rs b/src/dpi.rs index 027f5d44e6..912056e6f2 100644 --- a/src/dpi.rs +++ b/src/dpi.rs @@ -581,3 +581,421 @@ impl From> for Position { Position::Logical(position.cast()) } } + +#[cfg(test)] +mod tests { + use crate::dpi; + use std::collections::HashSet; + + macro_rules! test_pixel_int_impl { + ($($name:ident => $ty:ty),*) => {$( + #[test] + fn $name() { + use dpi::Pixel; + + assert_eq!( + <$ty as Pixel>::from_f64(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.5), + 38, + ); + assert_eq!( + <$ty as Pixel>::from_f64(37.9), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37), + 37, + ); + } + )*}; + } + + test_pixel_int_impl! { + test_pixel_int_u8 => u8, + test_pixel_int_u16 => u16, + test_pixel_int_u32 => u32, + test_pixel_int_i8 => i8, + test_pixel_int_i16 => i16 + } + + macro_rules! assert_approx_eq { + ($a:expr, $b:expr $(,)?) => { + assert!( + ($a - $b).abs() < 0.001, + "{} is not approximately equal to {}", + $a, + $b + ); + }; + } + + macro_rules! test_pixel_float_impl { + ($($name:ident => $ty:ty),*) => {$( + #[test] + fn $name() { + use dpi::Pixel; + + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.0), + 37.0, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.4), + 37.4, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.5), + 37.5, + ); + assert_approx_eq!( + <$ty as Pixel>::from_f64(37.9), + 37.9, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + + assert_eq!( + <$ty as Pixel>::cast::(37.0), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.4), + 37, + ); + assert_eq!( + <$ty as Pixel>::cast::(37.5), + 38, + ); + } + )*}; +} + + test_pixel_float_impl! { + test_pixel_float_f32 => f32, + test_pixel_float_f64 => f64 + } + + #[test] + fn test_validate_scale_factor() { + assert!(dpi::validate_scale_factor(1.0)); + assert!(dpi::validate_scale_factor(2.0)); + assert!(dpi::validate_scale_factor(3.0)); + assert!(dpi::validate_scale_factor(1.5)); + assert!(dpi::validate_scale_factor(0.5)); + + assert!(!dpi::validate_scale_factor(0.0)); + assert!(!dpi::validate_scale_factor(-1.0)); + assert!(!dpi::validate_scale_factor(f64::INFINITY)); + assert!(!dpi::validate_scale_factor(f64::NAN)); + assert!(!dpi::validate_scale_factor(f64::NEG_INFINITY)); + } + + #[test] + fn test_logical_position() { + let log_pos = dpi::LogicalPosition::new(1.0, 2.0); + assert_eq!( + log_pos.to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + log_pos.to_physical::(2.0), + dpi::PhysicalPosition::new(2, 4) + ); + assert_eq!(log_pos.cast::(), dpi::LogicalPosition::new(1, 2)); + assert_eq!( + log_pos, + dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(1.0, 2.0), 1.0) + ); + assert_eq!( + log_pos, + dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(2.0, 4.0), 2.0) + ); + assert_eq!( + dpi::LogicalPosition::from((2.0, 2.0)), + dpi::LogicalPosition::new(2.0, 2.0) + ); + assert_eq!( + dpi::LogicalPosition::from([2.0, 3.0]), + dpi::LogicalPosition::new(2.0, 3.0) + ); + + let x: (f64, f64) = log_pos.into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = log_pos.into(); + assert_eq!(x, [1.0, 2.0]); + } + + #[test] + fn test_physical_position() { + assert_eq!( + dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(1.0, 2.0), 1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(2.0, 4.0), 0.5), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::PhysicalPosition::from((2.0, 2.0)), + dpi::PhysicalPosition::new(2.0, 2.0) + ); + assert_eq!( + dpi::PhysicalPosition::from([2.0, 3.0]), + dpi::PhysicalPosition::new(2.0, 3.0) + ); + + let x: (f64, f64) = dpi::PhysicalPosition::new(1, 2).into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = dpi::PhysicalPosition::new(1, 2).into(); + assert_eq!(x, [1.0, 2.0]); + } + + #[test] + fn test_logical_size() { + let log_size = dpi::LogicalSize::new(1.0, 2.0); + assert_eq!( + log_size.to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + log_size.to_physical::(2.0), + dpi::PhysicalSize::new(2, 4) + ); + assert_eq!(log_size.cast::(), dpi::LogicalSize::new(1, 2)); + assert_eq!( + log_size, + dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(1.0, 2.0), 1.0) + ); + assert_eq!( + log_size, + dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(2.0, 4.0), 2.0) + ); + assert_eq!( + dpi::LogicalSize::from((2.0, 2.0)), + dpi::LogicalSize::new(2.0, 2.0) + ); + assert_eq!( + dpi::LogicalSize::from([2.0, 3.0]), + dpi::LogicalSize::new(2.0, 3.0) + ); + + let x: (f64, f64) = log_size.into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = log_size.into(); + assert_eq!(x, [1.0, 2.0]); + } + + #[test] + fn test_physical_size() { + assert_eq!( + dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(1.0, 2.0), 1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(2.0, 4.0), 0.5), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::PhysicalSize::from((2.0, 2.0)), + dpi::PhysicalSize::new(2.0, 2.0) + ); + assert_eq!( + dpi::PhysicalSize::from([2.0, 3.0]), + dpi::PhysicalSize::new(2.0, 3.0) + ); + + let x: (f64, f64) = dpi::PhysicalSize::new(1, 2).into(); + assert_eq!(x, (1.0, 2.0)); + let x: [f64; 2] = dpi::PhysicalSize::new(1, 2).into(); + assert_eq!(x, [1.0, 2.0]); + } + + #[test] + fn test_size() { + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)), + dpi::Size::Physical(dpi::PhysicalSize::new(1, 2)) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)), + dpi::Size::Logical(dpi::LogicalSize::new(1.0, 2.0)) + ); + + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(1.0), + dpi::LogicalSize::new(1.0, 2.0) + ); + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(2.0), + dpi::LogicalSize::new(0.5, 1.0) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_logical::(1.0), + dpi::LogicalSize::new(1.0, 2.0) + ); + + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(2.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(1.0), + dpi::PhysicalSize::new(1, 2) + ); + assert_eq!( + dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(2.0), + dpi::PhysicalSize::new(2, 4) + ); + + let small = dpi::Size::Physical((1, 2).into()); + let medium = dpi::Size::Logical((3, 4).into()); + let medium_physical = dpi::Size::new(medium.to_physical::(1.0)); + let large = dpi::Size::Physical((5, 6).into()); + assert_eq!(dpi::Size::clamp(medium, small, large, 1.0), medium_physical); + assert_eq!(dpi::Size::clamp(small, medium, large, 1.0), medium_physical); + assert_eq!(dpi::Size::clamp(large, small, medium, 1.0), medium_physical); + } + + #[test] + fn test_position() { + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)), + dpi::Position::Physical(dpi::PhysicalPosition::new(1, 2)) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)), + dpi::Position::Logical(dpi::LogicalPosition::new(1.0, 2.0)) + ); + + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(1.0), + dpi::LogicalPosition::new(1.0, 2.0) + ); + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(2.0), + dpi::LogicalPosition::new(0.5, 1.0) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_logical::(1.0), + dpi::LogicalPosition::new(1.0, 2.0) + ); + + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(2.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(1.0), + dpi::PhysicalPosition::new(1, 2) + ); + assert_eq!( + dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(2.0), + dpi::PhysicalPosition::new(2, 4) + ); + } + + // Eat coverage for the Debug impls et al + #[test] + fn ensure_attrs_do_not_panic() { + let _ = format!("{:?}", dpi::LogicalPosition::::default().clone()); + HashSet::new().insert(dpi::LogicalPosition::::default()); + + let _ = format!("{:?}", dpi::PhysicalPosition::::default().clone()); + HashSet::new().insert(dpi::PhysicalPosition::::default()); + + let _ = format!("{:?}", dpi::LogicalSize::::default().clone()); + HashSet::new().insert(dpi::LogicalSize::::default()); + + let _ = format!("{:?}", dpi::PhysicalSize::::default().clone()); + HashSet::new().insert(dpi::PhysicalSize::::default()); + + let _ = format!("{:?}", dpi::Size::Physical((1, 2).into()).clone()); + let _ = format!("{:?}", dpi::Position::Physical((1, 2).into()).clone()); + } +} diff --git a/src/error.rs b/src/error.rs index 758ac9b611..42a3f43cf9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -118,7 +118,7 @@ mod tests { // Eat attributes for testing #[test] - fn attr_coverage() { + fn ensure_fmt_does_not_panic() { let _ = format!( "{:?}, {}", NotSupportedError::new(), diff --git a/src/event.rs b/src/event.rs index e24b0a9c91..80d57544e0 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1102,3 +1102,206 @@ impl PartialEq for InnerSizeWriter { self.new_inner_size.as_ptr() == other.new_inner_size.as_ptr() } } + +#[cfg(test)] +mod tests { + use crate::event; + use std::collections::{BTreeSet, HashSet}; + + macro_rules! foreach_event { + ($closure:expr) => {{ + #[allow(unused_mut)] + let mut x = $closure; + let did = unsafe { event::DeviceId::dummy() }; + + #[allow(deprecated)] + { + use crate::event::{Event::*, Ime::Enabled, WindowEvent::*}; + use crate::window::WindowId; + + // Mainline events. + let wid = unsafe { WindowId::dummy() }; + x(UserEvent(())); + x(NewEvents(event::StartCause::Init)); + x(RedrawRequested(wid)); + x(AboutToWait); + x(LoopExiting); + x(Suspended); + x(Resumed); + + // Window events. + let with_window_event = |wev| { + x(WindowEvent { + window_id: wid, + event: wev, + }) + }; + + with_window_event(CloseRequested); + with_window_event(Destroyed); + with_window_event(Focused(true)); + with_window_event(Moved((0, 0).into())); + with_window_event(Resized((0, 0).into())); + with_window_event(DroppedFile("x.txt".into())); + with_window_event(HoveredFile("x.txt".into())); + with_window_event(HoveredFileCancelled); + with_window_event(Ime(Enabled)); + with_window_event(CursorMoved { + device_id: did, + position: (0, 0).into(), + }); + with_window_event(ModifiersChanged(event::Modifiers::default())); + with_window_event(CursorEntered { device_id: did }); + with_window_event(CursorLeft { device_id: did }); + with_window_event(MouseWheel { + device_id: did, + delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), + phase: event::TouchPhase::Started, + }); + with_window_event(MouseInput { + device_id: did, + state: event::ElementState::Pressed, + button: event::MouseButton::Other(0), + }); + with_window_event(TouchpadMagnify { + device_id: did, + delta: 0.0, + phase: event::TouchPhase::Started, + }); + with_window_event(SmartMagnify { device_id: did }); + with_window_event(TouchpadRotate { + device_id: did, + delta: 0.0, + phase: event::TouchPhase::Started, + }); + with_window_event(TouchpadPressure { + device_id: did, + pressure: 0.0, + stage: 0, + }); + with_window_event(AxisMotion { + device_id: did, + axis: 0, + value: 0.0, + }); + with_window_event(Touch(event::Touch { + device_id: did, + phase: event::TouchPhase::Started, + location: (0.0, 0.0).into(), + id: 0, + force: Some(event::Force::Normalized(0.0)), + })); + with_window_event(ThemeChanged(crate::window::Theme::Light)); + with_window_event(Occluded(true)); + } + + #[allow(deprecated)] + { + use event::DeviceEvent::*; + + let with_device_event = |dev_ev| { + x(event::Event::DeviceEvent { + device_id: did, + event: dev_ev, + }) + }; + + with_device_event(Added); + with_device_event(Removed); + with_device_event(MouseMotion { + delta: (0.0, 0.0).into(), + }); + with_device_event(MouseWheel { + delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), + }); + with_device_event(Motion { + axis: 0, + value: 0.0, + }); + with_device_event(Button { + button: 0, + state: event::ElementState::Pressed, + }); + with_device_event(Text { codepoint: 'a' }); + } + }}; + } + + #[allow(clippy::redundant_clone)] + #[test] + fn test_event_clone() { + foreach_event!(|event: event::Event<()>| { + let event2 = event.clone(); + assert_eq!(event, event2); + }) + } + + #[test] + fn test_map_nonuser_event() { + foreach_event!(|event: event::Event<()>| { + let is_user = matches!(event, event::Event::UserEvent(())); + let event2 = event.map_nonuser_event::<()>(); + if is_user { + assert_eq!(event2, Err(event::Event::UserEvent(()))); + } else { + assert!(event2.is_ok()); + } + }) + } + + #[test] + fn test_force_normalize() { + let force = event::Force::Normalized(0.0); + assert_eq!(force.normalized(), 0.0); + + let force2 = event::Force::Calibrated { + force: 5.0, + max_possible_force: 2.5, + altitude_angle: None, + }; + assert_eq!(force2.normalized(), 2.0); + + let force3 = event::Force::Calibrated { + force: 5.0, + max_possible_force: 2.5, + altitude_angle: Some(std::f64::consts::PI / 2.0), + }; + assert_eq!(force3.normalized(), 2.0); + } + + #[allow(clippy::clone_on_copy)] + #[test] + fn ensure_attrs_do_not_panic() { + foreach_event!(|event: event::Event<()>| { + let _ = format!("{:?}", event); + }); + let _ = event::StartCause::Init.clone(); + + let did = unsafe { crate::event::DeviceId::dummy() }.clone(); + HashSet::new().insert(did); + let mut set = [did, did, did]; + set.sort_unstable(); + let mut set2 = BTreeSet::new(); + set2.insert(did); + set2.insert(did); + + HashSet::new().insert(event::TouchPhase::Started.clone()); + HashSet::new().insert(event::MouseButton::Left.clone()); + HashSet::new().insert(event::Ime::Enabled); + + let _ = event::Touch { + device_id: did, + phase: event::TouchPhase::Started, + location: (0.0, 0.0).into(), + id: 0, + force: Some(event::Force::Normalized(0.0)), + } + .clone(); + let _ = event::Force::Calibrated { + force: 0.0, + max_possible_force: 0.0, + altitude_angle: None, + } + .clone(); + } +} diff --git a/tests/dpi.rs b/tests/dpi.rs deleted file mode 100644 index 589bfb7e6e..0000000000 --- a/tests/dpi.rs +++ /dev/null @@ -1,417 +0,0 @@ -// Tests for the DPI functionality of the library. - -use std::collections::HashSet; - -use winit::dpi; - -macro_rules! test_pixel_int_impl { - ($($name:ident => $ty:ty),*) => {$( - #[test] - fn $name() { - use dpi::Pixel; - - assert_eq!( - <$ty as Pixel>::from_f64(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::from_f64(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::from_f64(37.5), - 38, - ); - assert_eq!( - <$ty as Pixel>::from_f64(37.9), - 38, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37), - 37, - ); - } - )*}; -} - -test_pixel_int_impl! { - test_pixel_int_u8 => u8, - test_pixel_int_u16 => u16, - test_pixel_int_u32 => u32, - test_pixel_int_i8 => i8, - test_pixel_int_i16 => i16 -} - -macro_rules! assert_approx_eq { - ($a:expr, $b:expr $(,)?) => { - assert!( - ($a - $b).abs() < 0.001, - "{} is not approximately equal to {}", - $a, - $b - ); - }; -} - -macro_rules! test_pixel_float_impl { - ($($name:ident => $ty:ty),*) => {$( - #[test] - fn $name() { - use dpi::Pixel; - - assert_approx_eq!( - <$ty as Pixel>::from_f64(37.0), - 37.0, - ); - assert_approx_eq!( - <$ty as Pixel>::from_f64(37.4), - 37.4, - ); - assert_approx_eq!( - <$ty as Pixel>::from_f64(37.5), - 37.5, - ); - assert_approx_eq!( - <$ty as Pixel>::from_f64(37.9), - 37.9, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.5), - 38, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.5), - 38, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.5), - 38, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.5), - 38, - ); - - assert_eq!( - <$ty as Pixel>::cast::(37.0), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.4), - 37, - ); - assert_eq!( - <$ty as Pixel>::cast::(37.5), - 38, - ); - } - )*}; -} - -test_pixel_float_impl! { - test_pixel_float_f32 => f32, - test_pixel_float_f64 => f64 -} - -#[test] -fn test_validate_scale_factor() { - assert!(dpi::validate_scale_factor(1.0)); - assert!(dpi::validate_scale_factor(2.0)); - assert!(dpi::validate_scale_factor(3.0)); - assert!(dpi::validate_scale_factor(1.5)); - assert!(dpi::validate_scale_factor(0.5)); - - assert!(!dpi::validate_scale_factor(0.0)); - assert!(!dpi::validate_scale_factor(-1.0)); - assert!(!dpi::validate_scale_factor(f64::INFINITY)); - assert!(!dpi::validate_scale_factor(f64::NAN)); - assert!(!dpi::validate_scale_factor(f64::NEG_INFINITY)); -} - -#[test] -fn test_logical_position() { - let log_pos = dpi::LogicalPosition::new(1.0, 2.0); - assert_eq!( - log_pos.to_physical::(1.0), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - log_pos.to_physical::(2.0), - dpi::PhysicalPosition::new(2, 4) - ); - assert_eq!(log_pos.cast::(), dpi::LogicalPosition::new(1, 2)); - assert_eq!( - log_pos, - dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(1.0, 2.0), 1.0) - ); - assert_eq!( - log_pos, - dpi::LogicalPosition::from_physical(dpi::PhysicalPosition::new(2.0, 4.0), 2.0) - ); - assert_eq!( - dpi::LogicalPosition::from((2.0, 2.0)), - dpi::LogicalPosition::new(2.0, 2.0) - ); - assert_eq!( - dpi::LogicalPosition::from([2.0, 3.0]), - dpi::LogicalPosition::new(2.0, 3.0) - ); - - let x: (f64, f64) = log_pos.into(); - assert_eq!(x, (1.0, 2.0)); - let x: [f64; 2] = log_pos.into(); - assert_eq!(x, [1.0, 2.0]); -} - -#[test] -fn test_physical_position() { - assert_eq!( - dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(1.0, 2.0), 1.0), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - dpi::PhysicalPosition::from_logical(dpi::LogicalPosition::new(2.0, 4.0), 0.5), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - dpi::PhysicalPosition::from((2.0, 2.0)), - dpi::PhysicalPosition::new(2.0, 2.0) - ); - assert_eq!( - dpi::PhysicalPosition::from([2.0, 3.0]), - dpi::PhysicalPosition::new(2.0, 3.0) - ); - - let x: (f64, f64) = dpi::PhysicalPosition::new(1, 2).into(); - assert_eq!(x, (1.0, 2.0)); - let x: [f64; 2] = dpi::PhysicalPosition::new(1, 2).into(); - assert_eq!(x, [1.0, 2.0]); -} - -#[test] -fn test_logical_size() { - let log_size = dpi::LogicalSize::new(1.0, 2.0); - assert_eq!( - log_size.to_physical::(1.0), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - log_size.to_physical::(2.0), - dpi::PhysicalSize::new(2, 4) - ); - assert_eq!(log_size.cast::(), dpi::LogicalSize::new(1, 2)); - assert_eq!( - log_size, - dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(1.0, 2.0), 1.0) - ); - assert_eq!( - log_size, - dpi::LogicalSize::from_physical(dpi::PhysicalSize::new(2.0, 4.0), 2.0) - ); - assert_eq!( - dpi::LogicalSize::from((2.0, 2.0)), - dpi::LogicalSize::new(2.0, 2.0) - ); - assert_eq!( - dpi::LogicalSize::from([2.0, 3.0]), - dpi::LogicalSize::new(2.0, 3.0) - ); - - let x: (f64, f64) = log_size.into(); - assert_eq!(x, (1.0, 2.0)); - let x: [f64; 2] = log_size.into(); - assert_eq!(x, [1.0, 2.0]); -} - -#[test] -fn test_physical_size() { - assert_eq!( - dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(1.0, 2.0), 1.0), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - dpi::PhysicalSize::from_logical(dpi::LogicalSize::new(2.0, 4.0), 0.5), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - dpi::PhysicalSize::from((2.0, 2.0)), - dpi::PhysicalSize::new(2.0, 2.0) - ); - assert_eq!( - dpi::PhysicalSize::from([2.0, 3.0]), - dpi::PhysicalSize::new(2.0, 3.0) - ); - - let x: (f64, f64) = dpi::PhysicalSize::new(1, 2).into(); - assert_eq!(x, (1.0, 2.0)); - let x: [f64; 2] = dpi::PhysicalSize::new(1, 2).into(); - assert_eq!(x, [1.0, 2.0]); -} - -#[test] -fn test_size() { - assert_eq!( - dpi::Size::new(dpi::PhysicalSize::new(1, 2)), - dpi::Size::Physical(dpi::PhysicalSize::new(1, 2)) - ); - assert_eq!( - dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)), - dpi::Size::Logical(dpi::LogicalSize::new(1.0, 2.0)) - ); - - assert_eq!( - dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(1.0), - dpi::LogicalSize::new(1.0, 2.0) - ); - assert_eq!( - dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_logical::(2.0), - dpi::LogicalSize::new(0.5, 1.0) - ); - assert_eq!( - dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_logical::(1.0), - dpi::LogicalSize::new(1.0, 2.0) - ); - - assert_eq!( - dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(1.0), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - dpi::Size::new(dpi::PhysicalSize::new(1, 2)).to_physical::(2.0), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(1.0), - dpi::PhysicalSize::new(1, 2) - ); - assert_eq!( - dpi::Size::new(dpi::LogicalSize::new(1.0, 2.0)).to_physical::(2.0), - dpi::PhysicalSize::new(2, 4) - ); - - let small = dpi::Size::Physical((1, 2).into()); - let medium = dpi::Size::Logical((3, 4).into()); - let medium_physical = dpi::Size::new(medium.to_physical::(1.0)); - let large = dpi::Size::Physical((5, 6).into()); - assert_eq!(dpi::Size::clamp(medium, small, large, 1.0), medium_physical); - assert_eq!(dpi::Size::clamp(small, medium, large, 1.0), medium_physical); - assert_eq!(dpi::Size::clamp(large, small, medium, 1.0), medium_physical); -} - -#[test] -fn test_position() { - assert_eq!( - dpi::Position::new(dpi::PhysicalPosition::new(1, 2)), - dpi::Position::Physical(dpi::PhysicalPosition::new(1, 2)) - ); - assert_eq!( - dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)), - dpi::Position::Logical(dpi::LogicalPosition::new(1.0, 2.0)) - ); - - assert_eq!( - dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(1.0), - dpi::LogicalPosition::new(1.0, 2.0) - ); - assert_eq!( - dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_logical::(2.0), - dpi::LogicalPosition::new(0.5, 1.0) - ); - assert_eq!( - dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_logical::(1.0), - dpi::LogicalPosition::new(1.0, 2.0) - ); - - assert_eq!( - dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(1.0), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - dpi::Position::new(dpi::PhysicalPosition::new(1, 2)).to_physical::(2.0), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(1.0), - dpi::PhysicalPosition::new(1, 2) - ); - assert_eq!( - dpi::Position::new(dpi::LogicalPosition::new(1.0, 2.0)).to_physical::(2.0), - dpi::PhysicalPosition::new(2, 4) - ); -} - -// Eat coverage for the Debug impls et al -#[test] -fn attr_coverage() { - let _ = format!("{:?}", dpi::LogicalPosition::::default().clone()); - HashSet::new().insert(dpi::LogicalPosition::::default()); - - let _ = format!("{:?}", dpi::PhysicalPosition::::default().clone()); - HashSet::new().insert(dpi::PhysicalPosition::::default()); - - let _ = format!("{:?}", dpi::LogicalSize::::default().clone()); - HashSet::new().insert(dpi::LogicalSize::::default()); - - let _ = format!("{:?}", dpi::PhysicalSize::::default().clone()); - HashSet::new().insert(dpi::PhysicalSize::::default()); - - let _ = format!("{:?}", dpi::Size::Physical((1, 2).into()).clone()); - let _ = format!("{:?}", dpi::Position::Physical((1, 2).into()).clone()); -} diff --git a/tests/event.rs b/tests/event.rs deleted file mode 100644 index 7de8149413..0000000000 --- a/tests/event.rs +++ /dev/null @@ -1,280 +0,0 @@ -use std::collections::{BTreeSet, HashSet}; - -use winit::event; - -macro_rules! foreach_event { - ($closure:expr) => {{ - #[allow(unused_mut)] - let mut x = $closure; - let did = unsafe { event::DeviceId::dummy() }; - - #[allow(deprecated)] - { - use winit::event::{Event::*, Ime::Enabled, WindowEvent::*}; - use winit::window::WindowId; - - // Mainline events. - let wid = unsafe { WindowId::dummy() }; - x(UserEvent(())); - x(NewEvents(event::StartCause::Init)); - x(MainEventsCleared); - x(RedrawRequested(wid)); - x(RedrawEventsCleared); - x(LoopDestroyed); - x(Suspended); - x(Resumed); - - // Window events. - let with_window_event = |wev| { - x(WindowEvent { - window_id: wid, - event: wev, - }) - }; - - with_window_event(CloseRequested); - with_window_event(Destroyed); - with_window_event(Focused(true)); - with_window_event(Moved((0, 0).into())); - with_window_event(Resized((0, 0).into())); - with_window_event(ReceivedCharacter('a')); - with_window_event(DroppedFile("x.txt".into())); - with_window_event(HoveredFile("x.txt".into())); - with_window_event(HoveredFileCancelled); - with_window_event(KeyboardInput { - device_id: did, - is_synthetic: false, - input: event::KeyboardInput { - scancode: 0, - state: event::ElementState::Pressed, - virtual_keycode: Some(event::VirtualKeyCode::A), - modifiers: event::ModifiersState::default(), - }, - }); - with_window_event(Ime(Enabled)); - with_window_event(CursorMoved { - device_id: did, - position: (0, 0).into(), - modifiers: event::ModifiersState::default(), - }); - with_window_event(ModifiersChanged(event::ModifiersState::default())); - with_window_event(CursorEntered { device_id: did }); - with_window_event(CursorLeft { device_id: did }); - with_window_event(MouseWheel { - device_id: did, - delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), - phase: event::TouchPhase::Started, - modifiers: event::ModifiersState::default(), - }); - with_window_event(MouseInput { - device_id: did, - state: event::ElementState::Pressed, - button: event::MouseButton::Other(0), - modifiers: event::ModifiersState::default(), - }); - with_window_event(TouchpadMagnify { - device_id: did, - delta: 0.0, - phase: event::TouchPhase::Started, - }); - with_window_event(SmartMagnify { device_id: did }); - with_window_event(TouchpadRotate { - device_id: did, - delta: 0.0, - phase: event::TouchPhase::Started, - }); - with_window_event(TouchpadPressure { - device_id: did, - pressure: 0.0, - stage: 0, - }); - with_window_event(AxisMotion { - device_id: did, - axis: 0, - value: 0.0, - }); - with_window_event(Touch(event::Touch { - device_id: did, - phase: event::TouchPhase::Started, - location: (0.0, 0.0).into(), - id: 0, - force: Some(event::Force::Normalized(0.0)), - })); - with_window_event(ThemeChanged(winit::window::Theme::Light)); - with_window_event(Occluded(true)); - } - - #[allow(deprecated)] - { - use event::DeviceEvent::*; - - let with_device_event = |dev_ev| { - x(event::Event::DeviceEvent { - device_id: did, - event: dev_ev, - }) - }; - - with_device_event(Added); - with_device_event(Removed); - with_device_event(MouseMotion { - delta: (0.0, 0.0).into(), - }); - with_device_event(MouseWheel { - delta: event::MouseScrollDelta::LineDelta(0.0, 0.0), - }); - with_device_event(Motion { - axis: 0, - value: 0.0, - }); - with_device_event(Button { - button: 0, - state: event::ElementState::Pressed, - }); - with_device_event(Key(event::KeyboardInput { - scancode: 0, - state: event::ElementState::Pressed, - virtual_keycode: Some(event::VirtualKeyCode::A), - modifiers: event::ModifiersState::default(), - })); - with_device_event(Text { codepoint: 'a' }); - } - }}; -} - -#[test] -fn test_event_clone() { - foreach_event!(|event: event::Event<'static, ()>| { - let event2 = event.clone(); - assert_eq!(event, event2); - }) -} - -#[test] -#[should_panic] -fn test_cant_clone_scale_factor_changed() { - let inner_size = Box::new((0, 0).into()); - let ev: event::Event<'_, ()> = event::Event::WindowEvent { - window_id: unsafe { winit::window::WindowId::dummy() }, - event: event::WindowEvent::ScaleFactorChanged { - scale_factor: 1.0, - new_inner_size: Box::leak(inner_size), - }, - }; - let _ = ev.clone(); -} - -#[test] -fn test_map_nonuser_event() { - foreach_event!(|event: event::Event<'static, ()>| { - let is_user = matches!(event, event::Event::UserEvent(())); - let event2 = event.map_nonuser_event::<()>(); - if is_user { - assert_eq!(event2, Err(event::Event::UserEvent(()))); - } else { - assert!(event2.is_ok()); - } - }) -} - -#[test] -fn test_to_static() { - foreach_event!(|event: event::Event<'static, ()>| { - let event2 = event.clone().to_static(); - assert_eq!(Some(event), event2); - }) -} - -#[test] -fn test_scale_factor_changed_to_static() { - let mut inner_size = (0, 0).into(); - let ev: event::Event<'_, ()> = event::Event::WindowEvent { - window_id: unsafe { winit::window::WindowId::dummy() }, - event: event::WindowEvent::ScaleFactorChanged { - scale_factor: 1.0, - new_inner_size: &mut inner_size, - }, - }; - assert!(ev.to_static().is_none()); -} - -#[test] -fn test_force_normalize() { - let force = event::Force::Normalized(0.0); - assert_eq!(force.normalized(), 0.0); - - let force2 = event::Force::Calibrated { - force: 5.0, - max_possible_force: 2.5, - altitude_angle: None, - }; - assert_eq!(force2.normalized(), 2.0); - - let force3 = event::Force::Calibrated { - force: 5.0, - max_possible_force: 2.5, - altitude_angle: Some(std::f64::consts::PI / 2.0), - }; - assert_eq!(force3.normalized(), 2.0); -} - -#[test] -fn test_modifiers() { - assert!(event::ModifiersState::SHIFT.shift()); - assert!(event::ModifiersState::CTRL.ctrl()); - assert!(event::ModifiersState::ALT.alt()); - assert!(event::ModifiersState::LOGO.logo()); -} - -#[test] -fn attr_coverage() { - foreach_event!(|event: event::Event<'static, ()>| { - let _ = format!("{:?}", event); - }); - let _ = event::StartCause::Init.clone(); - - let did = unsafe { winit::event::DeviceId::dummy() }.clone(); - HashSet::new().insert(did); - let mut set = [did, did, did]; - set.sort_unstable(); - let mut set2 = BTreeSet::new(); - set2.insert(did); - set2.insert(did); - - HashSet::new().insert(event::KeyboardInput { - scancode: 0, - state: event::ElementState::Pressed, - virtual_keycode: Some(event::VirtualKeyCode::A), - #[allow(deprecated)] - modifiers: event::ModifiersState::default(), - }); - HashSet::new().insert(event::TouchPhase::Started.clone()); - HashSet::new().insert(event::MouseButton::Left.clone()); - HashSet::new().insert(event::Ime::Enabled); - - let _ = event::Touch { - device_id: did, - phase: event::TouchPhase::Started, - location: (0.0, 0.0).into(), - id: 0, - force: Some(event::Force::Normalized(0.0)), - } - .clone(); - let _ = event::Force::Calibrated { - force: 0.0, - max_possible_force: 0.0, - altitude_angle: None, - } - .clone(); - - let mut set = [ - event::VirtualKeyCode::A, - event::VirtualKeyCode::C, - event::VirtualKeyCode::B, - ]; - set.sort_unstable(); - let mut set2 = BTreeSet::new(); - set2.insert(event::VirtualKeyCode::A); - set2.insert(event::VirtualKeyCode::C); - set2.insert(event::VirtualKeyCode::B.clone()); -}