From 2dae0ddcdb13b394bad02e786b13a104136dac9f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Thu, 23 Jun 2022 14:18:53 +0200 Subject: [PATCH] Add test to verify closing window via the mouse --- crates/gpui/src/app.rs | 66 +++++++++++++++++--------------- crates/gpui/src/platform/test.rs | 2 +- crates/zed/src/zed.rs | 34 +++++++++++++++- 3 files changed, 69 insertions(+), 33 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index f5bfb390902ec1962f15963ad73b41b59022ab4c..245ee0da1ed9895944aa022d945419df951e253f 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -22,7 +22,7 @@ use smallvec::SmallVec; use smol::prelude::*; use std::{ any::{type_name, Any, TypeId}, - cell::RefCell, + cell::{RefCell, RefMut}, collections::{hash_map::Entry, BTreeMap, HashMap, HashSet, VecDeque}, fmt::{self, Debug}, hash::{Hash, Hasher}, @@ -521,16 +521,8 @@ impl TestAppContext { pub fn simulate_prompt_answer(&self, window_id: usize, answer: usize) { use postage::prelude::Sink as _; - let mut state = self.cx.borrow_mut(); - let (_, window) = state - .presenters_and_platform_windows - .get_mut(&window_id) - .unwrap(); - let test_window = window - .as_any_mut() - .downcast_mut::() - .unwrap(); - let mut done_tx = test_window + let mut done_tx = self + .window_mut(window_id) .pending_prompts .borrow_mut() .pop_front() @@ -539,30 +531,28 @@ impl TestAppContext { } pub fn has_pending_prompt(&self, window_id: usize) -> bool { - let mut state = self.cx.borrow_mut(); - let (_, window) = state - .presenters_and_platform_windows - .get_mut(&window_id) - .unwrap(); - let test_window = window - .as_any_mut() - .downcast_mut::() - .unwrap(); - let prompts = test_window.pending_prompts.borrow_mut(); + let window = self.window_mut(window_id); + let prompts = window.pending_prompts.borrow_mut(); !prompts.is_empty() } pub fn current_window_title(&self, window_id: usize) -> Option { - let mut state = self.cx.borrow_mut(); - let (_, window) = state - .presenters_and_platform_windows - .get_mut(&window_id) - .unwrap(); - let test_window = window - .as_any_mut() - .downcast_mut::() - .unwrap(); - test_window.title.clone() + self.window_mut(window_id).title.clone() + } + + pub fn simulate_window_close(&self, window_id: usize) -> bool { + let handler = self.window_mut(window_id).should_close_handler.take(); + if let Some(mut handler) = handler { + let should_close = handler(); + self.window_mut(window_id).should_close_handler = Some(handler); + should_close + } else { + false + } + } + + pub fn is_window_edited(&self, window_id: usize) -> bool { + self.window_mut(window_id).edited } pub fn leak_detector(&self) -> Arc> { @@ -576,6 +566,20 @@ impl TestAppContext { .lock() .assert_dropped(handle.id()) } + + fn window_mut(&self, window_id: usize) -> RefMut { + 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::() + .unwrap(); + test_window + }) + } } impl AsyncAppContext { diff --git a/crates/gpui/src/platform/test.rs b/crates/gpui/src/platform/test.rs index 9ffd95ea0e9be1db4d24f394f13a38378b4d586b..eb241797c3b8c570c5e5a1163bd9efd691d78f1a 100644 --- a/crates/gpui/src/platform/test.rs +++ b/crates/gpui/src/platform/test.rs @@ -37,7 +37,7 @@ pub struct Window { event_handlers: Vec bool>>, resize_handlers: Vec>, close_handlers: Vec>, - should_close_handler: Option bool>>, + pub(crate) should_close_handler: Option bool>>, pub(crate) title: Option, pub(crate) edited: bool, pub(crate) pending_prompts: RefCell>>, diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 35981bda0b638515305c36ad82450bec9ab98d13..0f869d5b5b0c7482f11095991a29e6b880de46ce 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -371,7 +371,9 @@ mod tests { use super::*; use assets::Assets; use editor::{Autoscroll, DisplayPoint, Editor}; - use gpui::{AssetSource, MutableAppContext, TestAppContext, ViewHandle}; + use gpui::{ + executor::Deterministic, AssetSource, MutableAppContext, TestAppContext, ViewHandle, + }; use project::ProjectPath; use serde_json::json; use std::{ @@ -439,6 +441,36 @@ mod tests { assert_eq!(cx.window_ids().len(), 2); } + #[gpui::test] + async fn test_closing_window_via_mouse(executor: Arc, 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::(cx.window_ids()[0]).unwrap(); + let editor = workspace.read_with(cx, |workspace, cx| { + workspace + .active_item(cx) + .unwrap() + .downcast::() + .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] async fn test_new_empty_workspace(cx: &mut TestAppContext) { let app_state = init(cx);