use taim_core::Size;
use taim_core::cache::{Budget, Class};
use taim_core::text::fallback::{FallbackChain, Script};
use taim_core::text::paragraph::Direction;
use taim_core::text::{Alignment, Ellipsis, Shaping, Wrapping};
use taim_graphics::text::cache::{Cache, Key};
use taim_graphics::text::cosmic_text;
use taim_wgpu::resource_budget::{Pressure, Tracker};
#[test]
fn a_tracker_reports_normal_pressure_for_a_realistic_light_workload() {
let mut tracker = Tracker::new();
tracker.report(Class::Essential, 4 * 1024 * 1024);
tracker.report(Class::Retained, 2 * 1024 * 1024);
assert_eq!(tracker.pressure(), Pressure::Normal);
}
#[test]
fn a_tracker_escalates_pressure_as_reported_usage_climbs_toward_the_limit() {
let mut tracker = Tracker::with_limit(1_000_000);
tracker.report(Class::Essential, 500_000);
assert_eq!(tracker.pressure(), Pressure::Normal);
tracker.report(Class::Essential, 750_000);
assert_eq!(tracker.pressure(), Pressure::Elevated);
tracker.report(Class::Essential, 950_000);
assert_eq!(tracker.pressure(), Pressure::Critical);
}
fn key(content: &str) -> Key<'_> {
Key {
content,
size: 16.0,
line_height: 20.0,
font: taim_core::Font::default(),
bounds: Size::new(200.0, 200.0),
shaping: Shaping::Basic,
align_x: Alignment::default(),
wrapping: Wrapping::default(),
ellipsis: Ellipsis::default(),
locale: None,
direction: Direction::Auto,
script: Script::Latin,
fallback: FallbackChain::for_script(Script::Latin),
axes: &[],
}
}
#[test]
fn a_text_cache_under_a_tight_budget_never_loses_an_entry_still_needed_this_frame() {
let mut cache = Cache::with_budget(Budget::Bytes(1));
let mut font_system = cosmic_text::FontSystem::new();
let (hash_a, _) = cache.allocate(&mut font_system, key("first entry"));
let (hash_b, _) = cache.allocate(&mut font_system, key("second entry"));
let (hash_c, _) = cache.allocate(&mut font_system, key("third entry"));
assert!(cache.get(&hash_a).is_some());
assert!(cache.get(&hash_b).is_some());
assert!(cache.get(&hash_c).is_some());
}
#[test]
fn a_text_cache_evicts_across_frames_once_the_budget_is_exceeded() {
let mut cache = Cache::with_budget(Budget::Bytes(400));
let mut font_system = cosmic_text::FontSystem::new();
let (first_hash, _) = cache.allocate(&mut font_system, key("aaaaaaaaaa"));
assert!(cache.get(&first_hash).is_some());
cache.trim();
let (second_hash, _) = cache.allocate(&mut font_system, key("bbbbbbbbbb"));
assert!(
cache.get(&second_hash).is_some(),
"the newly allocated entry must always be present"
);
assert!(
cache.current_bytes() <= 400,
"current_bytes ({}) must respect the configured budget once eviction has a chance to run",
cache.current_bytes()
);
}