Add test to verify closing window via the mouse

This commit is contained in:
Antonio Scandurra 2022-06-23 14:18:53 +02:00
parent d9b5357234
commit 2dae0ddcdb
3 changed files with 69 additions and 33 deletions

View file

@ -22,7 +22,7 @@ use smallvec::SmallVec;
use smol::prelude::*; use smol::prelude::*;
use std::{ use std::{
any::{type_name, Any, TypeId}, any::{type_name, Any, TypeId},
cell::RefCell, cell::{RefCell, RefMut},
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque},
fmt::{self, Debug}, fmt::{self, Debug},
hash::{Hash, Hasher}, hash::{Hash, Hasher},
@ -521,16 +521,8 @@ impl TestAppContext {
pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) { pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) {
use postage::prelude::Sink as _; use postage::prelude::Sink as _;
let mut state = self.cx.borrow_mut(); let mut done_tx = self
let (_, window) = state .window_mut(window_id)
.presenters_and_platform_windows
.get_mut(&window_id)
.unwrap();
let test_window = window
.as_any_mut()
.downcast_mut::<platform::test::Window>()
.unwrap();
let mut done_tx = test_window
.pending_prompts .pending_prompts
.borrow_mut() .borrow_mut()
.pop_front() .pop_front()
@ -539,30 +531,28 @@ impl TestAppContext {
} }
pub fn has_pending_prompt(&self, window_id: usize) -> bool { pub fn has_pending_prompt(&self, window_id: usize) -> bool {
let mut state = self.cx.borrow_mut(); let window = self.window_mut(window_id);
let (_, window) = state let prompts = window.pending_prompts.borrow_mut();
.presenters_and_platform_windows
.get_mut(&window_id)
.unwrap();
let test_window = window
.as_any_mut()
.downcast_mut::<platform::test::Window>()
.unwrap();
let prompts = test_window.pending_prompts.borrow_mut();
!prompts.is_empty() !prompts.is_empty()
} }
pub fn current_window_title(&self, window_id: usize) -> Option<String> { pub fn current_window_title(&self, window_id: usize) -> Option<String> {
let mut state = self.cx.borrow_mut(); self.window_mut(window_id).title.clone()
let (_, window) = state }
.presenters_and_platform_windows
.get_mut(&window_id) pub fn simulate_window_close(&self, window_id: usize) -> bool {
.unwrap(); let handler = self.window_mut(window_id).should_close_handler.take();
let test_window = window if let Some(mut handler) = handler {
.as_any_mut() let should_close = handler();
.downcast_mut::<platform::test::Window>() self.window_mut(window_id).should_close_handler = Some(handler);
.unwrap(); should_close
test_window.title.clone() } else {
false
}
}
pub fn is_window_edited(&self, window_id: usize) -> bool {
self.window_mut(window_id).edited
} }
pub fn leak_detector(&self) -> Arc<Mutex<LeakDetector>> { pub fn leak_detector(&self) -> Arc<Mutex<LeakDetector>> {
@ -576,6 +566,20 @@ impl TestAppContext {
.lock() .lock()
.assert_dropped(handle.id()) .assert_dropped(handle.id())
} }
fn window_mut(&self, window_id: usize) -> RefMut<platform::test::Window> {
RefMut::map(self.cx.borrow_mut(), |state| {
let (_, window) = state
.presenters_and_platform_windows
.get_mut(&window_id)
.unwrap();
let test_window = window
.as_any_mut()
.downcast_mut::<platform::test::Window>()
.unwrap();
test_window
})
}
} }
impl AsyncAppContext { impl AsyncAppContext {

View file

@ -37,7 +37,7 @@ pub struct Window {
event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>, event_handlers: Vec<Box<dyn FnMut(super::Event) -> bool>>,
resize_handlers: Vec<Box<dyn FnMut()>>, resize_handlers: Vec<Box<dyn FnMut()>>,
close_handlers: Vec<Box<dyn FnOnce()>>, close_handlers: Vec<Box<dyn FnOnce()>>,
should_close_handler: Option<Box<dyn FnMut() -> bool>>, pub(crate) should_close_handler: Option<Box<dyn FnMut() -> bool>>,
pub(crate) title: Option<String>, pub(crate) title: Option<String>,
pub(crate) edited: bool, pub(crate) edited: bool,
pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>, pub(crate) pending_prompts: RefCell<VecDeque<oneshot::Sender<usize>>>,

View file

@ -371,7 +371,9 @@ mod tests {
use super::*; use super::*;
use assets::Assets; use assets::Assets;
use editor::{Autoscroll, DisplayPoint, Editor}; use editor::{Autoscroll, DisplayPoint, Editor};
use gpui::{AssetSource, MutableAppContext, TestAppContext, ViewHandle}; use gpui::{
executor::Deterministic, AssetSource, MutableAppContext, TestAppContext, ViewHandle,
};
use project::ProjectPath; use project::ProjectPath;
use serde_json::json; use serde_json::json;
use std::{ use std::{
@ -439,6 +441,36 @@ mod tests {
assert_eq!(cx.window_ids().len(), 2); assert_eq!(cx.window_ids().len(), 2);
} }
#[gpui::test]
async fn test_closing_window_via_mouse(executor: Arc<Deterministic>, cx: &mut TestAppContext) {
let app_state = init(cx);
app_state
.fs
.as_fake()
.insert_tree("/root", json!({"a": "hey"}))
.await;
cx.update(|cx| open_paths(&[PathBuf::from("/root/a")], &app_state, cx))
.await;
assert_eq!(cx.window_ids().len(), 1);
let workspace = cx.root_view::<Workspace>(cx.window_ids()[0]).unwrap();
let editor = workspace.read_with(cx, |workspace, cx| {
workspace
.active_item(cx)
.unwrap()
.downcast::<Editor>()
.unwrap()
});
editor.update(cx, |editor, cx| editor.insert("EDIT", cx));
assert!(!cx.simulate_window_close(workspace.window_id()));
executor.run_until_parked();
cx.simulate_prompt_answer(workspace.window_id(), 1);
executor.run_until_parked();
assert_eq!(cx.window_ids().len(), 0);
}
#[gpui::test] #[gpui::test]
async fn test_new_empty_workspace(cx: &mut TestAppContext) { async fn test_new_empty_workspace(cx: &mut TestAppContext) {
let app_state = init(cx); let app_state = init(cx);