#![cfg(all(feature = "desktop", not(alloc_frugal)))]
use rust_widgets::core::Rect;
use rust_widgets::widget::capability::WidgetFactory;
#[test]
fn every_published_command_is_dispatched_by_its_control() {
let factory = WidgetFactory::new_with_defaults();
let mut total = 0usize;
let mut handled = 0usize;
let mut unresolvable: Vec<&str> = Vec::new();
let mut refused: Vec<(&str, &str)> = Vec::new();
for capability in factory.capabilities() {
if capability.commands.is_empty() {
continue;
}
let Some(mut widget) =
factory.create(capability.canonical_name, Rect::new(0, 0, 96, 64), "")
else {
continue;
};
if factory.capability_for_kind_instance(widget.as_ref()).is_none() {
unresolvable.push(capability.canonical_name);
continue;
}
for command in capability.commands {
total += 1;
match factory.invoke_command(widget.as_mut(), command) {
Ok(())
| Err(
rust_widgets::widget::capability::CapabilityAccessError::OutOfRange
| rust_widgets::widget::capability::CapabilityAccessError::UnsupportedOnWidget,
) => handled += 1,
Err(_) => refused.push((capability.canonical_name, *command)),
}
}
}
assert!(
total > 0,
"no capability publishes commands, so this test proves nothing — the schema field or \
its wiring has stopped reaching the property layer"
);
assert!(
unresolvable.is_empty(),
"a control publishing commands could not be resolved to its own capability, so its \
whole command list is unreachable: {unresolvable:?}"
);
assert!(
refused.is_empty(),
"a capability publishes commands its own control does not implement, so a generic \
consumer would offer an action that silently does nothing (control, command): \
{refused:?}"
);
assert_eq!(
handled, total,
"a control lost its `command` implementation: {handled}/{total} handled, refused: \
{refused:?}"
);
assert!(
total >= 496,
"fewer commands are published than the registry declares: {total} — a capability \
stopped advertising a list it still implements"
);
}
#[test]
fn implemented_commands_are_dispatched() {
use rust_widgets::widget::capability::CapabilityAccessError;
let factory = WidgetFactory::new_with_defaults();
let cases: &[(&str, &str)] = &[
("check_box", "toggle"),
("button", "click"),
("toggle_button", "toggle"),
("radio_button", "set_checked"),
("group_box", "toggle"),
];
let mut missing: Vec<(&str, &str)> = Vec::new();
for &(control, command) in cases {
let Some(mut widget) = factory.create(control, Rect::new(0, 0, 96, 64), "") else {
continue;
};
match factory.invoke_command(widget.as_mut(), command) {
Ok(()) | Err(CapabilityAccessError::OutOfRange) => {}
_other => missing.push((control, command)),
}
}
assert!(
missing.is_empty(),
"a command whose implementation exists is refused as unknown, so the dispatcher \
and the control disagree (control, command): {missing:?}"
);
}
#[test]
fn a_successful_command_has_an_observable_effect() {
let factory = WidgetFactory::new_with_defaults();
let mut check_box =
factory.create("check_box", Rect::new(0, 0, 96, 64), "").expect("check_box constructs");
let before = factory
.read_property(check_box.as_ref(), "checked")
.expect("check_box publishes `checked`");
assert_eq!(factory.invoke_command(check_box.as_mut(), "toggle"), Ok(()));
let after = factory
.read_property(check_box.as_ref(), "checked")
.expect("check_box publishes `checked`");
assert_ne!(
before, after,
"`toggle` returned Ok but left `checked` unchanged, so the command is a shell"
);
assert_eq!(factory.invoke_command(check_box.as_mut(), "toggle"), Ok(()));
let restored = factory
.read_property(check_box.as_ref(), "checked")
.expect("check_box publishes `checked`");
assert_eq!(restored, before, "a second `toggle` must return the control to its first state");
let mut radio =
factory.create("radio_button", Rect::new(0, 0, 96, 64), "").expect("radio_button");
factory
.write_property(
radio.as_mut(),
"checked",
rust_widgets::widget::capability::CapabilityValue::Bool(true),
)
.expect("radio_button accepts `checked`");
assert_eq!(
factory.read_property(radio.as_ref(), "checked").expect("read back"),
rust_widgets::widget::capability::CapabilityValue::Bool(true),
"the value a property write stored must be the value a read returns"
);
}
#[test]
fn an_unpublished_command_is_refused() {
use rust_widgets::widget::capability::CapabilityAccessError;
let factory = WidgetFactory::new_with_defaults();
let mut widget =
factory.create("check_box", Rect::new(0, 0, 96, 64), "").expect("check_box constructs");
assert_eq!(factory.invoke_command(widget.as_mut(), "toggle"), Ok(()));
let result = factory.invoke_command(widget.as_mut(), "definitely_not_a_command");
assert_eq!(
result,
Err(CapabilityAccessError::UnknownCommand),
"an unknown command name was accepted, so the dispatcher reports success for \
actions it did not perform"
);
}