Add TestAppContext::simulate_new_path_selection

Max Brunsfeld created

Change summary

gpui/src/app.rs           | 21 ++++++++++++++-------
gpui/src/platform/test.rs | 33 +++++++++++++++++++++++++++------
2 files changed, 41 insertions(+), 13 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -87,7 +87,7 @@ pub enum MenuItem<'a> {
 pub struct App(Rc<RefCell<MutableAppContext>>);
 
 #[derive(Clone)]
-pub struct TestAppContext(Rc<RefCell<MutableAppContext>>);
+pub struct TestAppContext(Rc<RefCell<MutableAppContext>>, Rc<platform::test::Platform>);
 
 impl App {
     pub fn test<T, A: AssetSource, F: FnOnce(&mut MutableAppContext) -> T>(
@@ -111,13 +111,16 @@ impl App {
         Fn: FnOnce(TestAppContext) -> F,
         F: Future<Output = T>,
     {
-        let platform = platform::test::platform();
+        let platform = Rc::new(platform::test::platform());
         let foreground = Rc::new(executor::Foreground::test());
-        let ctx = TestAppContext(Rc::new(RefCell::new(MutableAppContext::new(
-            foreground.clone(),
-            Rc::new(platform),
-            asset_source,
-        ))));
+        let ctx = TestAppContext(
+            Rc::new(RefCell::new(MutableAppContext::new(
+                foreground.clone(),
+                platform.clone(),
+                asset_source,
+            ))),
+            platform,
+        );
         ctx.0.borrow_mut().weak_self = Some(Rc::downgrade(&ctx.0));
 
         let future = f(ctx);
@@ -332,6 +335,10 @@ impl TestAppContext {
     pub fn platform(&self) -> Rc<dyn platform::Platform> {
         self.0.borrow().platform.clone()
     }
+
+    pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option<PathBuf>) {
+        self.1.as_ref().simulate_new_path_selection(result);
+    }
 }
 
 impl UpdateModel for TestAppContext {

gpui/src/platform/test.rs 🔗

@@ -1,11 +1,18 @@
 use crate::ClipboardItem;
 use pathfinder_geometry::vector::Vector2F;
-use std::{any::Any, cell::RefCell, path::Path, rc::Rc, sync::Arc};
-
-struct Platform {
+use std::{
+    any::Any,
+    cell::RefCell,
+    path::{Path, PathBuf},
+    rc::Rc,
+    sync::Arc,
+};
+
+pub(crate) struct Platform {
     dispatcher: Arc<dyn super::Dispatcher>,
     fonts: Arc<dyn super::FontSystem>,
     current_clipboard_item: RefCell<Option<ClipboardItem>>,
+    last_prompt_for_new_path_args: RefCell<Option<(PathBuf, Box<dyn FnOnce(Option<PathBuf>)>)>>,
 }
 
 struct Dispatcher;
@@ -23,9 +30,21 @@ impl Platform {
         Self {
             dispatcher: Arc::new(Dispatcher),
             fonts: Arc::new(super::current::FontSystem::new()),
-            current_clipboard_item: RefCell::new(None),
+            current_clipboard_item: Default::default(),
+            last_prompt_for_new_path_args: Default::default(),
         }
     }
+
+    pub(crate) fn simulate_new_path_selection(
+        &self,
+        result: impl FnOnce(PathBuf) -> Option<PathBuf>,
+    ) {
+        let (dir_path, callback) = self
+            .last_prompt_for_new_path_args
+            .take()
+            .expect("prompt_for_new_path was not called");
+        callback(result(dir_path));
+    }
 }
 
 impl super::Platform for Platform {
@@ -77,7 +96,9 @@ impl super::Platform for Platform {
     ) {
     }
 
-    fn prompt_for_new_path(&self, _: &Path, _: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {}
+    fn prompt_for_new_path(&self, path: &Path, f: Box<dyn FnOnce(Option<std::path::PathBuf>)>) {
+        *self.last_prompt_for_new_path_args.borrow_mut() = Some((path.to_path_buf(), f));
+    }
 
     fn write_to_clipboard(&self, item: ClipboardItem) {
         *self.current_clipboard_item.borrow_mut() = Some(item);
@@ -134,6 +155,6 @@ impl super::Window for Window {
     }
 }
 
-pub fn platform() -> impl super::Platform {
+pub(crate) fn platform() -> Platform {
     Platform::new()
 }