Append renderer name to iced_test snapshots
This commit is contained in:
parent
175a53bc86
commit
5e5c7c85ad
7 changed files with 66 additions and 12 deletions
|
|
@ -109,10 +109,17 @@ pub trait Headless {
|
|||
fn new(
|
||||
default_font: Font,
|
||||
default_text_size: Pixels,
|
||||
backend: Option<&str>,
|
||||
) -> impl Future<Output = Option<Self>>
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Returns the unique name of the renderer.
|
||||
///
|
||||
/// This name may be used by testing libraries to uniquely identify
|
||||
/// snapshots.
|
||||
fn name(&self) -> String;
|
||||
|
||||
/// Draws offscreen into a screenshot, returning a collection of
|
||||
/// bytes representing the rendered pixels in RGBA order.
|
||||
fn screenshot(
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
0e355b080ad33905145e9f70a3b29e2481197c8fc8f42491acd5358238ebbd5f
|
||||
|
|
@ -615,16 +615,23 @@ where
|
|||
async fn new(
|
||||
default_font: Font,
|
||||
default_text_size: Pixels,
|
||||
backend: Option<&str>,
|
||||
) -> Option<Self> {
|
||||
if let Some(renderer) = A::new(default_font, default_text_size).await {
|
||||
if let Some(renderer) =
|
||||
A::new(default_font, default_text_size, backend).await
|
||||
{
|
||||
return Some(Self::Primary(renderer));
|
||||
}
|
||||
|
||||
B::new(default_font, default_text_size)
|
||||
B::new(default_font, default_text_size, backend)
|
||||
.await
|
||||
.map(Self::Secondary)
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
delegate!(self, renderer, renderer.name())
|
||||
}
|
||||
|
||||
fn screenshot(
|
||||
&mut self,
|
||||
size: Size<u32>,
|
||||
|
|
|
|||
|
|
@ -109,6 +109,7 @@ use crate::runtime::UserInterface;
|
|||
use crate::runtime::user_interface;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
|
@ -186,10 +187,16 @@ where
|
|||
load_font(font).expect("Font must be valid");
|
||||
}
|
||||
|
||||
let mut renderer = iced_runtime::futures::futures::executor::block_on(
|
||||
Renderer::new(default_font, settings.default_text_size),
|
||||
)
|
||||
.expect("Create new headless renderer");
|
||||
let mut renderer = {
|
||||
let backend = env::var("ICED_BACKEND").ok();
|
||||
|
||||
iced_runtime::futures::futures::executor::block_on(Renderer::new(
|
||||
default_font,
|
||||
settings.default_text_size,
|
||||
backend.as_deref(),
|
||||
))
|
||||
.expect("Create new headless renderer")
|
||||
};
|
||||
|
||||
let raw = UserInterface::build(
|
||||
element,
|
||||
|
|
@ -457,6 +464,7 @@ where
|
|||
physical_size,
|
||||
f64::from(scale_factor),
|
||||
),
|
||||
renderer: self.renderer.name(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -472,6 +480,7 @@ where
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Snapshot {
|
||||
screenshot: window::Screenshot,
|
||||
renderer: String,
|
||||
}
|
||||
|
||||
impl Snapshot {
|
||||
|
|
@ -481,7 +490,7 @@ impl Snapshot {
|
|||
/// If the PNG image does not exist, it will be created by the [`Snapshot`] for future
|
||||
/// testing and `true` will be returned.
|
||||
pub fn matches_image(&self, path: impl AsRef<Path>) -> Result<bool, Error> {
|
||||
let path = snapshot_path(path, "png");
|
||||
let path = self.path(path, "png");
|
||||
|
||||
if path.exists() {
|
||||
let file = fs::File::open(&path)?;
|
||||
|
|
@ -522,7 +531,7 @@ impl Snapshot {
|
|||
pub fn matches_hash(&self, path: impl AsRef<Path>) -> Result<bool, Error> {
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
let path = snapshot_path(path, "sha256");
|
||||
let path = self.path(path, "sha256");
|
||||
|
||||
let hash = {
|
||||
let mut hasher = Sha256::new();
|
||||
|
|
@ -543,6 +552,20 @@ impl Snapshot {
|
|||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
fn path(&self, path: impl AsRef<Path>, extension: &str) -> PathBuf {
|
||||
let path = path.as_ref();
|
||||
|
||||
path.with_file_name(format!(
|
||||
"{name}-{renderer}",
|
||||
name = path
|
||||
.file_stem()
|
||||
.map(std::ffi::OsStr::to_string_lossy)
|
||||
.unwrap_or_default(),
|
||||
renderer = self.renderer
|
||||
))
|
||||
.with_extension(extension)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the sequence of events of a click.
|
||||
|
|
@ -635,7 +658,3 @@ fn load_font(font: impl Into<Cow<'static, [u8]>>) -> Result<(), Error> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn snapshot_path(path: impl AsRef<Path>, extension: &str) -> PathBuf {
|
||||
path.as_ref().with_extension(extension)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -410,10 +410,21 @@ impl renderer::Headless for Renderer {
|
|||
async fn new(
|
||||
default_font: Font,
|
||||
default_text_size: Pixels,
|
||||
backend: Option<&str>,
|
||||
) -> Option<Self> {
|
||||
if backend.is_some_and(|backend| {
|
||||
!["tiny-skia", "tiny_skia"].contains(&backend)
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Self::new(default_font, default_text_size))
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
"tiny-skia".to_owned()
|
||||
}
|
||||
|
||||
fn screenshot(
|
||||
&mut self,
|
||||
size: Size<u32>,
|
||||
|
|
|
|||
|
|
@ -815,7 +815,12 @@ impl renderer::Headless for Renderer {
|
|||
async fn new(
|
||||
default_font: Font,
|
||||
default_text_size: Pixels,
|
||||
backend: Option<&str>,
|
||||
) -> Option<Self> {
|
||||
if backend.is_some_and(|backend| backend != "wgpu") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
|
||||
backends: wgpu::Backends::from_env()
|
||||
.unwrap_or(wgpu::Backends::PRIMARY),
|
||||
|
|
@ -862,6 +867,10 @@ impl renderer::Headless for Renderer {
|
|||
Some(Self::new(engine, default_font, default_text_size))
|
||||
}
|
||||
|
||||
fn name(&self) -> String {
|
||||
"wgpu".to_owned()
|
||||
}
|
||||
|
||||
fn screenshot(
|
||||
&mut self,
|
||||
size: Size<u32>,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue