test: add screenshots (#86)

Co-authored-by: Thomas Van Laere <4990509+thomvanl@users.noreply.github.com>
This commit is contained in:
David LaneandThomas Van Laere authored and GitHub committed 2026-03-22 22:22:24 -04:00
1 parent 7ce2adc9b7
commit 7936cb35ae
13 files changed
+1395 -107

No files matched your search

+9
View File
@@ -18,9 +18,17 @@ if (WIN32)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) # cmake-lint: disable=C0103
endif ()
# extra libraries for tests
if (APPLE)
set(TEST_LIBS "-framework Cocoa")
elseif (WIN32)
set(TEST_LIBS gdi32 gdiplus)
endif()
file(GLOB_RECURSE TEST_SOURCES
${CMAKE_SOURCE_DIR}/tests/conftest.cpp
${CMAKE_SOURCE_DIR}/tests/utils.cpp
${CMAKE_SOURCE_DIR}/tests/screenshot_utils.cpp
${CMAKE_SOURCE_DIR}/tests/test_*.cpp)
add_executable(${PROJECT_NAME}
@@ -29,6 +37,7 @@ add_executable(${PROJECT_NAME}
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
target_link_directories(${PROJECT_NAME} PRIVATE ${TRAY_EXTERNAL_DIRECTORIES})
target_link_libraries(${PROJECT_NAME}
${TEST_LIBS}
${TRAY_EXTERNAL_LIBRARIES}
gtest
gtest_main) # if we use this we don't need our own main function
+57 -22
View File
@@ -1,11 +1,13 @@
// standard includes
#include <array>
#include <filesystem>
#include <mutex>
// lib includes
#include <gtest/gtest.h>
// test includes
#include "tests/screenshot_utils.h"
#include "tests/utils.h"
// Undefine the original TEST macro
@@ -31,12 +33,7 @@ protected:
// we can possibly use some internal googletest functions to capture stdout and stderr, but I have not tested this
// https://stackoverflow.com/a/33186201/11214013
BaseTest():
sbuf {nullptr},
pipe_stdout {nullptr},
pipe_stderr {nullptr} {
// intentionally empty
}
BaseTest() = default;
~BaseTest() override = default;
@@ -59,6 +56,8 @@ protected:
testBinaryDir = std::filesystem::current_path();
}
initializeScreenshotsOnce();
sbuf = std::cout.rdbuf(); // save cout buffer (std::cout)
std::cout.rdbuf(cout_buffer.rdbuf()); // redirect cout to buffer (std::cout)
}
@@ -99,9 +98,22 @@ protected:
std::stringstream cout_buffer; // declare cout_buffer
std::stringstream stdout_buffer; // declare stdout_buffer
std::stringstream stderr_buffer; // declare stderr_buffer
std::streambuf *sbuf;
FILE *pipe_stdout;
FILE *pipe_stderr;
std::streambuf *sbuf {nullptr};
FILE *pipe_stdout {nullptr};
FILE *pipe_stderr {nullptr};
bool screenshotsReady {false};
void initializeScreenshotsOnce() {
static std::once_flag screenshotInitFlag;
std::call_once(screenshotInitFlag, [this]() {
auto root = testBinaryDir;
if (!root.empty()) {
std::error_code ec;
std::filesystem::remove_all(root / "screenshots", ec);
}
screenshot::initialize(root);
});
}
int exec(const char *cmd) {
std::array<char, 128> buffer {};
@@ -124,6 +136,39 @@ protected:
}
return returnCode;
}
bool ensureScreenshotReady() {
if (screenshotsReady) {
return true;
}
if (std::string reason; !screenshot::is_available(&reason)) {
screenshotUnavailableReason = reason;
return false;
}
if (const auto root = screenshot::output_root(); root.empty()) {
screenshotUnavailableReason = "Screenshot output directory not initialized";
return false;
}
screenshotsReady = true;
return true;
}
bool captureScreenshot(const std::string &name) {
if (!screenshotsReady) {
return false;
}
bool ok = screenshot::capture(name);
if (!ok) {
std::cout << "Failed to capture screenshot: " << name << std::endl;
}
return ok;
}
std::filesystem::path screenshotsRoot() const {
return screenshot::output_root();
}
std::string screenshotUnavailableReason;
};
class LinuxTest: public BaseTest {
@@ -132,10 +177,7 @@ protected:
#ifndef __linux__
GTEST_SKIP_("Skipping, this test is for Linux only.");
#endif
}
void TearDown() override {
BaseTest::TearDown();
BaseTest::SetUp();
}
};
@@ -145,23 +187,16 @@ protected:
#if !defined(__APPLE__) || !defined(__MACH__)
GTEST_SKIP_("Skipping, this test is for macOS only.");
#endif
}
void TearDown() override {
BaseTest::TearDown();
BaseTest::SetUp();
}
};
class WindowsTest: public BaseTest {
protected:
void SetUp() override {
void SetUp() override { // NOSONAR(cpp:S1185) - contains platform skip logic, not a trivial override
#ifndef _WIN32
GTEST_SKIP_("Skipping, this test is for Windows only.");
#endif
BaseTest::SetUp();
}
void TearDown() override {
BaseTest::TearDown();
}
};
+250
View File
@@ -0,0 +1,250 @@
// test includes
#include "screenshot_utils.h"
// standard includes
#include <algorithm>
#include <chrono>
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
// clang-format off
// build fails if PropIdl.h and gdiplus.h are included before Windows.h
#include <PropIdl.h>
#include <gdiplus.h>
// clang-format on
#endif
namespace {
#if defined(__linux__) || defined(__APPLE__)
std::string quote_shell_path(const std::filesystem::path &path) {
const std::string input = path.string();
std::string output;
output.reserve(input.size() + 2);
output.push_back('"');
for (const char ch : input) {
if (ch == '"') {
output.append(R"(\")");
} else {
output.push_back(ch);
}
}
output.push_back('"');
return output;
}
#endif
#ifdef _WIN32
bool ensure_gdiplus() {
static std::once_flag gdiplusInitFlag;
static bool gdiplusReady = false;
static ULONG_PTR gdiplusToken = 0;
std::call_once(gdiplusInitFlag, []() {
Gdiplus::GdiplusStartupInput input;
gdiplusReady = Gdiplus::GdiplusStartup(&gdiplusToken, &input, nullptr) == Gdiplus::Ok;
});
return gdiplusReady;
}
bool ensure_dpi_awareness() {
static std::once_flag dpiFlag;
static bool dpiAware = false;
std::call_once(dpiFlag, []() {
using SetProcessDPIAwareFn = BOOL(WINAPI *)();
auto *fn = reinterpret_cast<SetProcessDPIAwareFn>( // NOSONAR(cpp:S3630) - required for GetProcAddress function pointer cast
GetProcAddress(GetModuleHandleA("user32.dll"), "SetProcessDPIAware")
);
dpiAware = fn == nullptr || fn() == TRUE;
});
return dpiAware;
}
bool png_encoder_clsid(CLSID *clsid) {
UINT num = 0;
UINT size = 0;
if (Gdiplus::GetImageEncodersSize(&num, &size) != Gdiplus::Ok || size == 0) {
return false;
}
std::vector<BYTE> buffer(size);
auto *info = static_cast<Gdiplus::ImageCodecInfo *>(static_cast<void *>(buffer.data()));
if (Gdiplus::GetImageEncoders(num, size, info) != Gdiplus::Ok) {
return false;
}
for (UINT i = 0; i < num; ++i) {
if (wcscmp(info[i].MimeType, L"image/png") == 0) {
*clsid = info[i].Clsid;
return true;
}
}
return false;
}
#endif
} // namespace
namespace screenshot {
inline std::filesystem::path &output_root_ref() {
static std::filesystem::path g_outputRoot; // NOSONAR(cpp:S6018) - function-local static is intentional for lazy, TU-local initialization
return g_outputRoot;
}
void initialize(const std::filesystem::path &rootDir) {
output_root_ref() = rootDir / "screenshots";
std::error_code ec;
std::filesystem::create_directories(output_root_ref(), ec);
}
std::filesystem::path output_root() {
return output_root_ref();
}
#ifdef __APPLE__
static bool capture_macos(const std::filesystem::path &file, const Options &) {
std::string cmd = "screencapture -x " + quote_shell_path(file);
return std::system(cmd.c_str()) == 0;
}
#endif
#ifdef __linux__
static bool capture_linux(const std::filesystem::path &file, const Options &) {
std::string target = quote_shell_path(file);
if (std::system("which import > /dev/null 2>&1") == 0) {
std::string cmd = "import -window root " + target;
if (std::system(cmd.c_str()) == 0) {
return true;
}
}
std::string cmd = "gnome-screenshot -f " + target;
return std::system(cmd.c_str()) == 0;
}
#endif
#ifdef _WIN32
static bool capture_windows(const std::filesystem::path &file, const Options &) {
if (!ensure_dpi_awareness()) {
std::cerr << "Failed to enable DPI awareness" << std::endl;
return false;
}
if (!ensure_gdiplus()) {
std::cerr << "GDI+ initialization failed" << std::endl;
return false;
}
int left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
if (HWND desktop = GetDesktopWindow(); (width <= 0 || height <= 0) && desktop != nullptr) {
RECT rect {};
if (GetWindowRect(desktop, &rect)) {
left = rect.left;
top = rect.top;
width = rect.right - rect.left;
height = rect.bottom - rect.top;
}
}
if (width <= 0 || height <= 0) {
std::cerr << "Desktop dimensions invalid" << std::endl;
return false;
}
HDC hdcScreen = GetDC(nullptr);
if (hdcScreen == nullptr) {
std::cerr << "GetDC(nullptr) failed" << std::endl;
return false;
}
HDC hdcMem = CreateCompatibleDC(hdcScreen);
if (hdcMem == nullptr) {
std::cerr << "CreateCompatibleDC failed" << std::endl;
ReleaseDC(nullptr, hdcScreen);
return false;
}
HBITMAP hbm = CreateCompatibleBitmap(hdcScreen, width, height);
if (hbm == nullptr) {
std::cerr << "CreateCompatibleBitmap failed" << std::endl;
DeleteDC(hdcMem);
ReleaseDC(nullptr, hdcScreen);
return false;
}
HGDIOBJ old = SelectObject(hdcMem, hbm);
BOOL ok = BitBlt(hdcMem, 0, 0, width, height, hdcScreen, left, top, SRCCOPY | CAPTUREBLT);
SelectObject(hdcMem, old);
DeleteDC(hdcMem);
ReleaseDC(nullptr, hdcScreen);
if (!ok) {
std::cerr << "BitBlt failed with error " << GetLastError() << std::endl;
DeleteObject(hbm);
return false;
}
Gdiplus::Bitmap bitmap(hbm, nullptr);
DeleteObject(hbm);
CLSID pngClsid;
if (!png_encoder_clsid(&pngClsid)) {
std::cerr << "PNG encoder CLSID not found" << std::endl;
return false;
}
if (bitmap.Save(file.wstring().c_str(), &pngClsid, nullptr) != Gdiplus::Ok) {
std::cerr << "GDI+ failed to write " << file << std::endl;
return false;
}
return true;
}
#endif
bool is_available(std::string *reason) {
#ifdef __APPLE__
return true;
#elif defined(__linux__)
if (std::system("which import > /dev/null 2>&1") == 0 || std::system("which gnome-screenshot > /dev/null 2>&1") == 0) {
return true;
}
if (reason) {
*reason = "Neither ImageMagick 'import' nor gnome-screenshot found";
}
return false;
#elif defined(_WIN32)
if (ensure_gdiplus()) {
return true;
}
if (reason) {
*reason = "Failed to initialize GDI+";
}
return false;
#else
if (reason) {
*reason = "Unsupported platform";
}
return false;
#endif
}
bool capture(const std::string &name, const Options &options) {
// Add a delay to allow UI elements to render before capturing
std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (output_root_ref().empty()) {
return false;
}
auto file = output_root_ref() / (name + ".png");
#ifdef __APPLE__
return capture_macos(file, options);
#elif defined(__linux__)
return capture_linux(file, options);
#elif defined(_WIN32)
return capture_windows(file, options);
#else
return false;
#endif
}
} // namespace screenshot
+19
View File
@@ -0,0 +1,19 @@
#pragma once
// standard includes
#include <filesystem>
#include <optional>
#include <string>
namespace screenshot {
struct Options {
std::optional<std::string> region; // reserved for future ROI support
};
void initialize(const std::filesystem::path &rootDir);
bool is_available(std::string *reason = nullptr);
bool capture(const std::string &name, const Options &options = {});
std::filesystem::path output_root();
} // namespace screenshot
+507 -67
View File
@@ -1,122 +1,251 @@
// test includes
#include "tests/conftest.cpp"
// standard includes
#include <array>
#include <chrono>
#include <cstring>
#include <thread>
#include <vector>
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
// clang-format off
// build fails if shellapi.h is included before Windows.h
#include <shellapi.h>
// clang-format on
#define TRAY_WINAPI 1
#elif defined(__linux__) || defined(linux) || defined(__linux)
#define TRAY_APPINDICATOR 1
#elif defined(__APPLE__) || defined(__MACH__)
#include <Carbon/Carbon.h>
#define TRAY_APPKIT 1
#endif
// local includes
#include "src/tray.h"
#include "tests/screenshot_utils.h"
#if TRAY_APPINDICATOR
#define TRAY_ICON1 "mail-message-new"
#define TRAY_ICON2 "mail-message-new"
constexpr const char *TRAY_ICON1 = "mail-message-new";
constexpr const char *TRAY_ICON2 = "mail-message-new";
#elif TRAY_APPKIT
#define TRAY_ICON1 "icon.png"
#define TRAY_ICON2 "icon.png"
constexpr const char *TRAY_ICON1 = "icon.png";
constexpr const char *TRAY_ICON2 = "icon.png";
#elif TRAY_WINAPI
#define TRAY_ICON1 "icon.ico"
#define TRAY_ICON2 "icon.ico"
constexpr const char *TRAY_ICON1 = "icon.ico";
constexpr const char *TRAY_ICON2 = "icon.ico";
#endif
class TrayTest: public BaseTest {
protected:
static struct tray testTray;
// File-scope tray data shared across all TrayTest instances
namespace {
struct tray_menu g_submenu7_8[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment
{.text = "7", .cb = nullptr},
{.text = "-"},
{.text = "8", .cb = nullptr},
{.text = nullptr}
};
struct tray_menu g_submenu5_6[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment
{.text = "5", .cb = nullptr},
{.text = "6", .cb = nullptr},
{.text = nullptr}
};
struct tray_menu g_submenu_second[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment
{.text = "THIRD", .submenu = g_submenu7_8},
{.text = "FOUR", .submenu = g_submenu5_6},
{.text = nullptr}
};
struct tray_menu g_submenu[] = { // NOSONAR(cpp:S5945, cpp:S5421) - C-style array with null sentinel required by tray C API; mutable for runtime callback assignment
{.text = "Hello", .cb = nullptr},
{.text = "Checked", .checked = 1, .checkbox = 1, .cb = nullptr},
{.text = "Disabled", .disabled = 1},
{.text = "-"},
{.text = "SubMenu", .submenu = g_submenu_second},
{.text = "-"},
{.text = "Quit", .cb = nullptr},
{.text = nullptr}
};
struct tray g_testTray = { // NOSONAR(cpp:S5421) - mutable global required for shared tray state across TEST_F instances
.icon = TRAY_ICON1,
.tooltip = "TestTray",
.menu = g_submenu
};
} // namespace
// Static arrays for submenus
static struct tray_menu submenu7_8[];
static struct tray_menu submenu5_6[];
static struct tray_menu submenu_second[];
static struct tray_menu submenu[];
// Non-static member functions
static void hello_cb(struct tray_menu *item) {
// Mock implementation
class TrayTest: public BaseTest { // NOSONAR(cpp:S3656) - fixture members must be protected for TEST_F-generated subclasses
protected: // NOSONAR(cpp:S3656) - TEST_F generates subclasses that need access to fixture state/methods
void ShutdownTray() {
if (!trayRunning) {
return;
}
tray_exit();
tray_loop(0);
trayRunning = false;
}
static void toggle_cb(struct tray_menu *item) {
item->checked = !item->checked;
tray_update(&testTray);
}
static void quit_cb(struct tray_menu *item) {
// Dismisses the open menu and exits the tray event loop from a background thread.
void closeMenuAndExit() {
#if defined(TRAY_WINAPI)
PostMessage(tray_get_hwnd(), WM_CANCELMODE, 0, 0);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
#elif defined(TRAY_APPKIT)
CGEventRef event = CGEventCreateKeyboardEvent(NULL, kVK_Escape, true);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
CGEventRef event2 = CGEventCreateKeyboardEvent(NULL, kVK_Escape, false);
CGEventPost(kCGHIDEventTap, event2);
CFRelease(event2);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
#endif
tray_exit();
}
static void submenu_cb(struct tray_menu *item) {
// Capture a screenshot while the tray menu is open, then dismiss and exit.
void captureMenuStateAndExit(const char *screenshotName) {
std::thread capture_thread([this, screenshotName]() { // NOSONAR(cpp:S6168) - std::jthread is unavailable on AppleClang 17/libc++ used in CI
EXPECT_TRUE(captureScreenshot(screenshotName));
closeMenuAndExit();
});
tray_show_menu();
while (tray_loop(0) == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
capture_thread.join();
}
bool trayRunning {false}; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
struct tray &testTray = g_testTray; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
struct tray_menu *submenu = g_submenu; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
struct tray_menu *submenu7_8 = g_submenu7_8; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
struct tray_menu *submenu5_6 = g_submenu5_6; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
struct tray_menu *submenu_second = g_submenu_second; // NOSONAR(cpp:S3656) - protected access required by gtest TEST_F subclass pattern
static void hello_cb([[maybe_unused]] struct tray_menu *item) {
// Mock implementation
tray_update(&testTray);
}
static void toggle_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
g_testTray.menu[1].checked = !g_testTray.menu[1].checked;
tray_update(&g_testTray);
}
static void quit_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
tray_exit();
}
static void submenu_cb([[maybe_unused]] struct tray_menu *item) { // NOSONAR(cpp:S1172) - unused param required by tray_menu.cb function pointer type
// Mock implementation
tray_update(&g_testTray);
}
void SetUp() override {
BaseTest::SetUp();
// Wire up callbacks (file-scope arrays can't use addresses of class statics at init time)
g_submenu[0].cb = hello_cb;
g_submenu[1].cb = toggle_cb;
g_submenu[6].cb = quit_cb;
g_submenu7_8[0].cb = submenu_cb;
g_submenu7_8[2].cb = submenu_cb;
g_submenu5_6[0].cb = submenu_cb;
g_submenu5_6[1].cb = submenu_cb;
// Skip tests if screenshot tooling is not available
if (!ensureScreenshotReady()) {
GTEST_SKIP() << "Screenshot tooling missing: " << screenshotUnavailableReason;
}
if (screenshot::output_root().empty()) {
GTEST_SKIP() << "Screenshot output path not initialized";
}
#if defined(TRAY_WINAPI) || defined(TRAY_APPKIT)
// Ensure icon files exist in test binary directory
std::filesystem::path projectRoot = testBinaryDir.parent_path();
std::filesystem::path iconSource;
if (std::filesystem::exists(projectRoot / "icons" / TRAY_ICON1)) {
iconSource = projectRoot / "icons" / TRAY_ICON1;
} else if (std::filesystem::exists(projectRoot / TRAY_ICON1)) {
iconSource = projectRoot / TRAY_ICON1;
} else if (std::filesystem::exists(std::filesystem::path(TRAY_ICON1))) {
iconSource = std::filesystem::path(TRAY_ICON1);
}
if (!iconSource.empty()) {
std::filesystem::path iconDest = testBinaryDir / TRAY_ICON1;
if (!std::filesystem::exists(iconDest)) {
std::error_code ec;
std::filesystem::copy_file(iconSource, iconDest, ec);
if (ec) {
std::cout << "Warning: Failed to copy icon file: " << ec.message() << std::endl;
}
}
}
#endif
trayRunning = false;
testTray.icon = TRAY_ICON1;
testTray.tooltip = "TestTray";
testTray.menu = submenu;
testTray.menu = g_submenu;
g_submenu[1].checked = 1;
}
void TearDown() override {
// Clean up any resources if needed
ShutdownTray();
BaseTest::TearDown();
}
};
// Define the static arrays
struct tray_menu TrayTest::submenu7_8[] = {
{.text = "7", .cb = submenu_cb},
{.text = "-"},
{.text = "8", .cb = submenu_cb},
{.text = nullptr}
};
struct tray_menu TrayTest::submenu5_6[] = {
{.text = "5", .cb = submenu_cb},
{.text = "6", .cb = submenu_cb},
{.text = nullptr}
};
struct tray_menu TrayTest::submenu_second[] = {
{.text = "THIRD", .submenu = submenu7_8},
{.text = "FOUR", .submenu = submenu5_6},
{.text = nullptr}
};
struct tray_menu TrayTest::submenu[] = {
{.text = "Hello", .cb = hello_cb},
{.text = "Checked", .checked = 1, .checkbox = 1, .cb = toggle_cb},
{.text = "Disabled", .disabled = 1},
{.text = "-"},
{.text = "SubMenu", .submenu = submenu_second},
{.text = "-"},
{.text = "Quit", .cb = quit_cb},
{.text = nullptr}
};
struct tray TrayTest::testTray = {
.icon = TRAY_ICON1,
.tooltip = "TestTray",
.menu = submenu
// Process pending events to allow tray icon to appear.
// Call this ONLY before screenshots to ensure the icon is visible.
void WaitForTrayReady() {
#if defined(TRAY_APPINDICATOR)
for (int i = 0; i < 100; i++) {
tray_loop(0);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
#elif defined(TRAY_APPKIT)
static std::thread::id main_thread_id = std::this_thread::get_id();
if (std::this_thread::get_id() == main_thread_id) {
for (int i = 0; i < 100; i++) {
tray_loop(0);
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
#endif
}
};
TEST_F(TrayTest, TestTrayInit) {
int result = tray_init(&testTray);
EXPECT_EQ(result, 0); // make sure return value is 0
trayRunning = (result == 0);
EXPECT_EQ(result, 0);
WaitForTrayReady();
EXPECT_TRUE(captureScreenshot("tray_icon_initial"));
}
TEST_F(TrayTest, TestTrayLoop) {
int result = tray_loop(1);
EXPECT_EQ(result, 0); // make sure return value is 0
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Test non-blocking loop (blocking=0) since blocking would hang without events
int result = tray_loop(0);
EXPECT_EQ(result, 0);
}
TEST_F(TrayTest, TestTrayUpdate) {
// check the initial values
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
EXPECT_EQ(testTray.icon, TRAY_ICON1);
EXPECT_EQ(testTray.tooltip, "TestTray");
// update the values
testTray.icon = TRAY_ICON2;
testTray.tooltip = "TestTray2";
tray_update(&testTray);
EXPECT_EQ(testTray.icon, TRAY_ICON2);
EXPECT_EQ(testTray.tooltip, "TestTray2");
// put back the original values
testTray.icon = TRAY_ICON1;
@@ -127,12 +256,323 @@ TEST_F(TrayTest, TestTrayUpdate) {
}
TEST_F(TrayTest, TestToggleCallback) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
bool initialCheckedState = testTray.menu[1].checked;
toggle_cb(&testTray.menu[1]);
EXPECT_EQ(testTray.menu[1].checked, !initialCheckedState);
}
TEST_F(TrayTest, TestMenuItemCallback) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Test hello callback - it should work without crashing
ASSERT_NE(testTray.menu[0].cb, nullptr);
testTray.menu[0].cb(&testTray.menu[0]);
}
TEST_F(TrayTest, TestDisabledMenuItem) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify disabled menu item
EXPECT_EQ(testTray.menu[2].disabled, 1);
EXPECT_STREQ(testTray.menu[2].text, "Disabled");
}
TEST_F(TrayTest, TestMenuSeparator) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify separator exists
EXPECT_STREQ(testTray.menu[3].text, "-");
EXPECT_EQ(testTray.menu[3].cb, nullptr);
}
TEST_F(TrayTest, TestSubmenuStructure) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify submenu structure
EXPECT_STREQ(testTray.menu[4].text, "SubMenu");
ASSERT_NE(testTray.menu[4].submenu, nullptr);
// Verify nested submenu levels
EXPECT_STREQ(testTray.menu[4].submenu[0].text, "THIRD");
ASSERT_NE(testTray.menu[4].submenu[0].submenu, nullptr);
EXPECT_STREQ(testTray.menu[4].submenu[0].submenu[0].text, "7");
}
TEST_F(TrayTest, TestSubmenuCallback) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Test submenu callback
ASSERT_NE(testTray.menu[4].submenu[0].submenu[0].cb, nullptr);
testTray.menu[4].submenu[0].submenu[0].cb(&testTray.menu[4].submenu[0].submenu[0]);
}
TEST_F(TrayTest, TestNotificationDisplay) {
#if !(defined(_WIN32) || defined(__linux__) || defined(__APPLE__))
GTEST_SKIP() << "Notifications only supported on desktop platforms";
#endif
#if defined(_WIN32)
QUERY_USER_NOTIFICATION_STATE notification_state;
if (HRESULT ns = SHQueryUserNotificationState(&notification_state);
ns != S_OK || notification_state != QUNS_ACCEPTS_NOTIFICATIONS) {
GTEST_SKIP() << "Notifications not accepted in this environment. SHQueryUserNotificationState result: " << ns << ", state: " << notification_state;
}
#endif
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Set notification properties
testTray.notification_title = "Test Notification";
testTray.notification_text = "This is a test notification message";
testTray.notification_icon = TRAY_ICON1;
tray_update(&testTray);
WaitForTrayReady();
EXPECT_TRUE(captureScreenshot("tray_notification_displayed"));
// Clear notification
testTray.notification_title = nullptr;
testTray.notification_text = nullptr;
testTray.notification_icon = nullptr;
tray_update(&testTray);
}
TEST_F(TrayTest, TestNotificationCallback) {
#if !(defined(_WIN32) || defined(__linux__) || defined(__APPLE__))
GTEST_SKIP() << "Notifications only supported on desktop platforms";
#endif
#if defined(_WIN32)
QUERY_USER_NOTIFICATION_STATE notification_state;
if (HRESULT ns = SHQueryUserNotificationState(&notification_state);
ns != S_OK || notification_state != QUNS_ACCEPTS_NOTIFICATIONS) {
GTEST_SKIP() << "Notifications not accepted in this environment. SHQueryUserNotificationState result: " << ns << ", state: " << notification_state;
}
#endif
static bool callbackInvoked = false;
auto notification_callback = []() {
callbackInvoked = true;
};
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Set notification with callback
testTray.notification_title = "Clickable Notification";
testTray.notification_text = "Click this notification to test callback";
testTray.notification_icon = TRAY_ICON1;
testTray.notification_cb = notification_callback;
tray_update(&testTray);
// Note: callback would be invoked by user interaction in real scenario
// In test environment, we verify it's set correctly
EXPECT_NE(testTray.notification_cb, nullptr);
// Clear notification
testTray.notification_title = nullptr;
testTray.notification_text = nullptr;
testTray.notification_icon = nullptr;
testTray.notification_cb = nullptr;
tray_update(&testTray);
}
TEST_F(TrayTest, TestTooltipUpdate) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Test initial tooltip
EXPECT_STREQ(testTray.tooltip, "TestTray");
// Update tooltip
testTray.tooltip = "Updated Tooltip Text";
tray_update(&testTray);
EXPECT_STREQ(testTray.tooltip, "Updated Tooltip Text");
// Restore original tooltip
testTray.tooltip = "TestTray";
tray_update(&testTray);
}
TEST_F(TrayTest, TestMenuItemContext) {
static int contextValue = 42;
static bool contextCallbackInvoked = false;
auto context_callback = [](struct tray_menu *item) { // NOSONAR(cpp:S995) - must match tray_menu.cb signature void(*)(struct tray_menu*)
if (item->context != nullptr) {
const auto *value = static_cast<const int *>(item->context);
contextCallbackInvoked = (*value == 42);
}
};
// Create menu with context
std::array<struct tray_menu, 2> context_menu_arr = {{{.text = "Context Item", .cb = context_callback, .context = &contextValue}, {.text = nullptr}}};
struct tray_menu *context_menu = context_menu_arr.data();
testTray.menu = context_menu;
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify context is set
EXPECT_EQ(testTray.menu[0].context, &contextValue);
// Invoke callback with context
testTray.menu[0].cb(&testTray.menu[0]);
EXPECT_TRUE(contextCallbackInvoked);
// Restore original menu
testTray.menu = submenu;
}
TEST_F(TrayTest, TestCheckboxStates) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
EXPECT_EQ(testTray.menu[1].checkbox, 1);
EXPECT_EQ(testTray.menu[1].checked, 1);
// Show menu open with checkbox in checked state
captureMenuStateAndExit("tray_menu_checkbox_checked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
// Re-initialize tray with checkbox unchecked
trayRunning = false;
testTray.menu[1].checked = 0;
initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Show menu open with checkbox in unchecked state
captureMenuStateAndExit("tray_menu_checkbox_unchecked"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
// Restore initial checked state
testTray.menu[1].checked = 1;
}
TEST_F(TrayTest, TestMultipleIconUpdates) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Update icon multiple times
testTray.icon = TRAY_ICON2;
tray_update(&testTray);
testTray.icon = TRAY_ICON1;
tray_update(&testTray);
}
TEST_F(TrayTest, TestCompleteMenuHierarchy) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify complete menu structure
int menuCount = 0;
for (const struct tray_menu *m = testTray.menu; m->text != nullptr; m++) {
menuCount++;
}
EXPECT_EQ(menuCount, 7); // Hello, Checked, Disabled, Sep, SubMenu, Sep, Quit
// Verify all nested submenus
ASSERT_NE(testTray.menu[4].submenu, nullptr);
ASSERT_NE(testTray.menu[4].submenu[0].submenu, nullptr);
ASSERT_NE(testTray.menu[4].submenu[1].submenu, nullptr);
}
TEST_F(TrayTest, TestIconPathArray) {
#if defined(TRAY_WINAPI)
// Test icon path array caching (Windows-specific feature)
// The tray struct has a flexible array member, so we allocate a raw buffer
// and use memcpy to initialize const fields before the object is used.
const size_t icon_count = 2;
const size_t buf_size = sizeof(struct tray) + icon_count * sizeof(const char *);
std::vector<std::byte> buf(buf_size, std::byte {0});
auto *iconCacheTray = reinterpret_cast<struct tray *>(buf.data()); // NOSONAR(cpp:S3630) - reinterpret_cast required to overlay struct onto raw buffer for flexible array member
iconCacheTray->icon = TRAY_ICON1;
iconCacheTray->tooltip = "Icon Cache Test";
iconCacheTray->notification_icon = nullptr;
iconCacheTray->notification_text = nullptr;
iconCacheTray->notification_title = nullptr;
iconCacheTray->notification_cb = nullptr;
iconCacheTray->menu = submenu;
// Write const fields via memcpy — const_cast is required to initialize const members in a C struct flexible array allocation
auto count_val = static_cast<int>(icon_count);
std::memcpy(const_cast<int *>(&iconCacheTray->iconPathCount), &count_val, sizeof(count_val)); // NOSONAR(cpp:S859) - required to initialize const member in C struct allocated via raw buffer
const char *icon1 = TRAY_ICON1;
const char *icon2 = TRAY_ICON2;
std::memcpy(const_cast<char **>(&iconCacheTray->allIconPaths[0]), &icon1, sizeof(icon1)); // NOSONAR(cpp:S859) - required to initialize const member in C struct allocated via raw buffer
std::memcpy(const_cast<char **>(&iconCacheTray->allIconPaths[1]), &icon2, sizeof(icon2)); // NOSONAR(cpp:S859) - required to initialize const member in C struct allocated via raw buffer
int initResult = tray_init(iconCacheTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify initial icon
EXPECT_EQ(iconCacheTray->icon, TRAY_ICON1);
// Switch to cached icon
iconCacheTray->icon = TRAY_ICON2;
tray_update(iconCacheTray);
// buf goes out of scope, no manual free needed
#else
// On non-Windows platforms, just test basic icon switching
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
EXPECT_EQ(testTray.icon, TRAY_ICON1);
testTray.icon = TRAY_ICON2;
tray_update(&testTray);
#endif
}
TEST_F(TrayTest, TestQuitCallback) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Verify quit callback exists
ASSERT_NE(testTray.menu[6].cb, nullptr);
EXPECT_STREQ(testTray.menu[6].text, "Quit");
// Note: Actually calling quit_cb would terminate the tray,
// which is tested separately in TestTrayExit
}
TEST_F(TrayTest, TestTrayShowMenu) {
int initResult = tray_init(&testTray);
trayRunning = (initResult == 0);
ASSERT_EQ(initResult, 0);
// Screenshot shows the full menu open, including the SubMenu entry that leads to nested items
captureMenuStateAndExit("tray_menu_shown"); // NOSONAR(cpp:S6168) - helper uses std::thread for AppleClang 17 compatibility
}
TEST_F(TrayTest, TestTrayExit) {
tray_exit();
// TODO: Check the state after tray_exit
}