Back to index

libappindicator  12.10.0
test-libappindicator-dbus-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 <app-indicator.h>
00029 #include "test-defines.h"
00030 
00031 #include "../src/dbus-shared.h"
00032 
00033 static GMainLoop * mainloop = NULL;
00034 static gboolean passed = TRUE;
00035 static int propcount = 0;
00036 
00037 static void
00038 check_propcount (void)
00039 {
00040        if (propcount >= 5) {
00041               g_main_loop_quit(mainloop);
00042        }
00043        return;
00044 }
00045 
00046 
00047 static void
00048 prop_id_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
00049 {
00050        propcount++;
00051 
00052        GError * error = NULL;
00053        GValue value = {0};
00054 
00055        if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
00056               g_warning("Getting ID failed: %s", error->message);
00057               g_error_free(error);
00058               passed = FALSE;
00059               check_propcount();
00060               return;
00061        }
00062 
00063        if (g_strcmp0(TEST_ID, g_value_get_string(&value))) {
00064               g_debug("Property ID Returned: FAILED");
00065               passed = FALSE;
00066        } else {
00067               g_debug("Property ID Returned: PASSED");
00068        }
00069 
00070        check_propcount();
00071        return;
00072 }
00073 
00074 static void
00075 prop_category_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
00076 {
00077        propcount++;
00078 
00079        GError * error = NULL;
00080        GValue value = {0};
00081 
00082        if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
00083               g_warning("Getting category failed: %s", error->message);
00084               g_error_free(error);
00085               passed = FALSE;
00086               check_propcount();
00087               return;
00088        }
00089 
00090        if (g_strcmp0(TEST_CATEGORY_S, g_value_get_string(&value))) {
00091               g_debug("Property category Returned: FAILED");
00092               passed = FALSE;
00093        } else {
00094               g_debug("Property category Returned: PASSED");
00095        }
00096 
00097        check_propcount();
00098        return;
00099 }
00100 
00101 static void
00102 prop_status_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
00103 {
00104        propcount++;
00105 
00106        GError * error = NULL;
00107        GValue value = {0};
00108 
00109        if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
00110               g_warning("Getting status failed: %s", error->message);
00111               g_error_free(error);
00112               passed = FALSE;
00113               check_propcount();
00114               return;
00115        }
00116 
00117        if (g_strcmp0(TEST_STATE_S, g_value_get_string(&value))) {
00118               g_debug("Property status Returned: FAILED");
00119               passed = FALSE;
00120        } else {
00121               g_debug("Property status Returned: PASSED");
00122        }
00123 
00124        check_propcount();
00125        return;
00126 }
00127 
00128 static void
00129 prop_icon_name_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
00130 {
00131        propcount++;
00132 
00133        GError * error = NULL;
00134        GValue value = {0};
00135 
00136        if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
00137               g_warning("Getting icon name failed: %s", error->message);
00138               g_error_free(error);
00139               passed = FALSE;
00140               check_propcount();
00141               return;
00142        }
00143 
00144        if (g_strcmp0(TEST_ICON_NAME, g_value_get_string(&value))) {
00145               g_debug("Property icon name Returned: FAILED");
00146               passed = FALSE;
00147        } else {
00148               g_debug("Property icon name Returned: PASSED");
00149        }
00150 
00151        check_propcount();
00152        return;
00153 }
00154 
00155 static void
00156 prop_attention_icon_name_cb (DBusGProxy * proxy, DBusGProxyCall * call, void * data)
00157 {
00158        propcount++;
00159 
00160        GError * error = NULL;
00161        GValue value = {0};
00162 
00163        if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_VALUE, &value, G_TYPE_INVALID)) {
00164               g_warning("Getting attention icon name failed: %s", error->message);
00165               g_error_free(error);
00166               passed = FALSE;
00167               check_propcount();
00168               return;
00169        }
00170 
00171        if (g_strcmp0(TEST_ATTENTION_ICON_NAME, g_value_get_string(&value))) {
00172               g_debug("Property attention icon name Returned: FAILED");
00173               passed = FALSE;
00174        } else {
00175               g_debug("Property attention icon name Returned: PASSED");
00176        }
00177 
00178        check_propcount();
00179        return;
00180 }
00181 
00182 gboolean
00183 kill_func (gpointer userdata)
00184 {
00185        g_main_loop_quit(mainloop);
00186        g_warning("Forced to Kill");
00187        passed = FALSE;
00188        return FALSE;
00189 }
00190 
00191 static DBusHandlerResult
00192 dbus_filter (DBusConnection * connection, DBusMessage * message, void * user_data)
00193 {
00194        if (dbus_message_is_method_call(message, NOTIFICATION_WATCHER_DBUS_ADDR, "RegisterStatusNotifierItem")) {
00195               DBusMessage * reply = dbus_message_new_method_return(message);
00196               dbus_connection_send(connection, reply, NULL);
00197               dbus_message_unref(reply);
00198               return DBUS_HANDLER_RESULT_HANDLED;
00199        }
00200 
00201        return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
00202 }
00203 
00204 gint
00205 main (gint argc, gchar * argv[])
00206 {
00207        g_type_init();
00208 
00209        GError * error = NULL;
00210        DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
00211        if (error != NULL) {
00212               g_error("Unable to get session bus: %s", error->message);
00213               return 1;
00214        }
00215 
00216     DBusGProxy * bus_proxy = dbus_g_proxy_new_for_name(session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
00217        guint nameret = 0;
00218 
00219        if (!org_freedesktop_DBus_request_name(bus_proxy, NOTIFICATION_WATCHER_DBUS_ADDR, 0, &nameret, &error)) {
00220               g_error("Unable to call to request name");
00221               return 1;
00222        }   
00223 
00224        if (nameret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
00225               g_error("Unable to get name");
00226               return 1;
00227        }
00228 
00229        dbus_connection_add_filter(dbus_g_connection_get_connection(session_bus), dbus_filter, NULL, NULL);
00230 
00231        g_usleep(500000);
00232 
00233        DBusGProxy * props = dbus_g_proxy_new_for_name_owner(session_bus,
00234                                                             ":1.2",
00235                                                             "/org/ayatana/NotificationItem/my_id",
00236                                                             DBUS_INTERFACE_PROPERTIES,
00237                                                             &error);
00238        if (error != NULL) {
00239               g_error("Unable to get property proxy: %s", error->message);
00240               return 1;
00241        }
00242 
00243        dbus_g_proxy_begin_call (props,
00244                                 "Get",
00245                                 prop_id_cb,
00246                                 NULL, NULL,
00247                                 G_TYPE_STRING, NOTIFICATION_ITEM_DBUS_IFACE,
00248                                 G_TYPE_STRING, "Id",
00249                                 G_TYPE_INVALID);
00250        dbus_g_proxy_begin_call (props,
00251                                 "Get",
00252                                 prop_category_cb,
00253                                 NULL, NULL,
00254                                 G_TYPE_STRING, NOTIFICATION_ITEM_DBUS_IFACE,
00255                                 G_TYPE_STRING, "Category",
00256                                 G_TYPE_INVALID);
00257        dbus_g_proxy_begin_call (props,
00258                                 "Get",
00259                                 prop_status_cb,
00260                                 NULL, NULL,
00261                                 G_TYPE_STRING, NOTIFICATION_ITEM_DBUS_IFACE,
00262                                 G_TYPE_STRING, "Status",
00263                                 G_TYPE_INVALID);
00264        dbus_g_proxy_begin_call (props,
00265                                 "Get",
00266                                 prop_icon_name_cb,
00267                                 NULL, NULL,
00268                                 G_TYPE_STRING, NOTIFICATION_ITEM_DBUS_IFACE,
00269                                 G_TYPE_STRING, "IconName",
00270                                 G_TYPE_INVALID);
00271        dbus_g_proxy_begin_call (props,
00272                                 "Get",
00273                                 prop_attention_icon_name_cb,
00274                                 NULL, NULL,
00275                                 G_TYPE_STRING, NOTIFICATION_ITEM_DBUS_IFACE,
00276                                 G_TYPE_STRING, "AttentionIconName",
00277                                 G_TYPE_INVALID);
00278 
00279        g_timeout_add_seconds(2, kill_func, NULL);
00280 
00281        mainloop = g_main_loop_new(NULL, FALSE);
00282        g_main_loop_run(mainloop);
00283 
00284        if (passed) {
00285               g_debug("Quiting");
00286               return 0;
00287        } else {
00288               g_debug("Quiting as we're a failure");
00289               return 1;
00290        }
00291        return 0;
00292 }