use taim_design::components::button::ButtonKind;
use taim_design::theme::Theme;
use taim_design::tokens::{Contrast, MaterialTokens, Scheme};
use taim_core::material::{Material, SceneLight};
use taim_core::{Background, Color};
use taim_widget::taim;
use taim_widget::{
button, checkbox, container, icon_button, list_tile, progress_bar, radio, text_input,
};
const EVERY_SCHEME: [Scheme; 3] = [Scheme::Light, Scheme::Dark, Scheme::HighContrast];
fn color_of(background: Background) -> Color {
match background {
Background::Color(color) => color,
Background::Gradient(_) => {
panic!("Taim only ever produces solid colour backgrounds")
}
}
}
#[test]
fn a_real_button_renders_taims_primary_token() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let class = <Theme as button::Catalog>::default();
let style = button::Catalog::style(&theme, &class, button::Status::Active);
assert_eq!(
style.background.map(color_of),
Some(theme.colors().primary),
"{scheme:?}: a filled button must paint the primary token"
);
assert_eq!(style.text_color, theme.colors().on_primary);
}
}
#[test]
fn a_real_button_is_readable_in_every_scheme() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let class = <Theme as button::Catalog>::default();
let style = button::Catalog::style(&theme, &class, button::Status::Active);
let background = style.background.map(color_of).expect("filled button");
assert!(
Contrast::body_text(style.text_color, background),
"{scheme:?}: rendered button text fails contrast (ratio {:.2})",
Contrast::ratio(style.text_color, background)
);
}
}
#[test]
fn a_real_buttons_state_layer_deepens_from_rest_to_hover_to_press() {
let theme = Theme::new(Scheme::Dark);
let class = <Theme as button::Catalog>::default();
let rest = button::Catalog::style(&theme, &class, button::Status::Active);
let hovered = button::Catalog::style(&theme, &class, button::Status::Hovered);
let pressed = button::Catalog::style(&theme, &class, button::Status::Pressed);
let rest_bg = rest.background.map(color_of).unwrap();
let hovered_bg = hovered.background.map(color_of).unwrap();
let pressed_bg = pressed.background.map(color_of).unwrap();
assert_ne!(rest_bg, hovered_bg, "hover must be visible");
assert_ne!(hovered_bg, pressed_bg, "press must differ from hover");
}
#[test]
fn a_disabled_real_button_fades_rather_than_changing_hue() {
let theme = Theme::new(Scheme::Dark);
let class = <Theme as button::Catalog>::default();
let active = button::Catalog::style(&theme, &class, button::Status::Active);
let disabled = button::Catalog::style(&theme, &class, button::Status::Disabled);
let active_bg = active.background.map(color_of).unwrap();
let disabled_bg = disabled.background.map(color_of).unwrap();
assert!(
disabled_bg.a < active_bg.a,
"a disabled button must fade: {} vs {}",
disabled_bg.a,
active_bg.a
);
assert_eq!(
(disabled_bg.r, disabled_bg.g, disabled_bg.b),
(active_bg.r, active_bg.g, active_bg.b),
"a disabled button must not change hue"
);
}
#[test]
fn every_button_variant_reaches_the_widget_with_its_own_tokens() {
let theme = Theme::new(Scheme::Dark);
let colors = theme.colors();
let filled = taim::button_style(ButtonKind::Filled)(&theme, button::Status::Active);
let destructive = taim::button_style(ButtonKind::Destructive)(&theme, button::Status::Active);
let outlined = taim::button_style(ButtonKind::Outlined)(&theme, button::Status::Active);
let text_only = taim::button_style(ButtonKind::Text)(&theme, button::Status::Active);
assert_eq!(filled.background.map(color_of), Some(colors.primary));
assert_eq!(destructive.background.map(color_of), Some(colors.error));
assert_eq!(outlined.background, None);
assert!(outlined.border.width > 0.0);
assert_eq!(outlined.border.color, colors.primary);
assert_eq!(text_only.background, None);
assert_eq!(text_only.border.width, 0.0);
}
#[test]
fn a_real_text_input_renders_taim_tokens_and_shows_focus() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let colors = theme.colors();
let class = <Theme as text_input::Catalog>::default();
let active = text_input::Catalog::style(&theme, &class, text_input::Status::Active);
let focused = text_input::Catalog::style(
&theme,
&class,
text_input::Status::Focused { is_hovered: false },
);
assert_eq!(color_of(active.background), colors.surface);
assert_eq!(active.value, colors.on_surface);
assert_eq!(active.selection, colors.primary);
assert_eq!(
focused.border.color, colors.focus,
"{scheme:?}: a focused field must border with the focus token"
);
assert_ne!(active.border.color, focused.border.color);
}
}
#[test]
fn a_real_text_inputs_value_and_placeholder_are_both_readable() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let class = <Theme as text_input::Catalog>::default();
let style = text_input::Catalog::style(&theme, &class, text_input::Status::Active);
let background = color_of(style.background);
assert!(
Contrast::body_text(style.value, background),
"{scheme:?}: field value fails contrast"
);
assert!(
Contrast::body_text(style.placeholder, background),
"{scheme:?}: placeholder fails contrast (ratio {:.2}) -- a placeholder \
must stay legible while reading as secondary",
Contrast::ratio(style.placeholder, background)
);
}
}
#[test]
fn a_real_checkbox_carries_selection_in_its_fill() {
let theme = Theme::new(Scheme::Dark);
let colors = theme.colors();
let class = <Theme as checkbox::Catalog>::default();
let unchecked = checkbox::Catalog::style(
&theme,
&class,
checkbox::Status::Active { is_checked: false },
);
let checked = checkbox::Catalog::style(
&theme,
&class,
checkbox::Status::Active { is_checked: true },
);
assert_eq!(
color_of(checked.background),
colors.primary,
"a checked box fills with primary"
);
assert_ne!(
color_of(unchecked.background),
color_of(checked.background),
"checked and unchecked must be distinguishable"
);
assert!(unchecked.border.width > 0.0);
}
#[test]
fn a_real_radio_is_distinguishable_when_selected() {
let theme = Theme::new(Scheme::Dark);
let colors = theme.colors();
let class = <Theme as radio::Catalog>::default();
let unselected =
radio::Catalog::style(&theme, &class, radio::Status::Active { is_selected: false });
let selected =
radio::Catalog::style(&theme, &class, radio::Status::Active { is_selected: true });
assert_eq!(selected.dot_color, colors.primary);
assert_ne!(
unselected.border_color, selected.border_color,
"selection must change the radio's outline"
);
}
#[test]
fn a_real_progress_bar_uses_the_primary_token_for_its_bar() {
let theme = Theme::new(Scheme::Dark);
let colors = theme.colors();
let class = <Theme as progress_bar::Catalog>::default();
let style = progress_bar::Catalog::style(&theme, &class);
assert_eq!(color_of(style.bar), colors.primary);
assert_eq!(color_of(style.background), colors.surface);
}
#[test]
fn a_card_container_paints_a_surface_but_a_bare_container_does_not() {
let theme = Theme::new(Scheme::Dark);
let colors = theme.colors();
let card = taim::card_style()(&theme);
assert_eq!(card.background.map(color_of), Some(colors.surface));
assert_eq!(card.text_color, Some(colors.on_surface));
let class = <Theme as container::Catalog>::default();
let bare = container::Catalog::style(&theme, &class);
assert_eq!(bare.background, None);
}
#[test]
fn a_widget_is_opaque_until_a_theme_opts_into_materials() {
let theme = Theme::new(Scheme::Dark);
assert_eq!(theme.materials(), MaterialTokens::FLAT);
assert_eq!(taim::card_style()(&theme).material, Material::OPAQUE);
assert_eq!(taim::glass_style()(&theme).material, Material::OPAQUE);
let class = <Theme as container::Catalog>::default();
let bare = container::Catalog::style(&theme, &class);
assert_eq!(bare.material, Material::OPAQUE);
}
#[test]
fn a_physical_theme_reaches_a_widgets_material() {
let theme = Theme::new(Scheme::Dark).with_materials(MaterialTokens::PHYSICAL);
assert_eq!(
taim::glass_style()(&theme).material,
MaterialTokens::PHYSICAL.overlay,
"a glass surface must take the theme's overlay material"
);
assert_eq!(
taim::card_style()(&theme).material,
MaterialTokens::PHYSICAL.surface,
"a card must take the theme's surface material"
);
assert!(taim::glass_style()(&theme).material.needs_backdrop());
}
#[test]
fn swapping_materials_bumps_the_theme_revision() {
let flat = Theme::new(Scheme::Dark);
let physical = flat.with_materials(MaterialTokens::PHYSICAL);
assert_ne!(physical.revision(), flat.revision());
assert_eq!(
flat.with_materials(MaterialTokens::FLAT).revision(),
flat.revision()
);
}
#[test]
fn the_scene_light_reaches_the_renderer_trait_through_the_theme() {
use taim_core::theme::Base as _;
let flat = Theme::new(Scheme::Dark);
assert_eq!(flat.light(), SceneLight::KEY);
let moved = SceneLight {
direction: [0.6, -0.5, 0.62],
..SceneLight::KEY
};
let relit = flat.with_materials(MaterialTokens {
light: moved,
..MaterialTokens::PHYSICAL
});
assert_eq!(relit.light(), moved);
}
#[test]
fn no_taim_widget_depends_on_a_shadow_to_be_seen() {
let theme = Theme::new(Scheme::Dark);
let button_class = <Theme as button::Catalog>::default();
let button_style = button::Catalog::style(&theme, &button_class, button::Status::Active);
assert_eq!(
button_style.shadow,
taim_core::Shadow::default(),
"a button must not need a shadow"
);
let card = taim::card_style()(&theme);
assert_eq!(
card.shadow,
taim_core::Shadow::default(),
"a card must not need a shadow"
);
}
#[test]
fn switching_the_scheme_changes_what_a_real_widget_paints() {
let class = <Theme as button::Catalog>::default();
let dark = Theme::new(Scheme::Dark);
let light = dark.with_scheme(Scheme::Light);
let dark_style = button::Catalog::style(&dark, &class, button::Status::Active);
let light_style = button::Catalog::style(&light, &class, button::Status::Active);
assert_ne!(
dark_style.background.map(color_of),
light_style.background.map(color_of),
"a scheme switch must change what the button paints"
);
assert!(light.revision() > dark.revision());
}
#[test]
fn a_real_icon_button_renders_taim_tokens() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let colors = theme.colors();
let class = <Theme as icon_button::Catalog>::default();
let rest = icon_button::Catalog::style(&theme, &class, icon_button::Status::Active);
let hovered = icon_button::Catalog::style(&theme, &class, icon_button::Status::Hovered);
assert_eq!(
rest.icon, colors.on_surface,
"{scheme:?}: an icon button's glyph is on_surface"
);
assert_eq!(
rest.background, None,
"{scheme:?}: at rest an icon button is content, not chrome -- it draws no fill"
);
assert!(
hovered.background.is_some(),
"{scheme:?}: hover must give it a surface"
);
}
}
#[test]
fn a_disabled_icon_button_dims_and_draws_no_fill() {
let theme = Theme::default();
let class = <Theme as icon_button::Catalog>::default();
let rest = icon_button::Catalog::style(&theme, &class, icon_button::Status::Active);
let disabled = icon_button::Catalog::style(&theme, &class, icon_button::Status::Disabled);
assert_eq!(
disabled.background, None,
"a disabled control must not look pressable"
);
assert_eq!(disabled.icon, rest.icon);
}
#[test]
fn a_real_list_tile_only_takes_a_surface_when_it_needs_one() {
let theme = Theme::default();
let class = <Theme as list_tile::Catalog>::default();
let rest = list_tile::Catalog::style(&theme, &class, list_tile::Status::Active, false);
let hovered = list_tile::Catalog::style(&theme, &class, list_tile::Status::Hovered, false);
let selected = list_tile::Catalog::style(&theme, &class, list_tile::Status::Active, true);
assert_eq!(rest.background, None, "a resting row draws no fill");
assert!(hovered.background.is_some(), "hover must be visible");
assert!(
selected.background.is_some(),
"a selected row must be distinguishable at rest"
);
}
#[test]
fn a_selected_list_tile_tints_its_text_with_primary() {
let theme = Theme::default();
let colors = theme.colors();
let class = <Theme as list_tile::Catalog>::default();
let unselected = list_tile::Catalog::style(&theme, &class, list_tile::Status::Active, false);
let selected = list_tile::Catalog::style(&theme, &class, list_tile::Status::Active, true);
assert_eq!(selected.text_color, colors.primary);
assert_ne!(unselected.text_color, selected.text_color);
}
#[test]
fn a_list_tile_stays_flush_so_a_list_reads_as_a_column() {
let theme = Theme::default();
let class = <Theme as list_tile::Catalog>::default();
let style = list_tile::Catalog::style(&theme, &class, list_tile::Status::Active, false);
assert_eq!(style.border.radius, 0.0.into());
}
#[test]
fn every_taim_native_widget_is_readable_on_the_surface_it_sits_on() {
for scheme in EVERY_SCHEME {
let theme = Theme::new(scheme);
let surface = theme.colors().surface;
let icon_class = <Theme as icon_button::Catalog>::default();
let icon = icon_button::Catalog::style(&theme, &icon_class, icon_button::Status::Active);
assert!(
Contrast::body_text(icon.icon, surface),
"{scheme:?}: an icon button's glyph must be legible on the surface"
);
let tile_class = <Theme as list_tile::Catalog>::default();
for selected in [false, true] {
let tile =
list_tile::Catalog::style(&theme, &tile_class, list_tile::Status::Active, selected);
let background = tile.background.map(color_of).unwrap_or(surface);
assert!(
Contrast::body_text(tile.text_color, background),
"{scheme:?}/selected={selected}: tile text fails contrast (ratio {:.2})",
Contrast::ratio(tile.text_color, background)
);
}
}
}