Debian Codegraph @

hyprtester/src/tests/main/snap.cpp

raw
1 #include <hyprutils/math/Vector2D.hpp>
2 #include <hyprutils/memory/WeakPtr.hpp>
3 #include <hyprutils/os/Process.hpp>
4
5 #include "../../shared.hpp"
6 #include "../../hyprctlCompat.hpp"
7 #include "../shared.hpp"
8 #include "tests.hpp"
9
10 using Hyprutils::Math::Vector2D;
11
12 static bool spawnFloatingKitty() {
13 if (!Tests::spawnKitty()) {
14 NLog::log("{}Error: kitty did not spawn", Colors::RED);
15 return false;
16 }
17 bool ok = true;
18 ok &= getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })") == "ok";
19 ok &= getFromSocket("/dispatch hl.dsp.window.resize({ x = 100, y = 100 })") == "ok";
20 return ok;
21 }
22
23 SUBTEST(expectSnapMove, double fromX, double fromY, double toX, double toY) {
24 const Vector2D FROM = {fromX, fromY};
25 const Vector2D TO = {toX, toY};
26 if (FROM == TO)
27 NLog::log("{}Expecting no snap when window is moved to ({},{})", Colors::YELLOW, FROM.x, FROM.y);
28 else
29 NLog::log("{}Expecting snap to ({},{}) when window is moved to ({},{})", Colors::YELLOW, TO.x, TO.y, FROM.x, FROM.y);
30
31 OK(getFromSocket(std::format("/dispatch hl.dsp.window.move({{ x = {}, y = {} }})", FROM.x, FROM.y)));
32 OK(getFromSocket("/eval hl.plugin.test.snapmove()"));
33 EXPECT_CONTAINS(getFromSocket("/activewindow"), std::format("at: {},{}", TO.x, TO.y));
34 }
35
36 SUBTEST(expectNoSnapMove, double x, double y) {
37 CALL_SUBTEST(expectSnapMove, x, y, x, y);
38 }
39
40 SUBTEST(testWindowSnap, const bool RESPECTGAPS) {
41 const int BORDERSIZE = 2;
42 const int WINDOWSIZE = 100;
43
44 const int OTHER = 500;
45 const int WINDOWGAP = 8;
46 const int GAPSIN = 5;
47 const int GAP = (RESPECTGAPS ? 2 * GAPSIN : 0) + (2 * BORDERSIZE);
48 const int END = GAP + WINDOWSIZE;
49
50 int x = WINDOWGAP + END;
51 CALL_SUBTEST(expectNoSnapMove, OTHER + x, OTHER);
52 CALL_SUBTEST(expectNoSnapMove, OTHER - x, OTHER);
53 CALL_SUBTEST(expectNoSnapMove, OTHER, OTHER + x);
54 CALL_SUBTEST(expectNoSnapMove, OTHER, OTHER - x);
55 x -= 1;
56 CALL_SUBTEST(expectSnapMove, OTHER + x, OTHER, OTHER + END, OTHER);
57 CALL_SUBTEST(expectSnapMove, OTHER - x, OTHER, OTHER - END, OTHER);
58 CALL_SUBTEST(expectSnapMove, OTHER, OTHER + x, OTHER, OTHER + END);
59 CALL_SUBTEST(expectSnapMove, OTHER, OTHER - x, OTHER, OTHER - END);
60 }
61
62 SUBTEST(testMonitorSnap, const bool RESPECTGAPS, const bool OVERLAP) {
63 const int BORDERSIZE = 2;
64 const int WINDOWSIZE = 100;
65
66 const int MONITORGAP = 10;
67 const int GAPSOUT = 20;
68 const int RESP = (RESPECTGAPS ? GAPSOUT : 0);
69 const int GAP = RESP + (OVERLAP ? 0 : BORDERSIZE);
70 const int END = GAP + WINDOWSIZE;
71
72 int x;
73 Vector2D predict;
74
75 x = MONITORGAP + GAP;
76 CALL_SUBTEST(expectNoSnapMove, x, x);
77 x -= 1;
78 CALL_SUBTEST(expectSnapMove, x, x, GAP, GAP);
79
80 x = MONITORGAP + END;
81 CALL_SUBTEST(expectNoSnapMove, 1920 - x, 1080 - x);
82 x -= 1;
83 CALL_SUBTEST(expectSnapMove, 1920 - x, 1080 - x, 1920 - END, 1080 - END);
84
85 // test reserved area
86 const int RESERVED = 200;
87 const int RGAP = RESERVED + RESP + BORDERSIZE;
88 const int REND = RGAP + WINDOWSIZE;
89
90 x = MONITORGAP + RGAP;
91 CALL_SUBTEST(expectNoSnapMove, x, x);
92 x -= 1;
93 CALL_SUBTEST(expectSnapMove, x, x, RGAP, RGAP);
94
95 x = MONITORGAP + REND;
96 CALL_SUBTEST(expectNoSnapMove, 1920 - x, 1080 - x);
97 x -= 1;
98 CALL_SUBTEST(expectSnapMove, 1920 - x, 1080 - x, 1920 - REND, 1080 - REND);
99 }
100
101 // TODO: decompose this into multiple test cases
102 TEST_CASE(snap) {
103 NLog::log("{}Testing snap", Colors::GREEN);
104
105 // move to monitor HEADLESS-2
106 NLog::log("{}Moving to monitor HEADLESS-2", Colors::YELLOW);
107 OK(getFromSocket("/dispatch hl.dsp.focus({ monitor = 'HEADLESS-2' })"));
108 NLog::log("{}Adding reserved monitor area to HEADLESS-2", Colors::YELLOW);
109 OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', reserved = { top = 200, right = 200, bottom = 200, left = 200 } })"));
110
111 // test on workspace "snap"
112 NLog::log("{}Dispatching workspace `snap`", Colors::YELLOW);
113 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'name:snap' })"));
114
115 // spawn a kitty terminal and move to (500,500)
116 NLog::log("{}Spawning kittyProcA", Colors::YELLOW);
117 if (!spawnFloatingKitty())
118 FAIL_TEST("Could not spawn kitty");
119
120 NLog::log("{}Expecting 1 window", Colors::YELLOW);
121 ASSERT(Tests::windowCount(), 1);
122
123 NLog::log("{}Move the kitty window to (500,500)", Colors::YELLOW);
124 OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 500, y = 500 })"));
125
126 // spawn a second kitty terminal
127 NLog::log("{}Spawning kittyProcB", Colors::YELLOW);
128 if (!spawnFloatingKitty())
129 FAIL_TEST("Could not spawn kitty");
130
131 NLog::log("{}Expecting 2 windows", Colors::YELLOW);
132 ASSERT(Tests::windowCount(), 2);
133
134 NLog::log("");
135 CALL_SUBTEST(testWindowSnap, false);
136 CALL_SUBTEST(testMonitorSnap, false, false);
137
138 NLog::log("\n{}Turning on respect_gaps", Colors::YELLOW);
139 OK(getFromSocket("/eval hl.config({ general = { snap = { respect_gaps = true } } })"));
140 CALL_SUBTEST(testWindowSnap, true);
141 CALL_SUBTEST(testMonitorSnap, true, false);
142
143 NLog::log("\n{}Turning on border_overlap", Colors::YELLOW);
144 OK(getFromSocket("/eval hl.config({ general = { snap = { respect_gaps = false } } })"));
145 OK(getFromSocket("/eval hl.config({ general = { snap = { border_overlap = true } } })"));
146 CALL_SUBTEST(testMonitorSnap, false, true);
147
148 NLog::log("\n{}Turning on both border_overlap and respect_gaps", Colors::YELLOW);
149 OK(getFromSocket("/eval hl.config({ general = { snap = { respect_gaps = true } } })"));
150 CALL_SUBTEST(testMonitorSnap, true, true);
151 }