Debian Codegraph @

hyprtester/src/tests/main/gestures.cpp

raw
1 #include "tests.hpp"
2 #include "../../shared.hpp"
3 #include "../../hyprctlCompat.hpp"
4 #include <thread>
5 #include <hyprutils/os/Process.hpp>
6 #include <hyprutils/memory/WeakPtr.hpp>
7 #include <hyprutils/string/Numeric.hpp>
8 #include <format>
9 #include "../shared.hpp"
10
11 using namespace Hyprutils::OS;
12 using namespace Hyprutils::Memory;
13 using namespace Hyprutils::String;
14
15 #define UP CUniquePointer
16 #define SP CSharedPointer
17
18 // TODO: refactor and reuse `Tests::waitUntilWindowsN`
19 static bool waitForWindowCount(int expectedWindowCnt, std::string_view expectation, int waitMillis = 100, int maxWaitCnt = 50) {
20 int counter = 0;
21 while (Tests::windowCount() != expectedWindowCnt) {
22 counter++;
23 std::this_thread::sleep_for(std::chrono::milliseconds(waitMillis));
24
25 if (counter > maxWaitCnt) {
26 NLog::log("{}Unmet expectation: {}", Colors::RED, expectation);
27 return false;
28 }
29 }
30 return true;
31 }
32
33 static std::string evalLua(std::string_view code) {
34 return getFromSocket(std::format("/eval {}", code));
35 }
36
37 TEST_CASE(live_gesture_callbacks) {
38 OK(evalLua(R"(
39 __liveGesture = { swipe = { start = 0, update = 0, finish = 0 } }
40
41 local function expect_eq(actual, expected, name)
42 if actual ~= expected then
43 error(name .. ': expected ' .. tostring(expected) .. ', got ' .. tostring(actual))
44 end
45 end
46
47 local function expect_vec(vec, x, y, name)
48 if vec == nil then
49 error(name .. ': missing vector')
50 end
51 expect_eq(vec.x, x, name .. '.x')
52 expect_eq(vec.y, y, name .. '.y')
53 end
54
55 hl.gesture({
56 fingers = 6,
57 direction = 'left',
58 action = {
59 start = function(e)
60 __liveGesture.swipe.start = __liveGesture.swipe.start + 1
61 expect_eq(e.phase, 'start', 'swipe start phase')
62 expect_eq(e.type, 'swipe', 'swipe start type')
63 expect_eq(e.direction, 'LEFT', 'swipe start direction')
64 expect_eq(e.fingers, 6, 'swipe start fingers')
65 expect_vec(e.delta, -300, 0, 'swipe start delta')
66 end,
67 update = function(e)
68 __liveGesture.swipe.update = __liveGesture.swipe.update + 1
69 expect_eq(e.phase, 'update', 'swipe update phase')
70 expect_eq(e.type, 'swipe', 'swipe update type')
71 expect_eq(e.direction, 'LEFT', 'swipe update direction')
72 expect_eq(e.fingers, 6, 'swipe update fingers')
73 expect_vec(e.delta, -300, 0, 'swipe update delta')
74 end,
75 finish = function(e)
76 __liveGesture.swipe.finish = __liveGesture.swipe.finish + 1
77 expect_eq(e.phase, 'end', 'swipe end phase')
78 expect_eq(e.type, 'swipe', 'swipe end type')
79 expect_eq(e.direction, 'LEFT', 'swipe end direction')
80 expect_eq(e.cancelled, false, 'swipe end cancelled')
81 expect_eq(e.fingers, nil, 'swipe end fingers')
82 expect_eq(e.delta, nil, 'swipe end delta')
83 end,
84 },
85 })
86 )"));
87
88 OK(evalLua(R"(
89 hl.plugin.test.gesture('left', 6)
90
91 if __liveGesture.swipe.start ~= 1 or __liveGesture.swipe.update ~= 1 or __liveGesture.swipe.finish ~= 1 then
92 error('unexpected swipe counts: start=' .. __liveGesture.swipe.start .. ', update=' .. __liveGesture.swipe.update .. ', end=' .. __liveGesture.swipe.finish)
93 end
94 )"));
95
96 OK(evalLua(R"(
97 __liveGesture.pinch = { start = 0, update = 0, finish = 0, last_scale = 0, last_rotation = 0 }
98
99 local function expect_eq(actual, expected, name)
100 if actual ~= expected then
101 error(name .. ': expected ' .. tostring(expected) .. ', got ' .. tostring(actual))
102 end
103 end
104
105 local function expect_vec(vec, x, y, name)
106 if vec == nil then
107 error(name .. ': missing vector')
108 end
109 expect_eq(vec.x, x, name .. '.x')
110 expect_eq(vec.y, y, name .. '.y')
111 end
112
113 hl.gesture({
114 fingers = 7,
115 direction = 'pinch',
116 action = {
117 start = function(e)
118 __liveGesture.pinch.start = __liveGesture.pinch.start + 1
119 expect_eq(e.phase, 'start', 'pinch start phase')
120 expect_eq(e.type, 'pinch', 'pinch start type')
121 expect_eq(e.direction, 'PINCH_IN', 'pinch start direction')
122 expect_eq(e.fingers, 7, 'pinch start fingers')
123 expect_vec(e.delta, 11, 12, 'pinch start delta')
124 expect_eq(e.scale, 1.2, 'pinch start scale')
125 expect_eq(e.rotation, 13, 'pinch start rotation')
126 end,
127 update = function(e)
128 __liveGesture.pinch.update = __liveGesture.pinch.update + 1
129 expect_eq(e.phase, 'update', 'pinch update phase')
130 expect_eq(e.type, 'pinch', 'pinch update type')
131 expect_eq(e.direction, 'PINCH_IN', 'pinch update direction')
132 expect_eq(e.fingers, 7, 'pinch update fingers')
133 __liveGesture.pinch.last_scale = e.scale
134 __liveGesture.pinch.last_rotation = e.rotation
135 end,
136 finish = function(e)
137 __liveGesture.pinch.finish = __liveGesture.pinch.finish + 1
138 expect_eq(e.phase, 'end', 'pinch end phase')
139 expect_eq(e.type, 'pinch', 'pinch end type')
140 expect_eq(e.direction, 'PINCH', 'pinch end direction')
141 expect_eq(e.cancelled, false, 'pinch end cancelled')
142 expect_eq(e.fingers, nil, 'pinch end fingers')
143 expect_eq(e.delta, nil, 'pinch end delta')
144 end,
145 },
146 })
147 )"));
148
149 OK(evalLua(R"(
150 hl.plugin.test.pinch_update(7, 1.2, 11, 12, 13)
151 hl.plugin.test.pinch_update(7, 1.4, 21, 22, 23)
152 hl.plugin.test.pinch_end()
153
154 if __liveGesture.pinch.start ~= 1 or __liveGesture.pinch.update ~= 2 or __liveGesture.pinch.finish ~= 1 then
155 error('unexpected pinch counts: start=' .. __liveGesture.pinch.start .. ', update=' .. __liveGesture.pinch.update .. ', end=' .. __liveGesture.pinch.finish)
156 end
157 if __liveGesture.pinch.last_scale ~= 1.4 or __liveGesture.pinch.last_rotation ~= 23 then
158 error('unexpected last pinch payload: scale=' .. tostring(__liveGesture.pinch.last_scale) .. ', rotation=' .. tostring(__liveGesture.pinch.last_rotation))
159 end
160 )"));
161
162 OK(evalLua(R"(
163 __liveGesture.legacy = { count = 0, argc = -1 }
164
165 hl.gesture({
166 fingers = 6,
167 direction = 'right',
168 action = function(...)
169 __liveGesture.legacy.count = __liveGesture.legacy.count + 1
170 __liveGesture.legacy.argc = select('#', ...)
171 end,
172 })
173
174 hl.plugin.test.gesture('right', 6)
175
176 if __liveGesture.legacy.count ~= 1 or __liveGesture.legacy.argc ~= 0 then
177 error('legacy gesture callback changed: count=' .. __liveGesture.legacy.count .. ', argc=' .. __liveGesture.legacy.argc)
178 end
179 )"));
180
181 EXPECT_CONTAINS(evalLua("hl.gesture({ fingers = 8, direction = 'up', action = { start = 1 } })"), "action.start must be a function");
182 EXPECT_CONTAINS(evalLua("hl.gesture({ fingers = 8, direction = 'up', action = {} })"), "must define at least one of start, update, end, or finish");
183 }
184
185 // TODO: decompose this into multiple test cases
186 TEST_CASE(gestures) {
187 Tests::spawnKitty();
188 ASSERT(Tests::windowCount(), 1);
189
190 // Give the shell a moment to initialize
191 std::this_thread::sleep_for(std::chrono::milliseconds(500));
192
193 OK(getFromSocket("/eval hl.plugin.test.gesture('up', 5)"));
194 OK(getFromSocket("/eval hl.plugin.test.gesture('down', 5)"));
195 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 5)"));
196 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 5)"));
197 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 4)"));
198
199 EXPECT(waitForWindowCount(0, "Gesture sent paste exit + enter to kitty"), true);
200
201 EXPECT(Tests::windowCount(), 0);
202
203 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));
204
205 EXPECT(waitForWindowCount(1, "Gesture spawned kitty"), true);
206
207 EXPECT(Tests::windowCount(), 1);
208
209 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
210
211 {
212 auto str = getFromSocket("/clients");
213 EXPECT_CONTAINS(str, "floating: 1");
214 }
215
216 OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));
217
218 {
219 auto str = getFromSocket("/clients");
220 EXPECT_CONTAINS(str, "fullscreen: 2");
221 }
222
223 OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));
224
225 {
226 auto str = getFromSocket("/clients");
227 EXPECT_CONTAINS(str, "fullscreen: 0");
228 }
229
230 OK(getFromSocket("/eval hl.plugin.test.alt(1)"));
231
232 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));
233
234 {
235 auto str = getFromSocket("/workspaces");
236 EXPECT_CONTAINS(str, "ID 2 (2)");
237 }
238
239 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
240
241 {
242 auto str = getFromSocket("/workspaces");
243 EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
244 }
245
246 // check for crashes
247 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
248
249 {
250 auto str = getFromSocket("/workspaces");
251 EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
252 }
253
254 OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 0 } })"));
255
256 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
257
258 {
259 auto str = getFromSocket("/workspaces");
260 EXPECT_CONTAINS(str, "ID 2 (2)");
261 }
262
263 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));
264
265 {
266 auto str = getFromSocket("/workspaces");
267 EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
268 }
269
270 OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 1 } })"));
271 OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_create_new = 0 } })"));
272
273 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));
274
275 {
276 auto str = getFromSocket("/workspaces");
277 EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
278 EXPECT_CONTAINS(str, "ID 1 (1)");
279 }
280
281 OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));
282
283 {
284 auto str = getFromSocket("/clients");
285 EXPECT_CONTAINS(str, "floating: 0");
286 }
287
288 OK(getFromSocket("/eval hl.plugin.test.alt(0)"));
289
290 OK(getFromSocket("/eval hl.plugin.test.gesture('up', 3)"));
291
292 EXPECT(waitForWindowCount(0, "Gesture closed kitty"), true);
293
294 ASSERT(Tests::windowCount(), 0);
295
296 // This test ensures that `movecursortocorner`, which expects
297 // a single-character direction argument, is parsed correctly.
298 Tests::spawnKitty();
299 OK(getFromSocket("/dispatch hl.dsp.cursor.move_to_corner({ corner = 0, window = 'activewindow' })"));
300 const std::string cursorPos1 = getFromSocket("/cursorpos");
301 OK(getFromSocket("/eval hl.plugin.test.gesture('left', 4)"));
302 const std::string cursorPos2 = getFromSocket("/cursorpos");
303 // The cursor should have moved because of the gesture
304 EXPECT(cursorPos1 != cursorPos2, true);
305
306 // Test that `workspace previous` works correctly after a workspace gesture.
307 {
308 OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 0 } })"));
309 OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_create_new = 1 } })"));
310 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '3' })"));
311
312 // Come to workspace 5 from workspace 3: 5 will remember that.
313 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '5' })"));
314 Tests::spawnKitty(); // Keep workspace 5 open
315
316 // Swipe from 1 to 5: 5 shall remember that.
317 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
318 OK(getFromSocket("/eval hl.plugin.test.alt(1)"));
319 OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
320 OK(getFromSocket("/eval hl.plugin.test.alt(0)"));
321 EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 5 (5)");
322
323 // Must return to 1 rather than 3
324 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'previous' })"));
325 EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 1 (1)");
326
327 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'previous' })"));
328 EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 5 (5)");
329
330 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
331 }
332 const std::string cursorPosBeforePinch = getFromSocket("/cursorpos");
333
334 OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 500, y = 500 })"));
335 OK(getFromSocket("/eval hl.config({ cursor = { zoom_factor = 1 } })"));
336 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));
337
338 OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 1.2)"));
339 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.2, 0.01)"));
340 OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 1.6)"));
341 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.6, 0.01)"));
342 OK(getFromSocket("/eval hl.plugin.test.pinch_end()"));
343 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.6, 0.01)"));
344
345 OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 0.64)"));
346 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));
347 OK(getFromSocket("/eval hl.plugin.test.pinch_end()"));
348 OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));
349
350 const auto comma = cursorPosBeforePinch.find(',');
351
352 if (comma != std::string::npos) {
353 auto xSv = std::string_view(cursorPosBeforePinch).substr(0, comma);
354 auto ySv = std::string_view(cursorPosBeforePinch).substr(comma + 1);
355 while (!xSv.empty() && xSv.front() == ' ')
356 xSv.remove_prefix(1);
357 while (!ySv.empty() && ySv.front() == ' ')
358 ySv.remove_prefix(1);
359
360 const auto x = strToNumber<int>(xSv);
361 const auto y = strToNumber<int>(ySv);
362
363 if (!x || !y)
364 FAIL_TEST("Failed to restore cursor pos");
365
366 OK(getFromSocket(std::format("/dispatch hl.dsp.cursor.move({{ x = {}, y = {} }})", x.value(), y.value())));
367 }
368 }