Debian Codegraph @

hyprtester/src/tests/clients/child-window.cpp

raw
1 #include "../../hyprctlCompat.hpp"
2 #include "../../Log.hpp"
3 #include "../shared.hpp"
4 #include "tests.hpp"
5 #include "build.hpp"
6
7 #include <hyprutils/os/FileDescriptor.hpp>
8 #include <hyprutils/os/Process.hpp>
9
10 #include <optional>
11 #include <sys/poll.h>
12 #include <unistd.h>
13 #include <csignal>
14 #include <thread>
15
16 using namespace Hyprutils::OS;
17 using namespace Hyprutils::Memory;
18
19 #define SP CSharedPointer
20
21 static bool waitForWindow(SP<CProcess> proc, int windowsBefore) {
22 int counter = 0;
23 while (Tests::processAlive(proc->pid()) && Tests::windowCount() == windowsBefore) {
24 counter++;
25 std::this_thread::sleep_for(std::chrono::milliseconds(100));
26
27 if (counter > 50)
28 return false;
29 }
30
31 NLog::log("{}Waited {} milliseconds for window to open", Colors::YELLOW, counter * 100);
32 return Tests::processAlive(proc->pid());
33 }
34
35 namespace {
36 class CClient {
37 SP<CProcess> proc;
38 std::array<char, 1024> readBuf;
39 CFileDescriptor readFd, writeFd;
40 struct pollfd fds;
41
42 public:
43 CClient();
44 ~CClient();
45 bool createChild();
46 };
47 }
48
49 CClient::CClient() {
50 NLog::log("{}Attempting to start child-window client", Colors::YELLOW);
51
52 this->proc = makeShared<CProcess>(binaryDir + "/child-window", std::vector<std::string>{});
53
54 this->proc->addEnv("WAYLAND_DISPLAY", WLDISPLAY);
55
56 int procInPipeFd[2], procOutPipeFd[2];
57 if (pipe(procInPipeFd) != 0 || pipe(procOutPipeFd) != 0) {
58 NLog::log("{}Unable to open pipe to client", Colors::RED);
59 throw std::exception();
60 }
61
62 this->writeFd = CFileDescriptor(procInPipeFd[1]);
63 this->proc->setStdinFD(procInPipeFd[0]);
64
65 this->readFd = CFileDescriptor(procOutPipeFd[0]);
66 this->proc->setStdoutFD(procOutPipeFd[1]);
67
68 if (!this->proc->runAsync()) {
69 NLog::log("{}Failed to run client", Colors::RED);
70 throw std::exception();
71 }
72
73 close(procInPipeFd[0]);
74 close(procOutPipeFd[1]);
75
76 if (!waitForWindow(this->proc, Tests::windowCount())) {
77 NLog::log("{}Window took too long to open", Colors::RED);
78 throw std::exception();
79 }
80
81 NLog::log("{}Started child-window client", Colors::YELLOW);
82 }
83
84 CClient::~CClient() {
85 std::string cmd = "exit\n";
86 write(this->writeFd.get(), cmd.c_str(), cmd.length());
87
88 kill(this->proc->pid(), SIGKILL);
89 this->proc.reset();
90 }
91
92 bool CClient::createChild() {
93 std::string cmd = "toplevel\n";
94 if ((size_t)write(this->writeFd.get(), cmd.c_str(), cmd.length()) != cmd.length())
95 return false;
96
97 if (!waitForWindow(this->proc, Tests::windowCount()))
98 NLog::log("{}Child window took too long to open", Colors::RED);
99
100 if (getFromSocket("/dispatch hl.dsp.focus({ window = 'class:child-test-child' })") != "ok") {
101 NLog::log("{}Failed to focus child window", Colors::RED);
102 return false;
103 }
104
105 return true;
106 }
107
108 TEST_CASE(childWindow) {
109 {
110 std::optional<CClient> client;
111 try {
112 client.emplace();
113 } catch (...) { FAIL_TEST("Couldn't start the client"); }
114
115 OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set', window = 'class:child-test-parent' })"));
116 OK(getFromSocket("/dispatch hl.dsp.window.pin({ action = 'set', window = 'class:child-test-parent' })"));
117
118 client->createChild();
119 EXPECT(Tests::windowCount(), 2);
120 EXPECT_COUNT_STRING(getFromSocket("/clients"), "pinned: 1", 2);
121 }
122
123 NLog::log("{}Reloading config", Colors::YELLOW);
124 OK(getFromSocket("/reload"));
125 Tests::killAllWindows();
126 EXPECT(Tests::windowCount(), 0);
127
128 // test that child windows (shouldBeFloated) are not auto-grouped
129 NLog::log("{}Test child windows are not auto-grouped", Colors::GREEN);
130 auto kitty = Tests::spawnKitty();
131 if (!kitty) {
132 FAIL_TEST("Couldn't spawn kitty");
133 }
134
135 // create group and enable auto-grouping
136 OK(getFromSocket("/dispatch hl.dsp.group.toggle()"));
137 OK(getFromSocket("/eval hl.config({ group = { auto_group = true } })"));
138
139 {
140 std::optional<CClient> client2;
141 try {
142 client2.emplace();
143 } catch (...) { FAIL_TEST("Couldn't start the client"); }
144
145 EXPECT(Tests::windowCount(), 2);
146 client2->createChild();
147 EXPECT(Tests::windowCount(), 3);
148
149 // child has set_parent so shouldBeFloated returns true, it should not be auto-grouped
150 EXPECT_COUNT_STRING(getFromSocket("/clients"), "grouped: 0", 1);
151 }
152
153 Tests::killAllWindows();
154 EXPECT(Tests::windowCount(), 0);
155 }