2 * Copyright © 2013 Lars Uebernickel
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Lars Uebernickel <lars@uebernic.de>
24 #include "gnotification-server.h"
25 #include "gdbus-sessionbus.h"
28 activate_app (GApplication *application,
31 GNotification *notification;
34 notification = g_notification_new ("Test");
36 g_application_send_notification (application, "test1", notification);
37 g_application_send_notification (application, "test2", notification);
38 g_application_withdraw_notification (application, "test1");
39 g_application_send_notification (application, "test3", notification);
41 icon = g_themed_icon_new ("i-c-o-n");
42 g_notification_set_icon (notification, icon);
43 g_object_unref (icon);
45 g_notification_set_body (notification, "body");
46 g_notification_set_urgent (notification, TRUE);
47 g_notification_set_default_action_and_target (notification, "app.action", "i", 42);
48 g_notification_add_button_with_target (notification, "label", "app.action2", "s", "bla");
50 g_application_send_notification (application, "test4", notification);
52 g_dbus_connection_flush_sync (g_application_get_dbus_connection (application), NULL, NULL);
54 g_object_unref (notification);
58 notification_received (GNotificationServer *server,
60 const gchar *notification_id,
61 GVariant *notification,
64 gint *count = user_data;
67 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
72 g_assert_cmpstr (notification_id, ==, "test1");
73 g_assert (g_variant_lookup (notification, "title", "&s", &title));
74 g_assert_cmpstr (title, ==, "Test");
78 g_assert_cmpstr (notification_id, ==, "test2");
82 g_assert_cmpstr (notification_id, ==, "test3");
86 g_assert_cmpstr (notification_id, ==, "test4");
88 g_notification_server_stop (server);
96 notification_removed (GNotificationServer *server,
98 const gchar *notification_id,
101 gint *count = user_data;
103 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
104 g_assert_cmpstr (notification_id, ==, "test1");
110 server_notify_is_running (GObject *object,
114 GMainLoop *loop = user_data;
115 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
117 if (g_notification_server_get_is_running (server))
121 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_FLAGS_NONE);
122 g_signal_connect (app, "activate", G_CALLBACK (activate_app), NULL);
124 g_application_run (app, 0, NULL);
126 g_object_unref (app);
130 g_main_loop_quit (loop);
135 timeout (gpointer user_data)
137 GNotificationServer *server = user_data;
139 g_notification_server_stop (server);
141 return G_SOURCE_REMOVE;
147 GNotificationServer *server;
149 gint received_count = 0;
150 gint removed_count = 0;
154 loop = g_main_loop_new (NULL, FALSE);
156 server = g_notification_server_new ();
157 g_signal_connect (server, "notification-received", G_CALLBACK (notification_received), &received_count);
158 g_signal_connect (server, "notification-removed", G_CALLBACK (notification_removed), &removed_count);
159 g_signal_connect (server, "notify::is-running", G_CALLBACK (server_notify_is_running), loop);
160 g_timeout_add_seconds (1, timeout, server);
162 g_main_loop_run (loop);
164 g_assert_cmpint (received_count, ==, 4);
165 g_assert_cmpint (removed_count, ==, 1);
167 g_object_unref (server);
168 g_main_loop_unref (loop);
172 struct _GNotification
181 gchar *default_action;
182 GVariant *default_action_target;
193 test_properties (void)
196 struct _GNotification *rn;
198 const gchar * const *names;
201 n = g_notification_new ("Test");
203 g_notification_set_title (n, "title");
204 g_notification_set_body (n, "body");
205 icon = g_themed_icon_new ("i-c-o-n");
206 g_notification_set_icon (n, icon);
207 g_object_unref (icon);
208 g_notification_set_urgent (n, TRUE);
209 g_notification_add_button (n, "label1", "app.action1::target1");
210 g_notification_set_default_action (n, "app.action2::target2");
212 rn = (struct _GNotification *)n;
214 g_assert_cmpstr (rn->title, ==, "title");
215 g_assert_cmpstr (rn->body, ==, "body");
216 g_assert (G_IS_THEMED_ICON (rn->icon));
217 names = g_themed_icon_get_names (G_THEMED_ICON (rn->icon));
218 g_assert_cmpstr (names[0], ==, "i-c-o-n");
219 g_assert (names[1] == NULL);
220 g_assert (rn->urgent);
222 g_assert_cmpint (rn->buttons->len, ==, 1);
223 b = (Button*)rn->buttons->pdata[0];
224 g_assert_cmpstr (b->label, ==, "label1");
225 g_assert_cmpstr (b->action_name, ==, "app.action1");
226 g_assert_cmpstr (g_variant_get_string (b->target, NULL), ==, "target1");
228 g_assert_cmpstr (rn->default_action, ==, "app.action2");
229 g_assert_cmpstr (g_variant_get_string (rn->default_action_target, NULL), ==, "target2");
234 int main (int argc, char *argv[])
236 g_test_init (&argc, &argv, NULL);
238 g_test_add_func ("/gnotification/basic", basic);
239 g_test_add_func ("/gnotification/properties", test_properties);
241 return g_test_run ();