From fff415e3e908f55e05e8218d99163e459c35a083 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Thu, 4 Jan 2024 21:59:36 -0700 Subject: [PATCH] Improve deactivate simulation --- crates/editor/src/link_go_to_definition.rs | 5 +--- crates/gpui/src/app/test_context.rs | 15 ++++------- crates/gpui/src/platform/test/platform.rs | 22 ++++++++++++++++ crates/gpui/src/platform/test/window.rs | 29 +++------------------- crates/workspace/src/workspace.rs | 8 +++--- 5 files changed, 35 insertions(+), 44 deletions(-) diff --git a/crates/editor/src/link_go_to_definition.rs b/crates/editor/src/link_go_to_definition.rs index 42f502daed..04dcf93015 100644 --- a/crates/editor/src/link_go_to_definition.rs +++ b/crates/editor/src/link_go_to_definition.rs @@ -930,10 +930,7 @@ mod tests { fn do_work() { «test»(); } "}); - // Deactivating the window dismisses the highlight - cx.update_workspace(|workspace, cx| { - workspace.on_window_activation_changed(cx); - }); + cx.cx.cx.deactivate_window(); cx.assert_editor_text_highlights::(indoc! {" fn test() { do_work(); } fn do_work() { test(); } diff --git a/crates/gpui/src/app/test_context.rs b/crates/gpui/src/app/test_context.rs index dfbffaf2e5..a5f66be09b 100644 --- a/crates/gpui/src/app/test_context.rs +++ b/crates/gpui/src/app/test_context.rs @@ -519,16 +519,11 @@ impl<'a> VisualTestContext<'a> { self.cx.simulate_input(self.window, input) } - pub fn simulate_activation(&mut self) { - self.cx - .test_window(self.window) - .simulate_active_status_change(true) - } - - pub fn simulate_deactivation(&mut self) { - self.cx - .test_window(self.window) - .simulate_active_status_change(false) + pub fn deactivate_window(&mut self) { + if Some(self.window) == self.test_platform.active_window() { + self.test_platform.set_active_window(None) + } + self.background_executor.run_until_parked(); } } diff --git a/crates/gpui/src/platform/test/platform.rs b/crates/gpui/src/platform/test/platform.rs index 889e8b971e..111fb83921 100644 --- a/crates/gpui/src/platform/test/platform.rs +++ b/crates/gpui/src/platform/test/platform.rs @@ -79,6 +79,28 @@ impl TestPlatform { self.prompts.borrow_mut().multiple_choice.push_back(tx); rx } + + pub(crate) fn set_active_window(&self, window: Option) { + let executor = self.foreground_executor().clone(); + let previous_window = self.active_window.borrow_mut().take(); + *self.active_window.borrow_mut() = window.clone(); + + executor + .spawn(async move { + if let Some(previous_window) = previous_window { + if let Some(window) = window.as_ref() { + if Arc::ptr_eq(&previous_window.0, &window.0) { + return; + } + } + previous_window.simulate_active_status_change(false); + } + if let Some(window) = window { + window.simulate_active_status_change(true); + } + }) + .detach(); + } } // todo!("implement out what our tests needed in GPUI 1") diff --git a/crates/gpui/src/platform/test/window.rs b/crates/gpui/src/platform/test/window.rs index eee2cf93c7..e0128d2129 100644 --- a/crates/gpui/src/platform/test/window.rs +++ b/crates/gpui/src/platform/test/window.rs @@ -1,6 +1,6 @@ use crate::{ px, AnyWindowHandle, AtlasKey, AtlasTextureId, AtlasTile, Bounds, InputEvent, KeyDownEvent, - Keystroke, Pixels, Platform, PlatformAtlas, PlatformDisplay, PlatformInputHandler, + Keystroke, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PlatformWindow, Point, Size, TestPlatform, TileId, WindowAppearance, WindowBounds, WindowOptions, }; @@ -76,7 +76,7 @@ impl TestWindow { self.0.lock().resize_callback = Some(callback); } - pub fn simulate_active_status_change(&self, active: bool) { + pub(crate) fn simulate_active_status_change(&self, active: bool) { let mut lock = self.0.lock(); let Some(mut callback) = lock.active_status_change_callback.take() else { return; @@ -184,33 +184,12 @@ impl PlatformWindow for TestWindow { } fn activate(&self) { - let this = self.clone(); - let executor = self - .0 + self.0 .lock() .platform .upgrade() .unwrap() - .foreground_executor() - .clone(); - - executor - .spawn(async move { - let state = this.0.lock(); - let platform = state.platform.upgrade().unwrap(); - let previous_window = platform.active_window.borrow_mut().replace(this.clone()); - drop(state); - drop(platform); - if let Some(previous_window) = previous_window { - if Arc::ptr_eq(&previous_window.0, &this.0) { - return; - } - previous_window.simulate_active_status_change(false); - } - - this.simulate_active_status_change(true); - }) - .detach(); + .set_active_window(Some(self.clone())) } fn set_title(&mut self, title: &str) { diff --git a/crates/workspace/src/workspace.rs b/crates/workspace/src/workspace.rs index 6c31366f93..37bf224d4f 100644 --- a/crates/workspace/src/workspace.rs +++ b/crates/workspace/src/workspace.rs @@ -4763,8 +4763,7 @@ mod tests { }); // Deactivating the window saves the file. - cx.simulate_deactivation(); - cx.executor().run_until_parked(); + cx.deactivate_window(); item.update(cx, |item, _| assert_eq!(item.save_count, 1)); // Autosave on focus change. @@ -4784,14 +4783,13 @@ mod tests { item.update(cx, |item, _| assert_eq!(item.save_count, 2)); // Deactivating the window still saves the file. - cx.simulate_activation(); + cx.update(|cx| cx.activate_window()); item.update(cx, |item, cx| { cx.focus_self(); item.is_dirty = true; }); - cx.simulate_deactivation(); + cx.deactivate_window(); - cx.executor().run_until_parked(); item.update(cx, |item, _| assert_eq!(item.save_count, 3)); // Autosave after delay.