#![cfg(full_widgets)]
use rust_widgets::core::ObjectId;
use rust_widgets::widget::capability::WidgetFactory;
use rust_widgets::widget::runtime::with_widget_mut;
const W: u32 = 400;
const H: u32 = 300;
fn mounted_control_name(id: ObjectId) -> Option<&'static str> {
with_widget_mut(id, |widget| {
let factory = WidgetFactory::new_with_defaults();
factory.capability_for_kind_instance(widget).map(|capability| capability.canonical_name)
})
.flatten()
}
type AliasCase = (&'static str, ObjectId, &'static str);
fn alias_cases(parent: ObjectId) -> Vec<AliasCase> {
vec![
("create_menu_bar", rust_widgets::create_menu_bar(parent, 0, 0, W, 24), "menu_bar"),
("create_menu", rust_widgets::create_menu(parent, "File", 0, 0, 80, 24), "menu"),
("create_tool_bar", rust_widgets::create_tool_bar(parent, 0, 0, W, 32), "tool_bar"),
(
"create_status_bar",
rust_widgets::create_status_bar(parent, "ready", 0, 0, W, 24),
"status_bar",
),
("create_list_view", rust_widgets::create_list_view(parent, 0, 0, W, H), "list_view"),
]
}
#[test]
fn every_alias_route_builds_the_control_it_is_named_after() {
let parent = rust_widgets::create_window("alias-gate", 0, 0, 1024, 768);
let mut failures = Vec::new();
for (method, id, expected) in alias_cases(parent) {
match mounted_control_name(id) {
Some(actual) if actual == expected => {}
Some(actual) => {
failures.push(format!("{method} mounted `{actual}`, expected `{expected}`"))
}
None => failures.push(format!(
"{method} returned id {id}, which addresses no control (expected `{expected}`)"
)),
}
}
assert!(
failures.is_empty(),
"alias gate is narrower than the variant gate in src/widget/kind.rs:\n {}",
failures.join("\n ")
);
}
#[test]
fn a_substituted_panel_is_not_mistaken_for_the_named_control() {
let parent = rust_widgets::create_window("alias-gate-negative", 0, 0, 1024, 768);
let panel = rust_widgets::create_panel(parent, 0, 0, W, H);
let panel_kind = with_widget_mut(panel, |widget| widget.base().kind())
.expect("the stand-in id must address a mounted control");
assert_eq!(
panel_kind,
rust_widgets::widget::WidgetKind::GroupBox,
"`Panel` is a `pub type` for `GroupBox`; the mounted kind pins that pairing"
);
assert_eq!(
mounted_control_name(panel),
Some("group_box"),
"`WidgetKind::GroupBox` resolves through registration order to `group_box`; \
see `panel_capability` and `capability_for_widget` in `src/widget/capability.rs`"
);
for (method, id, expected) in alias_cases(parent) {
let Some(actual) = mounted_control_name(id) else { continue };
let kind = with_widget_mut(id, |widget| widget.base().kind())
.expect("a valid alias route must address a mounted control");
assert_ne!(
kind,
rust_widgets::widget::WidgetKind::GroupBox,
"{method} reached the `Panel`/`GroupBox` stand-in (expected `{expected}`)"
);
assert_ne!(
actual, "group_box",
"{method} reached the `Panel`/`GroupBox` stand-in (expected `{expected}`)"
);
}
}