use renderer_core::TextWrap;
use telar::testing::mount;
use telar::{
Color, Component, Container, DrawCommand, FontFamily, LayoutItem, LayoutStyle, Raster,
RenderNode, Text, TextAlign, TextStyle, box_item,
};
fn resolved(build: impl FnOnce() -> Box<dyn LayoutItem>) -> TextStyle {
struct Root(Box<dyn LayoutItem>);
impl Component for Root {
fn view(&self) -> RenderNode {
self.0.view()
}
fn on_event(&mut self, _: &platform_core::Event) -> ui_core::EventResult {
ui_core::EventResult::Ignored
}
fn debug_name(&self) -> &'static str {
"BaselineRoot"
}
}
telar::install_default_text_metrics();
let tree = mount(Root(build()), 400, 200);
let style = tree.commands().iter().find_map(|c| match c {
DrawCommand::Text { style, .. } => Some((**style).clone()),
_ => None,
});
style.expect("the tree drew a text command")
}
#[test]
fn an_undeclared_text_style_resolves_to_these_exact_values() {
let style = resolved(|| {
box_item(Text::declaring(|| "baseline".to_string(), LayoutStyle::new(), |t| t).unwrap())
});
assert_eq!(style.font_size, 14.0);
assert_eq!(
style.color,
telar::Paint::Solid(Color::rgba(0.15, 0.15, 0.2, 1.0))
);
assert_eq!(style.font_family, FontFamily::SansSerif);
assert_eq!(style.font_weight, 400);
assert_eq!(style.font_style, telar::FontStyle::Normal);
assert_eq!(style.text_align, TextAlign::Start);
assert_eq!(style.clamp, telar::Clamp::None);
assert_eq!(style.line_height, telar::LineHeight::Natural);
assert_eq!(style.letter_spacing, 0.0);
assert_eq!(style.raster, Raster::Smooth);
assert_eq!(style.text_wrap, telar::TextWrap::Wrap);
assert_eq!(style.text_shadow, telar::TextShadow::None);
}
#[test]
fn a_declared_text_style_reaches_the_draw_command_intact() {
let declared = TextStyle::new(11.0, Color::WHITE)
.with_font_weight(700)
.with_font_style(telar::FontStyle::Italic)
.with_text_align(TextAlign::Center)
.with_clamp(2, true)
.with_line_height(1.5)
.with_letter_spacing(0.5)
.with_raster(Raster::Pixel)
.with_text_wrap(TextWrap::NoWrap)
.with_font_family("Some Face");
let expected = declared.clone();
let style = resolved(move || {
let declared = declared.clone();
box_item(
Text::new(
|| "declared".to_string(),
LayoutStyle::new(),
move || declared.clone(),
)
.unwrap(),
)
});
assert_eq!(style, expected);
}
#[test]
fn nesting_changes_nothing_about_a_text_style() {
let style = resolved(|| {
let leaf = Text::declaring(|| "nested".to_string(), LayoutStyle::new(), |t| t).unwrap();
let inner = Container::new(LayoutStyle::new().flex_column(), vec![box_item(leaf)]).unwrap();
let outer =
Container::new(LayoutStyle::new().flex_column(), vec![box_item(inner)]).unwrap();
box_item(outer)
});
assert_eq!(style.font_size, 14.0);
assert_eq!(style.font_weight, 400);
assert_eq!(style.font_family, FontFamily::SansSerif);
assert_eq!(style.raster, Raster::Smooth);
}
#[test]
fn the_markup_default_and_the_catalogue_default_are_one_number() {
#[derive(Clone)]
struct Eleven;
impl telar::ThemeTokens for Eleven {
fn root(&self) -> telar::Declared {
telar::Declared::default().with_font_size(11.0)
}
}
#[derive(Clone)]
struct Silent;
impl telar::ThemeTokens for Silent {}
telar::set_theme(Eleven);
let markup = resolved(|| {
box_item(Text::declaring(|| "label".to_string(), LayoutStyle::new(), |t| t).unwrap())
});
let catalogue = telar::Inherited::initial().text.font_size;
telar::set_theme(Silent);
assert_eq!(
markup.font_size, 11.0,
"a `text` naming no size takes the theme's"
);
assert_eq!(
catalogue, markup.font_size,
"and it is the same row the catalogue resolves against"
);
}
#[test]
fn a_container_declares_the_text_below_it() {
let style = resolved(|| {
let leaf = Text::declaring(|| "under".to_string(), LayoutStyle::new(), |t| t).unwrap();
let outer = Container::new(LayoutStyle::new().flex_column(), vec![box_item(leaf)])
.unwrap()
.declaring(|| {
telar::Declared::default()
.with_font_size(11.0)
.with_raster(Raster::Pixel)
});
box_item(outer)
});
assert_eq!(style.font_size, 11.0);
assert_eq!(style.raster, Raster::Pixel);
assert_eq!(
style.font_weight, 400,
"and nothing the declaration did not name"
);
}
#[test]
fn a_container_can_say_what_the_text_below_it_looks_like() {
telar::install_default_text_metrics();
struct Root(Box<dyn LayoutItem>);
impl Component for Root {
fn view(&self) -> RenderNode {
self.0.view()
}
fn on_event(&mut self, _: &platform_core::Event) -> ui_core::EventResult {
ui_core::EventResult::Ignored
}
fn debug_name(&self) -> &'static str {
"DeclaringRoot"
}
}
let leaf = Text::declaring(|| "inherits".to_string(), LayoutStyle::new(), |t| t).unwrap();
let outer = Container::new(LayoutStyle::new().flex_column(), vec![box_item(leaf)]).unwrap();
let outer_node = outer.layout_node();
telar::declare(
outer_node,
telar::Declared::default()
.with_font_size(11.0)
.with_font_weight(700),
);
let tree = mount(Root(box_item(outer)), 400, 200);
let drawn = |tree: &ui_core::ComponentList| {
tree.commands()
.iter()
.find_map(|c| match c {
DrawCommand::Text { style, .. } => Some((**style).clone()),
_ => None,
})
.expect("the tree drew a text command")
};
let style = drawn(&tree);
assert_eq!(style.font_size, 11.0, "the leaf takes the declared size");
assert_eq!(style.font_weight, 700, "and the declared weight");
assert_eq!(
style.raster,
telar::Raster::Smooth,
"and keeps the initial value of everything the declaration did not name"
);
ui_core::declare(outer_node, telar::Declared::default().with_font_size(9.0));
assert_eq!(
drawn(&tree).font_size,
9.0,
"a changed declaration repaints"
);
telar::undeclare(outer_node);
assert_eq!(
drawn(&tree).font_size,
telar::Inherited::initial().text.font_size,
"and withdrawing it returns the leaf to the initial row"
);
}
#[test]
fn a_themes_root_row_reaches_a_text_that_declares_nothing() {
#[derive(Clone)]
struct Serif;
impl telar::ThemeTokens for Serif {
fn root(&self) -> telar::Declared {
telar::Declared::default()
.with_font_family("Iosevka")
.with_line_height(1.75)
}
}
telar::set_theme(Serif);
let style = resolved(|| {
box_item(Text::declaring(|| "themed".to_string(), LayoutStyle::new(), |t| t).unwrap())
});
assert_eq!(style.font_family, FontFamily::from("Iosevka"));
assert_eq!(style.line_height, telar::LineHeight::Times(1.75));
assert_eq!(
style.font_size, 14.0,
"what the row leaves unsaid still comes from the tokens under it"
);
}