feat: Add a function for retrieving the window contents

This function is useful for testing the window contents in certain cases. In addition,
this means that we can now have reliable tests for softbuffer's actual functionality.

Signed-off-by: John Nunley <jtnunley01@gmail.com>
Co-authored-by: dAxpeDDa <daxpedda@gmail.com>
This commit is contained in:
John Nunley 2023-06-01 20:09:30 -07:00 committed by GitHub
parent daf304adf9
commit 44248477be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 258 additions and 22 deletions

View file

@ -0,0 +1,56 @@
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use winit::event_loop::EventLoopWindowTarget;
fn all_red(elwt: &EventLoopWindowTarget<()>) {
let window = winit::window::WindowBuilder::new()
.with_title("all_red")
.build(elwt)
.unwrap();
#[cfg(target_arch = "wasm32")]
{
use winit::platform::web::WindowExtWebSys;
web_sys::window()
.unwrap()
.document()
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas())
.unwrap();
}
// winit does not wait for the window to be mapped... sigh
#[cfg(not(target_arch = "wasm32"))]
std::thread::sleep(std::time::Duration::from_millis(1));
let context = unsafe { Context::new(elwt) }.unwrap();
let mut surface = unsafe { Surface::new(&context, &window) }.unwrap();
let size = window.inner_size();
// Set the size of the surface to the size of the window.
surface
.resize(
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();
// Set all pixels to red.
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(0x00FF0000);
buffer.present().unwrap();
// Check that all pixels are red.
let screen_contents = match surface.fetch() {
Err(softbuffer::SoftBufferError::Unimplemented) => return,
cont => cont.unwrap(),
};
for pixel in screen_contents.iter() {
assert_eq!(*pixel, 0x00FF0000);
}
}
winit_test::main!(all_red);