| 1 |
#include "../../hyprctlCompat.hpp" |
| 2 |
#include "../../shared.hpp" |
| 3 |
#include "../shared.hpp" |
| 4 |
#include "tests.hpp" |
| 5 |
|
| 6 |
#include <chrono> |
| 7 |
#include <format> |
| 8 |
#include <optional> |
| 9 |
#include <string> |
| 10 |
#include <thread> |
| 11 |
|
| 12 |
#include <hyprutils/utils/ScopeGuard.hpp> |
| 13 |
|
| 14 |
using namespace Hyprutils::Utils; |
| 15 |
|
| 16 |
static constexpr const char* TEST_MIRROR_SOURCE = "HYPRTEST-MIRROR-SOURCE"; |
| 17 |
static constexpr const char* TEST_MIRROR_TARGET = "HYPRTEST-MIRROR-TARGET"; |
| 18 |
|
| 19 |
static std::string monitorBlock(const std::string& response, const std::string& name) { |
| 20 |
const auto HEADER = std::format("Monitor {} (ID ", name); |
| 21 |
const auto BEGIN = response.find(HEADER); |
| 22 |
|
| 23 |
if (BEGIN == std::string::npos) |
| 24 |
return ""; |
| 25 |
|
| 26 |
const auto END = response.find("\n\n", BEGIN); |
| 27 |
return response.substr(BEGIN, END == std::string::npos ? std::string::npos : END - BEGIN); |
| 28 |
} |
| 29 |
|
| 30 |
static std::string monitorBlock(const std::string& name, bool all = true) { |
| 31 |
return monitorBlock(getFromSocket(all ? "/monitors all" : "/monitors"), name); |
| 32 |
} |
| 33 |
|
| 34 |
static std::optional<std::string> monitorID(const std::string& name) { |
| 35 |
const auto BLOCK = monitorBlock(name); |
| 36 |
const auto BEGIN = BLOCK.find("(ID "); |
| 37 |
|
| 38 |
if (BEGIN == std::string::npos) |
| 39 |
return std::nullopt; |
| 40 |
|
| 41 |
const auto END = BLOCK.find("):", BEGIN); |
| 42 |
if (END == std::string::npos) |
| 43 |
return std::nullopt; |
| 44 |
|
| 45 |
return BLOCK.substr(BEGIN + 4, END - BEGIN - 4); |
| 46 |
} |
| 47 |
|
| 48 |
static bool waitForMonitor(const std::string& name, bool present, bool all = true) { |
| 49 |
for (int i = 0; i < 50; ++i) { |
| 50 |
const bool IS_PRESENT = !monitorBlock(name, all).empty(); |
| 51 |
if (IS_PRESENT == present) |
| 52 |
return true; |
| 53 |
|
| 54 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 55 |
} |
| 56 |
|
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
static bool waitForMirror(const std::string& source, const std::string& targetID) { |
| 61 |
const auto MIRROR = std::format("mirrorOf: {}", targetID); |
| 62 |
|
| 63 |
for (int i = 0; i < 50; ++i) { |
| 64 |
if (monitorBlock(source).contains(MIRROR)) |
| 65 |
return true; |
| 66 |
|
| 67 |
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 68 |
} |
| 69 |
|
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
static void removeMirrorTestOutputs() { |
| 74 |
if (!monitorBlock(TEST_MIRROR_TARGET, false).empty()) { |
| 75 |
getFromSocket(std::format("/output remove {}", TEST_MIRROR_TARGET)); |
| 76 |
waitForMonitor(TEST_MIRROR_TARGET, false); |
| 77 |
waitForMonitor(TEST_MIRROR_SOURCE, true, false); |
| 78 |
} |
| 79 |
|
| 80 |
if (!monitorBlock(TEST_MIRROR_SOURCE, false).empty()) { |
| 81 |
getFromSocket(std::format("/output remove {}", TEST_MIRROR_SOURCE)); |
| 82 |
waitForMonitor(TEST_MIRROR_SOURCE, false); |
| 83 |
} |
| 84 |
|
| 85 |
getFromSocket("/reload"); |
| 86 |
} |
| 87 |
|
| 88 |
TEST_CASE(monitorMirrorAppliedWhenTargetAppears) { |
| 89 |
removeMirrorTestOutputs(); |
| 90 |
CScopeGuard guard = {[]() { removeMirrorTestOutputs(); }}; |
| 91 |
|
| 92 |
OK(getFromSocket(std::format("/eval hl.monitor({{ output = '{}', mode = '1920x1080@60', position = 'auto-right', scale = '1', mirror = '{}' }}); " |
| 93 |
"hl.monitor({{ output = '{}', mode = '1920x1080@60', position = 'auto-right', scale = '1' }})", |
| 94 |
TEST_MIRROR_SOURCE, TEST_MIRROR_TARGET, TEST_MIRROR_TARGET))); |
| 95 |
|
| 96 |
OK(getFromSocket(std::format("/output create headless {}", TEST_MIRROR_SOURCE))); |
| 97 |
ASSERT(waitForMonitor(TEST_MIRROR_SOURCE, true), true); |
| 98 |
EXPECT_CONTAINS(monitorBlock(TEST_MIRROR_SOURCE), "mirrorOf: none"); |
| 99 |
EXPECT(monitorBlock(TEST_MIRROR_SOURCE, false).empty(), false); |
| 100 |
|
| 101 |
OK(getFromSocket(std::format("/output create headless {}", TEST_MIRROR_TARGET))); |
| 102 |
ASSERT(waitForMonitor(TEST_MIRROR_TARGET, true), true); |
| 103 |
|
| 104 |
const auto TARGET_ID = monitorID(TEST_MIRROR_TARGET); |
| 105 |
ASSERT(TARGET_ID.has_value(), true); |
| 106 |
ASSERT(waitForMirror(TEST_MIRROR_SOURCE, *TARGET_ID), true); |
| 107 |
EXPECT(monitorBlock(TEST_MIRROR_SOURCE, false).empty(), true); |
| 108 |
EXPECT(monitorBlock(TEST_MIRROR_TARGET, false).empty(), false); |
| 109 |
ASSERT_CONTAINS(getFromSocket("/version"), "Hyprland"); |
| 110 |
|
| 111 |
OK(getFromSocket(std::format("/output remove {}", TEST_MIRROR_TARGET))); |
| 112 |
ASSERT(waitForMonitor(TEST_MIRROR_TARGET, false), true); |
| 113 |
ASSERT(waitForMonitor(TEST_MIRROR_SOURCE, true, false), true); |
| 114 |
EXPECT_CONTAINS(monitorBlock(TEST_MIRROR_SOURCE), "mirrorOf: none"); |
| 115 |
|
| 116 |
OK(getFromSocket(std::format("/output create headless {}", TEST_MIRROR_TARGET))); |
| 117 |
ASSERT(waitForMonitor(TEST_MIRROR_TARGET, true), true); |
| 118 |
|
| 119 |
const auto RECREATED_TARGET_ID = monitorID(TEST_MIRROR_TARGET); |
| 120 |
ASSERT(RECREATED_TARGET_ID.has_value(), true); |
| 121 |
ASSERT(waitForMirror(TEST_MIRROR_SOURCE, *RECREATED_TARGET_ID), true); |
| 122 |
EXPECT(monitorBlock(TEST_MIRROR_SOURCE, false).empty(), true); |
| 123 |
} |