From 1bec8087eed53c95fb0e513217da8b4dcd3fc555 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 13 Oct 2022 15:59:43 +0200 Subject: [PATCH] Add unit test for `ChildView` --- crates/gpui/src/app.rs | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 83936c617e..76aae930a1 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -7044,4 +7044,73 @@ mod tests { cx.simulate_window_activation(Some(window_3)); assert_eq!(mem::take(&mut *events.borrow_mut()), []); } + + #[crate::test(self)] + fn test_child_view(cx: &mut MutableAppContext) { + struct Child { + rendered: Rc>, + dropped: Rc>, + } + + impl super::Entity for Child { + type Event = (); + } + + impl super::View for Child { + fn ui_name() -> &'static str { + "child view" + } + + fn render(&mut self, _: &mut RenderContext) -> ElementBox { + self.rendered.set(true); + Empty::new().boxed() + } + } + + impl Drop for Child { + fn drop(&mut self) { + self.dropped.set(true); + } + } + + struct Parent { + child: Option>, + } + + impl super::Entity for Parent { + type Event = (); + } + + impl super::View for Parent { + fn ui_name() -> &'static str { + "parent view" + } + + fn render(&mut self, cx: &mut RenderContext) -> ElementBox { + if let Some(child) = self.child.as_ref() { + ChildView::new(child, cx).boxed() + } else { + Empty::new().boxed() + } + } + } + + let child_rendered = Rc::new(Cell::new(false)); + let child_dropped = Rc::new(Cell::new(false)); + let (_, root_view) = cx.add_window(Default::default(), |cx| Parent { + child: Some(cx.add_view(|_| Child { + rendered: child_rendered.clone(), + dropped: child_dropped.clone(), + })), + }); + assert!(child_rendered.take()); + assert!(!child_dropped.take()); + + root_view.update(cx, |view, cx| { + view.child.take(); + cx.notify(); + }); + assert!(!child_rendered.take()); + assert!(child_dropped.take()); + } }