Back to index

libappindicator  12.10.0
test-libappindicator-status-client.c
Go to the documentation of this file.
00001 /*
00002 Tests for the libappindicator library that are over DBus.  This is
00003 the client side of those tests.
00004 
00005 Copyright 2009 Canonical Ltd.
00006 
00007 Authors:
00008     Ted Gould <ted@canonical.com>
00009 
00010 This program is free software: you can redistribute it and/or modify it
00011 under the terms of the GNU General Public License version 3, as published
00012 by the Free Software Foundation.
00013 
00014 This program is distributed in the hope that it will be useful, but
00015 WITHOUT ANY WARRANTY; without even the implied warranties of
00016 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
00017 PURPOSE.  See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License along
00020 with this program.  If not, see <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 
00024 #include <glib.h>
00025 #include <dbus/dbus-glib.h>
00026 #include <dbus/dbus-glib-bindings.h>
00027 #include <dbus/dbus-glib-lowlevel.h>
00028 #include "../src/dbus-shared.h"
00029 
00030 static GMainLoop * mainloop = NULL;
00031 static gboolean passed = TRUE;
00032 static gboolean watchdog_hit = TRUE;
00033 static gboolean active = FALSE;
00034 static guint toggle_count = 0;
00035 
00036 #define PASSIVE_STR  "Passive"
00037 #define ACTIVE_STR   "Active"
00038 #define ATTN_STR     "NeedsAttention"
00039 
00040 static DBusHandlerResult
00041 dbus_reg_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
00042 {
00043        if (dbus_message_is_method_call(message, NOTIFICATION_WATCHER_DBUS_ADDR, "RegisterStatusNotifierItem")) {
00044               DBusMessage * reply = dbus_message_new_method_return(message);
00045               dbus_connection_send(connection, reply, NULL);
00046               dbus_message_unref(reply);
00047               return DBUS_HANDLER_RESULT_HANDLED;
00048        }
00049 
00050        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00051 }
00052 
00053 
00054 static DBusHandlerResult
00055 dbus_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
00056 {
00057        if (!dbus_message_is_signal(message, NOTIFICATION_ITEM_DBUS_IFACE, "NewStatus")) {
00058               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00059        }
00060 
00061        gchar * string;
00062 
00063        DBusError derror;
00064        dbus_error_init(&derror);
00065        if (!dbus_message_get_args(message, &derror,
00066                             DBUS_TYPE_STRING, &string,
00067                             DBUS_TYPE_INVALID)) {
00068               g_warning("Couldn't get parameters");
00069               dbus_error_free(&derror);
00070               return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00071        }
00072 
00073        watchdog_hit = TRUE;
00074 
00075        if (g_strcmp0(string, ACTIVE_STR) == 0) {
00076               if (active) {
00077                      g_warning("Got active when already active");
00078                      passed = FALSE;
00079                      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00080               }
00081               active = TRUE;
00082        } else {
00083               active = FALSE;
00084        }
00085 
00086        toggle_count++;
00087 
00088        if (toggle_count == 100) {
00089               g_main_loop_quit(mainloop);
00090        }
00091 
00092        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00093 }
00094 
00095 gboolean
00096 kill_func (gpointer userdata)
00097 {
00098        if (watchdog_hit == FALSE) {
00099               g_main_loop_quit(mainloop);
00100               g_warning("Forced to Kill");
00101               g_warning("Toggle count: %d", toggle_count);
00102               passed = FALSE;
00103               return FALSE;
00104        }
00105        watchdog_hit = FALSE;
00106        return TRUE;
00107 }
00108 
00109 gint
00110 main (gint argc, gchar * argv[])
00111 {
00112        g_type_init();
00113 
00114        GError * error = NULL;
00115        DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
00116        if (error != NULL) {
00117               g_error("Unable to get session bus: %s", error->message);
00118               return 1;
00119        }
00120 
00121     DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
00122        guint nameret = 0;
00123 
00124        if (!org_freedesktop_DBus_request_name(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, 0, &nameret, &error)) {
00125               g_error("Unable to call to request name");
00126               return 1;
00127        }   
00128 
00129        if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
00130               g_error("Unable to get name");
00131               return 1;
00132        }
00133 
00134        dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_reg_filter, NULL, NULL);
00135 
00136        dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_filter, NULL, NULL);
00137        dbus_bus_add_match(dbus_g_connection_get_connection(session_bus), "type='signal',interface='" NOTIFICATION_ITEM_DBUS_IFACE "',member='NewStatus'", NULL);
00138 
00139        watchdog_hit = TRUE;
00140        g_timeout_add(1000, kill_func, NULL);
00141 
00142        mainloop = g_main_loop_new(NULL, FALSE);
00143        g_main_loop_run(mainloop);
00144 
00145        if (passed) {
00146               g_debug("Quiting");
00147               return 0;
00148        } else {
00149               g_debug("Quiting as we're a failure");
00150               return 1;
00151        }
00152        return 0;
00153 }