|
| 1 | +// Copyright 2024 the Parley Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | + |
| 4 | +use crate::tests::utils::renderer::render_layout; |
| 5 | +use crate::{ |
| 6 | + FontContext, FontFamily, FontStack, Layout, LayoutContext, RangedBuilder, StyleProperty, |
| 7 | +}; |
| 8 | +use fontique::{Collection, CollectionOptions}; |
| 9 | +use peniko::Color; |
| 10 | +use std::path::{Path, PathBuf}; |
| 11 | +use tiny_skia::Pixmap; |
| 12 | + |
| 13 | +// Creates a new instance of TestEnv and put current function name in constructor |
| 14 | +#[macro_export] |
| 15 | +macro_rules! testenv { |
| 16 | + () => {{ |
| 17 | + // Get name of the current function |
| 18 | + fn f() {} |
| 19 | + fn type_name_of<T>(_: T) -> &'static str { |
| 20 | + std::any::type_name::<T>() |
| 21 | + } |
| 22 | + let name = type_name_of(f); |
| 23 | + let name = &name[..name.len() - 3]; |
| 24 | + let name = &name[name.rfind(':').map(|x| x + 1).unwrap_or(0)..]; |
| 25 | + |
| 26 | + // Create test env |
| 27 | + $crate::tests::utils::TestEnv::new(name) |
| 28 | + }}; |
| 29 | +} |
| 30 | + |
| 31 | +fn current_imgs_dir() -> PathBuf { |
| 32 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 33 | + .join("tests") |
| 34 | + .join("current") |
| 35 | +} |
| 36 | + |
| 37 | +fn snapshot_dir() -> PathBuf { |
| 38 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 39 | + .join("tests") |
| 40 | + .join("snapshots") |
| 41 | +} |
| 42 | + |
| 43 | +fn font_dir() -> PathBuf { |
| 44 | + Path::new(env!("CARGO_MANIFEST_DIR")) |
| 45 | + .join("tests") |
| 46 | + .join("assets") |
| 47 | + .join("roboto_fonts") |
| 48 | +} |
| 49 | + |
| 50 | +const DEFAULT_FONT_NAME: &str = "Roboto"; |
| 51 | + |
| 52 | +pub(crate) struct TestEnv { |
| 53 | + test_name: String, |
| 54 | + check_counter: u32, |
| 55 | + font_cx: FontContext, |
| 56 | + layout_cx: LayoutContext<Color>, |
| 57 | + foreground_color: Color, |
| 58 | + background_color: Color, |
| 59 | + tolerance: f32, |
| 60 | + errors: Vec<(PathBuf, String)>, |
| 61 | +} |
| 62 | + |
| 63 | +fn is_accept_mode() -> bool { |
| 64 | + std::env::var("PARLEY_TEST") |
| 65 | + .map(|x| x.to_ascii_lowercase() == "accept") |
| 66 | + .unwrap_or(false) |
| 67 | +} |
| 68 | + |
| 69 | +pub(crate) fn load_fonts_dir(collection: &mut Collection, path: &Path) -> std::io::Result<()> { |
| 70 | + let paths = std::fs::read_dir(path)?; |
| 71 | + for entry in paths { |
| 72 | + let entry = entry?; |
| 73 | + if !entry.metadata()?.is_file() { |
| 74 | + continue; |
| 75 | + } |
| 76 | + let path = entry.path(); |
| 77 | + if path |
| 78 | + .extension() |
| 79 | + .and_then(|ext| ext.to_str()) |
| 80 | + .map(|ext| !["ttf", "otf", "ttc", "otc"].contains(&ext)) |
| 81 | + .unwrap_or(true) |
| 82 | + { |
| 83 | + continue; |
| 84 | + } |
| 85 | + let font_data = std::fs::read(&path)?; |
| 86 | + collection.register_fonts(font_data); |
| 87 | + } |
| 88 | + Ok(()) |
| 89 | +} |
| 90 | + |
| 91 | +impl TestEnv { |
| 92 | + pub(crate) fn new(test_name: &str) -> Self { |
| 93 | + let file_prefix = format!("{}-", test_name); |
| 94 | + let entries = std::fs::read_dir(current_imgs_dir()).unwrap(); |
| 95 | + for entry in entries.flatten() { |
| 96 | + let path = entry.path(); |
| 97 | + if path |
| 98 | + .file_name() |
| 99 | + .and_then(|name| name.to_str()) |
| 100 | + .map(|name| name.starts_with(&file_prefix) && name.ends_with(".png")) |
| 101 | + .unwrap_or(false) |
| 102 | + { |
| 103 | + std::fs::remove_file(&path).unwrap(); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + let mut collection = Collection::new(CollectionOptions { |
| 108 | + shared: false, |
| 109 | + system_fonts: false, |
| 110 | + }); |
| 111 | + load_fonts_dir(&mut collection, &font_dir()).unwrap(); |
| 112 | + collection |
| 113 | + .family_id(DEFAULT_FONT_NAME) |
| 114 | + .unwrap_or_else(|| panic!("{} font not found", DEFAULT_FONT_NAME)); |
| 115 | + TestEnv { |
| 116 | + test_name: test_name.to_string(), |
| 117 | + check_counter: 0, |
| 118 | + font_cx: FontContext { |
| 119 | + collection, |
| 120 | + source_cache: Default::default(), |
| 121 | + }, |
| 122 | + tolerance: 0.0, |
| 123 | + layout_cx: LayoutContext::new(), |
| 124 | + foreground_color: Color::rgb8(0, 0, 0), |
| 125 | + background_color: Color::rgb8(255, 255, 255), |
| 126 | + errors: Vec::new(), |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + pub(crate) fn builder<'a>(&'a mut self, text: &'a str) -> RangedBuilder<'a, Color> { |
| 131 | + let mut builder = self.layout_cx.ranged_builder(&mut self.font_cx, text, 1.0); |
| 132 | + builder.push_default(StyleProperty::Brush(self.foreground_color)); |
| 133 | + builder.push_default(StyleProperty::FontStack(FontStack::Single( |
| 134 | + FontFamily::Named(DEFAULT_FONT_NAME.into()), |
| 135 | + ))); |
| 136 | + builder |
| 137 | + } |
| 138 | + |
| 139 | + fn image_name(&mut self, test_case_name: &str) -> String { |
| 140 | + if test_case_name.is_empty() { |
| 141 | + let name = format!("{}-{}.png", self.test_name, self.check_counter); |
| 142 | + self.check_counter += 1; |
| 143 | + name |
| 144 | + } else { |
| 145 | + assert!(test_case_name |
| 146 | + .chars() |
| 147 | + .all(|c| c == '_' || char::is_alphanumeric(c))); |
| 148 | + format!("{}-{}.png", self.test_name, test_case_name) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + fn check_images(&self, current_img: &Pixmap, snapshot_path: &Path) -> Result<(), String> { |
| 153 | + if !snapshot_path.is_file() { |
| 154 | + return Err(format!("Cannot find snapshot {}", snapshot_path.display())); |
| 155 | + } |
| 156 | + let snapshot_img = Pixmap::load_png(snapshot_path) |
| 157 | + .map_err(|_| format!("Loading snapshot {} failed", snapshot_path.display()))?; |
| 158 | + if snapshot_img.width() != current_img.width() |
| 159 | + || snapshot_img.height() != current_img.height() |
| 160 | + { |
| 161 | + return Err(format!( |
| 162 | + "Snapshot has different size: snapshot {}x{}; generated image: {}x{}", |
| 163 | + snapshot_img.width(), |
| 164 | + snapshot_img.height(), |
| 165 | + current_img.width(), |
| 166 | + current_img.height() |
| 167 | + )); |
| 168 | + } |
| 169 | + |
| 170 | + let mut n_different_pixels = 0; |
| 171 | + let mut color_cumulative_difference = 0.0; |
| 172 | + for (pixel1, pixel2) in snapshot_img.pixels().iter().zip(current_img.pixels()) { |
| 173 | + if pixel1 != pixel2 { |
| 174 | + n_different_pixels += 1; |
| 175 | + } |
| 176 | + let diff_r = (pixel1.red() as f32 - pixel2.red() as f32).abs(); |
| 177 | + let diff_g = (pixel1.green() as f32 - pixel2.green() as f32).abs(); |
| 178 | + let diff_b = (pixel1.blue() as f32 - pixel2.blue() as f32).abs(); |
| 179 | + color_cumulative_difference += diff_r.max(diff_g).max(diff_b); |
| 180 | + } |
| 181 | + if color_cumulative_difference > self.tolerance { |
| 182 | + return Err(format!( |
| 183 | + "Testing image differs in {n_different_pixels} pixels (color difference = {color_cumulative_difference})", |
| 184 | + )); |
| 185 | + } |
| 186 | + Ok(()) |
| 187 | + } |
| 188 | + |
| 189 | + pub(crate) fn check_snapshot_with_name( |
| 190 | + &mut self, |
| 191 | + test_case_name: &str, |
| 192 | + layout: &Layout<Color>, |
| 193 | + ) { |
| 194 | + let current_img = render_layout(layout, self.background_color, self.foreground_color); |
| 195 | + let image_name = self.image_name(test_case_name); |
| 196 | + |
| 197 | + let snapshot_path = snapshot_dir().join(&image_name); |
| 198 | + let comparison_path = current_imgs_dir().join(&image_name); |
| 199 | + |
| 200 | + if let Err(e) = self.check_images(¤t_img, &snapshot_path) { |
| 201 | + if is_accept_mode() { |
| 202 | + current_img.save_png(&snapshot_path).unwrap(); |
| 203 | + } else { |
| 204 | + current_img.save_png(&comparison_path).unwrap(); |
| 205 | + self.errors.push((comparison_path, e)); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + pub(crate) fn check_snapshot(&mut self, layout: &Layout<Color>) { |
| 211 | + self.check_snapshot_with_name("", layout); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +impl Drop for TestEnv { |
| 216 | + // Dropping of TestEnv cause panic (if there is not already one) |
| 217 | + // We do not panic immediately when error is detected because we want to |
| 218 | + // generate all images in the test and do visual confirmation of the whole |
| 219 | + // set and not stop at the first error. |
| 220 | + fn drop(&mut self) { |
| 221 | + if !self.errors.is_empty() && !std::thread::panicking() { |
| 222 | + use std::fmt::Write; |
| 223 | + let mut panic_msg = String::new(); |
| 224 | + for (path, msg) in &self.errors { |
| 225 | + write!( |
| 226 | + &mut panic_msg, |
| 227 | + "{}\nImage written into: {}\n", |
| 228 | + msg, |
| 229 | + path.display() |
| 230 | + ) |
| 231 | + .unwrap(); |
| 232 | + } |
| 233 | + panic!("{}", &panic_msg); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments