Simulate calling of text-insertion APIs in TestAppContext::dispatch_keystroke

Max Brunsfeld created

Change summary

crates/editor/src/editor.rs | 16 ++++++++--------
crates/editor/src/test.rs   |  6 ------
crates/gpui/src/app.rs      | 27 ++++++++++++++++++++++-----
3 files changed, 30 insertions(+), 19 deletions(-)

Detailed changes

crates/editor/src/editor.rs 🔗

@@ -9917,7 +9917,7 @@ mod tests {
             one|
             two
             three"});
-        cx.simulate_input(".");
+        cx.simulate_keystroke(".");
         handle_completion_request(
             &mut cx,
             indoc! {"
@@ -9963,9 +9963,9 @@ mod tests {
             two|
             three|
             additional edit"});
-        cx.simulate_input(" ");
+        cx.simulate_keystroke(" ");
         assert!(cx.editor(|e, _| e.context_menu.is_none()));
-        cx.simulate_input("s");
+        cx.simulate_keystroke("s");
         assert!(cx.editor(|e, _| e.context_menu.is_none()));
 
         cx.assert_editor_state(indoc! {"
@@ -9986,7 +9986,7 @@ mod tests {
         cx.condition(|editor, _| editor.context_menu_visible())
             .await;
 
-        cx.simulate_input("i");
+        cx.simulate_keystroke("i");
 
         handle_completion_request(
             &mut cx,
@@ -10021,11 +10021,11 @@ mod tests {
             })
         });
         cx.set_state("editor|");
-        cx.simulate_input(".");
+        cx.simulate_keystroke(".");
         assert!(cx.editor(|e, _| e.context_menu.is_none()));
-        cx.simulate_input("c");
-        cx.simulate_input("l");
-        cx.simulate_input("o");
+        cx.simulate_keystroke("c");
+        cx.simulate_keystroke("l");
+        cx.simulate_keystroke("o");
         cx.assert_editor_state("editor.clo|");
         assert!(cx.editor(|e, _| e.context_menu.is_none()));
         cx.update_editor(|editor, cx| {

crates/editor/src/test.rs 🔗

@@ -165,12 +165,6 @@ impl<'a> EditorTestContext<'a> {
         })
     }
 
-    pub fn simulate_input(&mut self, input: &str) {
-        self.editor.update(self.cx, |editor, cx| {
-            editor.handle_input(input, cx);
-        });
-    }
-
     pub fn update_buffer<F, T>(&mut self, update: F) -> T
     where
         F: FnOnce(&mut Buffer, &mut ModelContext<Buffer>) -> T,

crates/gpui/src/app.rs 🔗

@@ -475,7 +475,7 @@ impl TestAppContext {
     }
 
     pub fn dispatch_keystroke(&mut self, window_id: usize, keystroke: Keystroke, is_held: bool) {
-        self.cx.borrow_mut().update(|cx| {
+        let handled = self.cx.borrow_mut().update(|cx| {
             let presenter = cx
                 .presenters_and_platform_windows
                 .get(&window_id)
@@ -484,12 +484,29 @@ impl TestAppContext {
                 .clone();
             let dispatch_path = presenter.borrow().dispatch_path(cx.as_ref());
 
-            if !cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) {
-                presenter
-                    .borrow_mut()
-                    .dispatch_event(Event::KeyDown(KeyDownEvent { keystroke, is_held }), cx);
+            if cx.dispatch_keystroke(window_id, dispatch_path, &keystroke) {
+                return true;
             }
+            if presenter.borrow_mut().dispatch_event(
+                Event::KeyDown(KeyDownEvent {
+                    keystroke: keystroke.clone(),
+                    is_held,
+                }),
+                cx,
+            ) {
+                return true;
+            }
+
+            false
         });
+
+        if !handled && !keystroke.cmd && !keystroke.ctrl {
+            WindowInputHandler {
+                app: self.cx.clone(),
+                window_id,
+            }
+            .replace_text_in_range(None, &keystroke.key)
+        }
     }
 
     pub fn add_model<T, F>(&mut self, build_model: F) -> ModelHandle<T>