use taim::{Point, Rectangle, Size};
use taim_core::Event;
use taim_core::input_method::{self, CandidateRect, InputMethod, Preedit, Purpose};
use taim_core::widget::{self, Widget};
use taim_core::{Layout, Shell, layout, mouse};
use taim_core::{shell::Waker, window};
use taim_renderer::Renderer;
use taim_widget::text_input;
type Message = String;
type Paragraph = <Renderer as taim_core::text::Renderer>::Paragraph;
const BOUNDS: Size = Size {
width: 200.0,
height: 30.0,
};
fn headless_renderer() -> Renderer {
taim_futures::futures::executor::block_on(<Renderer as taim_core::renderer::Headless>::new(
taim_core::renderer::Settings::default(),
Some("tiny-skia"),
))
.expect("tiny-skia renderer must be available")
}
struct Field<'a> {
input: text_input::TextInput<'a, Message, taim::Theme, Renderer>,
tree: widget::Tree,
node: layout::Node,
renderer: Renderer,
}
impl<'a> Field<'a> {
fn new(value: &'a str, secure: bool) -> Self {
let mut input = text_input::TextInput::new("placeholder", value)
.on_input(|text| text)
.secure(secure);
let renderer = headless_renderer();
let mut tree = widget::Tree::new(&input as &dyn Widget<Message, taim::Theme, Renderer>);
tree.diff(&mut input as &mut dyn Widget<Message, taim::Theme, Renderer>);
let node = Widget::<Message, taim::Theme, Renderer>::layout(
&mut input,
&mut tree,
&renderer,
&layout::Limits::new(Size::ZERO, BOUNDS),
);
let mut field = Self {
input,
tree,
node,
renderer,
};
field
.tree
.state
.downcast_mut::<text_input::State<Paragraph>>()
.focus();
field
}
fn dispatch(&mut self, event: Event) -> (Vec<Message>, InputMethod) {
let mut messages = Vec::new();
let mut shell = Shell::new(&window::Headless, Waker::noop(), &mut messages);
let viewport = Rectangle::new(Point::ORIGIN, BOUNDS);
Widget::<Message, taim::Theme, Renderer>::update(
&mut self.input,
&mut self.tree,
&event,
Layout::new(&self.node),
mouse::Cursor::Available(Point::new(10.0, 10.0)),
&self.renderer,
&mut shell,
&viewport,
);
let requested = shell.input_method().clone();
(messages, requested)
}
fn ime(&mut self, event: input_method::Event) -> Vec<Message> {
self.dispatch(Event::InputMethod(event)).0
}
fn input_method(&mut self) -> InputMethod {
self.dispatch(Event::Window(window::Event::RedrawRequested(
taim_core::time::Instant::now(),
)))
.1
}
fn exposed_preedit(&mut self) -> Option<String> {
match self.input_method() {
InputMethod::Enabled { preedit, .. } => preedit.map(|preedit| preedit.content),
InputMethod::Disabled => None,
}
}
}
#[test]
fn a_full_composition_commits_exactly_once() {
let mut field = Field::new("", false);
assert!(field.ime(input_method::Event::Opened).is_empty());
assert!(
field
.ime(input_method::Event::Preedit("न".to_owned(), Some(0..3)))
.is_empty(),
"an in-flight composition must not publish a message"
);
assert!(
field
.ime(input_method::Event::Preedit("नम".to_owned(), Some(0..6)))
.is_empty()
);
let messages = field.ime(input_method::Event::Commit("नमस्ते".to_owned()));
assert_eq!(
messages,
vec!["नमस्ते".to_owned()],
"commit must publish the composed text exactly once"
);
}
#[test]
fn a_composition_is_visible_while_in_flight() {
let mut field = Field::new("", false);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("नम".to_owned(), Some(0..6)));
assert_eq!(
field.exposed_preedit().as_deref(),
Some("नम"),
"the composition must be visible while in flight"
);
}
#[test]
fn an_empty_preedit_clears_the_composition_display() {
let mut field = Field::new("", false);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("नम".to_owned(), Some(0..6)));
let _ = field.ime(input_method::Event::Preedit(String::new(), None));
assert_eq!(field.exposed_preedit().as_deref(), Some(""));
}
#[test]
fn an_abandoned_composition_commits_nothing_and_leaves_no_preedit() {
let mut field = Field::new("", false);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("नम".to_owned(), Some(0..6)));
assert_eq!(field.exposed_preedit().as_deref(), Some("नम"));
let messages = field.ime(input_method::Event::Closed);
assert!(
messages.is_empty(),
"an abandoned composition must not commit text"
);
assert_eq!(
field.exposed_preedit(),
None,
"closing the IME must clear the in-flight composition"
);
}
#[test]
fn reopening_after_cancellation_starts_from_an_empty_composition() {
let mut field = Field::new("", false);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("abandoned".to_owned(), None));
let _ = field.ime(input_method::Event::Closed);
let _ = field.ime(input_method::Event::Opened);
assert_eq!(
field.exposed_preedit().as_deref(),
Some(""),
"a new session must not resurrect the abandoned composition"
);
}
#[test]
fn a_focused_field_reports_caret_geometry_for_the_candidate_window() {
let mut field = Field::new("text", false);
let _ = field.ime(input_method::Event::Opened);
let InputMethod::Enabled { cursor, .. } = field.input_method() else {
panic!("a focused field must enable its input method");
};
assert!(cursor.height > 0.0, "the caret must have a real height");
assert!(
cursor.x >= 0.0 && cursor.y >= 0.0,
"the caret must have a real position"
);
}
#[test]
fn candidate_geometry_is_explicit_and_renderer_neutral() {
let candidate = CandidateRect {
bounds: Rectangle::new(Point::new(10.0, 20.0), Size::new(1.0, 24.0)),
};
assert_eq!(candidate.bounds.height, 24.0);
}
#[test]
fn preedit_retains_the_platform_selection_without_rewriting_content() {
let preedit = Preedit {
content: "नम".to_owned(),
selection: Some(0..3),
text_size: None,
};
assert_eq!(preedit.as_ref().content, "नम");
assert_eq!(preedit.as_ref().selection, Some(0..3));
}
#[test]
fn a_secure_field_marks_its_purpose_and_withholds_the_composition() {
let mut field = Field::new("", true);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("secret".to_owned(), None));
let InputMethod::Enabled {
purpose, preedit, ..
} = field.input_method()
else {
panic!("a focused secure field must still enable its input method");
};
assert_eq!(
purpose,
Purpose::Secure,
"a secure field must tell the platform it is secure"
);
assert!(
preedit.is_none(),
"a secure field must never hand its composition to the runtime"
);
}
#[test]
fn a_non_secure_field_does_hand_its_composition_to_the_runtime() {
let mut field = Field::new("", false);
let _ = field.ime(input_method::Event::Opened);
let _ = field.ime(input_method::Event::Preedit("नम".to_owned(), None));
let InputMethod::Enabled {
purpose, preedit, ..
} = field.input_method()
else {
panic!("a focused field must enable its input method");
};
assert_eq!(purpose, Purpose::Normal);
assert_eq!(
preedit.map(|preedit| preedit.content),
Some("नम".to_owned())
);
}