generated from rerun-io/rerun_template
-
Notifications
You must be signed in to change notification settings - Fork 4
/
basic_integration.rs
92 lines (79 loc) · 2.54 KB
/
basic_integration.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! This example shows how to build a basic kittest integration for some ui framework (in this case egui).
//! If you actually want to use kittest with egui, I suggest you check out the official
//! [egui_kittest][1] integration.
//!
//! [1]: <https://github.com/emilk/egui/tree/master/crates/egui_kittest>
use kittest::{Event, Node, Queryable};
/// The test Harness. This contains everything needed to run the test.
pub struct Harness<'a> {
/// A handle to the ui framework
ctx: egui::Context,
/// the ui component that should be tested (for egui that's just a closure).
app: Box<dyn FnMut(&egui::Context) + 'a>,
/// The kittest State
pub state: kittest::State,
}
impl<'a> Harness<'a> {
pub fn new(mut app: impl FnMut(&egui::Context) + 'a) -> Harness<'a> {
let ctx = egui::Context::default();
ctx.enable_accesskit();
let output = ctx.run(Default::default(), &mut app);
Self {
ctx,
app: Box::new(app),
state: kittest::State::new(
output
.platform_output
.accesskit_update
.expect("AccessKit not enabled"),
),
}
}
pub fn run_frame(&mut self) {
let events = self
.state
.take_events()
.into_iter()
.map(|e| match e {
Event::ActionRequest(action) => egui::Event::AccessKitActionRequest(action),
Event::Simulated(_) => {
panic!("Check egui_kittest for a full implementation");
}
})
.collect();
let output = self.ctx.run(
egui::RawInput {
events,
..Default::default()
},
self.app.as_mut(),
);
self.state.update(
output
.platform_output
.accesskit_update
.expect("AccessKit not enabled"),
);
}
}
// This allows us to directly query the harness as if it's the root node.
impl<'tree, 'node, 'app> Queryable<'tree, 'node> for Harness<'app>
where
'node: 'tree,
{
fn node(&'node self) -> Node<'tree> {
self.state.root()
}
}
fn main() {
let mut checked = false;
let mut harness = Harness::new(|ctx| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.checkbox(&mut checked, "Check me!");
});
});
harness.get_by_label("Check me!").click();
harness.run_frame();
drop(harness);
assert!(checked, "Should be checked");
}