mirror of
https://github.com/zed-industries/zed.git
synced 2024-12-28 03:26:40 +03:00
Add unit test for ChildView
This commit is contained in:
parent
a5a60eb854
commit
1bec8087ee
@ -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<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 New Issue
Block a user