Move activation simulation to AnyWindowHandle
This commit is contained in:
parent
486f5bc6ca
commit
0197d49230
3 changed files with 34 additions and 31 deletions
|
@ -4087,6 +4087,29 @@ impl AnyWindowHandle {
|
|||
pub fn remove<C: BorrowWindowContext>(&self, cx: &mut C) -> C::Result<()> {
|
||||
self.update(cx, |cx| cx.remove_window())
|
||||
}
|
||||
|
||||
pub fn simulate_activation(&self, cx: &mut TestAppContext) {
|
||||
self.update(cx, |cx| {
|
||||
let other_window_ids = cx
|
||||
.windows
|
||||
.keys()
|
||||
.filter(|window_id| **window_id != self.window_id)
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for window_id in other_window_ids {
|
||||
cx.window_changed_active_status(window_id, false)
|
||||
}
|
||||
|
||||
cx.window_changed_active_status(self.window_id, true)
|
||||
});
|
||||
}
|
||||
|
||||
pub fn simulate_deactivation(&self, cx: &mut TestAppContext) {
|
||||
self.update(cx, |cx| {
|
||||
cx.window_changed_active_status(self.window_id, false);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(transparent)]
|
||||
|
@ -6726,25 +6749,25 @@ mod tests {
|
|||
[("window 2", false), ("window 3", true)]
|
||||
);
|
||||
|
||||
cx.simulate_window_activation(Some(window_2.id()));
|
||||
window_2.simulate_activation(cx);
|
||||
assert_eq!(
|
||||
mem::take(&mut *events.borrow_mut()),
|
||||
[("window 3", false), ("window 2", true)]
|
||||
);
|
||||
|
||||
cx.simulate_window_activation(Some(window_1.id()));
|
||||
window_1.simulate_activation(cx);
|
||||
assert_eq!(
|
||||
mem::take(&mut *events.borrow_mut()),
|
||||
[("window 2", false), ("window 1", true)]
|
||||
);
|
||||
|
||||
cx.simulate_window_activation(Some(window_3.id()));
|
||||
window_3.simulate_activation(cx);
|
||||
assert_eq!(
|
||||
mem::take(&mut *events.borrow_mut()),
|
||||
[("window 1", false), ("window 3", true)]
|
||||
);
|
||||
|
||||
cx.simulate_window_activation(Some(window_3.id()));
|
||||
window_3.simulate_activation(cx);
|
||||
assert_eq!(mem::take(&mut *events.borrow_mut()), []);
|
||||
}
|
||||
|
||||
|
|
|
@ -156,17 +156,16 @@ impl TestAppContext {
|
|||
self.cx.borrow_mut().add_model(build_model)
|
||||
}
|
||||
|
||||
pub fn add_window<T, F>(&mut self, build_root_view: F) -> WindowHandle<T>
|
||||
pub fn add_window<V, F>(&mut self, build_root_view: F) -> WindowHandle<V>
|
||||
where
|
||||
T: View,
|
||||
F: FnOnce(&mut ViewContext<T>) -> T,
|
||||
V: View,
|
||||
F: FnOnce(&mut ViewContext<V>) -> V,
|
||||
{
|
||||
let window = self
|
||||
.cx
|
||||
.borrow_mut()
|
||||
.add_window(Default::default(), build_root_view);
|
||||
self.simulate_window_activation(Some(window.id()));
|
||||
|
||||
window.simulate_activation(self);
|
||||
WindowHandle::new(window.id())
|
||||
}
|
||||
|
||||
|
@ -321,25 +320,6 @@ impl TestAppContext {
|
|||
self.platform_window_mut(window_id).resize_handlers = handlers;
|
||||
}
|
||||
|
||||
pub fn simulate_window_activation(&self, to_activate: Option<usize>) {
|
||||
self.cx.borrow_mut().update(|cx| {
|
||||
let other_window_ids = cx
|
||||
.windows
|
||||
.keys()
|
||||
.filter(|window_id| Some(**window_id) != to_activate)
|
||||
.copied()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for window_id in other_window_ids {
|
||||
cx.window_changed_active_status(window_id, false)
|
||||
}
|
||||
|
||||
if let Some(to_activate) = to_activate {
|
||||
cx.window_changed_active_status(to_activate, true)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn is_window_edited(&self, window_id: usize) -> bool {
|
||||
self.platform_window_mut(window_id).edited
|
||||
}
|
||||
|
|
|
@ -4530,7 +4530,7 @@ mod tests {
|
|||
});
|
||||
|
||||
// Deactivating the window saves the file.
|
||||
cx.simulate_window_activation(None);
|
||||
window.simulate_deactivation(cx);
|
||||
deterministic.run_until_parked();
|
||||
item.read_with(cx, |item, _| assert_eq!(item.save_count, 1));
|
||||
|
||||
|
@ -4551,12 +4551,12 @@ mod tests {
|
|||
item.read_with(cx, |item, _| assert_eq!(item.save_count, 2));
|
||||
|
||||
// Deactivating the window still saves the file.
|
||||
cx.simulate_window_activation(Some(window.id()));
|
||||
window.simulate_activation(cx);
|
||||
item.update(cx, |item, cx| {
|
||||
cx.focus_self();
|
||||
item.is_dirty = true;
|
||||
});
|
||||
cx.simulate_window_activation(None);
|
||||
window.simulate_deactivation(cx);
|
||||
|
||||
deterministic.run_until_parked();
|
||||
item.read_with(cx, |item, _| assert_eq!(item.save_count, 3));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue