1/*
2 * Copyright © 2013 Lars Uebernickel
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
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.
13 *
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/>.
16 *
17 * Authors: Lars Uebernickel <lars@uebernic.de>
18 */
19
20#include <glib.h>
21
22#include "gnotification-server.h"
23#include "gdbus-sessionbus.h"
24
25static void
26activate_app (GApplication *application,
27 gpointer user_data)
28{
29 GNotification *notification;
30 GIcon *icon;
31
32 notification = g_notification_new (title: "Test");
33
34 g_application_send_notification (application, id: "test1", notification);
35 g_application_send_notification (application, id: "test2", notification);
36 g_application_withdraw_notification (application, id: "test1");
37 g_application_send_notification (application, id: "test3", notification);
38
39 icon = g_themed_icon_new (iconname: "i-c-o-n");
40 g_notification_set_icon (notification, icon);
41 g_object_unref (object: icon);
42
43 g_notification_set_body (notification, body: "body");
44 g_notification_set_priority (notification, priority: G_NOTIFICATION_PRIORITY_URGENT);
45 g_notification_set_default_action_and_target (notification, action: "app.action", target_format: "i", 42);
46 g_notification_add_button_with_target (notification, label: "label", action: "app.action2", target_format: "s", "bla");
47
48 g_application_send_notification (application, id: "test4", notification);
49 g_application_send_notification (application, NULL, notification);
50
51 g_dbus_connection_flush_sync (connection: g_application_get_dbus_connection (application), NULL, NULL);
52
53 g_object_unref (object: notification);
54}
55
56static void
57notification_received (GNotificationServer *server,
58 const gchar *app_id,
59 const gchar *notification_id,
60 GVariant *notification,
61 gpointer user_data)
62{
63 gint *count = user_data;
64 const gchar *title;
65
66 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
67
68 switch (*count)
69 {
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;
75
76 case 1:
77 g_assert_cmpstr (notification_id, ==, "test2");
78 break;
79
80 case 2:
81 g_assert_cmpstr (notification_id, ==, "test3");
82 break;
83
84 case 3:
85 g_assert_cmpstr (notification_id, ==, "test4");
86 break;
87
88 case 4:
89 g_assert (g_dbus_is_guid (notification_id));
90
91 g_notification_server_stop (server);
92 break;
93 }
94
95 (*count)++;
96}
97
98static void
99notification_removed (GNotificationServer *server,
100 const gchar *app_id,
101 const gchar *notification_id,
102 gpointer user_data)
103{
104 gint *count = user_data;
105
106 g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
107 g_assert_cmpstr (notification_id, ==, "test1");
108
109 (*count)++;
110}
111
112static void
113server_notify_is_running (GObject *object,
114 GParamSpec *pspec,
115 gpointer user_data)
116{
117 GMainLoop *loop = user_data;
118 GNotificationServer *server = G_NOTIFICATION_SERVER (object);
119
120 if (g_notification_server_get_is_running (server))
121 {
122 GApplication *app;
123
124 app = g_application_new (application_id: "org.gtk.TestApplication", flags: G_APPLICATION_FLAGS_NONE);
125 g_signal_connect (app, "activate", G_CALLBACK (activate_app), NULL);
126
127 g_application_run (application: app, argc: 0, NULL);
128
129 g_object_unref (object: app);
130 }
131 else
132 {
133 g_main_loop_quit (loop);
134 }
135}
136
137static gboolean
138timeout (gpointer user_data)
139{
140 GNotificationServer *server = user_data;
141
142 g_notification_server_stop (server);
143
144 return G_SOURCE_REMOVE;
145}
146
147static void
148basic (void)
149{
150 GNotificationServer *server;
151 GMainLoop *loop;
152 gint received_count = 0;
153 gint removed_count = 0;
154
155 session_bus_up ();
156
157 loop = g_main_loop_new (NULL, FALSE);
158
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 (interval: 1, function: timeout, data: server);
164
165 g_main_loop_run (loop);
166
167 g_assert_cmpint (received_count, ==, 5);
168 g_assert_cmpint (removed_count, ==, 1);
169
170 g_object_unref (object: server);
171 g_main_loop_unref (loop);
172 session_bus_stop ();
173}
174
175struct _GNotification
176{
177 GObject parent;
178
179 gchar *title;
180 gchar *body;
181 GIcon *icon;
182 GNotificationPriority priority;
183 GPtrArray *buttons;
184 gchar *default_action;
185 GVariant *default_action_target;
186};
187
188typedef struct
189{
190 gchar *label;
191 gchar *action_name;
192 GVariant *target;
193} Button;
194
195static void
196test_properties (void)
197{
198 GNotification *n;
199 struct _GNotification *rn;
200 GIcon *icon;
201 const gchar * const *names;
202 Button *b;
203
204 n = g_notification_new (title: "Test");
205
206 g_notification_set_title (notification: n, title: "title");
207 g_notification_set_body (notification: n, body: "body");
208 icon = g_themed_icon_new (iconname: "i-c-o-n");
209 g_notification_set_icon (notification: n, icon);
210 g_object_unref (object: icon);
211 g_notification_set_priority (notification: n, priority: G_NOTIFICATION_PRIORITY_HIGH);
212 g_notification_add_button (notification: n, label: "label1", detailed_action: "app.action1::target1");
213 g_notification_set_default_action (notification: n, detailed_action: "app.action2::target2");
214
215 rn = (struct _GNotification *)n;
216
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_cmpstr (names[1], ==, "i-c-o-n-symbolic");
223 g_assert_null (names[2]);
224 g_assert (rn->priority == G_NOTIFICATION_PRIORITY_HIGH);
225
226 g_assert_cmpint (rn->buttons->len, ==, 1);
227 b = (Button*)rn->buttons->pdata[0];
228 g_assert_cmpstr (b->label, ==, "label1");
229 g_assert_cmpstr (b->action_name, ==, "app.action1");
230 g_assert_cmpstr (g_variant_get_string (b->target, NULL), ==, "target1");
231
232 g_assert_cmpstr (rn->default_action, ==, "app.action2");
233 g_assert_cmpstr (g_variant_get_string (rn->default_action_target, NULL), ==, "target2");
234
235 g_object_unref (object: n);
236}
237
238int main (int argc, char *argv[])
239{
240 g_test_init (argc: &argc, argv: &argv, NULL);
241
242 g_test_add_func (testpath: "/gnotification/basic", test_func: basic);
243 g_test_add_func (testpath: "/gnotification/properties", test_func: test_properties);
244
245 return g_test_run ();
246}
247

source code of gtk/subprojects/glib/gio/tests/gnotification.c