makeover-immediate 0.68.0

The immediate-mode renderer for makeover-layout, backed by egui: no cascade, no retained tree, one stroke per widget.
Documentation
//! Tests for [`super`].

use super::*;

/// A palette with every field distinct, so a mapping that reads the wrong
/// token shows up as the wrong colour rather than as a coincidence.
pub(crate) fn palette() -> Palette {
    Palette {
        page: Color32::from_rgb(1, 1, 1),
        raised: Color32::from_rgb(2, 2, 2),
        overlay: Color32::from_rgb(3, 3, 3),
        well: Color32::from_rgb(4, 4, 4),
        sunken: Color32::from_rgb(5, 5, 5),
        bevel_light: Color32::from_rgb(6, 6, 6),
        bevel_dark: Color32::from_rgb(7, 7, 7),
        elevation: Color32::from_black_alpha(46),
        content: Color32::from_rgb(9, 9, 9),
        content_secondary: Color32::from_rgb(10, 10, 10),
        content_muted: Color32::from_rgb(11, 11, 11),
        content_disabled: Color32::from_rgb(41, 41, 41),
        action: Color32::from_rgb(12, 12, 12),
        content_on_action: Color32::from_rgb(250, 250, 250),
        danger: Color32::from_rgb(13, 13, 13),
        success: Color32::from_rgb(14, 14, 14),
        warning: Color32::from_rgb(15, 15, 15),
        info: Color32::from_rgb(16, 16, 16),
        border: Color32::from_rgb(17, 17, 17),
        info_surface: Color32::from_rgb(18, 18, 18),
        success_surface: Color32::from_rgb(19, 19, 19),
        warning_surface: Color32::from_rgb(20, 20, 20),
        danger_surface: Color32::from_rgb(21, 21, 21),
        row_hover: Color32::from_rgb(23, 23, 23),
        row_rule: Color32::from_rgb(24, 24, 24),
        row_selected: Color32::from_rgb(25, 25, 25),
        heats: [
            Color32::from_rgb(26, 26, 26),
            Color32::from_rgb(27, 27, 27),
            Color32::from_rgb(28, 28, 28),
            Color32::from_rgb(29, 29, 29),
            Color32::from_rgb(30, 30, 30),
        ],
    }
}

#[test]
fn a_heat_band_reads_its_own_fill_band_0_none_and_clamps_at_the_last() {
    let p = palette();
    assert_eq!(p.heat(Heat(0)), None);
    for (band, fill) in (1u8..).zip(&p.heats[1..]) {
        assert_eq!(p.heat(Heat(band)), Some(*fill));
    }
    assert_eq!(p.heat(Heat(u8::MAX)), Some(p.heats[4]));
}

#[test]
fn the_cast_hands_egui_the_themes_tone() {
    let p = palette();
    let cast = p.cast();
    assert_eq!(cast.color, p.elevation);
    assert!(cast.blur > 0, "a cast shadow is soft");
    assert_eq!(cast.offset, [0, 2], "it falls downward and only a little");
}

#[test]
fn a_well_resolves_to_its_own_token() {
    // No substitution left. The page-filled well was a stand-in for a
    // token that did not exist yet; it exists now.
    let w = Color32::from_rgb(99, 99, 99);
    let p = Palette {
        well: w,
        ..palette()
    };
    assert_eq!(p.fill(Fill::Well), Some(w));
    assert_ne!(p.fill(Fill::Well), Some(p.page));
}

#[test]
fn every_intent_is_a_plain_lookup() {
    let p = palette();
    assert_eq!(p.fill(Fill::Page), Some(p.page));
    assert_eq!(p.fill(Fill::Raised), Some(p.raised));
    assert_eq!(p.fill(Fill::Overlay), Some(p.overlay));
}

/// Sunken is its own colour, not the well's and not the page's. The two
/// are authored in opposite directions and an earlier cut of the
/// description conflated them.
#[test]
fn sunken_is_neither_the_well_nor_the_page() {
    let p = palette();
    assert_eq!(p.fill(Fill::Sunken), Some(p.sunken));
    assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Well));
    assert_ne!(p.fill(Fill::Sunken), p.fill(Fill::Page));
}

#[test]
fn a_raised_region_never_resolves_to_the_well_fill() {
    // The cross-app bug, asserted at the renderer boundary this time.
    let p = palette();
    let raised = Depth::Raised.fill().and_then(|f| p.fill(f));
    let well = Depth::Well.fill().and_then(|f| p.fill(f));
    assert_eq!(raised, Some(p.raised));
    assert_ne!(raised, well);
}

#[test]
fn an_overlay_is_cast_onto_the_page_and_takes_no_edge() {
    let p = palette();
    assert_eq!(
        Depth::Overlay.fill().and_then(|f| p.fill(f)),
        Some(p.overlay)
    );
    assert_eq!(Depth::Overlay.bevel(), None);
    // The shadow `frame` reaches for is the theme's tone rather than
    // egui's default, which is the whole reason `cast` exists.
    assert_eq!(p.cast().color, p.elevation);
}

#[test]
fn the_lit_edge_swaps_when_a_card_is_pressed() {
    let p = palette();
    let (tl, _) = Depth::Raised.bevel().unwrap().edges();
    let (ptl, _) = Depth::Raised.pressed().bevel().unwrap().edges();
    assert_eq!(p.edge(tl), p.bevel_light);
    assert_eq!(p.edge(ptl), p.bevel_dark);
}

#[test]
fn flat_asks_for_neither_fill_nor_edge() {
    assert!(Depth::Flat.fill().is_none());
    assert!(Depth::Flat.bevel().is_none());
}

#[test]
fn the_default_frame_is_square_and_one_point() {
    let d = FrameStyle::default();
    assert_eq!(d.radius, CornerRadius::ZERO);
    assert_eq!(d.margin, Margin::ZERO);
    assert!((d.stroke - 1.0).abs() < f32::EPSILON);
}

#[test]
fn a_badge_fills_with_its_tone_surface_and_edges_in_its_tone() {
    // The webview's badge: a neutral one is raised inside the border, and a
    // status one is its tone surface inside its tone. The tone is never the
    // ink, so it is never asked for here.
    let p = palette();
    assert_eq!(p.tone_surface(Tone::Neutral), p.raised);
    assert_eq!(p.tone_edge(Tone::Neutral), p.border);
    for (tone, surface, edge) in [
        (Tone::Info, p.info_surface, p.info),
        (Tone::Success, p.success_surface, p.success),
        (Tone::Warning, p.warning_surface, p.warning),
        (Tone::Danger, p.danger_surface, p.danger),
    ] {
        assert_eq!(p.tone_surface(tone), surface, "{tone:?}");
        assert_eq!(p.tone_edge(tone), edge, "{tone:?}");
    }
}

#[cfg(feature = "theme")]
#[test]
fn every_shipped_theme_resolves_to_a_whole_palette() {
    // `from_theme` fails on the first token a theme does not resolve. makeover
    // derives every one for every theme it ships, so none may fail here.
    for (id, source) in makeover::embedded_themes() {
        let id = makeover::ThemeId::new(id).expect("a shipped id is valid");
        let theme = makeover::parse_theme_str(&id, source, false).expect("a shipped theme parses");
        let palette = Palette::from_theme(&makeover::resolve(&theme));
        assert!(palette.is_ok(), "{id}: {palette:?}");
    }
}