Skip to content

Commit 7a5f111

Browse files
test
1 parent 7dd3e56 commit 7a5f111

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/tray_windows.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,18 @@ void tray_update(struct tray *tray) {
290290
MultiByteToWideChar(CP_UTF8, 0, tray->tooltip, -1, nid.szTip, sizeof(nid.szTip) / sizeof(wchar_t));
291291
nid.uFlags |= NIF_TIP;
292292
}
293-
QUERY_USER_NOTIFICATION_STATE notification_state;
294-
HRESULT ns = SHQueryUserNotificationState(&notification_state);
295-
int can_show_notifications = ns == S_OK && notification_state == QUNS_ACCEPTS_NOTIFICATIONS;
296-
if (can_show_notifications == 1 && tray->notification_title != 0 && strlen(tray->notification_title) > 0) {
293+
if (tray->notification_title != 0 && strlen(tray->notification_title) > 0) {
297294
MultiByteToWideChar(CP_UTF8, 0, tray->notification_title, -1, nid.szInfoTitle, sizeof(nid.szInfoTitle) / sizeof(wchar_t));
298295
nid.uFlags |= NIF_INFO;
299296
} else if ((nid.uFlags & NIF_INFO) == NIF_INFO) {
300297
nid.szInfoTitle[0] = L'\0';
301298
}
302-
if (can_show_notifications == 1 && tray->notification_text != 0 && strlen(tray->notification_text) > 0) {
299+
if (tray->notification_text != 0 && strlen(tray->notification_text) > 0) {
303300
MultiByteToWideChar(CP_UTF8, 0, tray->notification_text, -1, nid.szInfo, sizeof(nid.szInfo) / sizeof(wchar_t));
304301
} else if ((nid.uFlags & NIF_INFO) == NIF_INFO) {
305302
nid.szInfo[0] = L'\0';
306303
}
307-
if (can_show_notifications == 1 && tray->notification_cb != NULL) {
304+
if (tray->notification_cb != NULL) {
308305
notification_cb = tray->notification_cb;
309306
}
310307

@@ -330,6 +327,10 @@ void tray_exit(void) {
330327
UnregisterClass(WC_TRAY_CLASS_NAME, GetModuleHandle(NULL));
331328
}
332329

330+
/**
331+
* @brief Get the window handle of the tray.
332+
* @return Window handle.
333+
*/
333334
HWND tray_get_hwnd(void) {
334335
return hwnd;
335336
}

tests/unit/test_tray.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
#if defined(_WIN32) || defined(_WIN64)
99
#include <windows.h>
10+
// clang-format off
11+
// build fails if shellapi.h is included before windows.h
12+
#include <shellapi.h>
13+
// clang-format on
1014
#define TRAY_WINAPI 1
1115
#elif defined(__linux__) || defined(linux) || defined(__linux)
1216
#define TRAY_APPINDICATOR 1
@@ -278,6 +282,14 @@ TEST_F(TrayTest, TestNotificationDisplay) {
278282
GTEST_SKIP() << "Notifications only supported on desktop platforms";
279283
#endif
280284

285+
#if defined(_WIN32)
286+
QUERY_USER_NOTIFICATION_STATE notification_state;
287+
HRESULT ns = SHQueryUserNotificationState(&notification_state);
288+
if (ns != S_OK || notification_state != QUNS_ACCEPTS_NOTIFICATIONS) {
289+
GTEST_SKIP() << "Notifications not accepted in this environment";
290+
}
291+
#endif
292+
281293
int initResult = tray_init(&testTray);
282294
trayRunning = (initResult == 0);
283295
ASSERT_EQ(initResult, 0);
@@ -290,6 +302,8 @@ TEST_F(TrayTest, TestNotificationDisplay) {
290302
tray_update(&testTray);
291303
tray_loop(1);
292304

305+
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
306+
293307
EXPECT_TRUE(captureScreenshot("tray_notification_displayed"));
294308

295309
// Clear notification
@@ -304,6 +318,14 @@ TEST_F(TrayTest, TestNotificationCallback) {
304318
GTEST_SKIP() << "Notifications only supported on desktop platforms";
305319
#endif
306320

321+
#if defined(_WIN32)
322+
QUERY_USER_NOTIFICATION_STATE notification_state;
323+
HRESULT ns = SHQueryUserNotificationState(&notification_state);
324+
if (ns != S_OK || notification_state != QUNS_ACCEPTS_NOTIFICATIONS) {
325+
GTEST_SKIP() << "Notifications not accepted in this environment";
326+
}
327+
#endif
328+
307329
static bool callbackInvoked = false;
308330
auto notification_callback = []() {
309331
callbackInvoked = true;
@@ -322,6 +344,8 @@ TEST_F(TrayTest, TestNotificationCallback) {
322344
tray_update(&testTray);
323345
tray_loop(1);
324346

347+
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
348+
325349
EXPECT_TRUE(captureScreenshot("tray_notification_with_callback"));
326350

327351
// Note: callback would be invoked by user interaction in real scenario

0 commit comments

Comments
 (0)