use slate_framework::{AnyElement, HeadlessApp, Text};
#[test]
fn cache_misses_on_first_render() {
let mut app = HeadlessApp::new(200, 50).expect("headless app");
let text = Text::new("Hello, World!").font_size(14.0);
let _img = app.render(AnyElement::new(text)).expect("render");
assert_eq!(app.text_shaping_cache_misses(), 1);
assert_eq!(app.text_shaping_cache_hits(), 0);
}
#[test]
fn cache_hits_on_second_render_same_content() {
let mut app = HeadlessApp::new(200, 50).expect("headless app");
let text1 = Text::new("Hello, World!").font_size(14.0);
let _img1 = app.render(AnyElement::new(text1)).expect("first render");
let misses_after_first = app.text_shaping_cache_misses();
let hits_after_first = app.text_shaping_cache_hits();
let text2 = Text::new("Hello, World!").font_size(14.0);
let _img2 = app.render(AnyElement::new(text2)).expect("second render");
assert_eq!(app.text_shaping_cache_hits(), hits_after_first + 1);
assert_eq!(app.text_shaping_cache_misses(), misses_after_first);
}
#[test]
fn cache_misses_after_content_change() {
let mut app = HeadlessApp::new(200, 50).expect("headless app");
let text1 = Text::new("Hello, World!").font_size(14.0);
let _img1 = app.render(AnyElement::new(text1)).expect("first render");
let misses_after_first = app.text_shaping_cache_misses();
let text2 = Text::new("Goodbye, World!").font_size(14.0);
let _img2 = app.render(AnyElement::new(text2)).expect("second render");
assert_eq!(app.text_shaping_cache_misses(), misses_after_first + 1);
}
#[test]
fn cache_misses_after_style_change() {
let mut app = HeadlessApp::new(200, 50).expect("headless app");
let text1 = Text::new("Hello").font_size(14.0);
let _img1 = app.render(AnyElement::new(text1)).expect("first render");
let misses_after_first = app.text_shaping_cache_misses();
let hits_after_first = app.text_shaping_cache_hits();
let text2 = Text::new("Hello").font_size(20.0);
let _img2 = app.render(AnyElement::new(text2)).expect("second render");
assert_eq!(app.text_shaping_cache_misses(), misses_after_first + 1);
assert_eq!(app.text_shaping_cache_hits(), hits_after_first);
}
#[test]
fn cache_entry_survives_across_frames() {
let mut app = HeadlessApp::new(200, 50).expect("headless app");
for i in 0..5 {
let text = Text::new("Stable content").font_size(14.0);
let _img = app
.render(AnyElement::new(text))
.unwrap_or_else(|_| panic!("render {}", i));
}
assert_eq!(app.text_shaping_cache_misses(), 1);
assert_eq!(app.text_shaping_cache_hits(), 4);
assert_eq!(app.text_shaping_cache_len(), 1);
}
#[test]
fn same_tree_position_replaces_cache_entry() {
let mut app = HeadlessApp::new(300, 100).expect("headless app");
let text1 = Text::new("First text").font_size(14.0);
let _img1 = app.render(AnyElement::new(text1)).expect("first render");
let text2 = Text::new("Second text").font_size(14.0);
let _img2 = app.render(AnyElement::new(text2)).expect("second render");
assert_eq!(app.text_shaping_cache_misses(), 2);
assert_eq!(app.text_shaping_cache_len(), 1);
let text2_again = Text::new("Second text").font_size(14.0);
let _img3 = app
.render(AnyElement::new(text2_again))
.expect("third render");
assert_eq!(app.text_shaping_cache_hits(), 1);
let text1_again = Text::new("First text").font_size(14.0);
let _img4 = app
.render(AnyElement::new(text1_again))
.expect("fourth render");
assert_eq!(app.text_shaping_cache_misses(), 3);
}