mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 03:25:59 +00:00
Add unit test for ChildView
This commit is contained in:
parent
a5a60eb854
commit
1bec8087ee
1 changed files with 69 additions and 0 deletions
|
@ -7044,4 +7044,73 @@ mod tests {
|
||||||
cx.simulate_window_activation(Some(window_3));
|
cx.simulate_window_activation(Some(window_3));
|
||||||
assert_eq!(mem::take(&mut *events.borrow_mut()), []);
|
assert_eq!(mem::take(&mut *events.borrow_mut()), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[crate::test(self)]
|
||||||
|
fn test_child_view(cx: &mut MutableAppContext) {
|
||||||
|
struct Child {
|
||||||
|
rendered: Rc<Cell<bool>>,
|
||||||
|
dropped: Rc<Cell<bool>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Self>) -> ElementBox {
|
||||||
|
self.rendered.set(true);
|
||||||
|
Empty::new().boxed()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for Child {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.dropped.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Parent {
|
||||||
|
child: Option<ViewHandle<Child>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Self>) -> 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue