From 1fcbadaa990137b38b6e6bddcb352584ef7984ae Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 5 May 2021 11:04:39 -0700 Subject: [PATCH] Add TestAppContext::simulate_new_path_selection --- gpui/src/app.rs | 21 ++++++++++++++------- gpui/src/platform/test.rs | 33 +++++++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index 75d0ae01462be12ede526938f4126111c20ab076..798805f63ebb98923956d5ea73b2b558706e3545 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -87,7 +87,7 @@ pub enum MenuItem<'a> { pub struct App(Rc>); #[derive(Clone)] -pub struct TestAppContext(Rc>); +pub struct TestAppContext(Rc>, Rc); impl App { pub fn test T>( @@ -111,13 +111,16 @@ impl App { Fn: FnOnce(TestAppContext) -> F, F: Future, { - 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 { self.0.borrow().platform.clone() } + + pub fn simulate_new_path_selection(&self, result: impl FnOnce(PathBuf) -> Option) { + self.1.as_ref().simulate_new_path_selection(result); + } } impl UpdateModel for TestAppContext { diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index de385bbf844a0493579500a18aee8726bd34c608..6ee772c7e05467911dda82ef705ad512ada5b1da 100644 --- a/gpui/src/platform/test.rs +++ b/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, fonts: Arc, current_clipboard_item: RefCell>, + last_prompt_for_new_path_args: RefCell)>)>>, } 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, + ) { + 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)>) {} + fn prompt_for_new_path(&self, path: &Path, f: Box)>) { + *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() }