Skip to content

Commit

Permalink
Merge pull request #1274 from zappolowski/add-tests-for-notification-…
Browse files Browse the repository at this point in the history
…list

Add tests for NotificationListHistory
  • Loading branch information
zappolowski authored Mar 1, 2024
2 parents de6fd27 + a67db1c commit 660e8ba
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 48 deletions.
63 changes: 17 additions & 46 deletions src/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,8 @@ static void dbus_cb_dunst_NotificationListHistory(GDBusConnection *connection,
{
LOG_D("CMD: Listing all notifications from history");

GVariant *answer = NULL;
GVariantBuilder *builder;

builder = g_variant_builder_new(G_VARIANT_TYPE("aa{sv}"));
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}"));

GList *notification_list = queues_get_history();

Expand All @@ -344,8 +342,7 @@ static void dbus_cb_dunst_NotificationListHistory(GDBusConnection *connection,
n = g_list_nth_data(notification_list, i-1);

GVariantBuilder n_builder;

g_variant_builder_init(&n_builder, g_variant_type_new("a{sv}"));
g_variant_builder_init(&n_builder, G_VARIANT_TYPE("a{sv}"));

char *body, *msg, *summary, *appname, *category;
char *default_action_name, *icon_path;
Expand All @@ -359,49 +356,23 @@ static void dbus_cb_dunst_NotificationListHistory(GDBusConnection *connection,
"" : n->default_action_name;
icon_path = (n->icon_path == NULL) ? "" : n->icon_path;

g_variant_builder_add(&n_builder, "{sv}", "body",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(body, strlen(body)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "message",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(msg, strlen(msg)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "summary",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(summary, strlen(summary)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "appname",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(appname, strlen(appname)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "category",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(category, strlen(category)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "body", g_variant_new_string(body));
g_variant_builder_add(&n_builder, "{sv}", "message", g_variant_new_string(msg));
g_variant_builder_add(&n_builder, "{sv}", "summary", g_variant_new_string(summary));
g_variant_builder_add(&n_builder, "{sv}", "appname", g_variant_new_string(appname));
g_variant_builder_add(&n_builder, "{sv}", "category", g_variant_new_string(category));
g_variant_builder_add(&n_builder, "{sv}", "default_action_name",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(default_action_name,
strlen(default_action_name)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "icon_path",
g_variant_new_from_bytes(G_VARIANT_TYPE("s"),
g_bytes_new(icon_path, strlen(icon_path)+1), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "id",
g_variant_new_from_bytes(G_VARIANT_TYPE("i"),
g_bytes_new(&n->id, sizeof(int)), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "timestamp",
g_variant_new_from_bytes(G_VARIANT_TYPE("x"),
g_bytes_new(&n->timestamp, sizeof(gint64)), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "timeout",
g_variant_new_from_bytes(G_VARIANT_TYPE("x"),
g_bytes_new(&n->timeout, sizeof(gint64)), TRUE));
g_variant_builder_add(&n_builder, "{sv}", "progress",
g_variant_new_from_bytes(G_VARIANT_TYPE("i"),
g_bytes_new(&n->progress, sizeof(int)), TRUE));

g_variant_builder_add(builder, "a{sv}", &n_builder);

g_variant_new_string(default_action_name));
g_variant_builder_add(&n_builder, "{sv}", "icon_path", g_variant_new_string(icon_path));
g_variant_builder_add(&n_builder, "{sv}", "id", g_variant_new_int32(n->id));
g_variant_builder_add(&n_builder, "{sv}", "timestamp", g_variant_new_int64(n->timestamp));
g_variant_builder_add(&n_builder, "{sv}", "timeout", g_variant_new_int64(n->timeout));
g_variant_builder_add(&n_builder, "{sv}", "progress", g_variant_new_int32(n->progress));

g_variant_builder_add(&builder, "a{sv}", &n_builder);
}

answer = g_variant_new("(aa{sv})", builder);

g_clear_pointer(&builder, g_variant_builder_unref);
g_dbus_method_invocation_return_value(invocation, answer);
g_dbus_method_invocation_return_value(invocation, g_variant_new("(aa{sv})", &builder));
g_dbus_connection_flush(connection, NULL, NULL, NULL);
}

Expand Down
77 changes: 75 additions & 2 deletions test/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ void dbus_signal_unsubscribe_closed(struct signal_closed *closed)
closed->subscription_id = -1;
}

GVariant *dbus_invoke(const char *method, GVariant *params)
static GVariant *dbus_invoke_ifac(const char *method, GVariant *params, const char *ifac)
{
GDBusConnection *connection_client;
GVariant *retdata;
Expand All @@ -219,7 +219,7 @@ GVariant *dbus_invoke(const char *method, GVariant *params)
connection_client,
FDN_NAME,
FDN_PATH,
FDN_IFAC,
ifac,
method,
params,
NULL,
Expand All @@ -237,6 +237,11 @@ GVariant *dbus_invoke(const char *method, GVariant *params)
return retdata;
}

GVariant *dbus_invoke(const char *method, GVariant *params)
{
return dbus_invoke_ifac(method, params, FDN_IFAC);
}

struct dbus_notification {
const char* app_name;
guint replaces_id;
Expand Down Expand Up @@ -517,6 +522,73 @@ TEST test_dbus_cb_dunst_Properties_Set_pause_level(void)
PASS();
}

TEST test_dbus_cb_dunst_NotificationListHistory(void)
{
struct notification *n = notification_create();
gint64 timestamp1 = n->timestamp;
n->appname = g_strdup("dunstify");
n->summary = g_strdup("Testing");
queues_history_push(n);

n = notification_create();
gint64 timestamp2 = n->timestamp;
n->appname = g_strdup("notify-send");
n->summary = g_strdup("More testing");
queues_history_push(n);

GVariant *result = dbus_invoke_ifac("NotificationListHistory", NULL, DUNST_IFAC);
ASSERT(result != NULL);
ASSERT_STR_EQ("(aa{sv})", g_variant_get_type_string(result));

GVariantIter tuple_iter;
g_variant_iter_init(&tuple_iter, result);
GVariant *array = g_variant_iter_next_value(&tuple_iter);

GVariantIter array_iter;
g_variant_iter_init(&array_iter, array);
GVariant *dict = g_variant_iter_next_value(&array_iter);

GVariantDict d;
g_variant_dict_init(&d, dict);

char *str;
gint64 int64;

ASSERT(g_variant_dict_lookup(&d, "appname", "s", &str));
ASSERT_STR_EQ("notify-send", str);
g_free(str);

ASSERT(g_variant_dict_lookup(&d, "summary", "s", &str));
ASSERT_STR_EQ("More testing", str);
g_free(str);

ASSERT(g_variant_dict_lookup(&d, "timestamp", "x", &int64));
ASSERT_EQ(timestamp2, int64);

g_variant_unref(dict);
dict = g_variant_iter_next_value(&array_iter);
g_variant_dict_clear(&d);
g_variant_dict_init(&d, dict);

ASSERT(g_variant_dict_lookup(&d, "appname", "s", &str));
ASSERT_STR_EQ("dunstify", str);
g_free(str);

ASSERT(g_variant_dict_lookup(&d, "summary", "s", &str));
ASSERT_STR_EQ("Testing", str);
g_free(str);

ASSERT(g_variant_dict_lookup(&d, "timestamp", "x", &int64));
ASSERT_EQ(timestamp1, int64);

g_variant_dict_clear(&d);
g_variant_unref(dict);
g_variant_unref(array);
g_variant_unref(result);
queues_history_clear();
PASS();
}

TEST test_empty_notification(void)
{
struct dbus_notification *n = dbus_notification_new();
Expand Down Expand Up @@ -1198,6 +1270,7 @@ gpointer run_threaded_tests(gpointer data)
RUN_TEST(test_override_dbus_timeout);
RUN_TEST(test_match_dbus_timeout);
RUN_TEST(test_timeout);
RUN_TEST(test_dbus_cb_dunst_NotificationListHistory);

RUN_TEST(assert_methodlists_sorted);

Expand Down

0 comments on commit 660e8ba

Please sign in to comment.