summaryrefslogtreecommitdiff
path: root/hyprtester/src/tests/main/hyprctl.cpp
blob: 2946c630581a69d490981429de781d95f1d0f994 (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
#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 <csignal>
#include <cerrno>
#include "../shared.hpp"

static int ret = 0;

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

#define UP CUniquePointer
#define SP CSharedPointer

static std::string getCommandStdOut(std::string command) {
    CProcess process("bash", {"-c", command});
    process.addEnv("HYPRLAND_INSTANCE_SIGNATURE", HIS);
    process.runSync();

    const std::string& stdOut = process.stdOut();

    // Remove trailing new line
    return stdOut.substr(0, stdOut.length() - 1);
}

static bool testGetprop() {
    NLog::log("{}Testing hyprctl getprop", Colors::GREEN);
    if (!Tests::spawnKitty()) {
        NLog::log("{}Error: kitty did not spawn", Colors::RED);
        return false;
    }

    // animationstyle
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty animationstyle"), "(unset)");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty animationstyle -j"), R"({"animationstyle": ""})");
    getFromSocket("/dispatch setprop class:kitty animationstyle teststyle");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty animationstyle"), "teststyle");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty animationstyle -j"), R"({"animationstyle": "teststyle"})");

    // maxsize
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty maxsize"), "inf inf");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty maxsize -j"), R"({"maxsize": [null,null]})");
    getFromSocket("/dispatch setprop class:kitty maxsize 200 150");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty maxsize"), "200 150");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty maxsize -j"), R"({"maxsize": [200,150]})");

    // minsize
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty minsize"), "20 20");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty minsize -j"), R"({"minsize": [20,20]})");
    getFromSocket("/dispatch setprop class:kitty minsize 100 50");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty minsize"), "100 50");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty minsize -j"), R"({"minsize": [100,50]})");

    // alpha
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alpha"), "1");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alpha -j"), R"({"alpha": 1})");
    getFromSocket("/dispatch setprop class:kitty alpha 0.3");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alpha"), "0.3");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alpha -j"), R"({"alpha": 0.3})");

    // alphainactive
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactive"), "1");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactive -j"), R"({"alphainactive": 1})");
    getFromSocket("/dispatch setprop class:kitty alphainactive 0.5");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactive"), "0.5");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactive -j"), R"({"alphainactive": 0.5})");

    // alphafullscreen
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreen"), "1");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreen -j"), R"({"alphafullscreen": 1})");
    getFromSocket("/dispatch setprop class:kitty alphafullscreen 0.75");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreen"), "0.75");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreen -j"), R"({"alphafullscreen": 0.75})");

    // alphaoverride
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphaoverride"), "false");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphaoverride -j"), R"({"alphaoverride": false})");
    getFromSocket("/dispatch setprop class:kitty alphaoverride true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphaoverride"), "true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphaoverride -j"), R"({"alphaoverride": true})");

    // alphainactiveoverride
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactiveoverride"), "false");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactiveoverride -j"), R"({"alphainactiveoverride": false})");
    getFromSocket("/dispatch setprop class:kitty alphainactiveoverride true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactiveoverride"), "true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphainactiveoverride -j"), R"({"alphainactiveoverride": true})");

    // alphafullscreenoverride
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreenoverride"), "false");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreenoverride -j"), R"({"alphafullscreenoverride": false})");
    getFromSocket("/dispatch setprop class:kitty alphafullscreenoverride true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreenoverride"), "true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty alphafullscreenoverride -j"), R"({"alphafullscreenoverride": true})");

    // activebordercolor
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty activebordercolor"), "ee33ccff ee00ff99 45deg");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty activebordercolor -j"), R"({"activebordercolor": "ee33ccff ee00ff99 45deg"})");
    getFromSocket("/dispatch setprop class:kitty activebordercolor rgb(abcdef)");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty activebordercolor"), "ffabcdef 0deg");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty activebordercolor -j"), R"({"activebordercolor": "ffabcdef 0deg"})");

    // bool window properties
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty allowsinput"), "false");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty allowsinput -j"), R"({"allowsinput": false})");
    getFromSocket("/dispatch setprop class:kitty allowsinput true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty allowsinput"), "true");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty allowsinput -j"), R"({"allowsinput": true})");

    // int window properties
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty rounding"), "10");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty rounding -j"), R"({"rounding": 10})");
    getFromSocket("/dispatch setprop class:kitty rounding 4");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty rounding"), "4");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty rounding -j"), R"({"rounding": 4})");

    // float window properties
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty roundingpower"), "2");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty roundingpower -j"), R"({"roundingpower": 2})");
    getFromSocket("/dispatch setprop class:kitty roundingpower 1.25");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty roundingpower"), "1.25");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty roundingpower -j"), R"({"roundingpower": 1.25})");

    // errors
    EXPECT(getCommandStdOut("hyprctl getprop"), "not enough args");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty"), "not enough args");
    EXPECT(getCommandStdOut("hyprctl getprop class:nonexistantclass animationstyle"), "window not found");
    EXPECT(getCommandStdOut("hyprctl getprop class:kitty nonexistantprop"), "prop not found");

    // kill all
    NLog::log("{}Killing all windows", Colors::YELLOW);
    Tests::killAllWindows();

    NLog::log("{}Expecting 0 windows", Colors::YELLOW);
    EXPECT(Tests::windowCount(), 0);

    return true;
}

static bool test() {
    NLog::log("{}Testing hyprctl", Colors::GREEN);

    {
        NLog::log("{}Testing hyprctl descriptions for any json errors", Colors::GREEN);
        CProcess jqProc("bash", {"-c", "hyprctl descriptions | jq"});
        jqProc.addEnv("HYPRLAND_INSTANCE_SIGNATURE", HIS);
        jqProc.runSync();
        EXPECT(jqProc.exitCode(), 0);
    }

    if (!testGetprop())
        return false;

    return !ret;
}

REGISTER_TEST_FN(test);