summaryrefslogtreecommitdiff
path: root/hyprtester/src/tests/shared.cpp
blob: d08f4ea231b6e9b336f7b1738ac8770dbf50ee37 (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
#include "shared.hpp"
#include <cassert>
#include <csignal>
#include <cerrno>
#include <thread>
#include <print>
#include <fstream>
#include "../shared.hpp"
#include "../hyprctlCompat.hpp"

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

// Almost everywhere `Tests::spawnKitty` is used, the return value is immediately tested against `nullptr`
// and the test fails if it is.
// TODO: add a test macro for that.

CUniquePointer<CProcess> Tests::spawnKitty(const std::string& class_, const std::vector<std::string> args) {
    const auto               COUNT_BEFORE = windowCount();

    std::vector<std::string> programArgs = args;
    if (!class_.empty()) {
        programArgs.insert(programArgs.begin(), "--class");
        programArgs.insert(programArgs.begin() + 1, class_);
    }
    CUniquePointer<CProcess> kitty = makeUnique<CProcess>("kitty", programArgs);
    kitty->addEnv("WAYLAND_DISPLAY", WLDISPLAY);
    kitty->runAsync();

    std::this_thread::sleep_for(std::chrono::milliseconds(10));

    // wait while kitty spawns
    int counter = 0;
    while (processAlive(kitty->pid()) && windowCount() == COUNT_BEFORE) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        if (counter > 50)
            return nullptr;
    }

    if (!processAlive(kitty->pid()))
        return nullptr;

    return kitty;
}

CUniquePointer<CProcess> Tests::spawnLayerKitty(const std::string& namespace_, const std::vector<std::string> args) {
    std::vector<std::string> programArgs = args;
    if (!namespace_.empty()) {
        programArgs.insert(programArgs.begin(), "--class");
        programArgs.insert(programArgs.begin() + 1, namespace_);
    }

    programArgs.insert(programArgs.begin(), "+kitten");
    programArgs.insert(programArgs.begin() + 1, "panel");

    CUniquePointer<CProcess> kitty = makeUnique<CProcess>("kitty", programArgs);
    kitty->addEnv("WAYLAND_DISPLAY", WLDISPLAY);
    kitty->runAsync();

    std::this_thread::sleep_for(std::chrono::milliseconds(10));

    // wait while the layer spawns
    int counter = 0;
    while (processAlive(kitty->pid()) && countOccurrences(getFromSocket("/layers"), std::format("pid: {}", kitty->pid())) == 0) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        if (counter > 50)
            return nullptr;
    }

    if (!processAlive(kitty->pid()))
        return nullptr;

    return kitty;
}

bool Tests::processAlive(pid_t pid) {
    errno   = 0;
    int ret = kill(pid, 0);
    return ret != -1 || errno != ESRCH;
}

int Tests::windowCount() {
    return countOccurrences(getFromSocket("/clients"), "focusHistoryID: ");
}

int Tests::countOccurrences(const std::string& in, const std::string& what) {
    int  cnt = 0;
    auto pos = in.find(what);
    while (pos != std::string::npos) {
        cnt++;
        pos = in.find(what, pos + what.length());
    }

    return cnt;
}

bool Tests::killAllWindows() {
    auto str = getFromSocket("/clients");
    auto pos = str.find("Window ");
    while (pos != std::string::npos) {
        auto pos2 = str.find(" -> ", pos);
        getFromSocket("/dispatch hl.dsp.window.kill({ window = 'address:0x" + str.substr(pos + 7, pos2 - pos - 7) + "' })");
        pos = str.find("Window ", pos + 5);
    }

    int counter = 0;
    while (Tests::windowCount() != 0) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        if (counter > 50) {
            std::println("{}Timed out waiting for windows to close", Colors::RED);
            return false;
        }
    }

    return true;
}

void Tests::waitUntilWindowsN(int n) {
    int counter = 0;
    while (Tests::windowCount() != n) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        if (counter > 50) {
            std::println("{}Timed out waiting for windows", Colors::RED);
            return;
        }
    }
}

int Tests::layerCount() {
    return countOccurrences(getFromSocket("/layers"), "namespace: ");
}

bool Tests::killAllLayers() {
    auto str = getFromSocket("/layers");
    auto pos = str.find("pid: ");
    while (pos != std::string::npos) {
        auto pid = stoi(str.substr(pos + 5, str.find('\n', pos)));
        kill(pid, 15);

        // we need to wait for a bit because for some reason otherwise we'll end up
        // with layers with pid -1 if they are all removed at the same time
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        pos = str.find("pid: ", pos + 5);
    }

    int counter = 0;
    while (Tests::layerCount() != 0) {
        counter++;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));

        if (counter > 50) {
            std::println("{}Timed out waiting for layers to close", Colors::RED);
            return false;
        }
    }

    return true;
}

std::string Tests::execAndGet(const std::string& cmd) {
    CProcess proc("/bin/sh", {"-c", cmd});

    if (!proc.runSync()) {
        return "error";
    }

    return proc.stdOut();
}

bool Tests::writeFile(const std::string& name, const std::string& contents) {
    std::ofstream of(name, std::ios::trunc);
    if (!of.good())
        return false;

    of << contents;
    of.close();

    return true;
}

std::string Tests::getAttribute(const std::string& hyprlandResponse, std::string attr) {
    attr += ": ";
    auto pos = hyprlandResponse.find(attr);
    if (pos == std::string::npos) {
        NLog::log("{}Window attribute not found: '{}'", Colors::RED, attr);
        return "Wrong window attribute";
    }
    pos += attr.size();
    auto pos2 = hyprlandResponse.find('\n', pos);
    return hyprlandResponse.substr(pos, pos2 - pos);
}