Implement simulated prompts in TestPlatform

This commit is contained in:
Max Brunsfeld 2023-11-14 14:56:50 -08:00
parent 606ab74b9f
commit 860959fe13
4 changed files with 97 additions and 38 deletions

View file

@ -14,6 +14,7 @@ pub struct TestAppContext {
pub background_executor: BackgroundExecutor, pub background_executor: BackgroundExecutor,
pub foreground_executor: ForegroundExecutor, pub foreground_executor: ForegroundExecutor,
pub dispatcher: TestDispatcher, pub dispatcher: TestDispatcher,
pub test_platform: Rc<TestPlatform>,
} }
impl Context for TestAppContext { impl Context for TestAppContext {
@ -77,17 +78,15 @@ impl TestAppContext {
let arc_dispatcher = Arc::new(dispatcher.clone()); let arc_dispatcher = Arc::new(dispatcher.clone());
let background_executor = BackgroundExecutor::new(arc_dispatcher.clone()); let background_executor = BackgroundExecutor::new(arc_dispatcher.clone());
let foreground_executor = ForegroundExecutor::new(arc_dispatcher); let foreground_executor = ForegroundExecutor::new(arc_dispatcher);
let platform = Rc::new(TestPlatform::new( let platform = TestPlatform::new(background_executor.clone(), foreground_executor.clone());
background_executor.clone(),
foreground_executor.clone(),
));
let asset_source = Arc::new(()); let asset_source = Arc::new(());
let http_client = util::http::FakeHttpClient::with_404_response(); let http_client = util::http::FakeHttpClient::with_404_response();
Self { Self {
app: AppContext::new(platform, asset_source, http_client), app: AppContext::new(platform.clone(), asset_source, http_client),
background_executor, background_executor,
foreground_executor, foreground_executor,
dispatcher: dispatcher.clone(), dispatcher: dispatcher.clone(),
test_platform: platform,
} }
} }
@ -154,17 +153,17 @@ impl TestAppContext {
pub fn simulate_new_path_selection( pub fn simulate_new_path_selection(
&self, &self,
_select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>, select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>,
) { ) {
// self.test_platform.simulate_new_path_selection(select_path);
} }
pub fn simulate_prompt_answer(&self, _button_ix: usize) { pub fn simulate_prompt_answer(&self, button_ix: usize) {
// self.test_platform.simulate_prompt_answer(button_ix);
} }
pub fn has_pending_prompt(&self) -> bool { pub fn has_pending_prompt(&self) -> bool {
false self.test_platform.has_pending_prompt()
} }
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut) -> Task<R> pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut) -> Task<R>

View file

