From b5f1f31693af2497ceab78b8d63754c09fca5de9 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 7 May 2021 09:43:07 -0600 Subject: [PATCH] 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 --- gpui/src/app.rs | 52 ++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index a5758903fc..f62ab3f6be 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -734,6 +734,7 @@ impl MutableAppContext { self.pending_flushes += 1; let window_id = post_inc(&mut self.next_window_id); let root_view = self.add_view(window_id, build_root_view); + self.ctx.windows.insert( window_id, Window { @@ -743,6 +744,7 @@ impl MutableAppContext { }, ); self.open_platform_window(window_id); + root_view.update(self, |view, ctx| view.on_focus(ctx)); self.flush_effects(); (window_id, root_view) @@ -3186,13 +3188,13 @@ mod tests { #[test] fn test_focus() { - #[derive(Default)] struct View { - events: Vec, + name: String, + events: Arc>>, } impl Entity for View { - type Event = String; + type Event = (); } impl super::View for View { @@ -3204,40 +3206,42 @@ mod tests { "View" } - fn on_focus(&mut self, ctx: &mut ViewContext) { - self.events.push("self focused".into()); - ctx.emit("focused".into()); + fn on_focus(&mut self, _: &mut ViewContext) { + self.events.lock().push(format!("{} focused", &self.name)); } - fn on_blur(&mut self, ctx: &mut ViewContext) { - self.events.push("self blurred".into()); - ctx.emit("blurred".into()); + fn on_blur(&mut self, _: &mut ViewContext) { + self.events.lock().push(format!("{} blurred", &self.name)); } } App::test((), |app| { - let (window_id, view_1) = app.add_window(|_| View::default()); - let view_2 = app.add_view(window_id, |_| View::default()); - - view_1.update(app, |_, ctx| { - ctx.subscribe_to_view(&view_2, |view_1, _, event, _| { - view_1.events.push(format!("view 2 {}", event)); - }); - ctx.focus(&view_2); + let events: Arc>> = Default::default(); + let (window_id, view_1) = app.add_window(|_| View { + events: events.clone(), + name: "view 1".to_string(), + }); + let view_2 = app.add_view(window_id, |_| View { + events: events.clone(), + name: "view 2".to_string(), }); - view_1.update(app, |_, ctx| { - ctx.focus(&view_1); - }); + view_1.update(app, |_, ctx| ctx.focus(&view_2)); + 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!( - view_1.read(app).events, + *events.lock(), [ - "self focused".to_string(), - "self blurred".to_string(), + "view 1 focused".to_string(), + "view 1 blurred".to_string(), "view 2 focused".to_string(), - "self focused".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(), ], ); })