1use super::*;
2use editor::Editor;
3use gpui::{TestAppContext, VisualTestContext};
4use menu::SelectPrevious;
5use project::{Project, ProjectPath};
6use serde_json::json;
7use util::{path, rel_path::rel_path};
8use workspace::{ActivatePreviousItem, AppState, Workspace};
9
10#[ctor::ctor]
11fn init_logger() {
12 zlog::init_test();
13}
14
15#[gpui::test]
16async fn test_open_with_prev_tab_selected_and_cycle_on_toggle_action(
17 cx: &mut gpui::TestAppContext,
18) {
19 let app_state = init_test(cx);
20
21 app_state
22 .fs
23 .as_fake()
24 .insert_tree(
25 path!("/root"),
26 json!({
27 "1.txt": "First file",
28 "2.txt": "Second file",
29 "3.txt": "Third file",
30 "4.txt": "Fourth file",
31 }),
32 )
33 .await;
34
35 let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;
36 let (workspace, cx) =
37 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
38
39 let tab_1 = open_buffer("1.txt", &workspace, cx).await;
40 let tab_2 = open_buffer("2.txt", &workspace, cx).await;
41 let tab_3 = open_buffer("3.txt", &workspace, cx).await;
42 let tab_4 = open_buffer("4.txt", &workspace, cx).await;
43
44 // Starts with the previously opened item selected
45 let tab_switcher = open_tab_switcher(false, &workspace, cx);
46 tab_switcher.update(cx, |tab_switcher, _| {
47 assert_eq!(tab_switcher.delegate.matches.len(), 4);
48 assert_match_at_position(tab_switcher, 0, tab_4.boxed_clone());
49 assert_match_selection(tab_switcher, 1, tab_3.boxed_clone());
50 assert_match_at_position(tab_switcher, 2, tab_2.boxed_clone());
51 assert_match_at_position(tab_switcher, 3, tab_1.boxed_clone());
52 });
53
54 cx.dispatch_action(Toggle { select_last: false });
55 cx.dispatch_action(Toggle { select_last: false });
56 tab_switcher.update(cx, |tab_switcher, _| {
57 assert_eq!(tab_switcher.delegate.matches.len(), 4);
58 assert_match_at_position(tab_switcher, 0, tab_4.boxed_clone());
59 assert_match_at_position(tab_switcher, 1, tab_3.boxed_clone());
60 assert_match_at_position(tab_switcher, 2, tab_2.boxed_clone());
61 assert_match_selection(tab_switcher, 3, tab_1.boxed_clone());
62 });
63
64 cx.dispatch_action(SelectPrevious);
65 tab_switcher.update(cx, |tab_switcher, _| {
66 assert_eq!(tab_switcher.delegate.matches.len(), 4);
67 assert_match_at_position(tab_switcher, 0, tab_4.boxed_clone());
68 assert_match_at_position(tab_switcher, 1, tab_3.boxed_clone());
69 assert_match_selection(tab_switcher, 2, tab_2.boxed_clone());
70 assert_match_at_position(tab_switcher, 3, tab_1.boxed_clone());
71 });
72}
73
74#[gpui::test]
75async fn test_open_with_last_tab_selected(cx: &mut gpui::TestAppContext) {
76 let app_state = init_test(cx);
77
78 app_state
79 .fs
80 .as_fake()
81 .insert_tree(
82 path!("/root"),
83 json!({
84 "1.txt": "First file",
85 "2.txt": "Second file",
86 "3.txt": "Third file",
87 }),
88 )
89 .await;
90
91 let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;
92 let (workspace, cx) =
93 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
94
95 let tab_1 = open_buffer("1.txt", &workspace, cx).await;
96 let tab_2 = open_buffer("2.txt", &workspace, cx).await;
97 let tab_3 = open_buffer("3.txt", &workspace, cx).await;
98
99 // Starts with the last item selected
100 let tab_switcher = open_tab_switcher(true, &workspace, cx);
101 tab_switcher.update(cx, |tab_switcher, _| {
102 assert_eq!(tab_switcher.delegate.matches.len(), 3);
103 assert_match_at_position(tab_switcher, 0, tab_3);
104 assert_match_at_position(tab_switcher, 1, tab_2);
105 assert_match_selection(tab_switcher, 2, tab_1);
106 });
107}
108
109#[gpui::test]
110async fn test_open_item_on_modifiers_release(cx: &mut gpui::TestAppContext) {
111 let app_state = init_test(cx);
112
113 app_state
114 .fs
115 .as_fake()
116 .insert_tree(
117 path!("/root"),
118 json!({
119 "1.txt": "First file",
120 "2.txt": "Second file",
121 }),
122 )
123 .await;
124
125 let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;
126 let (workspace, cx) =
127 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
128
129 let tab_1 = open_buffer("1.txt", &workspace, cx).await;
130 let tab_2 = open_buffer("2.txt", &workspace, cx).await;
131
132 cx.simulate_modifiers_change(Modifiers::control());
133 let tab_switcher = open_tab_switcher(false, &workspace, cx);
134 tab_switcher.update(cx, |tab_switcher, _| {
135 assert_eq!(tab_switcher.delegate.matches.len(), 2);
136 assert_match_at_position(tab_switcher, 0, tab_2.boxed_clone());
137 assert_match_selection(tab_switcher, 1, tab_1.boxed_clone());
138 });
139
140 cx.simulate_modifiers_change(Modifiers::none());
141 cx.read(|cx| {
142 let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
143 assert_eq!(active_editor.read(cx).title(cx), "1.txt");
144 });
145 assert_tab_switcher_is_closed(workspace, cx);
146}
147
148#[gpui::test]
149async fn test_open_on_empty_pane(cx: &mut gpui::TestAppContext) {
150 let app_state = init_test(cx);
151 app_state.fs.as_fake().insert_tree("/root", json!({})).await;
152
153 let project = Project::test(app_state.fs.clone(), ["/root".as_ref()], cx).await;
154 let (workspace, cx) =
155 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
156
157 cx.simulate_modifiers_change(Modifiers::control());
158 let tab_switcher = open_tab_switcher(false, &workspace, cx);
159 tab_switcher.update(cx, |tab_switcher, _| {
160 assert!(tab_switcher.delegate.matches.is_empty());
161 });
162
163 cx.simulate_modifiers_change(Modifiers::none());
164 assert_tab_switcher_is_closed(workspace, cx);
165}
166
167#[gpui::test]
168async fn test_open_with_single_item(cx: &mut gpui::TestAppContext) {
169 let app_state = init_test(cx);
170 app_state
171 .fs
172 .as_fake()
173 .insert_tree(path!("/root"), json!({"1.txt": "Single file"}))
174 .await;
175
176 let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;
177 let (workspace, cx) =
178 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
179
180 let tab = open_buffer("1.txt", &workspace, cx).await;
181
182 let tab_switcher = open_tab_switcher(false, &workspace, cx);
183 tab_switcher.update(cx, |tab_switcher, _| {
184 assert_eq!(tab_switcher.delegate.matches.len(), 1);
185 assert_match_selection(tab_switcher, 0, tab);
186 });
187}
188
189#[gpui::test]
190async fn test_close_selected_item(cx: &mut gpui::TestAppContext) {
191 let app_state = init_test(cx);
192 app_state
193 .fs
194 .as_fake()
195 .insert_tree(
196 path!("/root"),
197 json!({
198 "1.txt": "First file",
199 "2.txt": "Second file",
200 "3.txt": "Third file",
201 "4.txt": "Fourth file",
202 }),
203 )
204 .await;
205
206 let project = Project::test(app_state.fs.clone(), [path!("/root").as_ref()], cx).await;
207 let (workspace, cx) =
208 cx.add_window_view(|window, cx| Workspace::test_new(project.clone(), window, cx));
209
210 let tab_1 = open_buffer("1.txt", &workspace, cx).await;
211 let tab_3 = open_buffer("3.txt", &workspace, cx).await;
212 let tab_2 = open_buffer("2.txt", &workspace, cx).await;
213 let tab_4 = open_buffer("4.txt", &workspace, cx).await;
214
215 // After opening all buffers, let's navigate to the previous item two times, finishing with:
216 //
217 // 1.txt | [3.txt] | 2.txt | 4.txt
218 //
219 // With 3.txt being the active item in the pane.
220 cx.dispatch_action(ActivatePreviousItem);
221 cx.dispatch_action(ActivatePreviousItem);
222 cx.run_until_parked();
223
224 cx.simulate_modifiers_change(Modifiers::control());
225 let tab_switcher = open_tab_switcher(false, &workspace, cx);
226 tab_switcher.update(cx, |tab_switcher, _| {
227 assert_eq!(tab_switcher.delegate.matches.len(), 4);
228 assert_match_at_position(tab_switcher, 0, tab_3.boxed_clone());
229 assert_match_selection(tab_switcher, 1, tab_2.boxed_clone());
230 assert_match_at_position(tab_switcher, 2, tab_4.boxed_clone());
231 assert_match_at_position(tab_switcher, 3, tab_1.boxed_clone());
232 });
233
234 cx.simulate_modifiers_change(Modifiers::control());
235 cx.dispatch_action(CloseSelectedItem);
236 tab_switcher.update(cx, |tab_switcher, _| {
237 assert_eq!(tab_switcher.delegate.matches.len(), 3);
238 assert_match_selection(tab_switcher, 0, tab_3);
239 assert_match_at_position(tab_switcher, 1, tab_4);
240 assert_match_at_position(tab_switcher, 2, tab_1);
241 });
242
243 // Still switches tab on modifiers release
244 cx.simulate_modifiers_change(Modifiers::none());
245 cx.read(|cx| {
246 let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
247 assert_eq!(active_editor.read(cx).title(cx), "3.txt");
248 });
249 assert_tab_switcher_is_closed(workspace, cx);
250}
251
252fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
253 cx.update(|cx| {
254 let state = AppState::test(cx);
255 theme::init(theme::LoadThemes::JustBase, cx);
256 super::init(cx);
257 editor::init(cx);
258 state
259 })
260}
261
262#[track_caller]
263fn open_tab_switcher(
264 select_last: bool,
265 workspace: &Entity<Workspace>,
266 cx: &mut VisualTestContext,
267) -> Entity<Picker<TabSwitcherDelegate>> {
268 cx.dispatch_action(Toggle { select_last });
269 get_active_tab_switcher(workspace, cx)
270}
271
272#[track_caller]
273fn get_active_tab_switcher(
274 workspace: &Entity<Workspace>,
275 cx: &mut VisualTestContext,
276) -> Entity<Picker<TabSwitcherDelegate>> {
277 workspace.update(cx, |workspace, cx| {
278 workspace
279 .active_modal::<TabSwitcher>(cx)
280 .expect("tab switcher is not open")
281 .read(cx)
282 .picker
283 .clone()
284 })
285}
286
287async fn open_buffer(
288 file_path: &str,
289 workspace: &Entity<Workspace>,
290 cx: &mut gpui::VisualTestContext,
291) -> Box<dyn ItemHandle> {
292 let project = workspace.read_with(cx, |workspace, _| workspace.project().clone());
293 let worktree_id = project.update(cx, |project, cx| {
294 let worktree = project.worktrees(cx).last().expect("worktree not found");
295 worktree.read(cx).id()
296 });
297 let project_path = ProjectPath {
298 worktree_id,
299 path: rel_path(file_path).into(),
300 };
301 workspace
302 .update_in(cx, move |workspace, window, cx| {
303 workspace.open_path(project_path, None, true, window, cx)
304 })
305 .await
306 .unwrap()
307}
308
309#[track_caller]
310fn assert_match_selection(
311 tab_switcher: &Picker<TabSwitcherDelegate>,
312 expected_selection_index: usize,
313 expected_item: Box<dyn ItemHandle>,
314) {
315 assert_eq!(
316 tab_switcher.delegate.selected_index(),
317 expected_selection_index,
318 "item is not selected"
319 );
320 assert_match_at_position(tab_switcher, expected_selection_index, expected_item);
321}
322
323#[track_caller]
324fn assert_match_at_position(
325 tab_switcher: &Picker<TabSwitcherDelegate>,
326 match_index: usize,
327 expected_item: Box<dyn ItemHandle>,
328) {
329 let match_item = tab_switcher
330 .delegate
331 .matches
332 .get(match_index)
333 .unwrap_or_else(|| panic!("Tab Switcher has no match for index {match_index}"));
334 assert_eq!(match_item.item.item_id(), expected_item.item_id());
335}
336
337#[track_caller]
338fn assert_tab_switcher_is_closed(workspace: Entity<Workspace>, cx: &mut VisualTestContext) {
339 workspace.update(cx, |workspace, cx| {
340 assert!(
341 workspace.active_modal::<TabSwitcher>(cx).is_none(),
342 "tab switcher is still open"
343 );
344 });
345}