Back to index

unity  6.0.0
TestPanelService.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  *
00019  */
00020 
00021 
00022 #include "config.h"
00023 
00024 #include "panel-service.h"
00025 #include <libindicator/indicator-object.h>
00026 
00027 //----------------------- TESTING INDICATOR STUFF -----------------------------
00028 
00029 #define TEST_TYPE_OBJECT (test_object_get_type ())
00030 
00031 #define TEST_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),\
00032   TEST_TYPE_OBJECT, TestObject))
00033 
00034 #define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),\
00035   TEST_TYPE_OBJECT, TestObjectClass))
00036 
00037 #define TEST_IS_OBJECT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),\
00038   TEST_TYPE_OBJECT))
00039 
00040 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),\
00041   TEST_TYPE_OBJECT))
00042 
00043 #define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),\
00044   TEST_TYPE_OBJECT, TestObjectClass))
00045 
00046 typedef struct _TestObject        TestObject;
00047 typedef struct _TestObjectClass   TestObjectClass;
00048 
00049 struct _TestObject
00050 {
00051   IndicatorObject parent;
00052 
00053   GList* entries;
00054 };
00055 
00056 struct _TestObjectClass
00057 {
00058   IndicatorObjectClass parent_class;
00059 };
00060 
00061 GType             test_object_get_type(void) G_GNUC_CONST;
00062 
00063 IndicatorObject*       test_object_new();
00064 
00065 IndicatorObjectEntry* test_object_add_entry(TestObject*  self,
00066                                             const gchar* label,
00067                                             const gchar* icon_name);
00068 
00069 //--- .c
00070 
00071 G_DEFINE_TYPE(TestObject, test_object, INDICATOR_OBJECT_TYPE);
00072 
00073 void
00074 test_object_dispose(GObject* object)
00075 {
00076   TestObject* self = TEST_OBJECT(object);
00077 
00078   GList* e;
00079   for (e = self->entries; e; e = e->next)
00080   {
00081     g_slice_free(IndicatorObjectEntry, e->data);
00082   }
00083   g_list_free(self->entries);
00084   self->entries = NULL;
00085 
00086   G_OBJECT_CLASS(test_object_parent_class)->dispose(object);
00087 }
00088 
00089 GList*
00090 test_object_get_entries(IndicatorObject* io)
00091 {
00092   g_return_val_if_fail(TEST_IS_OBJECT(io), NULL);
00093 
00094   return g_list_copy(TEST_OBJECT(io)->entries);
00095 }
00096 
00097 guint
00098 test_object_get_location(IndicatorObject*      io,
00099                          IndicatorObjectEntry* entry)
00100 {
00101   g_return_val_if_fail(TEST_IS_OBJECT(io), 0);
00102 
00103   return g_list_index(TEST_OBJECT(io)->entries, entry);
00104 }
00105 
00106 void
00107 test_object_entry_activate(IndicatorObject*      io,
00108                            IndicatorObjectEntry* entry,
00109                            guint                 timestamp)
00110 {
00111 
00112 }
00113 
00114 void
00115 test_object_class_init(TestObjectClass* klass)
00116 {
00117   GObjectClass*         obj_class = G_OBJECT_CLASS(klass);
00118   IndicatorObjectClass* ind_class = INDICATOR_OBJECT_CLASS(klass);
00119 
00120   obj_class->dispose = test_object_dispose;
00121 
00122   ind_class->get_entries    = test_object_get_entries;
00123   ind_class->get_location   = test_object_get_location;
00124   ind_class->entry_activate = test_object_entry_activate;
00125 }
00126 
00127 void
00128 test_object_init(TestObject* self)
00129 {
00130 
00131 }
00132 
00133 IndicatorObject*
00134 test_object_new()
00135 {
00136   return (IndicatorObject*)g_object_new(TEST_TYPE_OBJECT, NULL);
00137 }
00138 
00139 IndicatorObjectEntry*
00140 test_object_add_entry(TestObject*  self,
00141                       const gchar* label,
00142                       const gchar* icon_name)
00143 {
00144   IndicatorObjectEntry* entry;
00145   g_return_val_if_fail(TEST_IS_OBJECT(self), NULL);
00146 
00147   entry = g_slice_new0(IndicatorObjectEntry);
00148   entry->label = (GtkLabel*)gtk_label_new(label);
00149   entry->image = icon_name ? (GtkImage*)gtk_image_new_from_icon_name(icon_name,
00150                                                                      GTK_ICON_SIZE_MENU) : NULL;
00151   entry->menu = NULL;
00152 
00153   self->entries = g_list_append(self->entries, entry);
00154 
00155   g_signal_emit(self, INDICATOR_OBJECT_SIGNAL_ENTRY_ADDED_ID, 0, entry, TRUE);
00156 
00157   return entry;
00158 }
00159 
00160 void
00161 test_object_show_entry(TestObject*           self,
00162                        IndicatorObjectEntry* entry,
00163                        guint                 timestamp)
00164 {
00165   g_signal_emit(self, INDICATOR_OBJECT_SIGNAL_MENU_SHOW_ID, 0, entry, timestamp);
00166 }
00167 
00168 //----------------------- /TESTING INDICATOR STUFF ----------------------------
00169 
00170 //------------------------ USEFUL FUNCTIONS -----------------------------------
00171 
00172 guint
00173 get_n_indicators_in_result(GVariant* result)
00174 {
00175   guint  ret = 0;
00176   gchar* current_object_id = NULL;
00177   GVariantIter* iter;
00178   gchar*        indicator_id;
00179   gchar*        entry_id;
00180   gchar*        entry_name_hint;
00181   gchar*        label;
00182   gboolean      label_sensitive;
00183   gboolean      label_visible;
00184   guint32       image_type;
00185   gchar*        image_data;
00186   gboolean      image_sensitive;
00187   gboolean      image_visible;
00188 
00189   g_variant_get(result, "(a(ssssbbusbbi))", &iter);
00190   while (g_variant_iter_loop(iter, "(ssssbbusbbi)",
00191                              &indicator_id,
00192                              &entry_id,
00193                              &entry_name_hint,
00194                              &label,
00195                              &label_sensitive,
00196                              &label_visible,
00197                              &image_type,
00198                              &image_data,
00199                              &image_sensitive,
00200                              &image_visible))
00201   {
00202     if (g_strcmp0(current_object_id, indicator_id) != 0)
00203     {
00204       ret++;
00205       g_free(current_object_id);
00206       current_object_id = g_strdup(indicator_id);
00207     }
00208   }
00209   g_free(current_object_id);
00210 
00211   return ret;
00212 }
00213 
00214 guint
00215 get_n_entries_in_result(GVariant* result)
00216 {
00217   guint  ret = 0;
00218   GVariantIter* iter;
00219   gchar*        indicator_id;
00220   gchar*        entry_id;
00221   gchar*        entry_name_hint;
00222   gchar*        label;
00223   gboolean      label_sensitive;
00224   gboolean      label_visible;
00225   guint32       image_type;
00226   gchar*        image_data;
00227   gboolean      image_sensitive;
00228   gboolean      image_visible;
00229   gboolean      priority;
00230 
00231   g_variant_get(result, "(a(ssssbbusbbi))", &iter);
00232   while (g_variant_iter_loop(iter, "(ssssbbusbbi)",
00233                              &indicator_id,
00234                              &entry_id,
00235                              &entry_name_hint,
00236                              &label,
00237                              &label_sensitive,
00238                              &label_visible,
00239                              &image_type,
00240                              &image_data,
00241                              &image_sensitive,
00242                              &image_visible,
00243                              &priority))
00244   {
00245     if (g_strcmp0(entry_id, "") != 0)
00246       ret++;
00247   }
00248 
00249   return ret;
00250 }
00251 
00252 
00253 
00254 //------------------------ /USEFUL FUNCTIONS ----------------------------------
00255 
00256 static void TestAllocation(void);
00257 static void TestIndicatorLoading(void);
00258 static void TestEmptyIndicatorObject(void);
00259 static void TestEntryAddition(void);
00260 static void TestEntryActivateRequest(void);
00261 
00262 void
00263 TestPanelServiceCreateSuite()
00264 {
00265 #define _DOMAIN "/Unit/PanelService"
00266 
00267   g_test_add_func(_DOMAIN"/Allocation", TestAllocation);
00268   g_test_add_func(_DOMAIN"/IndicatorLoading", TestIndicatorLoading);
00269   g_test_add_func(_DOMAIN"/EmptyIndicatorObject", TestEmptyIndicatorObject);
00270   g_test_add_func(_DOMAIN"/EntryAddition", TestEntryAddition);
00271   g_test_add_func(_DOMAIN"/EntryActivateRequest", TestEntryActivateRequest);
00272 }
00273 
00274 static void
00275 TestAllocation()
00276 {
00277   PanelService* service;
00278 
00279   service = panel_service_get_default();
00280   g_assert(PANEL_IS_SERVICE(service));
00281 
00282   g_object_unref(service);
00283   g_assert(PANEL_IS_SERVICE(service) == FALSE);
00284 }
00285 
00286 static void
00287 TestIndicatorLoading()
00288 {
00289   PanelService*    service;
00290   IndicatorObject* object;
00291   GList*           objects = NULL;
00292 
00293   object = test_object_new();
00294   g_assert(INDICATOR_IS_OBJECT(object));
00295   objects = g_list_append(objects, object);
00296 
00297   service = panel_service_get_default_with_indicators(objects);
00298   g_assert(PANEL_IS_SERVICE(service));
00299 
00300   g_assert_cmpint(panel_service_get_n_indicators(service), == , 1);
00301 
00302   g_list_free(objects);
00303   g_object_unref(object);
00304   g_object_unref(service);
00305 }
00306 
00307 static void
00308 TestEmptyIndicatorObject()
00309 {
00310   PanelService*    service;
00311   IndicatorObject* object;
00312   GList*           objects = NULL;
00313   GVariant*        result;
00314 
00315   object = test_object_new();
00316   g_assert(INDICATOR_IS_OBJECT(object));
00317   objects = g_list_append(objects, object);
00318 
00319   service = panel_service_get_default_with_indicators(objects);
00320   g_assert(PANEL_IS_SERVICE(service));
00321 
00322   g_assert_cmpint(panel_service_get_n_indicators(service), == , 1);
00323 
00324   result = panel_service_sync(service);
00325   g_assert(result != NULL);
00326 
00327   g_assert_cmpint(get_n_indicators_in_result(result), == , 1);
00328 
00329   g_variant_unref(result);
00330   g_list_free(objects);
00331   g_object_unref(object);
00332   g_object_unref(service);
00333 }
00334 
00335 static void
00336 TestEntryAddition()
00337 {
00338   PanelService* service;
00339   TestObject*   object;
00340   GList*        objects = NULL;
00341   GVariant*     result;
00342   int           i;
00343 
00344   object = (TestObject*)test_object_new();
00345   test_object_add_entry(object, "Hello", "gtk-apply");
00346   g_assert(INDICATOR_IS_OBJECT(object));
00347   objects = g_list_append(objects, object);
00348 
00349   service = panel_service_get_default_with_indicators(objects);
00350   g_assert(PANEL_IS_SERVICE(service));
00351 
00352   result = panel_service_sync(service);
00353   g_assert(result != NULL);
00354   g_assert_cmpint(get_n_entries_in_result(result), == , 1);
00355 
00356   for (i = 2; i < 10; i++)
00357   {
00358     g_variant_unref(result);
00359 
00360     test_object_add_entry(object, "Bye", "gtk-forward");
00361     result = panel_service_sync(service);
00362     g_assert_cmpint(get_n_entries_in_result(result), == , i);
00363   }
00364 
00365   g_variant_unref(result);
00366   g_list_free(objects);
00367   g_object_unref(object);
00368   g_object_unref(service);
00369 }
00370 
00371 static void
00372 OnEntryActivateRequest(IndicatorObject* object,
00373                        const gchar*     entry_id,
00374                        const gchar*     entry_id_should_be)
00375 {
00376   g_assert_cmpstr(entry_id, == , entry_id_should_be);
00377 }
00378 
00379 static void
00380 TestEntryActivateRequest()
00381 {
00382   PanelService* service;
00383   TestObject*   object;
00384   GList*        objects = NULL;
00385   IndicatorObjectEntry* entry;
00386   gchar*        id;
00387 
00388   object = (TestObject*)test_object_new();
00389   entry = test_object_add_entry(object, "Hello", "gtk-apply");
00390   id = g_strdup_printf("%p", entry);
00391   g_assert(INDICATOR_IS_OBJECT(object));
00392   objects = g_list_append(objects, object);
00393 
00394   service = panel_service_get_default_with_indicators(objects);
00395   g_assert(PANEL_IS_SERVICE(service));
00396 
00397   g_signal_connect(service, "entry-activate-request",
00398                    G_CALLBACK(OnEntryActivateRequest),
00399                    id);
00400 
00401   test_object_show_entry(object, entry, 1234);
00402 
00403   g_free(id);
00404   g_list_free(objects);
00405   g_object_unref(object);
00406   g_object_unref(service);
00407 }