Simplify subprocesses in tests
[platform/upstream/glib.git] / gio / tests / gnotification.c
1 /*
2  * Copyright © 2013 Lars Uebernickel
3  *
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.
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Authors: Lars Uebernickel <lars@uebernic.de>
20  */
21
22 #include <glib.h>
23
24 #include "gnotification-server.h"
25 #include "gdbus-sessionbus.h"
26
27 static void
28 activate_app (GApplication *application,
29               gpointer      user_data)
30 {
31   GNotification *notification;
32   GIcon *icon;
33
34   notification = g_notification_new ("Test");
35
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);
40
41   icon = g_themed_icon_new ("i-c-o-n");
42   g_notification_set_icon (notification, icon);
43   g_object_unref (icon);
44
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");
49
50   g_application_send_notification (application, "test4", notification);
51
52   g_dbus_connection_flush_sync (g_application_get_dbus_connection (application), NULL, NULL);
53
54   g_object_unref (notification);
55 }
56
57 static void
58 notification_received (GNotificationServer *server,
59                        const gchar         *app_id,
60                        const gchar         *notification_id,
61                        GVariant            *notification,
62                        gpointer             user_data)
63 {
64   gint *count = user_data;
65   const gchar *title;
66
67   g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
68
69   switch (*count)
70     {
71     case 0:
72       g_assert_cmpstr (notification_id, ==, "test1");
73       g_assert (g_variant_lookup (notification, "title", "&s", &title));
74       g_assert_cmpstr (title, ==, "Test");
75       break;
76
77     case 1:
78       g_assert_cmpstr (notification_id, ==, "test2");
79       break;
80
81     case 2:
82       g_assert_cmpstr (notification_id, ==, "test3");
83       break;
84
85     case 3:
86       g_assert_cmpstr (notification_id, ==, "test4");
87
88       g_notification_server_stop (server);
89       break;
90     }
91
92   (*count)++;
93 }
94
95 static void
96 notification_removed (GNotificationServer *server,
97                       const gchar         *app_id,
98                       const gchar         *notification_id,
99                       gpointer             user_data)
100 {
101   gint *count = user_data;
102
103   g_assert_cmpstr (app_id, ==, "org.gtk.TestApplication");
104   g_assert_cmpstr (notification_id, ==, "test1");
105
106   (*count)++;
107 }
108
109 static void
110 server_notify_is_running (GObject    *object,
111                           GParamSpec *pspec,
112                           gpointer    user_data)
113 {
114   GMainLoop *loop = user_data;
115   GNotificationServer *server = G_NOTIFICATION_SERVER (object);
116
117   if (g_notification_server_get_is_running (server))
118     {
119       GApplication *app;
120
121       app = g_application_new ("org.gtk.TestApplication", G_APPLICATION_FLAGS_NONE);
122       g_signal_connect (app, "activate", G_CALLBACK (activate_app), NULL);
123
124       g_application_run (app, 0, NULL);
125
126       g_object_unref (app);
127     }
128   else
129     {
130       g_main_loop_quit (loop);
131     }
132 }
133
134 static gboolean
135 timeout (gpointer user_data)
136 {
137   GNotificationServer *server = user_data;
138
139   g_notification_server_stop (server);
140
141   return G_SOURCE_REMOVE;
142 }
143
144 static void
145 basic (void)
146 {
147   GNotificationServer *server;
148   GMainLoop *loop;
149   gint received_count = 0;
150   gint removed_count = 0;
151
152   session_bus_up ();
153
154   loop = g_main_loop_new (NULL, FALSE);
155
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);
161
162   g_main_loop_run (loop);
163
164   g_assert_cmpint (received_count, ==, 4);
165   g_assert_cmpint (removed_count, ==, 1);
166
167   g_object_unref (server);
168   g_main_loop_unref (loop);
169   session_bus_stop ();
170 }
171
172 struct _GNotification
173 {
174   GObject parent;
175
176   gchar *title;
177   gchar *body;
178   GIcon *icon;
179   gboolean urgent;
180   GPtrArray *buttons;
181   gchar *default_action;
182   GVariant *default_action_target;
183 };
184
185 typedef struct
186 {
187   gchar *label;
188   gchar *action_name;
189   GVariant *target;
190 } Button;
191
192 static void
193 test_properties (void)
194 {
195   GNotification *n;
196   struct _GNotification *rn;
197   GIcon *icon;
198   const gchar * const *names;
199   Button *b;
200
201   n = g_notification_new ("Test");
202
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");
211
212   rn = (struct _GNotification *)n;
213
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);
221
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");
227
228   g_assert_cmpstr (rn->default_action, ==, "app.action2");
229   g_assert_cmpstr (g_variant_get_string (rn->default_action_target, NULL), ==, "target2");
230
231   g_object_unref (n);
232 }
233
234 int main (int argc, char *argv[])
235 {
236   g_test_init (&argc, &argv, NULL);
237
238   g_test_add_func ("/gnotification/basic", basic);
239   g_test_add_func ("/gnotification/properties", test_properties);
240
241   return g_test_run ();
242 }