Back to index

bamf  0.2.120
test-view.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009-2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as 
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by Neil Jagdish Patel <neil.patel@canonical.com>
00017  *             Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *
00019  */
00020 
00021 #include <glib.h>
00022 #include <stdlib.h>
00023 #include "bamf-view.h"
00024 
00025 static void test_active              (void);
00026 static void test_active_event        (void);
00027 static void test_allocation          (void);
00028 static void test_child_added_event   (void);
00029 static void test_child_removed_event (void);
00030 static void test_children            (void);
00031 static void test_children_paths      (void);
00032 static void test_closed_event        (void);
00033 static void test_name                (void);
00034 static void test_path                (void);
00035 static void test_path_collision      (void);
00036 static void test_running             (void);
00037 static void test_running_event       (void);
00038 static void test_parent_child_out_of_order_unref (void);
00039 
00040 static GDBusConnection *gdbus_connection = NULL;
00041 
00042 void
00043 test_view_create_suite (GDBusConnection *connection)
00044 {
00045 #define DOMAIN "/View"
00046 
00047   gdbus_connection = connection;
00048 
00049   g_test_add_func (DOMAIN"/Allocation", test_allocation);
00050   g_test_add_func (DOMAIN"/Name", test_name);
00051   g_test_add_func (DOMAIN"/Active", test_active);
00052   g_test_add_func (DOMAIN"/Running", test_running);
00053   g_test_add_func (DOMAIN"/Path", test_path);
00054   g_test_add_func (DOMAIN"/Path/Collision", test_path_collision);
00055   g_test_add_func (DOMAIN"/Events/Close", test_closed_event);
00056   g_test_add_func (DOMAIN"/Events/Active", test_active_event);
00057   g_test_add_func (DOMAIN"/Events/Running", test_running_event);
00058   g_test_add_func (DOMAIN"/Events/ChildAdded", test_child_added_event);
00059   g_test_add_func (DOMAIN"/Events/ChildRemoved", test_child_removed_event);
00060   g_test_add_func (DOMAIN"/Children", test_children);
00061   g_test_add_func (DOMAIN"/Children/Paths", test_children_paths);
00062   g_test_add_func (DOMAIN"/Children/UnrefOrder", test_parent_child_out_of_order_unref);
00063 }
00064 
00065 static void
00066 test_allocation (void)
00067 {
00068   BamfView    *view;
00069 
00070   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00071   g_assert (BAMF_IS_VIEW (view));
00072 
00073   g_object_unref (G_OBJECT (view));
00074   g_assert (!BAMF_IS_VIEW (view));
00075 }
00076 
00077 static void
00078 test_name (void)
00079 {
00080   BamfView *view;
00081 
00082   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00083 
00084   g_assert (bamf_view_get_name (view) == NULL);
00085 
00086   bamf_view_set_name (view, "SomeName");
00087 
00088   g_assert (g_strcmp0 (bamf_view_get_name (view), "SomeName") == 0);
00089 
00090   g_object_unref (view);
00091   g_assert (!BAMF_IS_VIEW (view));
00092 }
00093 
00094 static void
00095 test_active (void)
00096 {
00097   BamfView *view;
00098 
00099   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00100   g_assert (!bamf_view_is_active (view));
00101 
00102   bamf_view_set_active (view, TRUE);
00103   g_assert (bamf_view_is_active (view));
00104 
00105   bamf_view_set_active (view, FALSE);
00106   g_assert (!bamf_view_is_active (view));
00107 
00108   g_object_unref (view);
00109   g_assert (!BAMF_IS_VIEW (view));
00110 }
00111 
00112 static void
00113 test_running (void)
00114 {
00115   BamfView *view;
00116 
00117   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00118   g_assert (!bamf_view_is_running (view));
00119 
00120   bamf_view_set_running (view, TRUE);
00121   g_assert (bamf_view_is_running (view));
00122 
00123   bamf_view_set_running (view, FALSE);
00124   g_assert (!bamf_view_is_running (view));
00125 
00126   g_object_unref (view);
00127   g_assert (!BAMF_IS_VIEW (view));
00128 }
00129 
00130 static void
00131 test_path (void)
00132 {
00133   BamfView *view;
00134   const char *path;
00135 
00136   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00137   g_assert (bamf_view_get_path (view) == NULL);
00138 
00139   path = bamf_view_export_on_bus (view, gdbus_connection);
00140   g_assert (path);
00141   g_assert (g_strcmp0 (path, bamf_view_get_path (view)) == 0);
00142 
00143   g_object_unref (view);
00144   g_assert (!BAMF_IS_VIEW (view));
00145 }
00146 
00147 static void
00148 test_path_collision (void)
00149 {
00150   int i, j;
00151   
00152   for (i = 0; i < 20; i++)
00153     {
00154       GList *views = NULL;
00155 
00156       for (j = 0; j < 2000; j++)
00157         {
00158           BamfView * view = g_object_new (BAMF_TYPE_VIEW, NULL);
00159           g_assert (BAMF_IS_VIEW (view));
00160 
00161           views = g_list_prepend (views, view);
00162 
00163           bamf_view_export_on_bus (view, gdbus_connection);
00164         }
00165 
00166       g_list_free_full (views, g_object_unref);
00167     }
00168 }
00169 
00170 static void
00171 test_children (void)
00172 {
00173   BamfView *parent;
00174   BamfView *child1, *child2, *child3;
00175 
00176   parent = g_object_new (BAMF_TYPE_VIEW, NULL);
00177   child1 = g_object_new (BAMF_TYPE_VIEW, NULL);
00178   child2 = g_object_new (BAMF_TYPE_VIEW, NULL);
00179   child3 = g_object_new (BAMF_TYPE_VIEW, NULL);
00180 
00181   g_assert (bamf_view_get_children (parent) == NULL);
00182 
00183   bamf_view_add_child (parent, child1);
00184   g_assert (g_list_length (bamf_view_get_children (parent)) == 1);
00185   g_assert (g_list_nth_data (bamf_view_get_children (parent), 0) == child1);
00186 
00187   bamf_view_add_child (parent, child2);
00188   bamf_view_add_child (parent, child3);
00189   g_assert (g_list_length (bamf_view_get_children (parent)) == 3);
00190 
00191   bamf_view_close (child1);
00192   g_object_unref (child1);
00193   g_assert (g_list_length (bamf_view_get_children (parent)) == 2);
00194 
00195   bamf_view_close (child2);
00196   g_object_unref (child2);
00197   g_assert (g_list_length (bamf_view_get_children (parent)) == 1);
00198 
00199   bamf_view_close (child3);
00200   g_object_unref (child3);
00201   g_assert (g_list_length (bamf_view_get_children (parent)) == 0);
00202 
00203   bamf_view_close (parent);
00204   g_object_unref (parent);
00205 }
00206 
00207 static void
00208 test_children_paths (void)
00209 {
00210   BamfView *parent;
00211   BamfView *child1, *child2, *child3;
00212   GVariant *container;
00213   GVariantIter *paths;
00214   const char *path;
00215   gboolean found;
00216 
00217   parent = g_object_new (BAMF_TYPE_VIEW, NULL);
00218   child1 = g_object_new (BAMF_TYPE_VIEW, NULL);
00219   child2 = g_object_new (BAMF_TYPE_VIEW, NULL);
00220   child3 = g_object_new (BAMF_TYPE_VIEW, NULL);
00221 
00222   bamf_view_export_on_bus (parent, gdbus_connection);
00223   bamf_view_export_on_bus (child1, gdbus_connection);
00224   bamf_view_export_on_bus (child2, gdbus_connection);
00225 
00226   g_assert (bamf_view_get_children (parent) == NULL);
00227 
00228   bamf_view_add_child (parent, child1);
00229   bamf_view_add_child (parent, child2);
00230   bamf_view_add_child (parent, child3);
00231   g_assert (g_list_length (bamf_view_get_children (parent)) == 3);
00232 
00233   container = bamf_view_get_children_paths (parent);
00234   g_assert (g_variant_type_equal (g_variant_get_type (container),
00235                                   G_VARIANT_TYPE ("(as)")));
00236   g_assert (g_variant_n_children (container) == 1);
00237   g_variant_get (container, "(as)", &paths);
00238   g_assert (g_variant_iter_n_children (paths) == 2);
00239   g_variant_iter_free (paths);
00240   g_variant_unref (container);
00241 
00242   bamf_view_export_on_bus (child3, gdbus_connection);
00243 
00244   container = bamf_view_get_children_paths (parent);
00245   g_variant_get (container, "(as)", &paths);
00246   g_assert (g_variant_iter_n_children (paths) == 3);
00247 
00248   found = FALSE;
00249   while (g_variant_iter_loop (paths, "s", &path))
00250     {
00251       if (g_strcmp0 (path, bamf_view_get_path (child1)) == 0)
00252         {
00253           found = TRUE;
00254           break;
00255         }
00256     }
00257 
00258   g_assert (found);
00259 
00260   found = FALSE;
00261   g_variant_get (container, "(as)", &paths);
00262   while (g_variant_iter_loop (paths, "s", &path))
00263     {
00264       if (g_strcmp0 (path, bamf_view_get_path (child2)) == 0)
00265         {
00266           found = TRUE;
00267           break;
00268         }
00269     }
00270   g_variant_iter_free (paths);
00271 
00272   g_assert (found);
00273 
00274   found = FALSE;
00275   g_variant_get (container, "(as)", &paths);
00276   while (g_variant_iter_loop (paths, "s", &path))
00277     {
00278       if (g_strcmp0 (path, bamf_view_get_path (child3)) == 0)
00279         {
00280           found = TRUE;
00281           break;
00282         }
00283     }
00284   g_variant_iter_free (paths);
00285 
00286   g_assert (found);
00287 
00288   g_variant_unref (container);
00289 
00290   g_object_unref (child1);
00291   g_object_unref (child2);
00292   g_object_unref (child3);
00293   g_object_unref (parent);
00294 }
00295 
00296 static gboolean active_event_fired;
00297 static gboolean active_event_result;
00298 
00299 static void
00300 on_active_event (BamfView *view, gboolean active, gpointer pointer)
00301 {
00302   active_event_fired = TRUE;
00303   active_event_result = active;
00304 }
00305 
00306 static void
00307 test_active_event (void)
00308 {
00309   BamfView *view;
00310 
00311   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00312   g_assert (!bamf_view_is_active (view));
00313 
00314   g_signal_connect (G_OBJECT (view), "active-changed",
00315                   (GCallback) on_active_event, NULL);
00316 
00317   active_event_fired = FALSE;
00318   bamf_view_set_active (view, TRUE);
00319   g_assert (bamf_view_is_active (view));
00320 
00321   g_assert (active_event_fired);
00322   g_assert (active_event_result);
00323 
00324   active_event_fired = FALSE;
00325   bamf_view_set_active (view, FALSE);
00326   g_assert (!bamf_view_is_active (view));
00327 
00328   g_assert (active_event_fired);
00329   g_assert (!active_event_result);
00330 
00331   g_object_unref (view);
00332   g_assert (!BAMF_IS_VIEW (view));
00333 }
00334 
00335 static gboolean running_event_fired;
00336 static gboolean running_event_result;
00337 
00338 static void
00339 on_running_event (BamfView *view, gboolean running, gpointer pointer)
00340 {
00341   running_event_fired = TRUE;
00342   running_event_result = running;
00343 }
00344 
00345 static void
00346 test_running_event (void)
00347 {
00348   BamfView *view;
00349 
00350   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00351   g_assert (!bamf_view_is_running (view));
00352 
00353   g_signal_connect (G_OBJECT (view), "running-changed",
00354                   (GCallback) on_running_event, NULL);
00355 
00356   running_event_fired = FALSE;
00357   bamf_view_set_running (view, TRUE);
00358   g_assert (bamf_view_is_running (view));
00359 
00360   g_assert (running_event_fired);
00361   g_assert (running_event_result);
00362 
00363   running_event_fired = FALSE;
00364   bamf_view_set_running (view, FALSE);
00365   g_assert (!bamf_view_is_running (view));
00366 
00367   g_assert (running_event_fired);
00368   g_assert (!running_event_result);
00369 
00370   g_object_unref (view);
00371   g_assert (!BAMF_IS_VIEW (view));
00372 }
00373 
00374 static gboolean child_added_event_fired;
00375 static char * child_added_event_result;
00376 
00377 static void
00378 on_child_added (BamfView *view, char *path, gpointer pointer)
00379 {
00380   child_added_event_fired = TRUE;
00381   child_added_event_result = g_strdup (path);
00382 }
00383 
00384 static void
00385 test_child_added_event (void)
00386 {
00387   BamfView *parent;
00388   BamfView *child;
00389 
00390   parent = g_object_new (BAMF_TYPE_VIEW, NULL);
00391   child = g_object_new (BAMF_TYPE_VIEW, NULL);
00392 
00393   bamf_view_export_on_bus (parent, gdbus_connection);
00394   bamf_view_export_on_bus (child, gdbus_connection);
00395 
00396   g_signal_connect (G_OBJECT (parent), "child-added",
00397                     (GCallback) on_child_added, NULL);
00398 
00399   child_added_event_fired = FALSE;
00400   bamf_view_add_child (parent, child);
00401 
00402   g_assert (child_added_event_fired);
00403   g_assert (g_strcmp0 (bamf_view_get_path (child), child_added_event_result) == 0);
00404 
00405   bamf_view_close (child);
00406   bamf_view_close (parent);
00407   
00408   g_object_unref (child);
00409   g_object_unref (parent);
00410 }
00411 
00412 static gboolean child_removed_event_fired;
00413 static char * child_removed_event_result;
00414 
00415 static void
00416 on_child_removed (BamfView *view, char *path, gpointer pointer)
00417 {
00418   child_removed_event_fired = TRUE;
00419   child_removed_event_result = g_strdup (path);
00420 }
00421 
00422 static void
00423 test_child_removed_event (void)
00424 {
00425   BamfView *parent;
00426   BamfView *child;
00427 
00428   parent = g_object_new (BAMF_TYPE_VIEW, NULL);
00429   child = g_object_new (BAMF_TYPE_VIEW, NULL);
00430 
00431   bamf_view_export_on_bus (parent, gdbus_connection);
00432   bamf_view_export_on_bus (child, gdbus_connection);
00433   bamf_view_add_child (parent, child);
00434 
00435   g_signal_connect (G_OBJECT (parent), "child-removed",
00436                     (GCallback) on_child_removed, NULL);
00437 
00438   child_removed_event_fired = FALSE;
00439   bamf_view_remove_child (parent, child);
00440 
00441   g_assert (child_removed_event_fired);
00442   g_assert (g_strcmp0 (bamf_view_get_path (child), child_removed_event_result) == 0);
00443 
00444   g_object_unref (child);
00445   g_object_unref (parent);
00446 }
00447 
00448 static gboolean closed_event_fired;
00449 
00450 static void
00451 on_closed (BamfView *view, gpointer pointer)
00452 {
00453   closed_event_fired = TRUE;
00454 }
00455 
00456 static void
00457 test_closed_event (void)
00458 {
00459   BamfView *view;
00460 
00461   view = g_object_new (BAMF_TYPE_VIEW, NULL);
00462   bamf_view_export_on_bus (view, gdbus_connection);
00463 
00464   g_signal_connect (G_OBJECT (view), "closed",
00465                     (GCallback) on_closed, NULL);
00466 
00467   closed_event_fired = FALSE;
00468 
00469   bamf_view_close (view);
00470   g_assert (closed_event_fired);
00471 
00472   g_object_unref (view);
00473 }
00474 
00475 static void
00476 test_parent_child_out_of_order_unref (void)
00477 {
00478   BamfView *parent, *child;
00479 
00480   parent = g_object_new (BAMF_TYPE_VIEW, NULL);
00481   child = g_object_new (BAMF_TYPE_VIEW, NULL);
00482 
00483   bamf_view_export_on_bus (parent, gdbus_connection);
00484   bamf_view_export_on_bus (child, gdbus_connection);
00485 
00486   bamf_view_add_child (parent, child);
00487 
00488   g_object_unref (parent);
00489   g_object_unref (child);
00490 }