| 1 |
#include "../shared.hpp" |
| 2 |
#include "../../shared.hpp" |
| 3 |
#include "../../hyprctlCompat.hpp" |
| 4 |
#include "tests.hpp" |
| 5 |
|
| 6 |
#include <chrono> |
| 7 |
#include <format> |
| 8 |
#include <thread> |
| 9 |
|
| 10 |
#include <hyprutils/utils/ScopeGuard.hpp> |
| 11 |
|
| 12 |
using namespace Hyprutils::Utils; |
| 13 |
|
| 14 |
static bool waitForMonitor(const char* name) { |
| 15 |
for (int i = 0; i < 50; ++i) { |
| 16 |
if (getFromSocket("/monitors").contains(name)) |
| 17 |
return true; |
| 18 |
|
| 19 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 20 |
} |
| 21 |
|
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
static bool waitForMonitorRemoved(const char* name) { |
| 26 |
for (int i = 0; i < 50; ++i) { |
| 27 |
if (!getFromSocket("/monitors").contains(name)) |
| 28 |
return true; |
| 29 |
|
| 30 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 31 |
} |
| 32 |
|
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
TEST_CASE(single_window_aspect_ratio) { |
| 37 |
OK(getFromSocket("/eval hl.config({ layout = { single_window_aspect_ratio = '1 1' } })")); |
| 38 |
|
| 39 |
Tests::spawnKitty(); |
| 40 |
|
| 41 |
{ |
| 42 |
auto str = getFromSocket("/activewindow"); |
| 43 |
EXPECT_CONTAINS(str, "at: 442,22"); |
| 44 |
EXPECT_CONTAINS(str, "size: 1036,1036"); |
| 45 |
} |
| 46 |
|
| 47 |
Tests::spawnKitty(); |
| 48 |
|
| 49 |
OK(getFromSocket("/dispatch hl.dsp.window.kill({ window = 'activewindow' })")); |
| 50 |
|
| 51 |
Tests::waitUntilWindowsN(1); |
| 52 |
|
| 53 |
{ |
| 54 |
auto str = getFromSocket("/activewindow"); |
| 55 |
EXPECT_CONTAINS(str, "at: 442,22"); |
| 56 |
EXPECT_CONTAINS(str, "size: 1036,1036"); |
| 57 |
} |
| 58 |
|
| 59 |
// don't use swar on maximized |
| 60 |
OK(getFromSocket("/dispatch hl.dsp.window.fullscreen({ mode = 'maximized' })")); |
| 61 |
|
| 62 |
{ |
| 63 |
auto str = getFromSocket("/activewindow"); |
| 64 |
EXPECT_CONTAINS(str, "at: 22,22"); |
| 65 |
EXPECT_CONTAINS(str, "size: 1876,1036"); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
// Don't crash when focus after global geometry changes |
| 70 |
TEST_CASE(crashOnGeomUpdate) { |
| 71 |
Tests::spawnKitty(); |
| 72 |
Tests::spawnKitty(); |
| 73 |
Tests::spawnKitty(); |
| 74 |
|
| 75 |
// move the layout |
| 76 |
OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '1000x0', scale = '1' })")); |
| 77 |
|
| 78 |
// shouldnt crash |
| 79 |
OK(getFromSocket("/dispatch hl.dsp.focus({ direction = 'right' })")); |
| 80 |
|
| 81 |
// restore HEADLESS-2 position so subsequent tests don't inherit the relocated monitor |
| 82 |
OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '0x0', scale = '1' })")); |
| 83 |
} |
| 84 |
|
| 85 |
// Don't crash when a pending layout refresh runs after monitor teardown. |
| 86 |
TEST_CASE(layoutRefreshDuringMonitorTeardown) { |
| 87 |
static constexpr const char* TEST_OUTPUT = "HEADLESS-5"; |
| 88 |
|
| 89 |
getFromSocket(std::format("/output remove {}", TEST_OUTPUT)); |
| 90 |
OK(getFromSocket(std::format("/output create headless {}", TEST_OUTPUT))); |
| 91 |
ASSERT(waitForMonitor(TEST_OUTPUT), true); |
| 92 |
|
| 93 |
CScopeGuard guard = {[&]() { |
| 94 |
Tests::killAllWindows(); |
| 95 |
OK(getFromSocket(std::format("/eval hl.monitor({{ output = '{}', disabled = false, mode = '1920x1080@60', position = '1920x0', scale = '1' }})", TEST_OUTPUT))); |
| 96 |
ASSERT(waitForMonitor(TEST_OUTPUT), true); |
| 97 |
OK(getFromSocket(std::format("/output remove {}", TEST_OUTPUT))); |
| 98 |
ASSERT(waitForMonitorRemoved(TEST_OUTPUT), true); |
| 99 |
OK(getFromSocket("/reload")); |
| 100 |
}}; |
| 101 |
|
| 102 |
OK(getFromSocket("/eval hl.monitor({ output = 'HEADLESS-2', mode = '1920x1080@60', position = '0x0', scale = '1' })")); |
| 103 |
OK(getFromSocket(std::format("/eval hl.monitor({{ output = '{}', disabled = false, mode = '1920x1080@60', position = '1920x0', scale = '1' }})", TEST_OUTPUT))); |
| 104 |
ASSERT(waitForMonitor(TEST_OUTPUT), true); |
| 105 |
|
| 106 |
OK(getFromSocket(std::format("/dispatch hl.dsp.focus({{ monitor = '{}' }})", TEST_OUTPUT))); |
| 107 |
OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '30' })")); |
| 108 |
ASSERT(!!Tests::spawnKitty("layout_teardown_a"), true); |
| 109 |
OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '31' })")); |
| 110 |
ASSERT(!!Tests::spawnKitty("layout_teardown_b"), true); |
| 111 |
|
| 112 |
OK(getFromSocket("/dispatch hl.dsp.focus({ monitor = 'HEADLESS-2' })")); |
| 113 |
OK(getFromSocket("/dispatch hl.dsp.focus({ workspace = '1' })")); |
| 114 |
|
| 115 |
OK(getFromSocket(std::format("/eval hl.monitor({{ output = '{}', disabled = true }}); hl.config({{ general = {{ layout = 'master' }} }})", TEST_OUTPUT))); |
| 116 |
|
| 117 |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 118 |
|
| 119 |
ASSERT_CONTAINS(getFromSocket("/version"), "Hyprland"); |
| 120 |
EXPECT_CONTAINS(getFromSocket("/monitors all"), TEST_OUTPUT); |
| 121 |
} |
| 122 |
|
| 123 |
// Test if size + pos is preserved after fs cycle |
| 124 |
TEST_CASE(posPreserve) { |
| 125 |
Tests::spawnKitty(); |
| 126 |
|
| 127 |
OK(getFromSocket("/dispatch hl.dsp.window.float({ action = 'set', window = 'class:kitty' })")); |
| 128 |
OK(getFromSocket("/dispatch hl.dsp.window.resize({ x = 1337, y = 69, window = 'class:kitty' })")); |
| 129 |
OK(getFromSocket("/dispatch hl.dsp.window.move({ x = 420, y = 420, window = 'class:kitty' })")); |
| 130 |
|
| 131 |
{ |
| 132 |
auto str = getFromSocket("/activewindow"); |
| 133 |
EXPECT_CONTAINS(str, "at: 420,420"); |
| 134 |
EXPECT_CONTAINS(str, "size: 1337,69"); |
| 135 |
} |
| 136 |
|
| 137 |
OK(getFromSocket("/dispatch hl.dsp.window.fullscreen()")); |
| 138 |
OK(getFromSocket("/dispatch hl.dsp.window.fullscreen()")); |
| 139 |
|
| 140 |
{ |
| 141 |
auto str = getFromSocket("/activewindow"); |
| 142 |
EXPECT_CONTAINS(str, "size: 1337,69"); |
| 143 |
} |
| 144 |
|
| 145 |
OK(getFromSocket("/dispatch hl.dsp.window.move({ direction = 'right' })")); |
| 146 |
|
| 147 |
{ |
| 148 |
auto str = getFromSocket("/activewindow"); |
| 149 |
EXPECT_CONTAINS(str, "at: 581,420"); |
| 150 |
EXPECT_CONTAINS(str, "size: 1337,69"); |
| 151 |
} |
| 152 |
|
| 153 |
OK(getFromSocket("/dispatch hl.dsp.window.fullscreen()")); |
| 154 |
OK(getFromSocket("/dispatch hl.dsp.window.fullscreen()")); |
| 155 |
|
| 156 |
{ |
| 157 |
auto str = getFromSocket("/activewindow"); |
| 158 |
EXPECT_CONTAINS(str, "at: 581,420"); |
| 159 |
EXPECT_CONTAINS(str, "size: 1337,69"); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
TEST_CASE(focusMRUAfterClose) { |
| 164 |
NLog::log("{}Testing focus after close (MRU order)", Colors::GREEN); |
| 165 |
|
| 166 |
OK(getFromSocket("/reload")); |
| 167 |
OK(getFromSocket("/eval hl.config({ dwindle = { default_split_ratio = 1.25 } })")); |
| 168 |
OK(getFromSocket("/eval hl.config({ input = { focus_on_close = 2 } })")); |
| 169 |
|
| 170 |
ASSERT(!!Tests::spawnKitty("kitty_A"), true); |
| 171 |
ASSERT(!!Tests::spawnKitty("kitty_B"), true); |
| 172 |
ASSERT(!!Tests::spawnKitty("kitty_C"), true); |
| 173 |
|
| 174 |
OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_A' })")); |
| 175 |
OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_B' })")); |
| 176 |
OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_C' })")); |
| 177 |
|
| 178 |
OK(getFromSocket("/dispatch hl.dsp.window.kill()")); |
| 179 |
Tests::waitUntilWindowsN(2); |
| 180 |
|
| 181 |
{ |
| 182 |
auto str = getFromSocket("/activewindow"); |
| 183 |
EXPECT(str.contains("class: kitty_B"), true); |
| 184 |
} |
| 185 |
|
| 186 |
OK(getFromSocket("/dispatch hl.dsp.window.kill()")); |
| 187 |
Tests::waitUntilWindowsN(1); |
| 188 |
|
| 189 |
{ |
| 190 |
auto str = getFromSocket("/activewindow"); |
| 191 |
EXPECT(str.contains("class: kitty_A"), true); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
TEST_CASE(focusPreservedLayoutChange) { |
| 196 |
OK(getFromSocket("r/eval hl.config({ general = { layout = 'master' } })")); |
| 197 |
|
| 198 |
ASSERT(!!Tests::spawnKitty("kitty_A"), true); |
| 199 |
ASSERT(!!Tests::spawnKitty("kitty_B"), true); |
| 200 |
ASSERT(!!Tests::spawnKitty("kitty_C"), true); |
| 201 |
ASSERT(!!Tests::spawnKitty("kitty_D"), true); |
| 202 |
|
| 203 |
OK(getFromSocket("/dispatch hl.dsp.focus({ window = 'class:kitty_C' })")); |
| 204 |
|
| 205 |
OK(getFromSocket("r/eval hl.config({ general = { layout = 'monocle' } })")); |
| 206 |
|
| 207 |
{ |
| 208 |
auto str = getFromSocket("/activewindow"); |
| 209 |
EXPECT(str.contains("class: kitty_C"), true); |
| 210 |
} |
| 211 |
} |