tests.rs raw

use super::*;
use crate::clipboard::NoClipboard;
use crate::event::{Modifiers, PointerButton};

fn click_at(expander: &mut Expander, y: f64) -> bool {
    expander.on_pointer(
        EventKind::Click,
        &PointerEvent {
            window: Point::new(20.0, y),
            local: Point::new(20.0, y),
            button: Some(PointerButton::Left),
            click_count: 1,
        },
        &mut TextContext::hermetic([]),
        &mut NoClipboard,
    )
}

/// Controlled: the widget *asks*, and the application's binding is what
/// actually opens it. Without that an accordion — one open at a time — could
/// not be expressed by binding, and the widget would fight the state.
#[test]
fn opening_is_a_request_not_an_action() {
    let mut expander = Expander::new();
    assert!(
        click_at(&mut expander, 10.0),
        "the title row took the click"
    );
    assert!(
        !expander.is_open(),
        "it did not open itself; the app answers by setting `:open`"
    );
    assert_eq!(
        expander.take_emitted(),
        vec![EventData::Toggled(true)],
        "it reported which way it was asked to go"
    );

    expander.set_open(true);
    assert!(expander.is_open());
    assert!(
        expander.take_emitted().is_empty(),
        "and a programmatic set is silent, so the binding cannot loop"
    );
}

/// A click in the content belongs to the content, not to the disclosure.
#[test]
fn only_the_title_row_toggles() {
    let mut expander = Expander::new();
    expander.set_open(true);
    let _ = expander.take_emitted();
    assert!(!click_at(&mut expander, HEADER_H + 20.0));
    assert!(expander.take_emitted().is_empty());
}

#[test]
fn enter_and_space_ask_too() {
    for key in [Key::Enter, Key::Character(" ".into())] {
        let mut expander = Expander::new();
        assert!(expander.on_key(
            &KeyInput {
                key,
                modifiers: Modifiers::default(),
                pressed: true
            },
            &mut TextContext::hermetic([]),
            &mut NoClipboard,
        ));
        assert_eq!(expander.take_emitted(), vec![EventData::Toggled(true)]);
    }
}

#[test]
fn it_announces_a_disclosure() {
    let mut expander = Expander::new();
    expander.set_title("Details");
    expander.set_open(true);
    let mut node = accesskit::Node::new(accesskit::Role::Button);
    expander.accessibility(&mut node);
    assert_eq!(node.label(), Some("Details"));
    assert!(node.is_expanded().unwrap_or(false));
}