Improve deactivate simulation

Conrad Irwin created

Change summary

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(-)

Detailed changes

@@ -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::<LinkGoToDefinitionState>(indoc! {"
                 fn test() { do_work(); }
                 fn do_work() { test(); }

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();
     }
 }
 

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<TestWindow>) {
+        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")

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) {

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.