diff options
author | Ngo Iok Ui (Wu Yu Wei) <yuweiwu@pm.me> | 2024-05-20 16:22:17 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-20 07:22:17 +0000 |
commit | c2076580f352f3c61f90969e03d78ada609935eb (patch) | |
tree | ea8412e25008ef2e2c0c36c0bf64c25b442a94ee /components/compositing/windowing.rs | |
parent | b44d064fae0d96b7564912a10187768f3f4203c4 (diff) | |
download | servo-c2076580f352f3c61f90969e03d78ada609935eb.tar.gz servo-c2076580f352f3c61f90969e03d78ada609935eb.zip |
Add unit test to EmbedderCoordinates (#32314)
* Add unit test to EmbedderCoordinates
* Fix typo
Diffstat (limited to 'components/compositing/windowing.rs')
-rw-r--r-- | components/compositing/windowing.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/components/compositing/windowing.rs b/components/compositing/windowing.rs index bb11457aa47..62d3ee7459c 100644 --- a/components/compositing/windowing.rs +++ b/components/compositing/windowing.rs @@ -263,3 +263,65 @@ impl EmbedderCoordinates { self.flip_rect(&self.get_viewport()) } } + +#[cfg(test)] +mod test { + use euclid::{Point2D, Scale, Size2D}; + use webrender_api::units::DeviceIntRect; + + use super::EmbedderCoordinates; + + #[test] + fn test() { + let pos = Point2D::new(0, 0); + let viewport = Size2D::new(800, 600); + let screen = Size2D::new(1080, 720); + let coordinates = EmbedderCoordinates { + hidpi_factor: Scale::new(1.), + screen, + screen_avail: screen, + window: (viewport, pos), + framebuffer: viewport, + viewport: DeviceIntRect::from_origin_and_size(pos, viewport), + }; + + // Check if viewport conversion is correct. + let viewport = DeviceIntRect::new(Point2D::new(0, 0), Point2D::new(800, 600)); + assert_eq!(coordinates.get_viewport(), viewport); + assert_eq!(coordinates.get_flipped_viewport(), viewport); + + // Check rects with different y positions inside the viewport. + let rect1 = DeviceIntRect::new(Point2D::new(0, 0), Point2D::new(800, 400)); + let rect2 = DeviceIntRect::new(Point2D::new(0, 100), Point2D::new(800, 600)); + let rect3 = DeviceIntRect::new(Point2D::new(0, 200), Point2D::new(800, 500)); + assert_eq!( + coordinates.flip_rect(&rect1), + DeviceIntRect::new(Point2D::new(0, 200), Point2D::new(800, 600)) + ); + assert_eq!( + coordinates.flip_rect(&rect2), + DeviceIntRect::new(Point2D::new(0, 0), Point2D::new(800, 500)) + ); + assert_eq!( + coordinates.flip_rect(&rect3), + DeviceIntRect::new(Point2D::new(0, 100), Point2D::new(800, 400)) + ); + + // Check rects with different x positions. + let rect1 = DeviceIntRect::new(Point2D::new(0, 0), Point2D::new(700, 400)); + let rect2 = DeviceIntRect::new(Point2D::new(100, 100), Point2D::new(800, 600)); + let rect3 = DeviceIntRect::new(Point2D::new(300, 200), Point2D::new(600, 500)); + assert_eq!( + coordinates.flip_rect(&rect1), + DeviceIntRect::new(Point2D::new(0, 200), Point2D::new(700, 600)) + ); + assert_eq!( + coordinates.flip_rect(&rect2), + DeviceIntRect::new(Point2D::new(100, 0), Point2D::new(800, 500)) + ); + assert_eq!( + coordinates.flip_rect(&rect3), + DeviceIntRect::new(Point2D::new(300, 100), Point2D::new(600, 400)) + ); + } +} |