mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
egui_kittest: Allow passing state to the app closure (#5313)
The allows us to pass any state to the ui closure. While it is possible to just store state in the closure itself, accessing that state after the harness was created to e.g. read or modify it would require interior mutability. With this change there are new `Harness::new_state`, `Harness::run_state`, ... methods that allow passing state on each run. This builds on top of #5301, which should be merged first --------- Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
@@ -121,12 +121,15 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
pub fn should_type() {
|
||||
let mut text = "Hello, world!".to_owned();
|
||||
let mut harness = Harness::new(move |ctx| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
ui.text_edit_singleline(&mut text);
|
||||
});
|
||||
});
|
||||
let text = "Hello, world!".to_owned();
|
||||
let mut harness = Harness::new_state(
|
||||
move |ctx, text| {
|
||||
CentralPanel::default().show(ctx, |ui| {
|
||||
ui.text_edit_singleline(text);
|
||||
});
|
||||
},
|
||||
text,
|
||||
);
|
||||
|
||||
harness.run();
|
||||
|
||||
@@ -144,5 +147,6 @@ mod tests {
|
||||
harness.run();
|
||||
let text_edit = harness.get_by_role(accesskit::Role::TextInput);
|
||||
assert_eq!(text_edit.value().as_deref(), Some("Hi there!"));
|
||||
assert_eq!(harness.state(), "Hi there!");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user