From 94d49db19621e80bd17efaecc3ed5665e8bfe8f0 Mon Sep 17 00:00:00 2001 From: WarmUpTill <19472752+WarmUpTill@users.noreply.github.com> Date: Wed, 8 Apr 2026 13:47:50 +0200 Subject: [PATCH] Add tests for process condition --- tests/CMakeLists.txt | 7 + tests/stubs/platform-funcs.cpp | 101 +++++++++++++++ tests/test-macro-condition-process.cpp | 170 +++++++++++++++++++++++++ 3 files changed, 278 insertions(+) create mode 100644 tests/stubs/platform-funcs.cpp create mode 100644 tests/test-macro-condition-process.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index d633d732..6ddc43ef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -187,6 +187,13 @@ target_sources( ${ADVSS_SOURCE_DIR}/lib/utils/file-selection.cpp ${ADVSS_SOURCE_DIR}/lib/variables/variable-text-edit.cpp) +# --- macro-condition-process --- # + +target_sources( + ${PROJECT_NAME} + PRIVATE test-macro-condition-process.cpp stubs/platform-funcs.cpp + ${ADVSS_SOURCE_DIR}/plugins/base/macro-condition-process.cpp) + # --- Testing --- # enable_testing() diff --git a/tests/stubs/platform-funcs.cpp b/tests/stubs/platform-funcs.cpp new file mode 100644 index 00000000..46d1afcc --- /dev/null +++ b/tests/stubs/platform-funcs.cpp @@ -0,0 +1,101 @@ +#include "platform-funcs.hpp" +#include "selection-helpers.hpp" + +#include +#include +#include +#include + +namespace advss { + +namespace { + +std::string g_foregroundProcessName; +std::string g_foregroundProcessPath; +QStringList g_processList; +QStringList g_processPathsFromName; + +} // namespace + +void SetStubForegroundProcessName(const std::string &name) +{ + g_foregroundProcessName = name; +} + +void SetStubForegroundProcessPath(const std::string &path) +{ + g_foregroundProcessPath = path; +} + +void SetStubProcessList(const QStringList &list) +{ + g_processList = list; +} + +void SetStubProcessPaths(const QStringList &paths) +{ + g_processPathsFromName = paths; +} + +// --- platform-funcs.hpp stubs --- + +std::vector GetWindowList() +{ + return {}; +} + +std::string GetCurrentWindowTitle() +{ + return {}; +} + +bool IsFullscreen(const std::string &) +{ + return false; +} + +bool IsMaximized(const std::string &) +{ + return false; +} + +std::optional GetTextInWindow(const std::string &) +{ + return {}; +} + +int SecondsSinceLastInput() +{ + return 0; +} + +QStringList GetProcessList() +{ + return g_processList; +} + +std::string GetForegroundProcessName() +{ + return g_foregroundProcessName; +} + +std::string GetForegroundProcessPath() +{ + return g_foregroundProcessPath; +} + +QStringList GetProcessPathsFromName(const QString &) +{ + return g_processPathsFromName; +} + +bool IsInFocus(const QString &) +{ + return false; +} + +// --- selection-helpers.hpp stubs --- + +void PopulateProcessSelection(QComboBox *, bool) {} + +} // namespace advss diff --git a/tests/test-macro-condition-process.cpp b/tests/test-macro-condition-process.cpp new file mode 100644 index 00000000..4b6a1ec9 --- /dev/null +++ b/tests/test-macro-condition-process.cpp @@ -0,0 +1,170 @@ +#include "catch.hpp" +#include "macro-condition-process.hpp" + +namespace advss { +void SetStubForegroundProcessName(const std::string &); +void SetStubForegroundProcessPath(const std::string &); +void SetStubProcessList(const QStringList &); +void SetStubProcessPaths(const QStringList &); +} // namespace advss + +using advss::MacroConditionProcess; + +// --------------------------------------------------------------------------- +// Name matching — no focus, no path +// --------------------------------------------------------------------------- + +TEST_CASE("no focus: exact name match returns true", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"game.exe", "obs64.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = false; + + REQUIRE(cond.CheckCondition()); +} + +TEST_CASE("no focus: exact name mismatch returns false", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"obs64.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = false; + + REQUIRE_FALSE(cond.CheckCondition()); +} + +TEST_CASE("no focus: regex name match returns true", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"game.exe", "obs64.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.*"; + cond._checkFocus = false; + cond._regex.SetEnabled(true); + + REQUIRE(cond.CheckCondition()); +} + +TEST_CASE("no focus: regex name mismatch returns false", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"obs64.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.*"; + cond._checkFocus = false; + cond._regex.SetEnabled(true); + + REQUIRE_FALSE(cond.CheckCondition()); +} + +// --------------------------------------------------------------------------- +// Focus — name only +// --------------------------------------------------------------------------- + +TEST_CASE("focus: foreground name matches returns true", + "[macro-condition-process]") +{ + advss::SetStubForegroundProcessName("game.exe"); + advss::SetStubForegroundProcessPath("C:/Games/Game/game.exe"); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = true; + + REQUIRE(cond.CheckCondition()); +} + +TEST_CASE("focus: foreground name does not match returns false", + "[macro-condition-process]") +{ + advss::SetStubForegroundProcessName("obs64.exe"); + advss::SetStubForegroundProcessPath("C:/OBS/obs64.exe"); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = true; + + REQUIRE_FALSE(cond.CheckCondition()); +} + +// --------------------------------------------------------------------------- +// Path matching — no focus +// --------------------------------------------------------------------------- + +TEST_CASE("no focus with path: name and path both match returns true", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"game.exe"}); + advss::SetStubProcessPaths({"C:/Steam/steamapps/common/Game/game.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = false; + cond._checkPath = true; + cond._processPath = "C:/Steam/steamapps/common/Game/game.exe"; + + REQUIRE(cond.CheckCondition()); +} + +TEST_CASE("no focus with path: name matches but path does not returns false", + "[macro-condition-process]") +{ + advss::SetStubProcessList({"game.exe"}); + advss::SetStubProcessPaths({"C:/Epic/Games/Game/game.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = false; + cond._checkPath = true; + cond._processPath = "Steam"; + + REQUIRE_FALSE(cond.CheckCondition()); +} + +// --------------------------------------------------------------------------- +// Focus + path +// +// When both focus and path are checked, both must be satisfied by the *same* +// foreground process instance. A background process that matches the path +// but is not in focus must not cause a false positive. +// --------------------------------------------------------------------------- + +TEST_CASE("focus with path: foreground matches both name and path returns true", + "[macro-condition-process]") +{ + advss::SetStubForegroundProcessName("game.exe"); + advss::SetStubForegroundProcessPath( + "C:/Steam/steamapps/common/Game/game.exe"); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = true; + cond._checkPath = true; + cond._processPath = "C:/Steam/steamapps/common/Game/game.exe"; + + REQUIRE(cond.CheckCondition()); +} + +TEST_CASE( + "focus with path: foreground name matches but path does not returns false", + "[macro-condition-process]") +{ + advss::SetStubForegroundProcessName("game.exe"); + advss::SetStubForegroundProcessPath("C:/Epic/Games/Game/game.exe"); + advss::SetStubProcessPaths({"C:/Steam/steamapps/common/Game/game.exe"}); + + MacroConditionProcess cond(nullptr); + cond._process = "game.exe"; + cond._checkFocus = true; + cond._checkPath = true; + cond._processPath = "C:/Steam/steamapps/common/Game/game.exe"; + + REQUIRE_FALSE(cond.CheckCondition()); +}