Verify keystrokes can be queried while views are on the stack

This commit is contained in:
Antonio Scandurra 2023-05-04 12:09:32 +02:00
parent 3d679ddb26
commit 5cc6304fa6

View file

@ -5678,7 +5678,7 @@ mod tests {
} }
#[crate::test(self)] #[crate::test(self)]
fn test_keystrokes_for_action(cx: &mut AppContext) { fn test_keystrokes_for_action(cx: &mut TestAppContext) {
actions!(test, [Action1, Action2, GlobalAction]); actions!(test, [Action1, Action2, GlobalAction]);
struct View1 {} struct View1 {}
@ -5708,70 +5708,76 @@ mod tests {
} }
} }
let (window_id, view_1) = cx.add_window(Default::default(), |_| View1 {}); let (_, view_1) = cx.add_window(|_| View1 {});
let view_2 = cx.add_view(&view_1, |cx| { let view_2 = cx.add_view(&view_1, |cx| {
cx.focus_self(); cx.focus_self();
View2 {} View2 {}
}); });
cx.add_action(|_: &mut View1, _: &Action1, _cx| {}); cx.update(|cx| {
cx.add_action(|_: &mut View2, _: &Action2, _cx| {}); cx.add_action(|_: &mut View1, _: &Action1, _cx| {});
cx.add_global_action(|_: &GlobalAction, _| {}); cx.add_action(|_: &mut View2, _: &Action2, _cx| {});
cx.add_global_action(|_: &GlobalAction, _| {});
cx.add_bindings(vec![ cx.add_bindings(vec![
Binding::new("a", Action1, Some("View1")), Binding::new("a", Action1, Some("View1")),
Binding::new("b", Action2, Some("View1 > View2")), Binding::new("b", Action2, Some("View1 > View2")),
Binding::new("c", GlobalAction, Some("View3")), // View 3 does not exist Binding::new("c", GlobalAction, Some("View3")), // View 3 does not exist
]); ]);
});
cx.update_window(window_id, |cx| { // Here we update the views to ensure that, even if they are on the stack,
// Sanity check // we can still retrieve keystrokes correctly.
assert_eq!( view_1.update(cx, |_, cx| {
cx.keystrokes_for_action(view_1.id(), &Action1) view_2.update(cx, |_, cx| {
.unwrap() // Sanity check
.as_slice(), assert_eq!(
&[Keystroke::parse("a").unwrap()] cx.keystrokes_for_action(view_1.id(), &Action1)
); .unwrap()
assert_eq!( .as_slice(),
cx.keystrokes_for_action(view_2.id(), &Action2) &[Keystroke::parse("a").unwrap()]
.unwrap() );
.as_slice(), assert_eq!(
&[Keystroke::parse("b").unwrap()] cx.keystrokes_for_action(view_2.id(), &Action2)
); .unwrap()
.as_slice(),
&[Keystroke::parse("b").unwrap()]
);
// The 'a' keystroke propagates up the view tree from view_2 // The 'a' keystroke propagates up the view tree from view_2
// to view_1. The action, Action1, is handled by view_1. // to view_1. The action, Action1, is handled by view_1.
assert_eq!( assert_eq!(
cx.keystrokes_for_action(view_2.id(), &Action1) cx.keystrokes_for_action(view_2.id(), &Action1)
.unwrap() .unwrap()
.as_slice(), .as_slice(),
&[Keystroke::parse("a").unwrap()] &[Keystroke::parse("a").unwrap()]
); );
// Actions that are handled below the current view don't have bindings // Actions that are handled below the current view don't have bindings
assert_eq!(cx.keystrokes_for_action(view_1.id(), &Action2), None); assert_eq!(cx.keystrokes_for_action(view_1.id(), &Action2), None);
// Actions that are handled in other branches of the tree should not have a binding // Actions that are handled in other branches of the tree should not have a binding
assert_eq!(cx.keystrokes_for_action(view_2.id(), &GlobalAction), None); assert_eq!(cx.keystrokes_for_action(view_2.id(), &GlobalAction), None);
// Check that global actions do not have a binding, even if a binding does exist in another view // Check that global actions do not have a binding, even if a binding does exist in another view
assert_eq!( assert_eq!(
&available_actions(view_1.id(), cx), &available_actions(view_1.id(), cx),
&[ &[
("test::Action1", vec![Keystroke::parse("a").unwrap()]), ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
("test::GlobalAction", vec![]) ("test::GlobalAction", vec![])
], ],
); );
// Check that view 1 actions and bindings are available even when called from view 2 // Check that view 1 actions and bindings are available even when called from view 2
assert_eq!( assert_eq!(
&available_actions(view_2.id(), cx), &available_actions(view_2.id(), cx),
&[ &[
("test::Action1", vec![Keystroke::parse("a").unwrap()]), ("test::Action1", vec![Keystroke::parse("a").unwrap()]),
("test::Action2", vec![Keystroke::parse("b").unwrap()]), ("test::Action2", vec![Keystroke::parse("b").unwrap()]),
("test::GlobalAction", vec![]), ("test::GlobalAction", vec![]),
], ],
); );
});
}); });
// Produces a list of actions and key bindings // Produces a list of actions and key bindings