#![cfg(all(
any(feature = "desktop", feature = "tablet", feature = "mobile"),
not(any(feature = "mini", feature = "embedded"))
))]
use rust_widgets::control_backend::{
get_control_backend_for_widget, route_preference_for_widget_kind, ControlBackendKind,
ControlRoutePreference,
};
use rust_widgets::platform::get_platform;
use rust_widgets::widget::WidgetKind;
const SAMPLE: [WidgetKind; 6] = [
WidgetKind::Button,
WidgetKind::Label,
WidgetKind::Slider,
WidgetKind::GroupBox,
WidgetKind::Chart,
WidgetKind::Canvas,
];
#[test]
fn every_kind_resolves_to_the_library_painted_route() {
for kind in SAMPLE {
assert_eq!(
route_preference_for_widget_kind(kind),
ControlRoutePreference::CustomRequired,
"{kind:?} must be painted by the library; another preference means a second \
mechanism came back"
);
let backend = get_control_backend_for_widget(kind);
assert_eq!(
backend.kind(),
ControlBackendKind::Custom,
"{kind:?} must resolve to the library backend, got {}",
backend.backend_name()
);
}
}
#[test]
fn the_single_backend_owns_real_state_for_a_hosted_widget() {
let backend = get_control_backend_for_widget(WidgetKind::GroupBox);
assert_eq!(backend.kind(), ControlBackendKind::Custom);
let window = backend.create_window("Router state", 0, 0, 320, 200);
assert_ne!(window, 0, "the backend must allocate a usable window id");
let label = backend.create_label(window, "hello", 10, 10, 120, 20);
assert_ne!(label, 0, "the backend must host a child of a live container");
assert_eq!(
backend.get_widget_text(label),
"hello",
"the backend must read back the text it just wrote"
);
}
#[test]
fn router_entry_point_is_public() {
let backend: &'static dyn rust_widgets::control_backend::ControlBackend =
get_control_backend_for_widget(WidgetKind::Label);
assert!(!backend.backend_name().is_empty());
}
#[test]
fn unified_entry_point_hosts_every_kind_without_the_caller_branching() {
rust_widgets::init();
let platform = get_platform();
let host = platform.create_window("unified", 0, 0, 400, 300);
assert_ne!(host, 0, "the host must supply a window to mount onto");
let mut outcomes = Vec::new();
for kind in [WidgetKind::Button, WidgetKind::GroupBox] {
let id = rust_widgets::create_widget_of_kind(kind, host, "x", 4, 4, 120, 40, None);
outcomes.push((kind, id));
}
let (first_kind, first_id) = outcomes[0];
let (second_kind, second_id) = outcomes[1];
assert_eq!(
first_id == 0,
second_id == 0,
"{first_kind:?} and {second_kind:?} must share one outcome (both hosted or both \
refused); a difference here means a per-kind route came back"
);
if first_id == 0 && platform.supports_surfaces() {
eprintln!(
"note: '{}' reports surfaces but refused a widget from this thread \
(single-main-thread toolkit); per-kind equality still verified",
platform.backend_name()
);
}
}
#[test]
fn library_window_is_linked_to_its_host_window() {
rust_widgets::init();
let platform = get_platform();
let window = rust_widgets::create_window("library window", 0, 0, 400, 300);
assert_ne!(window, 0, "the library must create a window");
let host = rust_widgets::widget::runtime::host_window_for(window);
let button = rust_widgets::create_widget_of_kind(
WidgetKind::Button,
window,
"click",
4,
4,
80,
30,
None,
);
if !platform.supports_surfaces() {
assert_eq!(
button, 0,
"a backend that cannot display must refuse rather than hand back a dead id"
);
return;
}
let host = host.filter(|id| *id != 0);
assert!(
host.is_some(),
"backend '{}' reports surfaces, so a library window must be linked to the host \
window built for it; without the link no control can ever mount onto it",
platform.backend_name()
);
if button == 0 {
return;
}
rust_widgets::set_widget_text(button, "hello");
assert_eq!(
rust_widgets::get_widget_text(button),
"hello",
"the mounted control must keep its own state"
);
}