summaryrefslogtreecommitdiff
path: root/hyprtester/src/tests/main/gestures.cpp
blob: f82a1613bc11d123d08a945f970bc8ec64f5284e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "tests.hpp"
#include "../../shared.hpp"
#include "../../hyprctlCompat.hpp"
#include <print>
#include <thread>
#include <chrono>
#include <hyprutils/os/Process.hpp>
#include <hyprutils/memory/WeakPtr.hpp>
#include <hyprutils/string/Numeric.hpp>
#include <csignal>
#include <cerrno>
#include "../shared.hpp"

using namespace Hyprutils::OS;
using namespace Hyprutils::Memory;
using namespace Hyprutils::String;

#define UP CUniquePointer
#define SP CSharedPointer

// TODO: refactor and reuse `Tests::waitUntilWindowsN`
static bool waitForWindowCount(int expectedWindowCnt, std::string_view expectation, int waitMillis = 100, int maxWaitCnt = 50) {
    int counter = 0;
    while (Tests::windowCount() != expectedWindowCnt) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(waitMillis));

        if (counter > maxWaitCnt) {
            NLog::log("{}Unmet expectation: {}", Colors::RED, expectation);
            return false;
        }
    }
    return true;
}

// TODO: decompose this into multiple test cases
TEST_CASE(gestures) {
    Tests::spawnKitty();
    ASSERT(Tests::windowCount(), 1);

    // Give the shell a moment to initialize
    std::this_thread::sleep_for(std::chrono::milliseconds(500));

    OK(getFromSocket("/eval hl.plugin.test.gesture('up', 5)"));
    OK(getFromSocket("/eval hl.plugin.test.gesture('down', 5)"));
    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 5)"));
    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 5)"));
    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 4)"));

    EXPECT(waitForWindowCount(0, "Gesture sent paste exit + enter to kitty"), true);

    EXPECT(Tests::windowCount(), 0);

    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));

    EXPECT(waitForWindowCount(1, "Gesture spawned kitty"), true);

    EXPECT(Tests::windowCount(), 1);

    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));

    {
        auto str = getFromSocket("/clients");
        EXPECT_CONTAINS(str, "floating: 1");
    }

    OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));

    {
        auto str = getFromSocket("/clients");
        EXPECT_CONTAINS(str, "fullscreen: 2");
    }

    OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));

    {
        auto str = getFromSocket("/clients");
        EXPECT_CONTAINS(str, "fullscreen: 0");
    }

    OK(getFromSocket("/eval hl.plugin.test.alt(1)"));

    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_CONTAINS(str, "ID 2 (2)");
    }

    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
    }

    // check for crashes
    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
    }

    OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 0 } })"));

    OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_CONTAINS(str, "ID 2 (2)");
    }

    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
    }

    OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 1 } })"));
    OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_create_new = 0 } })"));

    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 3)"));

    {
        auto str = getFromSocket("/workspaces");
        EXPECT_NOT_CONTAINS(str, "ID 2 (2)");
        EXPECT_CONTAINS(str, "ID 1 (1)");
    }

    OK(getFromSocket("/eval hl.plugin.test.gesture('down', 3)"));

    {
        auto str = getFromSocket("/clients");
        EXPECT_CONTAINS(str, "floating: 0");
    }

    OK(getFromSocket("/eval hl.plugin.test.alt(0)"));

    OK(getFromSocket("/eval hl.plugin.test.gesture('up', 3)"));

    EXPECT(waitForWindowCount(0, "Gesture closed kitty"), true);

    ASSERT(Tests::windowCount(), 0);

    // This test ensures that `movecursortocorner`, which expects
    // a single-character direction argument, is parsed correctly.
    Tests::spawnKitty();
    OK(getFromSocket("/dispatch hl.dsp.cursor.move_to_corner({ corner = 0, window = 'activewindow' })"));
    const std::string cursorPos1 = getFromSocket("/cursorpos");
    OK(getFromSocket("/eval hl.plugin.test.gesture('left', 4)"));
    const std::string cursorPos2 = getFromSocket("/cursorpos");
    // The cursor should have moved because of the gesture
    EXPECT(cursorPos1 != cursorPos2, true);

    // Test that `workspace previous` works correctly after a workspace gesture.
    {
        OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_invert = 0 } })"));
        OK(getFromSocket("/eval hl.config({ gestures = { workspace_swipe_create_new = 1 } })"));
        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '3' })"));

        // Come to workspace 5 from workspace 3: 5 will remember that.
        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '5' })"));
        Tests::spawnKitty(); // Keep workspace 5 open

        // Swipe from 1 to 5: 5 shall remember that.
        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
        OK(getFromSocket("/eval hl.plugin.test.alt(1)"));
        OK(getFromSocket("/eval hl.plugin.test.gesture('right', 3)"));
        OK(getFromSocket("/eval hl.plugin.test.alt(0)"));
        EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 5 (5)");

        // Must return to 1 rather than 3
        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'previous' })"));
        EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 1 (1)");

        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = 'previous' })"));
        EXPECT_CONTAINS(getFromSocket("/activeworkspace"), "ID 5 (5)");

        OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })"));
    }
    const std::string cursorPosBeforePinch = getFromSocket("/cursorpos");

    OK(getFromSocket("/dispatch hl.dsp.cursor.move({ x = 500, y = 500 })"));
    OK(getFromSocket("/eval hl.config({ cursor = { zoom_factor = 1 } })"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));

    OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 1.2)"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.2, 0.01)"));
    OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 1.6)"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.6, 0.01)"));
    OK(getFromSocket("/eval hl.plugin.test.pinch_end()"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1.6, 0.01)"));

    OK(getFromSocket("/eval hl.plugin.test.pinch_update(2, 0.64)"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));
    OK(getFromSocket("/eval hl.plugin.test.pinch_end()"));
    OK(getFromSocket("/eval hl.plugin.test.expect_cursor_zoom(1, 0.01)"));

    const auto comma = cursorPosBeforePinch.find(',');

    if (comma != std::string::npos) {
        auto xSv = std::string_view(cursorPosBeforePinch).substr(0, comma);
        auto ySv = std::string_view(cursorPosBeforePinch).substr(comma + 1);
        while (!xSv.empty() && xSv.front() == ' ')
            xSv.remove_prefix(1);
        while (!ySv.empty() && ySv.front() == ' ')
            ySv.remove_prefix(1);

        const auto x = strToNumber<int>(xSv);
        const auto y = strToNumber<int>(ySv);

        if (!x || !y)
            FAIL_TEST("Failed to restore cursor pos");

        OK(getFromSocket(std::format("/dispatch hl.dsp.cursor.move({{ x = {}, y = {} }})", x.value(), y.value())));
    }
}