| 1 | //! Panel doubles shared by the dock's tests. |
| 2 | //! |
| 3 | //! This lives beside the production modules rather than inside one module's |
| 4 | //! `mod tests` so `tab_group`, `dock_area`, and the cutover tests can share a |
| 5 | //! single panel double and a single ordered delivery log. Ported from the |
| 6 | //! `TabPanel` tests in `crates/component/src/dock/tab_panel.rs`. |
| 7 | |
| 8 | use std::sync::{Arc, Mutex}; |
| 9 | |
| 10 | use gpui::{ |
| 11 | App, AppContext as _, Context, Empty, Entity, EventEmitter, FocusHandle, Focusable, |
| 12 | IntoElement, Render, TestAppContext, VisualTestContext, WeakEntity, Window, |
| 13 | }; |
| 14 | |
| 15 | use super::layout::{NodeId, PanelId}; |
| 16 | use super::panel::{Panel, PanelEvent, PanelView}; |
| 17 | use super::tab_group::{TabGroup, TabGroupConstraints}; |
| 18 | |
| 19 | /// One thing a panel was told, in delivery order. |
| 20 | /// |
| 21 | /// `set_active` and the membership callbacks share one log because their |
| 22 | /// relative order is itself part of the contract under test: a panel joining a |
| 23 | /// group must see `Added` before it is told it is active. |
| 24 | /// |
| 25 | /// `Removed` is also where a deliberate divergence from `crates/component` shows up. |
| 26 | /// The old `TabPanel::detach_panel` calls `on_removed` on every detach, |
| 27 | /// including the detach half of a drag between groups, so a moved panel is |
| 28 | /// told it was removed and then added again. In the tree world a move never |
| 29 | /// leaves the tree — `PaneTree::move_panel` reports no `removed_panels`, and |
| 30 | /// `EditResult::removed_panels` documents that a moved panel is absent from it |
| 31 | /// precisely so its entity survives. So under the new contract a moved panel |
| 32 | /// must never see `Removed`. |
| 33 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 34 | pub(crate) enum PanelSignal { |
| 35 | Active(bool), |
| 36 | Zoomed(bool), |
| 37 | Added, |
| 38 | Removed, |
| 39 | } |
| 40 | |
| 41 | /// Shared, cross-panel ordered log of everything the panels were told. |
| 42 | pub(crate) type Log = Arc<Mutex<Vec<(&'static str, PanelSignal)>>>; |
| 43 | |
| 44 | pub(crate) fn log_of() -> Log { |
| 45 | Log::default() |
| 46 | } |
| 47 | |
| 48 | /// Take everything logged so far, leaving the log empty. |
| 49 | pub(crate) fn drain(log: &Log) -> Vec<(&'static str, PanelSignal)> { |
| 50 | std::mem::take(&mut *log.lock().unwrap()) |
| 51 | } |
| 52 | |
| 53 | /// [`drain`], keeping only the `set_active` edges. Most tests care about the |
| 54 | /// active-state contract and would otherwise have to filter the membership |
| 55 | /// callbacks out by hand at every assertion. |
| 56 | pub(crate) fn drain_active(log: &Log) -> Vec<(&'static str, bool)> { |
| 57 | drain(log) |
| 58 | .into_iter() |
| 59 | .filter_map(|(name, signal)| match signal { |
| 60 | PanelSignal::Active(active) => Some((name, active)), |
| 61 | _ => None, |
| 62 | }) |
| 63 | .collect() |
| 64 | } |
| 65 | |
| 66 | pub(crate) struct TestPanel { |
| 67 | name: &'static str, |
| 68 | focus_handle: FocusHandle, |
| 69 | log: Log, |
| 70 | visible: bool, |
| 71 | zoomable: bool, |
| 72 | pub(crate) group: Option<WeakEntity<TabGroup>>, |
| 73 | } |
| 74 | |
| 75 | impl TestPanel { |
| 76 | /// A panel whose deliveries go nowhere. For tests that only need a panel |
| 77 | /// to exist. |
| 78 | pub(crate) fn new(name: &'static str, cx: &mut App) -> Entity<Self> { |
| 79 | Self::logging(name, &log_of(), cx) |
| 80 | } |
| 81 | |
| 82 | pub(crate) fn logging(name: &'static str, log: &Log, cx: &mut App) -> Entity<Self> { |
| 83 | let log = log.clone(); |
| 84 | cx.new(|cx| Self { |
| 85 | name, |
| 86 | focus_handle: cx.focus_handle(), |
| 87 | log, |
| 88 | visible: true, |
| 89 | zoomable: true, |
| 90 | group: None, |
| 91 | }) |
| 92 | } |
| 93 | |
| 94 | pub(crate) fn set_visible(&mut self, visible: bool, cx: &mut Context<Self>) { |
| 95 | self.visible = visible; |
| 96 | cx.notify(); |
| 97 | } |
| 98 | |
| 99 | /// A panel that refuses to zoom, which its group must honour. |
| 100 | pub(crate) fn set_zoomable(&mut self, zoomable: bool, cx: &mut Context<Self>) { |
| 101 | self.zoomable = zoomable; |
| 102 | cx.notify(); |
| 103 | } |
| 104 | |
| 105 | fn record(&self, signal: PanelSignal) { |
| 106 | self.log.lock().unwrap().push((self.name, signal)); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl Panel for TestPanel { |
| 111 | /// The panel's own name, so a dumped layout is legible in assertions. |
| 112 | fn panel_name(&self) -> &'static str { |
| 113 | self.name |
| 114 | } |
| 115 | |
| 116 | fn visible(&self, _: &App) -> bool { |
| 117 | self.visible |
| 118 | } |
| 119 | |
| 120 | fn zoomable(&self, _: &App) -> bool { |
| 121 | self.zoomable |
| 122 | } |
| 123 | |
| 124 | fn set_active(&mut self, active: bool, _: &mut Window, _: &mut Context<Self>) { |
| 125 | self.record(PanelSignal::Active(active)); |
| 126 | } |
| 127 | |
| 128 | fn set_zoomed(&mut self, zoomed: bool, _: &mut Window, _: &mut Context<Self>) { |
| 129 | self.record(PanelSignal::Zoomed(zoomed)); |
| 130 | } |
| 131 | |
| 132 | fn on_added_to(&mut self, group: WeakEntity<TabGroup>, _: &mut Window, _: &mut Context<Self>) { |
| 133 | self.group = Some(group); |
| 134 | self.record(PanelSignal::Added); |
| 135 | } |
| 136 | |
| 137 | fn on_removed(&mut self, _: &mut Window, _: &mut Context<Self>) { |
| 138 | self.group = None; |
| 139 | self.record(PanelSignal::Removed); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | impl EventEmitter<PanelEvent> for TestPanel {} |
| 144 | |
| 145 | impl Focusable for TestPanel { |
| 146 | fn focus_handle(&self, _: &App) -> FocusHandle { |
| 147 | self.focus_handle.clone() |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | impl Render for TestPanel { |
| 152 | fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement { |
| 153 | Empty |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | pub(crate) fn panel_id(panel: &Entity<TestPanel>, cx: &TestAppContext) -> PanelId { |
| 158 | cx.read(|cx| Arc::new(panel.clone()).panel_id(cx)) |
| 159 | } |
| 160 | |
| 161 | /// Open a window holding one tab group over freshly built panels named |
| 162 | /// `names`, with the first tab displayed. |
| 163 | pub(crate) fn build_group<'a>( |
| 164 | log: &Log, |
| 165 | names: &[&'static str], |
| 166 | cx: &'a mut TestAppContext, |
| 167 | ) -> ( |
| 168 | Entity<TabGroup>, |
| 169 | Vec<Entity<TestPanel>>, |
| 170 | &'a mut VisualTestContext, |
| 171 | ) { |
| 172 | let (group, cx) = |
| 173 | cx.add_window_view(|window, cx| TabGroup::new(NodeId::from_u64(1), window, cx)); |
| 174 | |
| 175 | let names = names.to_vec(); |
| 176 | let log = log.clone(); |
| 177 | let panels = cx.update(|window, cx| { |
| 178 | let panels: Vec<_> = names |
| 179 | .iter() |
| 180 | .map(|name| TestPanel::logging(name, &log, cx)) |
| 181 | .collect(); |
| 182 | let views: Vec<Arc<dyn PanelView>> = panels |
| 183 | .iter() |
| 184 | .map(|panel| Arc::new(panel.clone()) as Arc<dyn PanelView>) |
| 185 | .collect(); |
| 186 | group.update(cx, |group, cx| { |
| 187 | // A freshly built group is sealed; place it beside siblings so |
| 188 | // tests that do not care about constraints get a working group. |
| 189 | group.set_constraints(TabGroupConstraints::in_split(false), window, cx); |
| 190 | group.sync_from_tree(views, 0, window, cx); |
| 191 | }); |
| 192 | panels |
| 193 | }); |
| 194 | |
| 195 | (group, panels, cx) |
| 196 | } |