@ -3,8 +3,15 @@ use crate::{
PlatformDisplay, PlatformTextSystem, TestDisplay, TestWindow, WindowOptions, PlatformDisplay, PlatformTextSystem, TestDisplay, TestWindow, WindowOptions,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use collections::VecDeque;
use futures::channel::oneshot;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{rc::Rc, sync::Arc}; use std::{
cell::RefCell,
path::PathBuf,
rc::{Rc, Weak},
sync::Arc,
};
pub struct TestPlatform { pub struct TestPlatform {
background_executor: BackgroundExecutor, background_executor: BackgroundExecutor,
@ -13,18 +20,60 @@ pub struct TestPlatform {
active_window: Arc<Mutex<Option<AnyWindowHandle>>>, active_window: Arc<Mutex<Option<AnyWindowHandle>>>,
active_display: Rc<dyn PlatformDisplay>, active_display: Rc<dyn PlatformDisplay>,
active_cursor: Mutex<CursorStyle>, active_cursor: Mutex<CursorStyle>,
pub(crate) prompts: RefCell<TestPrompts>,
weak: Weak<Self>,
}
#[derive(Default)]
pub(crate) struct TestPrompts {
multiple_choice: VecDeque<oneshot::Sender<usize>>,
new_path: VecDeque<(PathBuf, oneshot::Sender<Option<PathBuf>>)>,
} }
impl TestPlatform { impl TestPlatform {
pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Self { pub fn new(executor: BackgroundExecutor, foreground_executor: ForegroundExecutor) -> Rc<Self> {
TestPlatform { Rc::new_cyclic(|weak| TestPlatform {
background_executor: executor, background_executor: executor,
foreground_executor, foreground_executor,
prompts: Default::default(),
active_cursor: Default::default(), active_cursor: Default::default(),
active_display: Rc::new(TestDisplay::new()), active_display: Rc::new(TestDisplay::new()),
active_window: Default::default(), active_window: Default::default(),
} weak: weak.clone(),
})
}
pub(crate) fn simulate_new_path_selection(
&self,
select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>,
) {
let (path, tx) = self
.prompts
.borrow_mut()
.new_path
.pop_front()
.expect("no pending new path prompt");
tx.send(select_path(&path)).ok();
}
pub(crate) fn simulate_prompt_answer(&self, response_ix: usize) {
let tx = self
.prompts
.borrow_mut()
.multiple_choice
.pop_front()
.expect("no pending multiple choice prompt");
tx.send(response_ix).ok();
}
pub(crate) fn has_pending_prompt(&self) -> bool {
!self.prompts.borrow().multiple_choice.is_empty()
}
pub(crate) fn prompt(&self) -> oneshot::Receiver<usize> {
let (tx, rx) = oneshot::channel();
self.prompts.borrow_mut().multiple_choice.push_back(tx);
rx
} }
} }
@ -88,7 +137,11 @@ impl Platform for TestPlatform {
options: WindowOptions, options: WindowOptions,
) -> Box<dyn crate::PlatformWindow> { ) -> Box<dyn crate::PlatformWindow> {
*self.active_window.lock() = Some(handle); *self.active_window.lock() = Some(handle);
Box::new(TestWindow::new(options, self.active_display.clone())) Box::new(TestWindow::new(
options,
self.weak.clone(),
self.active_display.clone(),
))
} }
fn set_display_link_output_callback( fn set_display_link_output_callback(
@ -118,15 +171,20 @@ impl Platform for TestPlatform {
fn prompt_for_paths( fn prompt_for_paths(
&self, &self,
_options: crate::PathPromptOptions, _options: crate::PathPromptOptions,
) -> futures::channel::oneshot::Receiver<Option<Vec<std::path::PathBuf>>> { ) -> oneshot::Receiver<Option<Vec<std::path::PathBuf>>> {
unimplemented!() unimplemented!()
} }
fn prompt_for_new_path( fn prompt_for_new_path(
&self, &self,
_directory: &std::path::Path, directory: &std::path::Path,
) -> futures::channel::oneshot::Receiver<Option<std::path::PathBuf>> { ) -> oneshot::Receiver<Option<std::path::PathBuf>> {
unimplemented!() let (tx, rx) = oneshot::channel();
self.prompts
.borrow_mut()
.new_path
.push_back((directory.to_path_buf(), tx));
rx
} }
fn reveal_path(&self, _path: &std::path::Path) { fn reveal_path(&self, _path: &std::path::Path) {

View file

@ -1,15 +1,13 @@
use std::{
rc::Rc,
sync::{self, Arc},
};
use collections::HashMap;
use parking_lot::Mutex;
use crate::{ use crate::{
px, AtlasKey, AtlasTextureId, AtlasTile, Pixels, PlatformAtlas, PlatformDisplay, px, AtlasKey, AtlasTextureId, AtlasTile, Pixels, PlatformAtlas, PlatformDisplay,
PlatformInputHandler, PlatformWindow, Point, Scene, Size, TileId, WindowAppearance, PlatformInputHandler, PlatformWindow, Point, Scene, Size, TestPlatform, TileId,
WindowBounds, WindowOptions, WindowAppearance, WindowBounds, WindowOptions,
};
use collections::HashMap;
use parking_lot::Mutex;
use std::{
rc::{Rc, Weak},
sync::{self, Arc},
}; };
#[derive(Default)] #[derive(Default)]
@ -25,16 +23,22 @@ pub struct TestWindow {
current_scene: Mutex<Option<Scene>>, current_scene: Mutex<Option<Scene>>,
display: Rc<dyn PlatformDisplay>, display: Rc<dyn PlatformDisplay>,
input_handler: Option<Box<dyn PlatformInputHandler>>, input_handler: Option<Box<dyn PlatformInputHandler>>,
handlers: Mutex<Handlers>, handlers: Mutex<Handlers>,
platform: Weak<TestPlatform>,
sprite_atlas: Arc<dyn PlatformAtlas>, sprite_atlas: Arc<dyn PlatformAtlas>,
} }
impl TestWindow { impl TestWindow {
pub fn new(options: WindowOptions, display: Rc<dyn PlatformDisplay>) -> Self { pub fn new(
options: WindowOptions,
platform: Weak<TestPlatform>,
display: Rc<dyn PlatformDisplay>,
) -> Self {
Self { Self {
bounds: options.bounds, bounds: options.bounds,
current_scene: Default::default(), current_scene: Default::default(),
display, display,
platform,
input_handler: None, input_handler: None,
sprite_atlas: Arc::new(TestAtlas::new()), sprite_atlas: Arc::new(TestAtlas::new()),
handlers: Default::default(), handlers: Default::default(),
@ -89,7 +93,7 @@ impl PlatformWindow for TestWindow {
_msg: &str, _msg: &str,
_answers: &[&str], _answers: &[&str],
) -> futures::channel::oneshot::Receiver<usize> { ) -> futures::channel::oneshot::Receiver<usize> {
todo!() self.platform.upgrade().expect("platform dropped").prompt()
} }
fn activate(&self) { fn activate(&self) {

View file

@ -2033,7 +2033,7 @@ mod tests {
); );
} }
#[gpui::test(iterations = 30)] #[gpui::test(iterations = 10)]
async fn test_adding_directories_via_file(cx: &mut gpui::TestAppContext) { async fn test_adding_directories_via_file(cx: &mut gpui::TestAppContext) {
init_test(cx); init_test(cx);
@ -2653,17 +2653,15 @@ mod tests {
.unwrap(); .unwrap();
// "Save as"" the buffer, creating a new backing file for it // "Save as"" the buffer, creating a new backing file for it
workspace let save_task = workspace
.update(cx, |workspace, cx| { .update(cx, |workspace, cx| {
workspace.save_active_item(workspace::SaveIntent::Save, cx) workspace.save_active_item(workspace::SaveIntent::Save, cx)
}) })
.unwrap()
.await
.unwrap(); .unwrap();
cx.executor().run_until_parked(); cx.executor().run_until_parked();
cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/new"))); cx.simulate_new_path_selection(|_| Some(PathBuf::from("/root/new")));
cx.executor().run_until_parked(); save_task.await.unwrap();
// Rename the file // Rename the file
select_path(&panel, "root/new", cx); select_path(&panel, "root/new", cx);