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>
22 #include "gnotification-server.h"
23 #include "gdbus-sessionbus.h"
26 activate_app (GApplication
*application
,
29 GNotification
*notification
;
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
);
57 notification_received (GNotificationServer
*server
,
59 const gchar
*notification_id
,
60 GVariant
*notification
,
63 gint
*count
= user_data
;
66 g_assert_cmpstr (app_id
, ==, "org.gtk.TestApplication");
71 g_assert_cmpstr (notification_id
, ==, "test1");
72 g_assert (g_variant_lookup (notification
, "title", "&s", &title
));
73 g_assert_cmpstr (title
, ==, "Test");
77 g_assert_cmpstr (notification_id
, ==, "test2");
81 g_assert_cmpstr (notification_id
, ==, "test3");
85 g_assert_cmpstr (notification_id
, ==, "test4");
89 g_assert (g_dbus_is_guid (notification_id
));
91 g_notification_server_stop (server
);
99 notification_removed (GNotificationServer
*server
,
101 const gchar
*notification_id
,
104 gint
*count
= user_data
;
106 g_assert_cmpstr (app_id
, ==, "org.gtk.TestApplication");
107 g_assert_cmpstr (notification_id
, ==, "test1");
113 server_notify_is_running (GObject
*object
,
117 GMainLoop
*loop
= user_data
;
118 GNotificationServer
*server
= G_NOTIFICATION_SERVER (object
);
120 if (g_notification_server_get_is_running (server
))
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
);
133 g_main_loop_quit (loop
);
138 timeout (gpointer user_data
)
140 GNotificationServer
*server
= user_data
;
142 g_notification_server_stop (server
);
144 return G_SOURCE_REMOVE
;
150 GNotificationServer
*server
;
152 gint received_count
= 0;
153 gint removed_count
= 0;
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
);
175 struct _GNotification
182 GNotificationPriority priority
;
184 gchar
*default_action
;
185 GVariant
*default_action_target
;
196 test_properties (void)
199 struct _GNotification
*rn
;
201 const gchar
* const *names
;
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");
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 ();