use super::*;
use crate::text::Font;
use std::sync::Arc;
#[test]
fn test_programmatic_focus_request() {
let font = Arc::new(Font::from_slice(TEST_FONT).unwrap());
const FIELD_ID: u64 = 4242;
let mut root = Container::new().with_padding(4.0);
root.children_mut()
.push(Box::new(TextField::new(Arc::clone(&font)).with_font_size(14.0)));
root.children_mut().push(Box::new(
TextField::new(Arc::clone(&font))
.with_font_size(14.0)
.with_focus_id(FIELD_ID),
));
let mut app = App::new(Box::new(root));
app.layout(Size::new(200.0, 200.0));
assert!(
app.focused_widget_type_name().is_none(),
"nothing focused before a request"
);
crate::focus::request_focus(FIELD_ID);
app.layout(Size::new(200.0, 200.0));
assert_eq!(
app.focused_widget_type_name(),
Some("TextField"),
"the field with the matching focus id should be focused"
);
assert!(crate::focus::take_focus_request().is_none());
}
#[test]
fn test_programmatic_focus_request_unmatched_id() {
let font = Arc::new(Font::from_slice(TEST_FONT).unwrap());
let mut root = Container::new().with_padding(4.0);
root.children_mut()
.push(Box::new(TextField::new(Arc::clone(&font)).with_font_size(14.0)));
let mut app = App::new(Box::new(root));
crate::focus::request_focus(9999);
app.layout(Size::new(200.0, 200.0));
assert!(app.focused_widget_type_name().is_none());
assert!(
crate::focus::take_focus_request().is_none(),
"an unmatched request is still consumed"
);
}