Back to index

libindicator  12.10.0
test-loader.c
Go to the documentation of this file.
00001 /*
00002 Test for libindicator
00003 
00004 Copyright 2009 Canonical Ltd.
00005 
00006 Authors:
00007     Ted Gould <ted@canonical.com>
00008 
00009 This library is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 version 3.0 as published by the Free Software Foundation.
00012 
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License version 3.0 for more details.
00017 
00018 You should have received a copy of the GNU General Public
00019 License along with this library. If not, see
00020 <http://www.gnu.org/licenses/>.
00021 */
00022 
00023 #include <gtk/gtk.h>
00024 #include "libindicator/indicator-object.h"
00025 
00026 #include "dummy-indicator-entry-func.h"
00027 
00028 void
00029 entry_func_swap (IndicatorObject * io)
00030 {
00031        static void (*saved_func) (IndicatorObject * io, IndicatorObjectEntry * entry, guint windowid, guint timestamp) = NULL;
00032        IndicatorObjectClass * klass = INDICATOR_OBJECT_GET_CLASS(io);
00033 
00034        if (saved_func == NULL) {
00035               saved_func = klass->entry_activate_window;
00036        }
00037 
00038        if (klass->entry_activate_window == NULL) {
00039               klass->entry_activate_window = saved_func;
00040        } else {
00041               klass->entry_activate_window = NULL;
00042        }
00043 
00044        return;
00045 }
00046 
00047 void
00048 test_loader_entry_func_window (void)
00049 {
00050        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-entry-func.so");
00051        g_assert(object != NULL);
00052 
00053        DummyIndicatorEntryFunc * entryfunc = (DummyIndicatorEntryFunc *)(object);
00054 
00055        entryfunc->entry_activate_called = FALSE;
00056        entryfunc->entry_activate_window_called = FALSE;
00057        entryfunc->entry_close_called = FALSE;
00058 
00059        entry_func_swap(object);
00060        indicator_object_entry_activate_window(object, NULL, 0, 0);
00061        g_assert(entryfunc->entry_activate_called);
00062 
00063        entry_func_swap(object);
00064        indicator_object_entry_activate_window(object, NULL, 0, 0);
00065        g_assert(entryfunc->entry_activate_window_called);
00066 
00067        g_object_unref(object);
00068 
00069        return;
00070 }
00071 
00072 void
00073 test_loader_entry_funcs (void)
00074 {
00075        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-entry-func.so");
00076        g_assert(object != NULL);
00077 
00078        DummyIndicatorEntryFunc * entryfunc = (DummyIndicatorEntryFunc *)(object);
00079 
00080        entryfunc->entry_activate_called = FALSE;
00081        entryfunc->entry_activate_window_called = FALSE;
00082        entryfunc->entry_close_called = FALSE;
00083 
00084        indicator_object_entry_activate(object, NULL, 0);
00085        g_assert(entryfunc->entry_activate_called);
00086 
00087        indicator_object_entry_activate_window(object, NULL, 0, 0);
00088        g_assert(entryfunc->entry_activate_window_called);
00089 
00090        indicator_object_entry_close(object, NULL, 0);
00091        g_assert(entryfunc->entry_close_called);
00092 
00093        g_object_unref(object);
00094 
00095        return;
00096 }
00097 
00098 void destroy_cb (gpointer data, GObject * object);
00099 
00100 void
00101 entry_change_cb (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer data)
00102 {
00103        IndicatorObjectEntry *other_entry = data;
00104        other_entry->name_hint = entry->name_hint;
00105        other_entry->parent_object = entry->parent_object;
00106        return;
00107 }
00108 
00109 void
00110 entry_move_cb (IndicatorObject * io, IndicatorObjectEntry * entry, gint old, gint new, gpointer data)
00111 {
00112        return entry_change_cb(io, entry, data);
00113 }
00114 
00115 void
00116 test_loader_filename_dummy_signaler (void)
00117 {
00118        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-signaler.so");
00119        g_assert(object != NULL);
00120 
00121        IndicatorObjectEntry *added_entry, *moved_entry, *removed_entry;
00122        IndicatorObjectEntry entries[3];
00123 
00124        added_entry = &entries[0];
00125        moved_entry = &entries[1];
00126        removed_entry = &entries[2];
00127 
00128        g_signal_connect_after(G_OBJECT(object), INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED,   G_CALLBACK(entry_change_cb), added_entry);
00129        g_signal_connect_after(G_OBJECT(object), INDICATOR_OBJECT_SIGNAL_ENTRY_MOVED,   G_CALLBACK(entry_move_cb), moved_entry);
00130        g_signal_connect_after(G_OBJECT(object), INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED, G_CALLBACK(entry_change_cb), removed_entry);
00131 
00132        GList * list = indicator_object_get_entries(object);
00133        g_assert(list != NULL);
00134        g_list_free(list);
00135 
00136        while (g_main_context_pending(NULL)) {
00137               g_main_context_iteration(NULL, TRUE);
00138        }
00139 
00140        g_assert(g_strcmp0(added_entry->name_hint, "added") == 0);
00141        g_assert(g_strcmp0(removed_entry->name_hint, "removed") == 0);
00142        g_assert(g_strcmp0(moved_entry->name_hint, "moved") == 0);
00143 
00144        g_assert(added_entry->parent_object == object);
00145        g_assert(removed_entry->parent_object == NULL);
00146 
00147        g_object_unref(object);
00148 
00149        return;
00150 }
00151 
00152 /***
00153 ****
00154 ***/
00155 
00156 static void
00157 visible_entry_added (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer box)
00158 {
00159        // make a frame for the entry, and add the frame to the box
00160        GtkWidget * frame = gtk_frame_new (NULL);
00161        GtkWidget * child = GTK_WIDGET(entry->label);
00162        g_assert (child != NULL);
00163        gtk_container_add (GTK_CONTAINER(frame), child);
00164        gtk_box_pack_start (GTK_BOX(box), frame, FALSE, FALSE, 0);
00165        g_object_set_data (G_OBJECT(child), "frame-parent", frame);
00166 }
00167 
00168 static void
00169 visible_entry_removed (IndicatorObject * io, IndicatorObjectEntry * entry, gpointer box)
00170 {
00171        // destroy this entry's frame
00172        gpointer parent = g_object_steal_data (G_OBJECT(entry->label), "frame-parent");
00173        if (GTK_IS_WIDGET(parent))
00174               gtk_widget_destroy(GTK_WIDGET(parent));
00175 }
00176 
00177 void
00178 test_loader_filename_dummy_visible (void)
00179 {
00180        const GQuark is_hidden_quark = g_quark_from_static_string ("is-hidden");
00181        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-visible.so");
00182        g_assert(object != NULL);
00183 
00184        // create our local parent widgetry
00185 #if GTK_CHECK_VERSION(3,0,0)
00186        GtkWidget * box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
00187 #else
00188        GtkWidget * box = gtk_hbox_new (TRUE, 0);
00189 #endif
00190        g_signal_connect(object, INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED,
00191                         G_CALLBACK(visible_entry_added), box);
00192        g_signal_connect(object, INDICATOR_OBJECT_SIGNAL_ENTRY_REMOVED,
00193                         G_CALLBACK(visible_entry_removed), box);
00194 
00195        // on startup, DummyVisible has one entry and it has a label
00196        GList * list = indicator_object_get_entries(object);
00197        g_assert(g_list_length(list) == 1);
00198        IndicatorObjectEntry * entry = list->data;
00199        g_assert(entry != NULL);
00200        g_list_free(list);
00201        g_assert(GTK_IS_LABEL(entry->label));
00202        g_assert(entry->parent_object == object);
00203        g_assert(INDICATOR_IS_OBJECT(entry->parent_object));
00204        GtkWidget * label = GTK_WIDGET(entry->label);
00205         g_assert(g_object_get_qdata(G_OBJECT(label), is_hidden_quark) == NULL);
00206 
00207        // add the inital entry to our local parent widgetry
00208        visible_entry_added (object, entry, box);
00209        entry = NULL;
00210        list = gtk_container_get_children (GTK_CONTAINER(box));
00211        g_assert(g_list_length(list) == 1);
00212        g_list_free(list);
00213        
00214        // hide the entries and confirm that the label survived
00215        indicator_object_set_visible (object, FALSE);
00216        while (g_main_context_pending(NULL))
00217               g_main_context_iteration(NULL, TRUE);
00218        g_assert(GTK_IS_LABEL(label));
00219         g_assert(g_object_get_qdata(G_OBJECT(label), is_hidden_quark) != NULL);
00220        list = gtk_container_get_children (GTK_CONTAINER(box));
00221        g_assert(g_list_length(list) == 0);
00222        g_list_free(list);
00223 
00224        // restore the entries and confirm that the label survived
00225        indicator_object_set_visible (object, TRUE);
00226        while (g_main_context_pending(NULL))
00227               g_main_context_iteration(NULL, TRUE);
00228        g_assert(GTK_IS_LABEL(label));
00229         g_assert(g_object_get_qdata(G_OBJECT(label), is_hidden_quark) == NULL);
00230        list = gtk_container_get_children (GTK_CONTAINER(box));
00231        g_assert(g_list_length(list) == 1);
00232        g_list_free(list);
00233 
00234        // cleanup
00235        g_object_unref(object);
00236        gtk_widget_destroy(box);
00237 }
00238 
00239 /***
00240 ****
00241 ***/
00242 
00243 void
00244 test_loader_filename_dummy_simple_location (void)
00245 {
00246        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-simple.so");
00247        g_assert(object != NULL);
00248 
00249        GList * entries = indicator_object_get_entries(object);
00250        g_assert(entries != NULL);
00251        g_assert(g_list_length(entries) == 1);
00252 
00253        IndicatorObjectEntry *entry = entries->data;
00254 
00255        g_assert(indicator_object_get_location(object, entry) == 0);
00256        g_assert(indicator_object_get_location(object, NULL) == 0);
00257        g_assert(entry->parent_object == object);
00258 
00259        g_object_unref(object);
00260 
00261        return;
00262 }
00263 
00264 void
00265 test_loader_filename_dummy_simple_accessors (void)
00266 {
00267        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-simple.so");
00268        g_assert(object != NULL);
00269 
00270        g_assert(indicator_object_get_entries(object) != NULL);
00271 
00272        g_object_unref(object);
00273 
00274        return;
00275 }
00276 
00277 void
00278 test_loader_filename_dummy_simple (void)
00279 {
00280        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-simple.so");
00281        g_assert(object != NULL);
00282 
00283        gboolean unreffed = FALSE;
00284        g_object_weak_ref(G_OBJECT(object), destroy_cb, &unreffed);
00285 
00286        g_object_unref(object);
00287        g_assert(unreffed == TRUE);
00288 
00289        return;
00290 }
00291 
00292 void
00293 test_loader_filename_dummy_blank (void)
00294 {
00295        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-blank.so");
00296        g_assert(object == NULL);
00297        return;
00298 }
00299 
00300 void
00301 test_loader_filename_dummy_null (void)
00302 {
00303        IndicatorObject * object = indicator_object_new_from_file(BUILD_DIR "/.libs/libdummy-indicator-null.so");
00304        g_assert(object != NULL);
00305        g_assert(indicator_object_get_entries(object) == NULL);
00306        g_object_unref(G_OBJECT(object));
00307        return;
00308 }
00309 
00310 void
00311 test_loader_filename_bad (void)
00312 {
00313        IndicatorObject * object = indicator_object_new_from_file("/this/file/should/not/exist.so");
00314        g_assert(object == NULL);
00315        return;
00316 }
00317 
00318 void
00319 destroy_cb (gpointer data, GObject * object)
00320 {
00321        gboolean * bob = (gboolean *)data;
00322        *bob = TRUE;
00323        return;
00324 }
00325 
00326 void
00327 test_loader_refunref (void)
00328 {
00329        GObject * object = g_object_new(INDICATOR_OBJECT_TYPE, NULL);
00330 
00331        gboolean unreffed = FALSE;
00332        g_object_weak_ref(object, destroy_cb, &unreffed);
00333 
00334        g_object_unref(object);
00335 
00336        g_assert(unreffed == TRUE);
00337 
00338        return;
00339 }
00340 
00341 void
00342 test_loader_creation_deletion_suite (void)
00343 {
00344        g_test_add_func ("/libindicator/loader/ref_and_unref", test_loader_refunref);
00345        g_test_add_func ("/libindicator/loader/filename_bad",  test_loader_filename_bad);
00346        g_test_add_func ("/libindicator/loader/dummy/null_load",  test_loader_filename_dummy_null);
00347        g_test_add_func ("/libindicator/loader/dummy/blank_load",  test_loader_filename_dummy_null);
00348        g_test_add_func ("/libindicator/loader/dummy/simple_load",  test_loader_filename_dummy_simple);
00349        g_test_add_func ("/libindicator/loader/dummy/simple_accessors", test_loader_filename_dummy_simple_accessors);
00350        g_test_add_func ("/libindicator/loader/dummy/simple_location", test_loader_filename_dummy_simple_location);
00351        g_test_add_func ("/libindicator/loader/dummy/signaler",  test_loader_filename_dummy_signaler);
00352        g_test_add_func ("/libindicator/loader/dummy/entry_funcs",  test_loader_entry_funcs);
00353        g_test_add_func ("/libindicator/loader/dummy/entry_func_window",  test_loader_entry_func_window);
00354        g_test_add_func ("/libindicator/loader/dummy/visible",  test_loader_filename_dummy_visible);
00355 
00356        return;
00357 }
00358 
00359 
00360 int
00361 main (int argc, char ** argv)
00362 {
00363        g_type_init (); 
00364        g_test_init (&argc, &argv, NULL);
00365        gtk_init(&argc, &argv);
00366 
00367        test_loader_creation_deletion_suite();
00368 
00369        g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
00370 
00371        return g_test_run();
00372 }