1 /*
2 Tests for the libappindicator library that are over DBus. This is
3 the client side of those tests.
4
5 Copyright 2009 Canonical Ltd.
6
7 Authors:
8 Ted Gould <ted@canonical.com>
9
10 This program is free software: you can redistribute it and/or modify it
11 under the terms of the GNU General Public License version 3, as published
12 by the Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranties of
16 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
17 PURPOSE. See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License along
20 with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include <glib.h>
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-bindings.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28 #include "../src/dbus-shared.h"
29
30 static GMainLoop * mainloop = NULL;
31 static gboolean passed = TRUE;
32 static gboolean watchdog_hit = TRUE;
33 static gboolean active = FALSE;
34 static guint toggle_count = 0;
35
36 #define PASSIVE_STR "Passive"
37 #define ACTIVE_STR "Active"
38 #define ATTN_STR "NeedsAttention"
39
40 static DBusHandlerResult
dbus_reg_filter(DBusConnection * connection,DBusMessage * message,void * user_data)41 dbus_reg_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
42 {
43 if (dbus_message_is_method_call(message, NOTIFICATION_WATCHER_DBUS_ADDR, "RegisterStatusNotifierItem")) {
44 DBusMessage * reply = dbus_message_new_method_return(message);
45 dbus_connection_send(connection, reply, NULL);
46 dbus_message_unref(reply);
47 return DBUS_HANDLER_RESULT_HANDLED;
48 }
49
50 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
51 }
52
53
54 static DBusHandlerResult
dbus_filter(DBusConnection * connection,DBusMessage * message,void * user_data)55 dbus_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
56 {
57 if (!dbus_message_is_signal(message, NOTIFICATION_ITEM_DBUS_IFACE, "NewStatus")) {
58 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
59 }
60
61 gchar * string;
62
63 DBusError derror;
64 dbus_error_init(&derror);
65 if (!dbus_message_get_args(message, &derror,
66 DBUS_TYPE_STRING, &string,
67 DBUS_TYPE_INVALID)) {
68 g_warning("Couldn't get parameters");
69 dbus_error_free(&derror);
70 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
71 }
72
73 watchdog_hit = TRUE;
74
75 if (g_strcmp0(string, ACTIVE_STR) == 0) {
76 if (active) {
77 g_warning("Got active when already active");
78 passed = FALSE;
79 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
80 }
81 active = TRUE;
82 } else {
83 active = FALSE;
84 }
85
86 toggle_count++;
87
88 if (toggle_count == 100) {
89 g_main_loop_quit(mainloop);
90 }
91
92 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
93 }
94
95 gboolean
kill_func(gpointer userdata)96 kill_func (gpointer userdata)
97 {
98 if (watchdog_hit == FALSE) {
99 g_main_loop_quit(mainloop);
100 g_warning("Forced to Kill");
101 g_warning("Toggle count: %d", toggle_count);
102 passed = FALSE;
103 return FALSE;
104 }
105 watchdog_hit = FALSE;
106 return TRUE;
107 }
108
109 gint
main(gint argc,gchar * argv[])110 main (gint argc, gchar * argv[])
111 {
112 GError * error = NULL;
113 DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
114 if (error != NULL) {
115 g_error("Unable to get session bus: %s", error->message);
116 return 1;
117 }
118
119 DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
120 guint nameret = 0;
121
122 if (!org_freedesktop_DBus_request_name(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, 0, &nameret, &error)) {
123 g_error("Unable to call to request name");
124 return 1;
125 }
126
127 if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
128 g_error("Unable to get name");
129 return 1;
130 }
131
132 dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_reg_filter, NULL, NULL);
133
134 dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_filter, NULL, NULL);
135 dbus_bus_add_match(dbus_g_connection_get_connection(session_bus), "type='signal',interface='" NOTIFICATION_ITEM_DBUS_IFACE "',member='NewStatus'", NULL);
136
137 watchdog_hit = TRUE;
138 g_timeout_add_seconds(20, kill_func, NULL);
139
140 mainloop = g_main_loop_new(NULL, FALSE);
141 g_main_loop_run(mainloop);
142
143 if (passed) {
144 g_debug("Quiting");
145 return 0;
146 } else {
147 g_debug("Quiting as we're a failure");
148 return 1;
149 }
150 return 0;
151 }
152