Debian Codegraph @

hyprtester/src/tests/main/refactor_state.cpp

raw
1 #include "../../hyprctlCompat.hpp"
2 #include "../../shared.hpp"
3 #include "../shared.hpp"
4 #include "tests.hpp"
5
6 #include <chrono>
7 #include <format>
8 #include <string>
9 #include <thread>
10
11 #include <hyprutils/utils/ScopeGuard.hpp>
12
13 using namespace Hyprutils::Utils;
14
15 static constexpr const char* TEST_MONITOR_LEFT = "HEADLESS-2";
16 static constexpr const char* TEST_MONITOR_RIGHT = "HYPRTEST-REFACTOR-R";
17
18 static bool waitForMonitorAvailable(const std::string& name) {
19 for (int i = 0; i < 50; ++i) {
20 if (getFromSocket("/monitors all").contains(name))
21 return true;
22
23 std::this_thread::sleep_for(std::chrono::milliseconds(100));
24 }
25
26 return false;
27 }
28
29 static bool waitForMonitorEnabled(const std::string& name) {
30 // "/monitors" without "all" lists only enabled monitors,
31 // an output can exist but is disabled
32 for (int i = 0; i < 50; ++i) {
33 if (getFromSocket("/monitors").contains(name))
34 return true;
35
36 std::this_thread::sleep_for(std::chrono::milliseconds(100));
37 }
38
39 return false;
40 }
41
42 static bool ensureOutputPresent(const std::string& name) {
43 if (getFromSocket("/monitors all").contains(name))
44 return true;
45
46 if (getFromSocket(std::format("/output create headless {}", name)) != "ok")
47 return false;
48
49 return waitForMonitorAvailable(name);
50 }
51
52 static bool prepareRefactorStateMonitors() {
53 if (!ensureOutputPresent(TEST_MONITOR_LEFT) || !ensureOutputPresent(TEST_MONITOR_RIGHT))
54 return false;
55
56 if (getFromSocket(
57 std::format("/eval hl.monitor({{ output = '{}', disabled = false, mode = '1920x1080@60', position = '0x0', scale = '1', transform = 0 }})", TEST_MONITOR_LEFT)) != "ok")
58 return false;
59
60 if (getFromSocket(std::format("/eval hl.monitor({{ output = '{}', disabled = false, mode = '1920x1080@60', position = '1920x0', scale = '1', transform = 0 }})",
61 TEST_MONITOR_RIGHT)) != "ok")
62 return false;
63
64 if (!waitForMonitorEnabled(TEST_MONITOR_LEFT) || !waitForMonitorEnabled(TEST_MONITOR_RIGHT))
65 return false;
66
67 Tests::sync();
68 return true;
69 }
70
71 static void cleanupRefactorStateMonitors() {
72 Tests::killAllWindows();
73
74 if (getFromSocket("/monitors all").contains(TEST_MONITOR_RIGHT))
75 getFromSocket(std::format("/output remove {}", TEST_MONITOR_RIGHT));
76
77 getFromSocket("/reload");
78 }
79
80 static std::string clientBlockForClass(const std::string& className) {
81 const auto clients = getFromSocket("/clients");
82 const auto classPos = clients.find(std::format("class: {}", className));
83 if (classPos == std::string::npos)
84 return "";
85
86 auto blockStart = clients.rfind("Window ", classPos);
87 if (blockStart == std::string::npos)
88 blockStart = 0;
89
90 auto blockEnd = clients.find("\n\n", classPos);
91 if (blockEnd == std::string::npos)
92 blockEnd = clients.length();
93
94 return clients.substr(blockStart, blockEnd - blockStart);
95 }
96
97 TEST_CASE(workspacePlacementActiveMove) {
98 CScopeGuard guard = {[&]() { cleanupRefactorStateMonitors(); }};
99
100 ASSERT(prepareRefactorStateMonitors(), true);
101
102 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
103 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '270' })"));
104 ASSERT(!!Tests::spawnKitty("workspace_move_active"), true);
105
106 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
107 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '271' })"));
108
109 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
110 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '270' })"));
111
112 OK(getFromSocket(std::format("/dispatch hl.dsp.workspace.move({{ workspace = '270', monitor = '{}' }})", TEST_MONITOR_RIGHT)));
113
114 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
115 {
116 const auto str = getFromSocket("/activeworkspace");
117 EXPECT_CONTAINS(str, "workspace ID 270");
118 EXPECT_CONTAINS(str, TEST_MONITOR_RIGHT);
119 }
120
121 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
122 {
123 const auto str = getFromSocket("/activeworkspace");
124 EXPECT_NOT_CONTAINS(str, "workspace ID 270");
125 EXPECT_CONTAINS(str, TEST_MONITOR_LEFT);
126 }
127 }
128
129 TEST_CASE(workspacePlacementWindowState) {
130 CScopeGuard guard = {[&]() { cleanupRefactorStateMonitors(); }};
131
132 ASSERT(prepareRefactorStateMonitors(), true);
133 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
134 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '280' })"));
135
136 ASSERT(!!Tests::spawnKitty("workspace_move_pinned"), true);
137 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })"));
138 OK(getFromSocket("/dispatch hl.dsp.window.pin()"));
139
140 ASSERT(!!Tests::spawnKitty("workspace_move_float"), true);
141 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })"));
142 OK(getFromSocket("/dispatch hl.dsp.window.resize({ x = 400, y = 300 })"));
143 OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 200, y = 200 })"));
144
145 ASSERT(!!Tests::spawnKitty("workspace_move_fullscreen"), true);
146 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen', action = 'set' })"));
147
148 OK(getFromSocket(std::format("/dispatch hl.dsp.workspace.move({{ workspace = '280', monitor = '{}' }})", TEST_MONITOR_RIGHT)));
149
150 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
151 {
152 const auto str = clientBlockForClass("workspace_move_pinned");
153 ASSERT_CONTAINS(str, "class: workspace_move_pinned");
154 EXPECT_CONTAINS(str, "pinned: 1");
155 EXPECT_CONTAINS(str, "floating: 1");
156 EXPECT_CONTAINS(str, "visible: 1");
157 }
158
159 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
160 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:workspace_move_float' })"));
161 {
162 const auto str = getFromSocket("/activewindow");
163 EXPECT_CONTAINS(str, "class: workspace_move_float");
164 EXPECT_CONTAINS(str, "at: 2120,200");
165 }
166
167 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:workspace_move_fullscreen' })"));
168 {
169 const auto str = getFromSocket("/activewindow");
170 EXPECT_CONTAINS(str, "class: workspace_move_fullscreen");
171 EXPECT_CONTAINS(str, "fullscreen: 2");
172 EXPECT_CONTAINS(str, "at: 1920,0");
173 EXPECT_CONTAINS(str, "size: 1920,1080");
174 }
175 }
176
177 TEST_CASE(globalWindowMoveState) {
178 CScopeGuard guard = {[&]() { cleanupRefactorStateMonitors(); }};
179
180 ASSERT(prepareRefactorStateMonitors(), true);
181 OK(getFromSocket("/eval hl.config({ group = { group_on_movetoworkspace = true } })"));
182
183 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
184 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '291' })"));
185
186 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
187 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '290' })"));
188 ASSERT(!!Tests::spawnKitty("global_move_fullscreen"), true);
189 OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'fullscreen', action = 'set' })"));
190 OK(getFromSocket("/dispatch hl.dsp.window.move({ workspace = '291', follow = false })"));
191
192 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
193 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '291' })"));
194 OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:global_move_fullscreen' })"));
195 EXPECT_CONTAINS(getFromSocket("/activewindow"), "fullscreen: 2");
196
197 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '294' })"));
198
199 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
200 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '292' })"));
201 ASSERT(!!Tests::spawnKitty("global_move_float"), true);
202 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set' })"));
203 OK(getFromSocket("/dispatch hl.dsp.window.resize({ x = 400, y = 300 })"));
204 OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 200, y = 200 })"));
205
206 OK(getFromSocket("/dispatch hl.dsp.window.move({ workspace = '294', follow = true })"));
207 {
208 const auto str = getFromSocket("/activewindow");
209 EXPECT_CONTAINS(str, "class: global_move_float");
210 EXPECT_CONTAINS(str, "at: 2120,200");
211 }
212
213 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '293' })"));
214 ASSERT(!!Tests::spawnKitty("global_move_group_anchor"), true);
215 OK(getFromSocket("/dispatch hl.dsp.group.toggle()"));
216
217 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
218 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '295' })"));
219 ASSERT(!!Tests::spawnKitty("global_move_group_member"), true);
220 OK(getFromSocket("/dispatch hl.dsp.window.move({ workspace = '293', follow = true })"));
221 EXPECT_CONTAINS(getFromSocket("/activewindow"), "class: global_move_group_member");
222
223 OK(getFromSocket("/dispatch hl.dsp.group.next()"));
224 EXPECT_CONTAINS(getFromSocket("/activewindow"), "class: global_move_group_anchor");
225 }
226
227 TEST_CASE(pointerNoWarpsFocusesMonitorOnly) {
228 CScopeGuard guard = {[&]() {
229 OK(getFromSocket("/eval hl.config({ cursor = { no_warps = false } })"));
230 cleanupRefactorStateMonitors();
231 }};
232
233 ASSERT(prepareRefactorStateMonitors(), true);
234 Tests::killAllWindows();
235
236 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
237 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '301' })"));
238 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_LEFT)));
239 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '300' })"));
240
241 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 100, y = 100 })"));
242 const auto originalCursor = getFromSocket("/cursorpos");
243 OK(getFromSocket("/eval hl.config({ cursor = { no_warps = true } })"));
244
245 OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_MONITOR_RIGHT)));
246 EXPECT(getFromSocket("/cursorpos"), originalCursor);
247 {
248 const auto str = getFromSocket("/activeworkspace");
249 EXPECT_CONTAINS(str, "workspace ID 301");
250 EXPECT_CONTAINS(str, TEST_MONITOR_RIGHT);
251 }
252
253 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 2000, y = 10 })"));
254 EXPECT_NOT(getFromSocket("/cursorpos"), originalCursor);
255 }