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"
);
}
#[test]
fn rich_text_edit_is_a_focused_text_input() {
use crate::widgets::rich_text::{uniform_resolver, RichDoc, RichTextEdit};
let font = Arc::new(Font::from_slice(TEST_FONT).unwrap());
const FIELD_ID: u64 = 7777;
let mut root = Container::new().with_padding(4.0);
root.children_mut().push(Box::new(
RichTextEdit::new(RichDoc::new(), uniform_resolver(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_is_text_input(),
"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("RichTextEdit"));
assert!(
app.focused_is_text_input(),
"a focused RichTextEdit must count as a text input for the clipboard bridge"
);
}
#[test]
fn accepts_text_input_gates_the_clipboard_bridge() {
use crate::widget::Widget;
use crate::widgets::rich_text::{uniform_resolver, RichDoc, RichTextEdit};
let font = Arc::new(Font::from_slice(TEST_FONT).unwrap());
let editor = RichTextEdit::new(RichDoc::new(), uniform_resolver(Arc::clone(&font)));
assert!(editor.accepts_text_input(), "RichTextEdit accepts text input");
let button = Button::new("ok", Arc::clone(&font));
assert!(
!button.accepts_text_input(),
"a Button does not accept text input"
);
}