[glib] Add basic test for the GNotification gtk backend
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] Add basic test for the GNotification gtk backend
- Date: Sat, 23 Nov 2013 20:17:25 +0000 (UTC)
commit 65da592249b3eff7fbb5edb31d94e028cc3c43d0
Author: Lars Uebernickel <lars uebernic de>
Date: Tue Oct 29 00:01:38 2013 -0700
Add basic test for the GNotification gtk backend
https://bugzilla.gnome.org/show_bug.cgi?id=711051
gio/tests/.gitignore | 1 +
gio/tests/Makefile.am | 2 +
gio/tests/gnotification-server.c | 339 ++++++++++++++++++++++++++++++++++++++
gio/tests/gnotification-server.h | 46 +++++
gio/tests/gnotification.c | 163 ++++++++++++++++++
5 files changed, 551 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index c20f02f..5a403d4 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -127,3 +127,4 @@ vfs
volumemonitor
xdgdatadir
xdgdatahome
+gnotification
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 78690ce..49aac1b 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -397,6 +397,7 @@ test_programs += \
gdbus-test-codegen-old \
gdbus-threading \
gmenumodel \
+ gnotification \
$(NULL)
gdbus_proxy_threads_CFLAGS = $(AM_CFLAGS) $(DBUS1_CFLAGS)
@@ -424,6 +425,7 @@ nodist_gdbus_test_codegen_old_SOURCES = gdbus-test-codegen-generated.c gdbus-
gdbus_test_codegen_old_CPPFLAGS = $(AM_CPPFLAGS) -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_36
-DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_36
gdbus_threading_SOURCES = $(gdbus_sessionbus_sources) gdbus-threading.c
gmenumodel_SOURCES = $(gdbus_sessionbus_sources) gmenumodel.c
+gnotification_SOURCES = $(gdbus_sessionbus_sources) gnotification.c
gnotification-server.h gnotification-server.c
gdbus-test-codegen.o: gdbus-test-codegen-generated.h
gdbus-test-codegen-generated.h gdbus-test-codegen-generated.c: test-codegen.xml Makefile
$(top_builddir)/gio/gdbus-2.0/codegen/gdbus-codegen
diff --git a/gio/tests/gnotification-server.c b/gio/tests/gnotification-server.c
new file mode 100644
index 0000000..a028e03
--- /dev/null
+++ b/gio/tests/gnotification-server.c
@@ -0,0 +1,339 @@
+/*
+ * Copyright © 2013 Lars Uebernickel
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Lars Uebernickel <lars uebernic de>
+ */
+
+#include "gnotification-server.h"
+
+#include <gio/gio.h>
+
+typedef GObjectClass GNotificationServerClass;
+
+struct _GNotificationServer
+{
+ GObject parent;
+
+ GDBusConnection *connection;
+ guint name_owner_id;
+ guint object_id;
+
+ guint is_running;
+
+ /* app_ids -> hashtables of notification ids -> a{sv} */
+ GHashTable *applications;
+};
+
+G_DEFINE_TYPE (GNotificationServer, g_notification_server, G_TYPE_OBJECT);
+
+enum
+{
+ PROP_0,
+ PROP_IS_RUNNING
+};
+
+static GDBusInterfaceInfo *
+org_gtk_Notifications_get_interface (void)
+{
+ static GDBusInterfaceInfo *iface_info;
+
+ if (iface_info == NULL)
+ {
+ GDBusNodeInfo *info;
+ GError *error = NULL;
+
+ info = g_dbus_node_info_new_for_xml (
+ "<node>"
+ " <interface name='org.gtk.Notifications'>"
+ " <method name='AddNotification'>"
+ " <arg type='s' direction='in' />"
+ " <arg type='s' direction='in' />"
+ " <arg type='a{sv}' direction='in' />"
+ " </method>"
+ " <method name='RemoveNotification'>"
+ " <arg type='s' direction='in' />"
+ " <arg type='s' direction='in' />"
+ " </method>"
+ " </interface>"
+ "</node>", &error);
+
+ if (info == NULL)
+ g_error ("%s", error->message);
+
+ iface_info = g_dbus_node_info_lookup_interface (info, "org.gtk.Notifications");
+ g_assert (iface_info);
+
+ g_dbus_interface_info_ref (iface_info);
+ g_dbus_node_info_unref (info);
+ }
+
+ return iface_info;
+}
+
+static void
+g_notification_server_notification_added (GNotificationServer *server,
+ const gchar *app_id,
+ const gchar *notification_id,
+ GVariant *notification)
+{
+ GHashTable *notifications;
+
+ notifications = g_hash_table_lookup (server->applications, app_id);
+ if (notifications == NULL)
+ {
+ notifications = g_hash_table_new_full (g_str_hash, g_str_equal,
+ g_free, (GDestroyNotify) g_variant_unref);
+ g_hash_table_insert (server->applications, g_strdup (app_id), notifications);
+ }
+
+ g_hash_table_replace (notifications, g_strdup (notification_id), g_variant_ref (notification));
+
+ g_signal_emit_by_name (server, "notification-received", app_id, notification_id, notification);
+}
+
+static void
+g_notification_server_notification_removed (GNotificationServer *server,
+ const gchar *app_id,
+ const gchar *notification_id)
+{
+ GHashTable *notifications;
+
+ notifications = g_hash_table_lookup (server->applications, app_id);
+ if (notifications)
+ {
+ g_hash_table_remove (notifications, notification_id);
+ if (g_hash_table_size (notifications) == 0)
+ g_hash_table_remove (server->applications, app_id);
+ }
+
+ g_signal_emit_by_name (server, "notification-removed", app_id, notification_id);
+}
+
+static void
+org_gtk_Notifications_method_call (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ GNotificationServer *server = user_data;
+
+ if (g_str_equal (method_name, "AddNotification"))
+ {
+ const gchar *app_id;
+ const gchar *notification_id;
+ GVariant *notification;
+
+ g_variant_get (parameters, "(&s&s a{sv})", &app_id, ¬ification_id, ¬ification);
+ g_notification_server_notification_added (server, app_id, notification_id, notification);
+ g_dbus_method_invocation_return_value (invocation, NULL);
+
+ g_variant_unref (notification);
+ }
+ else if (g_str_equal (method_name, "RemoveNotification"))
+ {
+ const gchar *app_id;
+ const gchar *notification_id;
+
+ g_variant_get (parameters, "(&s&s)", &app_id, ¬ification_id);
+ g_notification_server_notification_removed (server, app_id, notification_id);
+ g_dbus_method_invocation_return_value (invocation, NULL);
+ }
+ else
+ {
+ g_dbus_method_invocation_return_dbus_error (invocation, "UnknownMethod", "No such method");
+ }
+}
+
+static void
+g_notification_server_dispose (GObject *object)
+{
+ GNotificationServer *server = G_NOTIFICATION_SERVER (object);
+
+ g_notification_server_stop (server);
+
+ g_clear_pointer (&server->applications, g_hash_table_unref);
+ g_clear_object (&server->connection);
+
+ G_OBJECT_CLASS (g_notification_server_parent_class)->dispose (object);
+}
+
+static void
+g_notification_server_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GNotificationServer *server = G_NOTIFICATION_SERVER (object);
+
+ switch (property_id)
+ {
+ case PROP_IS_RUNNING:
+ g_value_set_boolean (value, server->is_running);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ }
+}
+
+static void
+g_notification_server_class_init (GNotificationServerClass *class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+ object_class->get_property = g_notification_server_get_property;
+ object_class->dispose = g_notification_server_dispose;
+
+ g_object_class_install_property (object_class, PROP_IS_RUNNING,
+ g_param_spec_boolean ("is-running", "", "", FALSE,
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+
+ g_signal_new ("notification-received", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
+ 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 3,
+ G_TYPE_STRING, G_TYPE_STRING, G_TYPE_VARIANT);
+
+ g_signal_new ("notification-removed", G_TYPE_NOTIFICATION_SERVER, G_SIGNAL_RUN_FIRST,
+ 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
+ G_TYPE_STRING, G_TYPE_STRING);
+}
+
+static void
+g_notification_server_bus_acquired (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ const GDBusInterfaceVTable vtable = {
+ org_gtk_Notifications_method_call, NULL, NULL
+ };
+ GNotificationServer *server = user_data;
+
+ server->object_id = g_dbus_connection_register_object (connection, "/org/gtk/Notifications",
+ org_gtk_Notifications_get_interface (),
+ &vtable, server, NULL, NULL);
+
+ /* register_object only fails if the same object is exported more than once */
+ g_assert (server->object_id > 0);
+
+ server->connection = g_object_ref (connection);
+}
+
+static void
+g_notification_server_name_acquired (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ GNotificationServer *server = user_data;
+
+ server->is_running = TRUE;
+ g_object_notify (G_OBJECT (server), "is-running");
+}
+
+static void
+g_notification_server_name_lost (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ GNotificationServer *server = user_data;
+
+ g_notification_server_stop (server);
+
+ if (connection == NULL && server->connection)
+ g_clear_object (&server->connection);
+}
+
+static void
+g_notification_server_init (GNotificationServer *server)
+{
+ server->applications = g_hash_table_new_full (g_str_hash, g_str_equal,
+ g_free, (GDestroyNotify) g_hash_table_unref);
+
+ server->name_owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
+ "org.gtk.Notifications",
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ g_notification_server_bus_acquired,
+ g_notification_server_name_acquired,
+ g_notification_server_name_lost,
+ server, NULL);
+}
+
+GNotificationServer *
+g_notification_server_new (void)
+{
+ return g_object_new (G_TYPE_NOTIFICATION_SERVER, NULL);
+}
+
+void
+g_notification_server_stop (GNotificationServer *server)
+{
+ g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), FALSE);
+
+ if (server->name_owner_id)
+ {
+ g_bus_unown_name (server->name_owner_id);
+ server->name_owner_id = 0;
+ }
+
+ if (server->object_id && server->connection)
+ {
+ g_dbus_connection_unregister_object (server->connection, server->object_id);
+ server->object_id = 0;
+ }
+
+ if (server->is_running)
+ {
+ server->is_running = FALSE;
+ g_object_notify (G_OBJECT (server), "is-running");
+ }
+}
+
+gboolean
+g_notification_server_get_is_running (GNotificationServer *server)
+{
+ g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), FALSE);
+
+ return server->is_running;
+}
+
+gchar **
+g_notification_server_list_applications (GNotificationServer *server)
+{
+ g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
+
+ return (gchar **) g_hash_table_get_keys_as_array (server->applications, NULL);
+}
+
+gchar **
+g_notification_server_list_notifications (GNotificationServer *server,
+ const gchar *app_id)
+{
+ GHashTable *notifications;
+
+ g_return_val_if_fail (G_IS_NOTIFICATION_SERVER (server), NULL);
+ g_return_val_if_fail (app_id != NULL, NULL);
+
+ notifications = g_hash_table_lookup (server->applications, app_id);
+
+ if (notifications == NULL)
+ return NULL;
+
+ return (gchar **) g_hash_table_get_keys_as_array (notifications, NULL);
+}
diff --git a/gio/tests/gnotification-server.h b/gio/tests/gnotification-server.h
new file mode 100644
index 0000000..beec619
--- /dev/null
+++ b/gio/tests/gnotification-server.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright © 2013 Lars Uebernickel
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Lars Uebernickel <lars uebernic de>
+ */
+
+#ifndef __G_NOTIFICATION_SERVER_H__
+#define __G_NOTIFICATION_SERVER_H__
+
+#include <glib-object.h>
+
+#define G_TYPE_NOTIFICATION_SERVER (g_notification_server_get_type ())
+#define G_NOTIFICATION_SERVER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_NOTIFICATION_SERVER,
GNotificationServer))
+#define G_IS_NOTIFICATION_SERVER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_NOTIFICATION_SERVER))
+
+typedef struct _GNotificationServer GNotificationServer;
+
+GType g_notification_server_get_type (void);
+
+GNotificationServer * g_notification_server_new (void);
+
+void g_notification_server_stop (GNotificationServer *server);
+
+gboolean g_notification_server_get_is_running (GNotificationServer *server);
+
+gchar ** g_notification_server_list_applications (GNotificationServer *server);
+
+gchar ** g_notification_server_list_notifications (GNotificationServer *server,
+ const gchar *app_id);
+
+#endif
diff --git a/gio/tests/gnotification.c b/gio/tests/gnotification.c
new file mode 100644
index 0000000..2db78e3
--- /dev/null
+++ b/gio/tests/gnotification.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright © 2013 Lars Uebernickel
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Lars Uebernickel <lars uebernic de>
+ */
+
+#include <glib.h>
+
+#include "gnotification-server.h"
+#include "gdbus-sessionbus.h"
+
+static void
+activate_app (GApplication *application,
+ gpointer user_data)
+{
+ GNotification *notification;
+
+ notification = g_notification_new ("Test");
+
+ g_application_send_notification (application, "test1", notification);
+ g_application_send_notification (application, "test2", notification);
+ g_application_withdraw_notification (application, "test1");
+ g_application_send_notification (application, "test3", notification);
+
+ g_dbus_connection_flush_sync (g_application_get_dbus_connection (application), NULL, NULL);
+
+ g_object_unref (notification);
+}
+
+static void
+notification_received (GNotificationServer *server,
+ const gchar *app_id,
+ const gchar *notification_id,
+ GVariant *notification,
+ gpointer user_data)
+{
+ gint *count = user_data;
+ const gchar *title;
+
+ g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
+
+ switch (*count)
+ {
+ case 0:
+ g_assert_cmpstr (notification_id, ==, "test1");
+ g_assert (g_variant_lookup (notification, "title", "&s", &title));
+ g_assert_cmpstr (title, ==, "Test");
+ break;
+
+ case 1:
+ g_assert_cmpstr (notification_id, ==, "test2");
+ break;
+
+ case 2:
+ g_assert_cmpstr (notification_id, ==, "test3");
+
+ g_notification_server_stop (server);
+ break;
+ }
+
+ (*count)++;
+}
+
+static void
+notification_removed (GNotificationServer *server,
+ const gchar *app_id,
+ const gchar *notification_id,
+ gpointer user_data)
+{
+ gint *count = user_data;
+
+ g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
+ g_assert_cmpstr (notification_id, ==, "test1");
+
+ (*count)++;
+}
+
+static void
+server_notify_is_running (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data)
+{
+ GMainLoop *loop = user_data;
+ GNotificationServer *server = G_NOTIFICATION_SERVER (object);
+
+ if (g_notification_server_get_is_running (server))
+ {
+ GApplication *app;
+
+ app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_FLAGS_NONE);
+ g_signal_connect (app, "activate", G_CALLBACK (activate_app), NULL);
+
+ g_application_run (app, 0, NULL);
+
+ g_object_unref (app);
+ }
+ else
+ {
+ g_main_loop_quit (loop);
+ }
+}
+
+static gboolean
+timeout (gpointer user_data)
+{
+ GNotificationServer *server = user_data;
+
+ g_notification_server_stop (server);
+
+ return G_SOURCE_REMOVE;
+}
+
+static void
+basic (void)
+{
+ GNotificationServer *server;
+ GMainLoop *loop;
+ gint received_count = 0;
+ gint removed_count = 0;
+
+ session_bus_up ();
+
+ loop = g_main_loop_new (NULL, FALSE);
+
+ server = g_notification_server_new ();
+ g_signal_connect (server, "notification-received", G_CALLBACK (notification_received), &received_count);
+ g_signal_connect (server, "notification-removed", G_CALLBACK (notification_removed), &removed_count);
+ g_signal_connect (server, "notify::is-running", G_CALLBACK (server_notify_is_running), loop);
+ g_timeout_add_seconds (1, timeout, server);
+
+ g_main_loop_run (loop);
+
+ g_assert_cmpint (received_count, ==, 3);
+ g_assert_cmpint (removed_count, ==, 1);
+
+ g_object_unref (server);
+ g_main_loop_unref (loop);
+ session_bus_stop ();
+}
+
+int main (int argc, char *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/gnotification/basic", basic);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]