From b643844d908edb6fa43da7f1f84a4016e7505456 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Tue, 25 Jun 2024 16:49:06 +0200 Subject: [PATCH] Add a test that verifies that grab_window() returns something when using the Skia OpenGL or FemtoVG renderer --- internal/renderers/femtovg/Cargo.toml | 9 ++++ .../renderers/femtovg/tests/screenshot.rs | 48 +++++++++++++++++++ internal/renderers/skia/Cargo.toml | 9 ++++ internal/renderers/skia/tests/screenshot.rs | 48 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 internal/renderers/femtovg/tests/screenshot.rs create mode 100644 internal/renderers/skia/tests/screenshot.rs diff --git a/internal/renderers/femtovg/Cargo.toml b/internal/renderers/femtovg/Cargo.toml index 2781386a062..48c1bfab369 100644 --- a/internal/renderers/femtovg/Cargo.toml +++ b/internal/renderers/femtovg/Cargo.toml @@ -52,3 +52,12 @@ core-text = { version = "20.1.0" } [target.'cfg(target_arch = "wasm32")'.dependencies] web-sys = { version = "0.3", features=["console", "WebGlContextAttributes", "CanvasRenderingContext2d", "HtmlInputElement", "HtmlCanvasElement", "Window", "Document"] } wasm-bindgen = { version = "0.2" } + +[dev-dependencies] +slint = { workspace = true, default-features = true } +i-slint-backend-winit = { workspace = true, features = ["renderer-femtovg"] } + +[[test]] +name = "screenshot" +path = "tests/screenshot.rs" +harness = false diff --git a/internal/renderers/femtovg/tests/screenshot.rs b/internal/renderers/femtovg/tests/screenshot.rs new file mode 100644 index 00000000000..e0c20bf3ce1 --- /dev/null +++ b/internal/renderers/femtovg/tests/screenshot.rs @@ -0,0 +1,48 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +fn main() { + slint::platform::set_platform(Box::new( + i_slint_backend_winit::Backend::new_with_renderer_by_name(Some("femtovg")).unwrap(), + )) + .unwrap(); + + slint::slint! { + export component App inherits Window { + Text { text: "Ok"; } + } + } + + let app = App::new().unwrap(); + let slint_window = app.window(); + let app_weak = app.as_weak(); + + let mut rendered_once = false; + let screenshot = std::rc::Rc::new(std::cell::RefCell::new(None)); + + slint_window + .set_rendering_notifier({ + let screenshot = screenshot.clone(); + move |state, _| match state { + slint::RenderingState::BeforeRendering => { + if rendered_once { + *screenshot.borrow_mut() = Some(app_weak.unwrap().window().grab_window()); + slint::quit_event_loop().unwrap(); + } + } + slint::RenderingState::AfterRendering => { + rendered_once = true; + app_weak.unwrap().window().request_redraw(); + } + _ => {} + } + }) + .unwrap(); + + app.show().unwrap(); + app.run().unwrap(); + + let screenshot = screenshot.borrow_mut().take().unwrap().unwrap(); + assert!(screenshot.width() > 0); + assert!(screenshot.height() > 0); +} diff --git a/internal/renderers/skia/Cargo.toml b/internal/renderers/skia/Cargo.toml index 943dac4ab5a..faa042130e1 100644 --- a/internal/renderers/skia/Cargo.toml +++ b/internal/renderers/skia/Cargo.toml @@ -75,3 +75,12 @@ skia-safe = { version = "0.75.0", features = ["gl"] } [build-dependencies] cfg_aliases = { workspace = true } + +[dev-dependencies] +slint = { workspace = true, default-features = true } +i-slint-backend-winit = { workspace = true, features = ["renderer-skia"] } + +[[test]] +name = "screenshot" +path = "tests/screenshot.rs" +harness = false diff --git a/internal/renderers/skia/tests/screenshot.rs b/internal/renderers/skia/tests/screenshot.rs new file mode 100644 index 00000000000..0c8d00d405f --- /dev/null +++ b/internal/renderers/skia/tests/screenshot.rs @@ -0,0 +1,48 @@ +// Copyright © SixtyFPS GmbH +// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 + +fn main() { + slint::platform::set_platform(Box::new( + i_slint_backend_winit::Backend::new_with_renderer_by_name(Some("skia-opengl")).unwrap(), + )) + .unwrap(); + + slint::slint! { + export component App inherits Window { + Text { text: "Ok"; } + } + } + + let app = App::new().unwrap(); + let slint_window = app.window(); + let app_weak = app.as_weak(); + + let mut rendered_once = false; + let screenshot = std::rc::Rc::new(std::cell::RefCell::new(None)); + + slint_window + .set_rendering_notifier({ + let screenshot = screenshot.clone(); + move |state, _| match state { + slint::RenderingState::BeforeRendering => { + if rendered_once { + *screenshot.borrow_mut() = Some(app_weak.unwrap().window().grab_window()); + slint::quit_event_loop().unwrap(); + } + } + slint::RenderingState::AfterRendering => { + rendered_once = true; + app_weak.unwrap().window().request_redraw(); + } + _ => {} + } + }) + .unwrap(); + + app.show().unwrap(); + app.run().unwrap(); + + let screenshot = screenshot.borrow_mut().take().unwrap().unwrap(); + assert!(screenshot.width() > 0); + assert!(screenshot.height() > 0); +}