diff --git a/src/notification.c b/src/notification.c index c08fbe2..f4c6abf 100644 --- a/src/notification.c +++ b/src/notification.c @@ -215,6 +215,13 @@ static void notification_private_free(NotificationPrivate *p) g_free(p); } +/* see notification.h */ +gint notification_refcount_get(struct notification *n) +{ + assert(n->priv->refcount > 0); + return g_atomic_int_get(&n->priv->refcount); +} + /* see notification.h */ void notification_ref(struct notification *n) { diff --git a/src/notification.h b/src/notification.h index 0764152..ffc887b 100644 --- a/src/notification.h +++ b/src/notification.h @@ -101,6 +101,11 @@ struct notification { */ struct notification *notification_create(void); +/** + * Retrieve the current reference count of the notification + */ +gint notification_refcount_get(struct notification *n); + /** * Increase the reference counter of the notification. */ diff --git a/test/notification.c b/test/notification.c index 3a29e98..b6afe2f 100644 --- a/test/notification.c +++ b/test/notification.c @@ -98,6 +98,24 @@ TEST test_notification_replace_single_field(void) PASS(); } +TEST test_notification_referencing(void) +{ + struct notification *n = notification_create(); + ASSERT(notification_refcount_get(n) == 1); + + notification_ref(n); + ASSERT(notification_refcount_get(n) == 2); + + notification_unref(n); + ASSERT(notification_refcount_get(n) == 1); + + // Now we have to rely on valgrind to test, that + // it gets actually freed + notification_unref(n); + + PASS(); +} + SUITE(suite_notification) { cmdline_load(0, NULL); @@ -125,6 +143,7 @@ SUITE(suite_notification) notification_unref(b); RUN_TEST(test_notification_replace_single_field); + RUN_TEST(test_notification_referencing); g_clear_pointer(&settings.icon_path, g_free); }