Back to index

libappindicator  12.10.0
test-libappindicator.c
Go to the documentation of this file.
00001 /*
00002 Tests for the libappindicator library.
00003 
00004 Copyright 2009 Canonical Ltd.
00005 
00006 Authors:
00007     Ted Gould <ted@canonical.com>
00008 
00009 This program is free software: you can redistribute it and/or modify it 
00010 under the terms of the GNU General Public License version 3, as published 
00011 by the Free Software Foundation.
00012 
00013 This program is distributed in the hope that it will be useful, but 
00014 WITHOUT ANY WARRANTY; without even the implied warranties of 
00015 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
00016 PURPOSE.  See the GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License along 
00019 with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 
00023 #include <glib.h>
00024 #include <glib-object.h>
00025 
00026 #include <app-indicator.h>
00027 
00028 #include <libdbusmenu-glib/menuitem.h>
00029 #include <libdbusmenu-glib/server.h>
00030 
00031 void
00032 test_libappindicator_prop_signals_status_helper (AppIndicator * ci, gchar * status, gboolean * signalactivated)
00033 {
00034        *signalactivated = TRUE;
00035        return;
00036 }
00037 
00038 void
00039 test_libappindicator_prop_signals_helper (AppIndicator * ci, gboolean * signalactivated)
00040 {
00041        *signalactivated = TRUE;
00042        return;
00043 }
00044 
00045 void
00046 test_libappindicator_prop_signals (void)
00047 {
00048         AppIndicator * ci = app_indicator_new ("test-app-indicator",
00049                                                "indicator-messages",
00050                                                APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00051 
00052        g_assert(ci != NULL);
00053 
00054        gboolean signaled = FALSE;
00055        gulong handlerid;
00056 
00057        handlerid = 0;
00058        handlerid = g_signal_connect(G_OBJECT(ci), "new-icon", G_CALLBACK(test_libappindicator_prop_signals_helper), &signaled);
00059        g_assert(handlerid != 0);
00060 
00061        handlerid = 0;
00062        handlerid = g_signal_connect(G_OBJECT(ci), "new-attention-icon", G_CALLBACK(test_libappindicator_prop_signals_helper), &signaled);
00063        g_assert(handlerid != 0);
00064 
00065        handlerid = 0;
00066        handlerid = g_signal_connect(G_OBJECT(ci), "new-status", G_CALLBACK(test_libappindicator_prop_signals_status_helper), &signaled);
00067        g_assert(handlerid != 0);
00068 
00069 
00070        signaled = FALSE;
00071        app_indicator_set_icon(ci, "bob");
00072        g_assert(signaled);
00073 
00074        signaled = FALSE;
00075        app_indicator_set_icon(ci, "bob");
00076        g_assert(!signaled);
00077 
00078        signaled = FALSE;
00079        app_indicator_set_icon(ci, "al");
00080        g_assert(signaled);
00081 
00082 
00083        signaled = FALSE;
00084        app_indicator_set_attention_icon(ci, "bob");
00085        g_assert(signaled);
00086 
00087        signaled = FALSE;
00088        app_indicator_set_attention_icon(ci, "bob");
00089        g_assert(!signaled);
00090 
00091        signaled = FALSE;
00092        app_indicator_set_attention_icon(ci, "al");
00093        g_assert(signaled);
00094 
00095 
00096        signaled = FALSE;
00097        app_indicator_set_status(ci, APP_INDICATOR_STATUS_PASSIVE);
00098        g_assert(!signaled);
00099 
00100        signaled = FALSE;
00101        app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
00102        g_assert(signaled);
00103 
00104        signaled = FALSE;
00105        app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
00106        g_assert(!signaled);
00107 
00108        signaled = FALSE;
00109        app_indicator_set_status(ci, APP_INDICATOR_STATUS_ATTENTION);
00110        g_assert(signaled);
00111 
00112        return;
00113 }
00114 
00115 void
00116 test_libappindicator_init_set_props (void)
00117 {
00118         AppIndicator * ci = app_indicator_new ("my-id",
00119                                                "my-name",
00120                                                APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00121 
00122        g_assert(ci != NULL);
00123 
00124        app_indicator_set_status(ci, APP_INDICATOR_STATUS_ACTIVE);
00125        app_indicator_set_attention_icon(ci, "my-attention-name");
00126        app_indicator_set_title(ci, "My Title");
00127 
00128        g_assert(!g_strcmp0("my-id", app_indicator_get_id(ci)));
00129        g_assert(!g_strcmp0("my-name", app_indicator_get_icon(ci)));
00130        g_assert(!g_strcmp0("my-attention-name", app_indicator_get_attention_icon(ci)));
00131        g_assert(!g_strcmp0("My Title", app_indicator_get_title(ci)));
00132        g_assert(app_indicator_get_status(ci) == APP_INDICATOR_STATUS_ACTIVE);
00133        g_assert(app_indicator_get_category(ci) == APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00134 
00135        g_object_unref(G_OBJECT(ci));
00136        return;
00137 }
00138 
00139 void
00140 test_libappindicator_init_with_props (void)
00141 {
00142         AppIndicator * ci = app_indicator_new ("my-id",
00143                                                "my-name",
00144                                                APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00145 
00146         app_indicator_set_status (ci, APP_INDICATOR_STATUS_ACTIVE);
00147         app_indicator_set_attention_icon (ci, "my-attention-name");
00148 
00149        g_assert(ci != NULL);
00150 
00151        g_assert(!g_strcmp0("my-id", app_indicator_get_id(ci)));
00152        g_assert(!g_strcmp0("my-name", app_indicator_get_icon(ci)));
00153        g_assert(!g_strcmp0("my-attention-name", app_indicator_get_attention_icon(ci)));
00154        g_assert(app_indicator_get_status(ci) == APP_INDICATOR_STATUS_ACTIVE);
00155        g_assert(app_indicator_get_category(ci) == APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00156 
00157        g_object_unref(G_OBJECT(ci));
00158        return;
00159 }
00160 
00161 void
00162 test_libappindicator_init (void)
00163 {
00164         AppIndicator * ci = app_indicator_new ("my-id", "my-name", APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00165        g_assert(ci != NULL);
00166        g_object_unref(G_OBJECT(ci));
00167        return;
00168 }
00169 
00170 void
00171 test_libappindicator_set_label (void)
00172 {
00173        AppIndicator * ci = app_indicator_new ("my-id",
00174                                               "my-name",
00175                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00176 
00177        g_assert(ci != NULL);
00178        g_assert(app_indicator_get_label(ci) == NULL);
00179        g_assert(app_indicator_get_label_guide(ci) == NULL);
00180 
00181        /* First check all the clearing modes, this is important as
00182           we're going to use them later, we need them to work. */
00183        app_indicator_set_label(ci, NULL, NULL);
00184 
00185        g_assert(app_indicator_get_label(ci) == NULL);
00186        g_assert(app_indicator_get_label_guide(ci) == NULL);
00187 
00188        app_indicator_set_label(ci, "", NULL);
00189 
00190        g_assert(app_indicator_get_label(ci) == NULL);
00191        g_assert(app_indicator_get_label_guide(ci) == NULL);
00192 
00193        app_indicator_set_label(ci, NULL, "");
00194 
00195        g_assert(app_indicator_get_label(ci) == NULL);
00196        g_assert(app_indicator_get_label_guide(ci) == NULL);
00197 
00198        app_indicator_set_label(ci, "", "");
00199 
00200        g_assert(app_indicator_get_label(ci) == NULL);
00201        g_assert(app_indicator_get_label_guide(ci) == NULL);
00202 
00203        app_indicator_set_label(ci, "label", "");
00204 
00205        g_assert(g_strcmp0(app_indicator_get_label(ci), "label") == 0);
00206        g_assert(app_indicator_get_label_guide(ci) == NULL);
00207 
00208        app_indicator_set_label(ci, NULL, NULL);
00209 
00210        g_assert(app_indicator_get_label(ci) == NULL);
00211        g_assert(app_indicator_get_label_guide(ci) == NULL);
00212 
00213        app_indicator_set_label(ci, "label", "guide");
00214 
00215        g_assert(g_strcmp0(app_indicator_get_label(ci), "label") == 0);
00216        g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "guide") == 0);
00217 
00218        app_indicator_set_label(ci, "label2", "guide");
00219 
00220        g_assert(g_strcmp0(app_indicator_get_label(ci), "label2") == 0);
00221        g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "guide") == 0);
00222 
00223        app_indicator_set_label(ci, "trick-label", "trick-guide");
00224 
00225        g_assert(g_strcmp0(app_indicator_get_label(ci), "trick-label") == 0);
00226        g_assert(g_strcmp0(app_indicator_get_label_guide(ci), "trick-guide") == 0);
00227 
00228        g_object_unref(G_OBJECT(ci));
00229        return;
00230 }
00231 
00232 void
00233 test_libappindicator_set_menu (void)
00234 {
00235        AppIndicator * ci = app_indicator_new ("my-id",
00236                                               "my-name",
00237                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00238 
00239        g_assert(ci != NULL);
00240 
00241        GtkMenu * menu = GTK_MENU(gtk_menu_new());
00242 
00243        GtkMenuItem * item = GTK_MENU_ITEM(gtk_menu_item_new_with_label("Test Label"));
00244        gtk_menu_shell_append(GTK_MENU_SHELL(menu), GTK_WIDGET(item));
00245        gtk_widget_show(GTK_WIDGET(item));
00246 
00247        app_indicator_set_menu(ci, menu);
00248 
00249        g_assert(app_indicator_get_menu(ci) != NULL);
00250 
00251        GValue serverval = {0};
00252        g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
00253        g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);
00254 
00255        DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
00256        g_assert(server != NULL);
00257 
00258        GValue rootval = {0};
00259        g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
00260        g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
00261        DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
00262        g_assert(root != NULL);
00263 
00264        GList * children = dbusmenu_menuitem_get_children(root);
00265        g_assert(children != NULL);
00266        g_assert(g_list_length(children) == 1);
00267 
00268        const gchar * label = dbusmenu_menuitem_property_get(DBUSMENU_MENUITEM(children->data), DBUSMENU_MENUITEM_PROP_LABEL);
00269        g_assert(label != NULL);
00270        g_assert(g_strcmp0(label, "Test Label") == 0);
00271 
00272        /* Interesting, eh?  We need this because we send out events on the bus
00273           but they don't come back until the idle is run.  So we need those
00274           events to clear before removing the object */
00275        while (g_main_context_pending(NULL)) {
00276               g_main_context_iteration(NULL, TRUE);
00277        }
00278 
00279        g_object_unref(G_OBJECT(ci));
00280        return;
00281 }
00282 
00283 void
00284 label_signals_cb (AppIndicator * appindicator, gchar * label, gchar * guide, gpointer user_data)
00285 {
00286        gint * label_signals_count = (gint *)user_data;
00287        (*label_signals_count)++;
00288        return;
00289 }
00290 
00291 void
00292 label_signals_check (void)
00293 {
00294        while (g_main_context_pending(NULL)) {
00295               g_main_context_iteration(NULL, TRUE);
00296        }
00297 
00298        return;
00299 }
00300 
00301 void
00302 test_libappindicator_label_signals (void)
00303 {
00304        gint label_signals_count = 0;
00305        AppIndicator * ci = app_indicator_new ("my-id",
00306                                               "my-name",
00307                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00308 
00309        g_assert(ci != NULL);
00310        g_assert(app_indicator_get_label(ci) == NULL);
00311        g_assert(app_indicator_get_label_guide(ci) == NULL);
00312 
00313        g_signal_connect(G_OBJECT(ci), APP_INDICATOR_SIGNAL_NEW_LABEL, G_CALLBACK(label_signals_cb), &label_signals_count);
00314 
00315        /* Shouldn't be a signal as it should be stuck in idle */
00316        app_indicator_set_label(ci, "label", "guide");
00317        g_assert(label_signals_count == 0);
00318 
00319        /* Should show up after idle processing */
00320        label_signals_check();
00321        g_assert(label_signals_count == 1);
00322 
00323        /* Shouldn't signal with no change */
00324        label_signals_count = 0;
00325        app_indicator_set_label(ci, "label", "guide");
00326        label_signals_check();
00327        g_assert(label_signals_count == 0);
00328 
00329        /* Change one, we should get one signal */
00330        app_indicator_set_label(ci, "label2", "guide");
00331        label_signals_check();
00332        g_assert(label_signals_count == 1);
00333 
00334        /* Change several times, one signal */
00335        label_signals_count = 0;
00336        app_indicator_set_label(ci, "label1", "guide0");
00337        app_indicator_set_label(ci, "label1", "guide1");
00338        app_indicator_set_label(ci, "label2", "guide2");
00339        app_indicator_set_label(ci, "label3", "guide3");
00340        label_signals_check();
00341        g_assert(label_signals_count == 1);
00342 
00343        /* Clear should signal too */
00344        label_signals_count = 0;
00345        app_indicator_set_label(ci, NULL, NULL);
00346        label_signals_check();
00347        g_assert(label_signals_count == 1);
00348 
00349        return;
00350 }
00351 
00352 void
00353 test_libappindicator_desktop_menu (void)
00354 {
00355        AppIndicator * ci = app_indicator_new ("my-id-desktop-menu",
00356                                               "my-name",
00357                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00358 
00359        g_assert(ci != NULL);
00360        g_assert(app_indicator_get_label(ci) == NULL);
00361        g_assert(app_indicator_get_label_guide(ci) == NULL);
00362 
00363        app_indicator_build_menu_from_desktop(ci, SRCDIR "/test-libappindicator.desktop", "Test Program");
00364 
00365        GValue serverval = {0};
00366        g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
00367        g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);
00368 
00369        DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
00370        g_assert(server != NULL);
00371 
00372        GValue rootval = {0};
00373        g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
00374        g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
00375        DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
00376        g_assert(root != NULL);
00377 
00378        GList * children = dbusmenu_menuitem_get_children(root);
00379        g_assert(children != NULL);
00380        g_assert(g_list_length(children) == 3);
00381 
00382 
00383 
00384        g_object_unref(G_OBJECT(ci));
00385        return;
00386 }
00387 
00388 void
00389 test_libappindicator_desktop_menu_bad (void)
00390 {
00391        AppIndicator * ci = app_indicator_new ("my-id-desktop-menu-bad",
00392                                               "my-name",
00393                                               APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
00394 
00395        g_assert(ci != NULL);
00396        g_assert(app_indicator_get_label(ci) == NULL);
00397        g_assert(app_indicator_get_label_guide(ci) == NULL);
00398 
00399        app_indicator_build_menu_from_desktop(ci, SRCDIR "/test-libappindicator.desktop", "Not Test Program");
00400 
00401        GValue serverval = {0};
00402        g_value_init(&serverval, DBUSMENU_TYPE_SERVER);
00403        g_object_get_property(G_OBJECT(ci), "dbus-menu-server", &serverval);
00404 
00405        DbusmenuServer * server = DBUSMENU_SERVER(g_value_get_object(&serverval));
00406        g_assert(server != NULL);
00407 
00408        GValue rootval = {0};
00409        g_value_init(&rootval, DBUSMENU_TYPE_MENUITEM);
00410        g_object_get_property(G_OBJECT(server), DBUSMENU_SERVER_PROP_ROOT_NODE, &rootval);
00411        DbusmenuMenuitem * root = DBUSMENU_MENUITEM(g_value_get_object(&rootval));
00412        g_assert(root != NULL);
00413 
00414        GList * children = dbusmenu_menuitem_get_children(root);
00415        g_assert(g_list_length(children) == 0);
00416 
00417        g_object_unref(G_OBJECT(ci));
00418        return;
00419 }
00420 
00421 void
00422 test_libappindicator_props_suite (void)
00423 {
00424        g_test_add_func ("/indicator-application/libappindicator/init",            test_libappindicator_init);
00425        g_test_add_func ("/indicator-application/libappindicator/init_props",      test_libappindicator_init_with_props);
00426        g_test_add_func ("/indicator-application/libappindicator/init_set_props",  test_libappindicator_init_set_props);
00427        g_test_add_func ("/indicator-application/libappindicator/prop_signals",    test_libappindicator_prop_signals);
00428        g_test_add_func ("/indicator-application/libappindicator/set_label",       test_libappindicator_set_label);
00429        g_test_add_func ("/indicator-application/libappindicator/set_menu",        test_libappindicator_set_menu);
00430        g_test_add_func ("/indicator-application/libappindicator/label_signals",   test_libappindicator_label_signals);
00431        g_test_add_func ("/indicator-application/libappindicator/desktop_menu",    test_libappindicator_desktop_menu);
00432        g_test_add_func ("/indicator-application/libappindicator/desktop_menu_bad",test_libappindicator_desktop_menu_bad);
00433 
00434        return;
00435 }
00436 
00437 gint
00438 main (gint argc, gchar * argv[])
00439 {
00440        gtk_init(&argc, &argv);
00441        g_test_init(&argc, &argv, NULL);
00442 
00443        /* Test suites */
00444        test_libappindicator_props_suite();
00445 
00446 
00447        return g_test_run ();
00448 }