Update German translation
[glib.git] / gio / tests / gnotification.c
blobdb14b49f56ce564abc4c63e9752bf789cab7d80a
1 /*
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, see <http://www.gnu.org/licenses/>.
17 * Authors: Lars Uebernickel <lars@uebernic.de>
20 #include <glib.h>
22 #include "gnotification-server.h"
23 #include "gdbus-sessionbus.h"
25 static void
26 activate_app (GApplication *application,
27 gpointer user_data)
29 GNotification *notification;
30 GIcon *icon;
32 notification = g_notification_new ("Test");
34 g_application_send_notification (application, "test1", notification);
35 g_application_send_notification (application, "test2", notification);
36 g_application_withdraw_notification (application, "test1");
37 g_application_send_notification (application, "test3", notification);
39 icon = g_themed_icon_new ("i-c-o-n");
40 g_notification_set_icon (notification, icon);
41 g_object_unref (icon);
43 g_notification_set_body (notification, "body");
44 g_notification_set_priority (notification, G_NOTIFICATION_PRIORITY_URGENT);
45 g_notification_set_default_action_and_target (notification, "app.action", "i", 42);
46 g_notification_add_button_with_target (notification, "label", "app.action2", "s", "bla");
48 g_application_send_notification (application, "test4", notification);
49 g_application_send_notification (application, NULL, notification);
51 g_dbus_connection_flush_sync (g_application_get_dbus_connection (application), NULL, NULL);
53 g_object_unref (notification);
56 static void
57 notification_received (GNotificationServer *server,
58 const gchar *app_id,
59 const gchar *notification_id,
60 GVariant *notification,
61 gpointer user_data)
63 gint *count = user_data;
64 const gchar *title;
66 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
68 switch (*count)
70 case 0:
71 g_assert_cmpstr (notification_id, ==, "test1");
72 g_assert (g_variant_lookup (notification, "title", "&s", &title));
73 g_assert_cmpstr (title, ==, "Test");
74 break;
76 case 1:
77 g_assert_cmpstr (notification_id, ==, "test2");
78 break;
80 case 2:
81 g_assert_cmpstr (notification_id, ==, "test3");
82 break;
84 case 3:
85 g_assert_cmpstr (notification_id, ==, "test4");
86 break;
88 case 4:
89 g_assert (g_dbus_is_guid (notification_id));
91 g_notification_server_stop (server);
92 break;
95 (*count)++;
98 static void
99 notification_removed (GNotificationServer *server,
100 const gchar *app_id,
101 const gchar *notification_id,
102 gpointer user_data)
104 gint *count = user_data;
106 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
107 g_assert_cmpstr (notification_id, ==, "test1");
109 (*count)++;
112 static void
113 server_notify_is_running (GObject *object,
114 GParamSpec *pspec,
115 gpointer user_data)
117 GMainLoop *loop = user_data;
118 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
120 if (g_notification_server_get_is_running (server))
122 GApplication *app;
124 app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_FLAGS_NONE);
125 g_signal_connect (app, "activate", G_CALLBACK (activate_app), NULL);
127 g_application_run (app, 0, NULL);
129 g_object_unref (app);
131 else
133 g_main_loop_quit (loop);
137 static gboolean
138 timeout (gpointer user_data)
140 GNotificationServer *server = user_data;
142 g_notification_server_stop (server);
144 return G_SOURCE_REMOVE;
147 static void
148 basic (void)
150 GNotificationServer *server;
151 GMainLoop *loop;
152 gint received_count = 0;
153 gint removed_count = 0;
155 session_bus_up ();
157 loop = g_main_loop_new (NULL, FALSE);
159 server = g_notification_server_new ();
160 g_signal_connect (server, "notification-received", G_CALLBACK (notification_received), &received_count);
161 g_signal_connect (server, "notification-removed", G_CALLBACK (notification_removed), &removed_count);
162 g_signal_connect (server, "notify::is-running", G_CALLBACK (server_notify_is_running), loop);
163 g_timeout_add_seconds (1, timeout, server);
165 g_main_loop_run (loop);
167 g_assert_cmpint (received_count, ==, 5);
168 g_assert_cmpint (removed_count, ==, 1);
170 g_object_unref (server);
171 g_main_loop_unref (loop);
172 session_bus_stop ();
175 struct _GNotification
177 GObject parent;
179 gchar *title;
180 gchar *body;
181 GIcon *icon;
182 GNotificationPriority priority;
183 GPtrArray *buttons;
184 gchar *default_action;
185 GVariant *default_action_target;
188 typedef struct
190 gchar *label;
191 gchar *action_name;
192 GVariant *target;
193 } Button;
195 static void
196 test_properties (void)
198 GNotification *n;
199 struct _GNotification *rn;
200 GIcon *icon;
201 const gchar * const *names;
202 Button *b;
204 n = g_notification_new ("Test");
206 g_notification_set_title (n, "title");
207 g_notification_set_body (n, "body");
208 icon = g_themed_icon_new ("i-c-o-n");
209 g_notification_set_icon (n, icon);
210 g_object_unref (icon);
211 g_notification_set_priority (n, G_NOTIFICATION_PRIORITY_HIGH);
212 g_notification_add_button (n, "label1", "app.action1::target1");
213 g_notification_set_default_action (n, "app.action2::target2");
215 rn = (struct _GNotification *)n;
217 g_assert_cmpstr (rn->title, ==, "title");
218 g_assert_cmpstr (rn->body, ==, "body");
219 g_assert (G_IS_THEMED_ICON (rn->icon));
220 names = g_themed_icon_get_names (G_THEMED_ICON (rn->icon));
221 g_assert_cmpstr (names[0], ==, "i-c-o-n");
222 g_assert (names[1] == NULL);
223 g_assert (rn->priority == G_NOTIFICATION_PRIORITY_HIGH);
225 g_assert_cmpint (rn->buttons->len, ==, 1);
226 b = (Button*)rn->buttons->pdata[0];
227 g_assert_cmpstr (b->label, ==, "label1");
228 g_assert_cmpstr (b->action_name, ==, "app.action1");
229 g_assert_cmpstr (g_variant_get_string (b->target, NULL), ==, "target1");
231 g_assert_cmpstr (rn->default_action, ==, "app.action2");
232 g_assert_cmpstr (g_variant_get_string (rn->default_action_target, NULL), ==, "target2");
234 g_object_unref (n);
237 int main (int argc, char *argv[])
239 g_test_init (&argc, &argv, NULL);
241 g_test_add_func ("/gnotification/basic", basic);
242 g_test_add_func ("/gnotification/properties", test_properties);
244 return g_test_run ();