Debian Codegraph @

hyprtester/src/tests/clients/surface-scale-transform.cpp

raw
1 #include "../../hyprctlCompat.hpp"
2 #include "../../Log.hpp"
3 #include "../../shared.hpp"
4 #include "../shared.hpp"
5 #include "build.hpp"
6 #include "tests.hpp"
7
8 #include <array>
9 #include <cerrno>
10 #include <chrono>
11 #include <csignal>
12 #include <cstring>
13 #include <format>
14 #include <hyprutils/os/FileDescriptor.hpp>
15 #include <hyprutils/os/Process.hpp>
16 #include <hyprutils/utils/ScopeGuard.hpp>
17 #include <optional>
18 #include <stdexcept>
19 #include <string>
20 #include <sys/poll.h>
21 #include <unistd.h>
22
23 using namespace Hyprutils::Memory;
24 using namespace Hyprutils::OS;
25 using namespace Hyprutils::Utils;
26
27 #define SP CSharedPointer
28
29 namespace {
30 class CClient {
31 public:
32 CClient();
33 ~CClient();
34
35 std::string command(const std::string& command);
36
37 private:
38 SP<CProcess> m_proc;
39 std::array<char, 2048> m_readBuf;
40 CFileDescriptor m_readFd, m_writeFd;
41 struct pollfd m_fds = {};
42 };
43 }
44
45 static bool waitForClientWindow(pid_t pid, int timeoutMs) {
46 const auto deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
47 const auto needle = std::format("pid: {}", pid);
48
49 while (Tests::processAlive(pid)) {
50 if (getFromSocket("/clients").contains(needle))
51 return true;
52
53 if (std::chrono::steady_clock::now() >= deadline)
54 return false;
55 }
56
57 return false;
58 }
59
60 CClient::CClient() {
61 m_proc = makeShared<CProcess>(binaryDir + "/surface-scale-transform", std::vector<std::string>{});
62 m_proc->addEnv("WAYLAND_DISPLAY", WLDISPLAY);
63
64 int pipeFds1[2], pipeFds2[2];
65 if (pipe(pipeFds1) != 0 || pipe(pipeFds2) != 0)
66 throw std::runtime_error(std::format("pipe failed: {}", strerror(errno)));
67
68 m_writeFd = CFileDescriptor(pipeFds1[1]);
69 m_proc->setStdinFD(pipeFds1[0]);
70
71 m_readFd = CFileDescriptor(pipeFds2[0]);
72 m_proc->setStdoutFD(pipeFds2[1]);
73
74 const int COUNT_BEFORE = Tests::windowCount();
75 m_proc->runAsync();
76
77 close(pipeFds1[0]);
78 close(pipeFds2[1]);
79
80 m_fds = {.fd = m_readFd.get(), .events = POLLIN};
81 m_fds.revents = 0;
82 int pollRet = 0;
83 do {
84 pollRet = poll(&m_fds, 1, 30000);
85 } while (pollRet == -1 && errno == EINTR);
86
87 if (pollRet != 1 || !(m_fds.revents & POLLIN))
88 throw std::runtime_error(std::format("startup stdout poll failed: ret={} revents={} alive={} pid={} binary={}", pollRet, m_fds.revents, Tests::processAlive(m_proc->pid()),
89 m_proc->pid(), binaryDir + "/surface-scale-transform"));
90
91 m_readBuf.fill(0);
92 const ssize_t bytesRead = read(m_readFd.get(), m_readBuf.data(), m_readBuf.size() - 1);
93 if (bytesRead <= 0)
94 throw std::runtime_error(std::format("startup stdout read failed: bytes={} errno={} ({}) revents={} alive={} pid={}", bytesRead, errno, strerror(errno), m_fds.revents,
95 Tests::processAlive(m_proc->pid()), m_proc->pid()));
96
97 const std::string ret = std::string{m_readBuf.data()};
98 if (!ret.contains("started")) {
99 NLog::log("{}Failed to start surface-scale-transform client, read {}", Colors::RED, ret);
100 throw std::runtime_error(std::format("client reported '{}'", ret));
101 }
102
103 if (!waitForClientWindow(m_proc->pid(), 10000)) {
104 NLog::log("{}Timed out waiting for surface-scale-transform window. count before: {}, count after: {}", Colors::RED, COUNT_BEFORE, Tests::windowCount());
105 throw std::runtime_error(std::format("window did not appear for pid {}, count before {}, count after {}", m_proc->pid(), COUNT_BEFORE, Tests::windowCount()));
106 }
107 }
108
109 CClient::~CClient() {
110 const std::string cmd = "exit\n";
111 write(m_writeFd.get(), cmd.c_str(), cmd.length());
112
113 if (m_proc)
114 kill(m_proc->pid(), SIGKILL);
115 }
116
117 std::string CClient::command(const std::string& command) {
118 const std::string cmd = command + "\n";
119 if ((size_t)write(m_writeFd.get(), cmd.c_str(), cmd.length()) != cmd.length())
120 return "";
121
122 m_fds.revents = 0;
123 if (poll(&m_fds, 1, 10000) != 1 || !(m_fds.revents & POLLIN))
124 return "";
125
126 const ssize_t bytesRead = read(m_fds.fd, m_readBuf.data(), m_readBuf.size() - 1);
127 if (bytesRead <= 0)
128 return "";
129
130 m_readBuf[bytesRead] = 0;
131 std::string ret = std::string{m_readBuf.data()};
132 if (!ret.empty() && ret.back() == '\n')
133 ret.pop_back();
134 return ret;
135 }
136
137 static std::string waitCommandContains(CClient& client, const std::string& command, const std::string& needle) {
138 std::string ret;
139 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
140
141 while (std::chrono::steady_clock::now() < deadline) {
142 ret = client.command(command);
143 if (ret.contains(needle))
144 return ret;
145
146 getFromSocket("/version");
147 }
148
149 return ret;
150 }
151
152 static std::string waitCommandDiffers(CClient& client, const std::string& command, const std::string& previous) {
153 std::string ret;
154 const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
155
156 while (std::chrono::steady_clock::now() < deadline) {
157 ret = client.command(command);
158 if (ret != previous)
159 return ret;
160
161 getFromSocket("/version");
162 }
163
164 return ret;
165 }
166
167 TEST_CASE(surfaceScaleTransform) {
168 CScopeGuard guard = {[&]() {
169 OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '0x0', scale = '1', transform = 0 })"));
170 Tests::killAllWindows();
171 }};
172
173 OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '0x0', scale = '1.5', transform = 0 })"));
174 OK(getFromSocket("/dispatch hl.dsp.focus({ monitor = 'HEADLESS-2' })"));
175 OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '260' })"));
176
177 std::optional<CClient> client;
178 try {
179 client.emplace();
180 } catch (const std::exception& e) { FAIL_TEST("Couldn't start the surface scale/transform client: {}", e.what()); }
181
182 ASSERT_CONTAINS(client->command("report"), "root_scale=2");
183 ASSERT_CONTAINS(client->command("report"), "root_fraction=180");
184 ASSERT_CONTAINS(client->command("report"), "root_transform=0");
185
186 OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '0x0', scale = '1.5', transform = 1 })"));
187 ASSERT_CONTAINS(waitCommandContains(*client, "report", "root_transform=1"), "root_transform=1");
188
189 const auto beforeUnmap = client->command("report");
190 ASSERT_CONTAINS(beforeUnmap, "root_scale_count=");
191
192 ASSERT(client->command("unmap"), "ok");
193 ASSERT(client->command("remap"), "ok");
194 const auto afterRemap = waitCommandDiffers(*client, "report", beforeUnmap);
195 EXPECT_CONTAINS(afterRemap, "root_scale=2");
196 EXPECT_CONTAINS(afterRemap, "root_fraction=180");
197 EXPECT_NOT(afterRemap, beforeUnmap);
198
199 ASSERT(client->command("subsurface"), "ok");
200 const auto withChild = client->command("report");
201 EXPECT_CONTAINS(withChild, "child_scale=2");
202 EXPECT_CONTAINS(withChild, "child_fraction=180");
203 EXPECT_CONTAINS(withChild, "child_transform=1");
204 }