Fix focus test

Call on_focus on the root view when the window is originally created. Test dropping a focused view. Simplify test to avoid relying on emitting events.

Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
Nathan Sobo 2021-05-07 09:43:07 -06:00
parent 35e0eaaae2
commit b5f1f31693

View file

@ -734,6 +734,7 @@ impl MutableAppContext {
self.pending_flushes += 1; self.pending_flushes += 1;
let window_id = post_inc(&mut self.next_window_id); let window_id = post_inc(&mut self.next_window_id);
let root_view = self.add_view(window_id, build_root_view); let root_view = self.add_view(window_id, build_root_view);
self.ctx.windows.insert( self.ctx.windows.insert(
window_id, window_id,
Window { Window {
@ -743,6 +744,7 @@ impl MutableAppContext {
}, },
); );
self.open_platform_window(window_id); self.open_platform_window(window_id);
root_view.update(self, |view, ctx| view.on_focus(ctx));
self.flush_effects(); self.flush_effects();
(window_id, root_view) (window_id, root_view)
@ -3186,13 +3188,13 @@ mod tests {
#[test] #[test]
fn test_focus() { fn test_focus() {
#[derive(Default)]
struct View { struct View {
events: Vec<String>, name: String,
events: Arc<Mutex<Vec<String>>>,
} }
impl Entity for View { impl Entity for View {
type Event = String; type Event = ();
} }
impl super::View for View { impl super::View for View {
@ -3204,40 +3206,42 @@ mod tests {
"View" "View"
} }
fn on_focus(&mut self, ctx: &mut ViewContext<Self>) { fn on_focus(&mut self, _: &mut ViewContext<Self>) {
self.events.push("self focused".into()); self.events.lock().push(format!("{} focused", &self.name));
ctx.emit("focused".into());
} }
fn on_blur(&mut self, ctx: &mut ViewContext<Self>) { fn on_blur(&mut self, _: &mut ViewContext<Self>) {
self.events.push("self blurred".into()); self.events.lock().push(format!("{} blurred", &self.name));
ctx.emit("blurred".into());
} }
} }
App::test((), |app| { App::test((), |app| {
let (window_id, view_1) = app.add_window(|_| View::default()); let events: Arc<Mutex<Vec<String>>> = Default::default();
let view_2 = app.add_view(window_id, |_| View::default()); let (window_id, view_1) = app.add_window(|_| View {
events: events.clone(),
view_1.update(app, |_, ctx| { name: "view 1".to_string(),
ctx.subscribe_to_view(&view_2, |view_1, _, event, _| {
view_1.events.push(format!("view 2 {}", event));
}); });
ctx.focus(&view_2); let view_2 = app.add_view(window_id, |_| View {
events: events.clone(),
name: "view 2".to_string(),
}); });
view_1.update(app, |_, ctx| { view_1.update(app, |_, ctx| ctx.focus(&view_2));
ctx.focus(&view_1); view_1.update(app, |_, ctx| ctx.focus(&view_1));
}); view_1.update(app, |_, ctx| ctx.focus(&view_2));
view_1.update(app, |_, _| drop(view_2));
assert_eq!( assert_eq!(
view_1.read(app).events, *events.lock(),
[ [
"self focused".to_string(), "view 1 focused".to_string(),
"self blurred".to_string(), "view 1 blurred".to_string(),
"view 2 focused".to_string(), "view 2 focused".to_string(),
"self focused".to_string(),
"view 2 blurred".to_string(), "view 2 blurred".to_string(),
"view 1 focused".to_string(),
"view 1 blurred".to_string(),
"view 2 focused".to_string(),
"view 1 focused".to_string(),
], ],
); );
}) })