Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(rendering): check the text cache against each shaping input

Scott Newton00b2e290d058
diff --git a/crates/rendering/src/layers/text.rs b/crates/rendering/src/layers/text.rs
index fe4e40ed5..ebcbb6fc3 100644
--- a/crates/rendering/src/layers/text.rs
+++ b/crates/rendering/src/layers/text.rs
@@ -1054,6 +1054,66 @@ mod gpu_tests {
assert_eq!(after_edit, fresh(&frame));
}
+ #[test]
+ #[ignore = "requires a working wgpu adapter"]
+ fn reused_layouts_follow_each_shaping_input() {
+ type Edit = (&'static str, fn(&mut PreparedText));
+ let (device, queue) = device().expect("A working wgpu adapter is required for this test");
+ let base = PreparedText {
+ color: [0.0, 0.0, 0.0, 1.0],
+ background_color: None,
+ content: "Hi yo\ngq".to_string(),
+ font_size: 16.0,
+ line_height: 1.25,
+ ..text([4.0, 4.0, 60.0, 60.0], [0.0; 4])
+ };
+ let edits: [Edit; 8] = [
+ ("font size", |text| {
+ text.font_size = 20.0;
+ text.line_height = 1.0;
+ }),
+ ("font family", |text| {
+ text.font_family = "monospace".to_string()
+ }),
+ ("font weight", |text| text.font_weight = 800.0),
+ ("italic", |text| text.italic = true),
+ ("letter spacing", |text| text.letter_spacing = 3.0),
+ ("alignment", |text| text.align = TextAlign::Right),
+ ("line height", |text| text.line_height = 2.0),
+ ("wrap width", |text| text.bounds[2] = 24.0),
+ ];
+ let render_fresh = |text: &PreparedText| {
+ let mut layer = TextLayer::new(&device, &queue);
+ layer.prepare(
+ &device,
+ &queue,
+ (OUTPUT, OUTPUT),
+ std::slice::from_ref(text),
+ );
+ render_pixels(&device, &queue, &layer)
+ };
+ let base_pixels = render_fresh(&base);
+ assert!(base_pixels.chunks_exact(4).any(|pixel| pixel != [255; 4]));
+
+ for (name, edit) in edits {
+ let mut edited = base.clone();
+ edit(&mut edited);
+ let expected = render_fresh(&edited);
+ assert!(
+ expected != base_pixels,
+ "{name} edit should change the output"
+ );
+
+ let mut reused = TextLayer::new(&device, &queue);
+ reused.prepare(&device, &queue, (OUTPUT, OUTPUT), &[base.clone()]);
+ reused.prepare(&device, &queue, (OUTPUT, OUTPUT), &[edited]);
+ assert!(
+ render_pixels(&device, &queue, &reused) == expected,
+ "{name} edit reused a stale layout"
+ );
+ }
+ }
+
#[test]
#[ignore = "requires a working wgpu adapter"]
fn text_background_timeline_preserves_order_and_shrinks_without_stale_layers() {