Debian Codegraph @

hyprtester/src/tests/main/master.cpp

raw
1 #include "../shared.hpp"
2 #include "../../shared.hpp"
3 #include "../../hyprctlCompat.hpp"
4 #include <algorithm>
5 #include <array>
6 #include <cmath>
7 #include <map>
8 #include <string>
9 #include <utility>
10 #include <vector>
11 #include <thread>
12 #include "tests.hpp"
13
14 TEST_CASE(focusMasterPrevious) {
15 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
16
17 // setup
18 NLog::log("{}Spawning 1 master and 3 slave windows", Colors::YELLOW);
19 // order of windows set according to new_status = master (set in test.lua)
20 for (auto const& win : {"slave1", "slave2", "slave3", "master"}) {
21 if (!Tests::spawnKitty(win))
22 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
23 }
24 NLog::log("{}Ensuring focus is on master before testing", Colors::YELLOW);
25 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster master')"));
26 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
27
28 // test
29 NLog::log("{}Testing fallback to focusmaster auto", Colors::YELLOW);
30
31 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster previous')"));
32 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: slave1");
33
34 NLog::log("{}Testing focusing from slave to master", Colors::YELLOW);
35
36 OK(getFromSocket("/dispatch hl.dsp.layout('cyclenext noloop')"));
37 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: slave2");
38 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster previous')"));
39 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
40
41 NLog::log("{}Testing focusing on previous window", Colors::YELLOW);
42
43 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster previous')"));
44 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: slave2");
45
46 NLog::log("{}Testing focusing back to master", Colors::YELLOW);
47
48 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster previous')"));
49 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
50
51 OK(getFromSocket("r/eval hl.config({ master = { orientation = 'top' } })"));
52
53 // top
54 {
55 auto str = getFromSocket("/activewindow");
56 EXPECT_CONTAINS(str, "at: 22,22");
57 EXPECT_CONTAINS(str, "size: 1876");
58 }
59
60 // cycle = top, right, bottom, center, left
61
62 // right
63 OK(getFromSocket("/dispatch hl.dsp.layout('orientationnext')"));
64 {
65 auto str = getFromSocket("/activewindow");
66 EXPECT_CONTAINS(str, "at: 873,22");
67 EXPECT_CONTAINS(str, "size: 1025,1036");
68 }
69
70 // bottom
71 OK(getFromSocket("/dispatch hl.dsp.layout('orientationnext')"));
72 {
73 auto str = getFromSocket("/activewindow");
74 EXPECT_CONTAINS(str, "at: 22,495");
75 EXPECT_CONTAINS(str, "size: 1876");
76 }
77
78 // center
79 OK(getFromSocket("/dispatch hl.dsp.layout('orientationnext')"));
80 {
81 auto str = getFromSocket("/activewindow");
82 EXPECT_CONTAINS(str, "at: 450,22");
83 EXPECT_CONTAINS(str, "size: 1020,1036");
84 }
85
86 // left
87 OK(getFromSocket("/dispatch hl.dsp.layout('orientationnext')"));
88 {
89 auto str = getFromSocket("/activewindow");
90 EXPECT_CONTAINS(str, "at: 22,22");
91 EXPECT_CONTAINS(str, "size: 1025,1036");
92 }
93 }
94
95 /*
96 Fullscreen Tests
97
98 Tests with `Shared test among all default handled FS` comment are duplicated among all layouts to test each layout individually
99
100 */
101
102 TEST_CASE(masterTestFsFocusUnderFSWindow) {
103
104 // Shared test among all default handled FS
105
106 // Master will re-send data to fullscreen / maximized windows, which can interfere with misc:on_focus_under_fullscreen
107 // check that it doesn't.
108
109 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
110
111 for (auto const& win : {"master", "slave1", "slave2"}) {
112 if (!Tests::spawnKitty(win))
113 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
114 }
115
116 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:master' })"));
117 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
118
119 {
120 auto str = getFromSocket("/activewindow");
121 EXPECT_CONTAINS(str, "at: 22,22");
122 EXPECT_CONTAINS(str, "size: 1876,1036");
123 EXPECT_CONTAINS(str, "class: master");
124 }
125
126 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 1 } })"));
127
128 Tests::spawnKitty("new_master");
129
130 {
131 auto str = getFromSocket("/activewindow");
132 EXPECT_CONTAINS(str, "at: 22,22");
133 EXPECT_CONTAINS(str, "size: 1876,1036");
134 EXPECT_CONTAINS(str, "class: new_master");
135 EXPECT_CONTAINS(str, "fullscreen: 1");
136 }
137
138 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 0 } })"));
139
140 Tests::spawnKitty("ignored");
141
142 {
143 auto str = getFromSocket("/activewindow");
144 EXPECT_CONTAINS(str, "at: 22,22");
145 EXPECT_CONTAINS(str, "size: 1876,1036");
146 EXPECT_CONTAINS(str, "class: new_master");
147 EXPECT_CONTAINS(str, "fullscreen: 1");
148 }
149
150 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 2 } })"));
151
152 Tests::spawnKitty("vaxwashere");
153
154 {
155 auto str = getFromSocket("/activewindow");
156 EXPECT_CONTAINS(str, "class: vaxwashere");
157 EXPECT_CONTAINS(str, "fullscreen: 0");
158 }
159 }
160
161 TEST_CASE(masterFullscreenMaximiseDispatchers) {
162
163 // Shared test among all default handled FS
164
165 OK(getFromSocket("/eval hl.config({ general = { layout = 'master' } })"));
166
167 Tests::spawnKitty("kitty_A");
168 Tests::spawnKitty("kitty_B");
169
170 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_A' })"));
171 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen', action = 'set' })"));
172
173 {
174 auto str = getFromSocket("/activewindow");
175 EXPECT_CONTAINS(str, "fullscreen: 2");
176 EXPECT_CONTAINS(str, "fullscreenClient: 2");
177 }
178
179 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen', action = 'unset' })"));
180
181 {
182 auto str = getFromSocket("/activewindow");
183 EXPECT_CONTAINS(str, "fullscreen: 0");
184 EXPECT_CONTAINS(str, "fullscreenClient: 0");
185 }
186
187 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized', action = 'toggle' })"));
188
189 {
190 auto str = getFromSocket("/activewindow");
191 EXPECT_CONTAINS(str, "fullscreen: 1");
192 EXPECT_CONTAINS(str, "fullscreenClient: 1");
193 }
194
195 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized', action = 'toggle' })"));
196
197 {
198 auto str = getFromSocket("/activewindow");
199 EXPECT_CONTAINS(str, "fullscreen: 0");
200 EXPECT_CONTAINS(str, "fullscreenClient: 0");
201 }
202
203 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 2, client = 2, action = 'set' })"));
204
205 {
206 auto str = getFromSocket("/activewindow");
207 EXPECT_CONTAINS(str, "fullscreen: 2");
208 EXPECT_CONTAINS(str, "fullscreenClient: 2");
209 }
210
211 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 2, client = 2, action = 'set' })"));
212
213 {
214 auto str = getFromSocket("/activewindow");
215 EXPECT_CONTAINS(str, "fullscreen: 2");
216 EXPECT_CONTAINS(str, "fullscreenClient: 2");
217 }
218
219 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 2, client = 2, action = 'toggle' })"));
220
221 {
222 auto str = getFromSocket("/activewindow");
223 EXPECT_CONTAINS(str, "fullscreen: 0");
224 EXPECT_CONTAINS(str, "fullscreenClient: 0");
225 }
226
227 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 2, client = 2, action = 'toggle' })"));
228
229 {
230 auto str = getFromSocket("/activewindow");
231 EXPECT_CONTAINS(str, "fullscreen: 2");
232 EXPECT_CONTAINS(str, "fullscreenClient: 2");
233 }
234 }
235
236 TEST_CASE(masterNewWindowTakesOverFullscreen) {
237
238 // Shared test among all default handled FS
239
240 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
241
242 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 0 } })"));
243
244 Tests::spawnKitty("kitty_A");
245
246 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
247
248 {
249 auto str = getFromSocket("/activewindow");
250 EXPECT_CONTAINS(str, "fullscreen: 2");
251 EXPECT_CONTAINS(str, "fullscreenClient: 2");
252 EXPECT_CONTAINS(str, "kitty_A");
253 }
254
255 Tests::spawnKitty("kitty_B");
256
257 {
258 auto str = getFromSocket("/activewindow");
259 EXPECT_CONTAINS(str, "fullscreen: 2");
260 EXPECT_CONTAINS(str, "fullscreenClient: 2");
261 EXPECT_CONTAINS(str, "kitty_A");
262 }
263
264 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_B' })"));
265
266 {
267 // should be ignored as per focus_under_fullscreen 0
268 auto str = getFromSocket("/activewindow");
269 EXPECT_CONTAINS(str, "fullscreen: 2");
270 EXPECT_CONTAINS(str, "fullscreenClient: 2");
271 EXPECT_CONTAINS(str, "kitty_A");
272 }
273
274 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 1 } })"));
275
276 Tests::spawnKitty("kitty_C");
277
278 {
279 auto str = getFromSocket("/activewindow");
280 EXPECT_CONTAINS(str, "fullscreen: 2");
281 EXPECT_CONTAINS(str, "fullscreenClient: 2");
282 EXPECT_CONTAINS(str, "kitty_C");
283 }
284
285 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 2 } })"));
286
287 Tests::spawnKitty("kitty_D");
288
289 {
290 auto str = getFromSocket("/activewindow");
291 EXPECT_CONTAINS(str, "fullscreen: 0");
292 EXPECT_CONTAINS(str, "fullscreenClient: 0");
293 EXPECT_CONTAINS(str, "kitty_D");
294 }
295
296 OK(getFromSocket("/eval hl.config({ misc = { on_focus_under_fullscreen = 0 } })"));
297
298 Tests::killAllWindows();
299 }
300
301 TEST_CASE(masterExitWindowRetainsFullscreen) {
302
303 // Shared test among all default handled FS
304
305 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
306
307 OK(getFromSocket("/eval hl.config({ misc = { exit_window_retains_fullscreen = false } })"));
308
309 Tests::spawnKitty("kitty_A");
310 Tests::spawnKitty("kitty_B");
311
312 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
313
314 {
315 auto str = getFromSocket("/activewindow");
316 EXPECT_CONTAINS(str, "fullscreen: 2");
317 EXPECT_CONTAINS(str, "fullscreenClient: 2");
318 }
319
320 OK(getFromSocket("/dispatch hl.dsp.window.kill({ window = 'activewindow' })"));
321 Tests::waitUntilWindowsN(1);
322
323 {
324 auto str = getFromSocket("/activewindow");
325 EXPECT_CONTAINS(str, "fullscreen: 0");
326 EXPECT_CONTAINS(str, "fullscreenClient: 0");
327 }
328
329 Tests::spawnKitty("kitty_B");
330 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
331 OK(getFromSocket("/eval hl.config({ misc = { exit_window_retains_fullscreen = true } })"));
332
333 OK(getFromSocket("/dispatch hl.dsp.window.kill({ window = 'activewindow' })"));
334 Tests::waitUntilWindowsN(1);
335
336 {
337 auto str = getFromSocket("/activewindow");
338 EXPECT_CONTAINS(str, "fullscreen: 2");
339 EXPECT_CONTAINS(str, "fullscreenClient: 2");
340 }
341
342 Tests::killAllWindows();
343 }
344
345 TEST_CASE(masterFullscreenPinnedWindows) {
346
347 // Shared test among all default handled FS
348
349 /*
350
351 allow_pin_fullscreen -> Allow internal FSing a pinned window at all?
352
353 if true: FSed pinned window doesn't behave as pinned while it is FS but continues to behave as pinned when it's unFS
354 if false: doesn't allow FSing it at all (client can be set if de-syncing internal and client)
355
356 */
357
358 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
359
360 Tests::spawnKitty("cake");
361
362 OK(getFromSocket("/dispatch hl.dsp.window.float({action = 'enable', window = 'class:cake'})"));
363
364 // resize to expected floating value: 200 x 200
365 OK(getFromSocket("/dispatch hl.dsp.window.resize({x = 200, y = 200, relative = false, window = 'class:cake'})"));
366
367 // Workspace we are testing on: 1
368 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
369
370 // Pin the window
371 OK(getFromSocket("r/dispatch hl.dsp.window.pin({ window = 'class:cake' })"));
372
373 // set to false, try to FS; expect the cake to be a lie
374 OK(getFromSocket("r/eval hl.config({ binds = { allow_pin_fullscreen = false } })"));
375 OK(getFromSocket("/dispatch hl.dsp.focus({window = 'class:cake'})"));
376
377 // Try with fullscreen
378 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
379 {
380 auto str = getFromSocket("/activewindow");
381 EXPECT_CONTAINS(str, "class: cake");
382 EXPECT_CONTAINS(str, "pinned: 1");
383 EXPECT_CONTAINS(str, "pinFullscreened: 0");
384 EXPECT_CONTAINS(str, "fullscreen: 0");
385 EXPECT_CONTAINS(str, "fullscreenClient: 0");
386 EXPECT_CONTAINS(str, "size: 200,200");
387 }
388
389 // Try with maximised
390 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
391 {
392 auto str = getFromSocket("/activewindow");
393 EXPECT_CONTAINS(str, "class: cake");
394 EXPECT_CONTAINS(str, "pinned: 1");
395 EXPECT_CONTAINS(str, "pinFullscreened: 0");
396 EXPECT_CONTAINS(str, "fullscreen: 0");
397 EXPECT_CONTAINS(str, "fullscreenClient: 0");
398 EXPECT_CONTAINS(str, "size: 200,200");
399 }
400
401 // Move to another workspace, expect it to follow
402 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '2' })"));
403 {
404 auto str = getFromSocket("/clients");
405 EXPECT_CONTAINS(str, "class: cake");
406 EXPECT_CONTAINS(str, "pinned: 1");
407 EXPECT_CONTAINS(str, "pinFullscreened: 0");
408 EXPECT_CONTAINS(str, "fullscreen: 0");
409 EXPECT_CONTAINS(str, "fullscreenClient: 0");
410 EXPECT_CONTAINS(str, "size: 200,200");
411 EXPECT_CONTAINS(str, "workspace: 2");
412 }
413
414 // Move back to primary testing workspace, assumed it'll follow since the last test passed
415 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
416
417 // While syncing FS state, is not supposed to set either mode. If internal and client are decoupled, client is expected to go through
418 // Try with fullscreen
419 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 2, client = 2, action = 'set', window = 'activewindow' })"));
420 {
421 auto str = getFromSocket("/clients");
422 EXPECT_CONTAINS(str, "class: cake");
423 EXPECT_CONTAINS(str, "pinned: 1");
424 EXPECT_CONTAINS(str, "pinFullscreened: 0");
425 EXPECT_CONTAINS(str, "fullscreen: 0");
426 EXPECT_CONTAINS(str, "fullscreenClient: 2");
427 EXPECT_CONTAINS(str, "size: 200,200");
428 EXPECT_CONTAINS(str, "workspace: 1");
429 }
430
431 // Try with maximised
432 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 1, client = 1, action = 'set', window = 'activewindow' })"));
433 {
434 auto str = getFromSocket("/clients");
435 EXPECT_CONTAINS(str, "class: cake");
436 EXPECT_CONTAINS(str, "pinned: 1");
437 EXPECT_CONTAINS(str, "pinFullscreened: 0");
438 EXPECT_CONTAINS(str, "fullscreen: 0");
439 EXPECT_CONTAINS(str, "fullscreenClient: 1");
440 EXPECT_CONTAINS(str, "size: 200,200");
441 EXPECT_CONTAINS(str, "workspace: 1");
442 }
443
444 // re-set its FS values for the next test
445 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 0, client = 0, action = 'set', window = 'activewindow' })"));
446
447 // set to true, try to FS; expect the cake to be real
448 OK(getFromSocket("r/eval hl.config({ binds = { allow_pin_fullscreen = true } })"));
449 OK(getFromSocket("/dispatch hl.dsp.focus({window = 'class:cake'})"));
450
451 // Try with fullscreen
452 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
453 {
454 auto str = getFromSocket("/activewindow");
455 EXPECT_CONTAINS(str, "class: cake");
456 EXPECT_CONTAINS(str, "pinned: 0");
457 EXPECT_CONTAINS(str, "pinFullscreened: 1");
458 EXPECT_CONTAINS(str, "fullscreen: 2");
459 EXPECT_CONTAINS(str, "fullscreenClient: 2");
460 EXPECT_CONTAINS(str, "at: 0,0");
461 ASSERT_CONTAINS(str, "size: 1920,1080");
462 }
463
464 // Try with maximised
465 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
466 {
467 auto str = getFromSocket("/activewindow");
468 EXPECT_CONTAINS(str, "class: cake");
469 EXPECT_CONTAINS(str, "pinned: 0");
470 EXPECT_CONTAINS(str, "pinFullscreened: 1");
471 EXPECT_CONTAINS(str, "fullscreen: 1");
472 EXPECT_CONTAINS(str, "fullscreenClient: 1");
473 EXPECT_CONTAINS(str, "at: 2,2");
474 EXPECT_CONTAINS(str, "size: 1916,1076");
475 }
476
477 // unFs it, move to another workspace - expect it to follow
478 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen_state({ internal = 0, client = 0, action = 'set', window = 'activewindow' })"));
479 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '2' })"));
480 {
481 auto str = getFromSocket("/clients");
482 EXPECT_CONTAINS(str, "class: cake");
483 // After the FSed pinned window is unFSed, expect its pinned value to come back
484 EXPECT_CONTAINS(str, "pinned: 1");
485 EXPECT_CONTAINS(str, "pinFullscreened: 0");
486 EXPECT_CONTAINS(str, "fullscreen: 0");
487 EXPECT_CONTAINS(str, "fullscreenClient: 0");
488 EXPECT_CONTAINS(str, "size: 200,200");
489 EXPECT_CONTAINS(str, "workspace: 2");
490 }
491
492 // set the variable to false, unpin it and expect it to be FS-able
493 OK(getFromSocket("r/eval hl.config({ binds = { allow_pin_fullscreen = false } })"));
494 OK(getFromSocket("r/dispatch hl.dsp.window.pin({ window = 'class:cake' })"));
495
496 // Try with fullscreen
497 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen' })"));
498 {
499 auto str = getFromSocket("/activewindow");
500 EXPECT_CONTAINS(str, "class: cake");
501 EXPECT_CONTAINS(str, "pinned: 0");
502 EXPECT_CONTAINS(str, "pinFullscreened: 0");
503 EXPECT_CONTAINS(str, "fullscreen: 2");
504 EXPECT_CONTAINS(str, "fullscreenClient: 2");
505 EXPECT_CONTAINS(str, "at: 0,0");
506 ASSERT_CONTAINS(str, "size: 1920,1080");
507 }
508
509 // Try with maximised
510 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
511 {
512 auto str = getFromSocket("/activewindow");
513 EXPECT_CONTAINS(str, "class: cake");
514 EXPECT_CONTAINS(str, "pinned: 0");
515 EXPECT_CONTAINS(str, "pinFullscreened: 0");
516 EXPECT_CONTAINS(str, "fullscreen: 1");
517 EXPECT_CONTAINS(str, "fullscreenClient: 1");
518 EXPECT_CONTAINS(str, "at: 2,2");
519 EXPECT_CONTAINS(str, "size: 1916,1076");
520 }
521 }
522
523 TEST_CASE(masterFullscreenNonInterference) {
524
525 // Shared test among all default handled FS
526
527 /*
528
529 When a tiled/floating window is default handled FSed, it must not cause the windows under it to have moved/resized after it is unFSed
530
531 also tests if floating pos/size is properly restored after fS-unfs
532
533 */
534
535 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
536
537 Tests::spawnKitty("red");
538 Tests::spawnKitty("crimson");
539 Tests::spawnKitty("blue");
540 Tests::spawnKitty("cyan");
541 Tests::spawnKitty("azure");
542 Tests::spawnKitty("green");
543
544 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
545
546 // Testing tiled first
547 {
548
549 // save all pos/size inc red
550
551 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
552 auto redPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
553 auto redSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
554
555 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:crimson' })"));
556 auto crimsonPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
557 auto crimsonSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
558
559 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:blue' })"));
560 auto bluePos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
561 auto blueSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
562
563 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:cyan' })"));
564 auto cyanPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
565 auto cyanSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
566
567 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:azure' })"));
568 auto azurePos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
569 auto azureSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
570
571 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:green' })"));
572 auto greenPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
573 auto greenSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
574
575 // FS and unFS red, then check all positions are unchanged
576 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
577 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
578 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
579
580 // red
581 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
582 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), redPos);
583 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), redSize);
584
585 // crimson
586 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:crimson' })"));
587 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), crimsonPos);
588 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), crimsonSize);
589
590 // blue
591 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:blue' })"));
592 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), bluePos);
593 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), blueSize);
594
595 // cyan
596 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:cyan' })"));
597 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), cyanPos);
598 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), cyanSize);
599
600 // azure
601 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:azure' })"));
602 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), azurePos);
603 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), azureSize);
604
605 // green
606 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:green' })"));
607 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), greenPos);
608 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), greenSize);
609 }
610
611 // test floating
612
613 {
614
615 // float red
616 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set', window = 'class:red' })"));
617
618 // save all pos/size (red's size will be its floating size)
619 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
620 auto redPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
621 auto redSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
622
623 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:crimson' })"));
624 auto crimsonPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
625 auto crimsonSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
626
627 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:blue' })"));
628 auto bluePos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
629 auto blueSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
630
631 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:cyan' })"));
632 auto cyanPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
633 auto cyanSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
634
635 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:azure' })"));
636 auto azurePos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
637 auto azureSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
638
639 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:green' })"));
640 auto greenPos = Tests::getAttribute(getFromSocket("/activewindow"), "at");
641 auto greenSize = Tests::getAttribute(getFromSocket("/activewindow"), "size");
642
643 // FS and unFS red, then check all positions are unchanged
644 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
645 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
646 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })"));
647
648 // red
649 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:red' })"));
650 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), redPos);
651 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), redSize);
652
653 // crimson
654 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:crimson' })"));
655 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), crimsonPos);
656 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), crimsonSize);
657
658 // blue
659 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:blue' })"));
660 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), bluePos);
661 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), blueSize);
662
663 // cyan
664 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:cyan' })"));
665 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), cyanPos);
666 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), cyanSize);
667
668 // azure
669 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:azure' })"));
670 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), azurePos);
671 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), azureSize);
672
673 // green
674 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:green' })"));
675 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "at"), greenPos);
676 EXPECT(Tests::getAttribute(getFromSocket("/activewindow"), "size"), greenSize);
677 }
678 }
679
680 TEST_CASE(rollFocus) {
681 // test rollnext/rollprev dispatchers
682
683 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
684
685 // set up windows
686 std::vector<std::string> windows = {"slave1", "slave2", "slave3", "master"};
687
688 // helper lambda thing
689 auto roll = [&](const std::string& dir) {
690 auto pivot = (dir == "rollnext") ? windows.begin() + 1 : windows.end() - 1;
691
692 // rotate the windows vector along with the actual windows
693 // the rolling behavior of the window focus should follow the
694 // rotating behavior of std::ranges::rotate
695 OK(getFromSocket("/dispatch hl.dsp.layout('" + dir + "')"));
696 std::ranges::rotate(windows.begin(), pivot, windows.end());
697 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: " + windows.back());
698 };
699
700 for (auto const& win : windows) {
701 if (!Tests::spawnKitty(win))
702 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
703 }
704
705 // focus master
706 OK(getFromSocket("/dispatch hl.dsp.layout('focusmaster master')"));
707 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
708
709 // put the windows in the washing machine
710 NLog::log("{}Testing rollnext", Colors::YELLOW);
711 for (int i = 0; i < 20; ++i) {
712 roll("rollnext");
713 }
714
715 NLog::log("{}Testing rollprev", Colors::YELLOW);
716 for (int i = 0; i < 20; ++i) {
717 roll("rollprev");
718 }
719
720 NLog::log("{}Testing rollnext with rollprev", Colors::YELLOW);
721 for (int i = 0; i < 10; ++i) {
722 for (int j = 0; j < 5; ++j) {
723 roll("rollnext");
724 }
725 roll("rollprev");
726 }
727
728 NLog::log("{}Testing rollnext/rollprev alternation", Colors::YELLOW);
729 for (int i = 0; i < 20; ++i) {
730 if (i % 2 == 0) {
731 roll("rollnext");
732 } else {
733 roll("rollprev");
734 }
735 }
736
737 NLog::log("{}Testing rollnext/rollprev burst calls", Colors::YELLOW);
738 for (int i = 0; i < 20; ++i) {
739 if (i / 5 % 2 == 0) {
740 roll("rollnext");
741 } else {
742 roll("rollprev");
743 }
744 }
745 }
746
747 TEST_CASE(focusMasterClose) {
748 //Test behaviour of master:focus_master_on_close
749 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' }, master = { focus_master_on_close = true } })"));
750
751 std::vector<pid_t> pids;
752 for (auto const& win : {"slave1", "slave2", "slave3", "master"}) {
753 auto p = Tests::spawnKitty(win);
754 if (!p)
755 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
756 pids.push_back(p->pid());
757 }
758
759 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:slave1' })"));
760 OK(getFromSocket("/dispatch hl.dsp.window.close({ window = 'class:slave1' })"));
761 while (Tests::processAlive(pids[0]))
762 std::this_thread::sleep_for(std::chrono::milliseconds(25l));
763 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
764
765 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:slave2' })"));
766 OK(getFromSocket("/dispatch hl.dsp.window.close({ window = 'class:slave2' })"));
767 while (Tests::processAlive(pids[1]))
768 std::this_thread::sleep_for(std::chrono::milliseconds(25l));
769 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
770
771 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:slave3' })"));
772 OK(getFromSocket("/dispatch hl.dsp.window.close({ window = 'class:slave3' })"));
773 while (Tests::processAlive(pids[2]))
774 std::this_thread::sleep_for(std::chrono::milliseconds(25l));
775 ASSERT_CONTAINS(getFromSocket("/activewindow"), "class: master");
776 }
777
778 TEST_CASE(centerMasterColumnResize) {
779 // Center master, odd slave count. The fallback-side column holds the extra slave;
780 // resizeTarget() used to assume it went right, so with the default 'left' fallback the two left
781 // windows of a 1-master/3-slave layout silently refused to resize vertically.
782
783 // focus a window by class and read its {left edge x, height} from /activewindow
784 auto geomOf = [&](const std::string& cls) -> std::pair<double, double> {
785 getFromSocket("/dispatch hl.dsp.focus({ window = 'class:" + cls + "' })");
786 const auto STR = getFromSocket("/activewindow");
787 const auto AT = Tests::getAttribute(STR, "at"); // "x,y"
788 const auto SZ = Tests::getAttribute(STR, "size"); // "w,h"
789 return {std::stod(AT.substr(0, AT.find(','))), std::stod(SZ.substr(SZ.find(',') + 1))};
790 };
791 auto leftOf = [&](const std::string& cls) { return geomOf(cls).first; };
792 auto heightOf = [&](const std::string& cls) { return geomOf(cls).second; };
793
794 // resizeactive-style relative resize of a specific window along y
795 auto resizeY = [&](const std::string& cls, int dy) {
796 return getFromSocket("/dispatch hl.dsp.window.resize({ x = 0, y = " + std::to_string(dy) + ", relative = true, window = 'class:" + cls + "' })");
797 };
798
799 // `top` and `bottom` share one column and must resize vertically (one grows, the other shrinks,
800 // total column height preserved); `single` lives in the other column and must stay untouched.
801 auto expectColumnResizes = [&](const std::string& top, const std::string& bottom, const std::string& single) {
802 const double TX = leftOf(top), BX = leftOf(bottom), SX = leftOf(single);
803 ASSERT_MAX_DELTA(BX, TX, 1); // top & bottom share a column (same left edge)
804 ASSERT(std::abs(SX - TX) > 100, true); // single sits in the other column
805
806 const double T0 = heightOf(top), B0 = heightOf(bottom), S0 = heightOf(single);
807
808 OK(resizeY(top, 80)); // grow the upper window of the pair
809
810 const double T1 = heightOf(top), B1 = heightOf(bottom), S1 = heightOf(single);
811 EXPECT(T1 > T0, true); // upper window grew
812 EXPECT(B1 < B0, true); // lower window shrank to compensate
813 EXPECT_MAX_DELTA(T1 + B1, T0 + B0, 4); // column height preserved
814 EXPECT_MAX_DELTA(S1, S0, 2); // the other column is untouched
815
816 OK(resizeY(top, -80)); // restore the split for later phases
817 };
818
819 // 1 master + 3 slaves => slaves slave1, slave2, slave3 in stack order (new_status = master)
820 OK(getFromSocket(
821 "r/eval hl.config({ general = { layout = 'master' }, master = { orientation = 'center', center_master_fallback = 'left', slave_count_for_center_master = 2 } })"));
822 for (auto const& win : {"slave1", "slave2", "slave3", "master"}) {
823 if (!Tests::spawnKitty(win))
824 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
825 }
826
827 // default `left` fallback: slave1 (top) + slave3 (bottom) on the left, slave2 alone on the right
828 NLog::log("{}center master, left fallback, 3 slaves: left column must resize (smart_resizing on)", Colors::YELLOW);
829 expectColumnResizes("slave1", "slave3", "slave2");
830
831 NLog::log("{}center master, left fallback, 3 slaves: left column must resize (smart_resizing off)", Colors::YELLOW);
832 OK(getFromSocket("r/eval hl.config({ master = { smart_resizing = false } })"));
833 expectColumnResizes("slave1", "slave3", "slave2");
834 OK(getFromSocket("r/eval hl.config({ master = { smart_resizing = true } })"));
835
836 // symmetry: `right` fallback puts the pair (slave1, slave3) on the right, slave2 alone on the left
837 NLog::log("{}center master, right fallback, 3 slaves: right column must resize", Colors::YELLOW);
838 OK(getFromSocket("r/eval hl.config({ master = { center_master_fallback = 'right' } })"));
839 expectColumnResizes("slave1", "slave3", "slave2");
840
841 // even count, no regression: a 4th slave makes 2 left / 2 right; the left pair still resizes.
842 // new_status = master => `extra` becomes the master and `master` drops to the 4th slave.
843 NLog::log("{}center master, left fallback, 4 slaves: columns still resize (no regression)", Colors::YELLOW);
844 OK(getFromSocket("r/eval hl.config({ master = { center_master_fallback = 'left' } })"));
845 if (!Tests::spawnKitty("extra"))
846 FAIL_TEST("Could not spawn kitty with win class `{}`", "extra");
847 expectColumnResizes("slave1", "slave3", "slave2");
848
849 // even count, no regression: 2 slaves => 1 per column, so a vertical resize is a no-op
850 NLog::log("{}center master, 2 slaves: single-window columns don't resize (no regression)", Colors::YELLOW);
851 if (!Tests::killAllWindows())
852 FAIL_TEST("Could not kill all windows before the {}-slave phase", 2);
853 for (auto const& win : {"slave1", "slave2", "master"}) {
854 if (!Tests::spawnKitty(win))
855 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
856 }
857 const double H0 = heightOf("slave1");
858 OK(resizeY("slave1", 80));
859 EXPECT_MAX_DELTA(heightOf("slave1"), H0, 2);
860 }
861
862 // In a centered master layout with three windows w1/w2/w3, return their classes ordered
863 // visually left-to-right: { left slave, master (center), right slave }.
864 static std::array<std::string, 3> detectCenterArrangement() {
865 std::vector<std::pair<int, std::string>> wins;
866 for (auto const& cls : {"w1", "w2", "w3"}) {
867 getFromSocket(std::format("/dispatch hl.dsp.focus({{ window = 'class:{}' }})", cls));
868 const auto at = Tests::getAttribute(getFromSocket("/activewindow"), "at");
869 wins.emplace_back(std::stoi(at.substr(0, at.find(','))), cls);
870 }
871 std::ranges::sort(wins, {}, &std::pair<int, std::string>::first);
872 return {wins[0].second, wins[1].second, wins[2].second};
873 }
874
875 // Drag the window currently playing role `pick` (one of "L"/"M"/"R", i.e. the left
876 // slave, the master, or the right slave) and drop it on the left or right side of the
877 // screen. Assert the windows end up in the expected left/center/right roles afterwards.
878 SUBTEST(expectCenterDrop, const std::string& pick, bool dropRight, const std::string& expLeft, const std::string& expCenter, const std::string& expRight) {
879 // start from a fresh set of three windows
880 if (!Tests::killAllWindows())
881 FAIL_TEST("Could not clear windows{}", "");
882
883 for (auto const& win : {"w1", "w2", "w3"}) {
884 if (!Tests::spawnKitty(win))
885 FAIL_TEST("Could not spawn kitty with win class `{}`", win);
886 }
887 Tests::waitUntilWindowsN(3);
888
889 const auto INITIAL = detectCenterArrangement();
890 std::map<std::string, std::string> role = {
891 {"L", INITIAL[0]},
892 {"M", INITIAL[1]},
893 {"R", INITIAL[2]},
894 };
895
896 const double DROPX = dropRight ? 1800.0 : 100.0;
897 NLog::log("{}Picking up {} ({}) and dropping it on the {} side", Colors::YELLOW, pick, role[pick], dropRight ? "right" : "left");
898
899 OK(getFromSocket(std::format("/eval hl.plugin.test.drag_window('{}', {}, {})", role[pick], DROPX, 540)));
900
901 const auto FINAL = detectCenterArrangement();
902 EXPECT(FINAL[0], role[expLeft]);
903 EXPECT(FINAL[1], role[expCenter]);
904 EXPECT(FINAL[2], role[expRight]);
905 }
906
907 TEST_CASE(masterCenterDropAtCursor) {
908 OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })"));
909 OK(getFromSocket("/eval hl.config({ master = { orientation = 'center', new_status = 'slave', drop_at_cursor = true, slave_count_for_center_master = 0, center_master_fallback "
910 "= 'left' } })"));
911
912 NLog::log("{}Testing center master drop_at_cursor rearrangement", Colors::GREEN);
913
914 CALL_SUBTEST(expectCenterDrop, "L", false, "L", "M", "R");
915 CALL_SUBTEST(expectCenterDrop, "L", true, "R", "M", "L");
916 CALL_SUBTEST(expectCenterDrop, "M", false, "M", "L", "R");
917 CALL_SUBTEST(expectCenterDrop, "M", true, "R", "L", "M");
918 CALL_SUBTEST(expectCenterDrop, "R", false, "R", "M", "L");
919 CALL_SUBTEST(expectCenterDrop, "R", true, "L", "M", "R");
920 }