Back to index

bamf  0.2.120
test-application.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 Jason Smith <jason.smith@canonical.com>
00017  *             Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <glib.h>
00023 #include <glib-object.h>
00024 #include "bamf-application.h"
00025 #include "bamf-window.h"
00026 #include "bamf-legacy-window.h"
00027 #include "bamf-legacy-window-test.h"
00028 
00029 #define DESKTOP_FILE "/usr/share/applications/gnome-terminal.desktop"
00030 
00031 static void test_allocation          (void);
00032 static void test_desktop_file        (void);
00033 static void test_urgent              (void);
00034 static void test_active              (void);
00035 static void test_get_xids            (void);
00036 static void test_manages_xid         (void);
00037 static void test_user_visible        (void);
00038 static void test_urgent              (void);
00039 static void test_window_added        (void);
00040 static void test_window_removed      (void);
00041 
00042 static gboolean          signal_seen   = FALSE;
00043 static gboolean          signal_result = FALSE;
00044 static char *            signal_window = NULL;
00045 static GDBusConnection * gdbus_connection = NULL;
00046 
00047 void
00048 test_application_create_suite (GDBusConnection *connection)
00049 {
00050 #define DOMAIN "/Application"
00051 
00052   gdbus_connection = connection;
00053 
00054   g_test_add_func (DOMAIN"/Allocation", test_allocation);
00055   g_test_add_func (DOMAIN"/DesktopFile", test_desktop_file);
00056   g_test_add_func (DOMAIN"/ManagesXid", test_manages_xid);
00057   g_test_add_func (DOMAIN"/Xids", test_get_xids);
00058   g_test_add_func (DOMAIN"/Events/Active", test_active);
00059   g_test_add_func (DOMAIN"/Events/Urgent", test_urgent);
00060   g_test_add_func (DOMAIN"/Events/UserVisible", test_user_visible);
00061   g_test_add_func (DOMAIN"/Events/WindowAdded", test_window_added);
00062   g_test_add_func (DOMAIN"/Events/WindowRemoved", test_window_removed);
00063 }
00064 
00065 static void
00066 test_allocation (void)
00067 {
00068   BamfApplication    *application;
00069 
00070   /* Check it allocates */
00071   application = bamf_application_new ();
00072   g_assert (BAMF_IS_APPLICATION (application));
00073 
00074   g_object_unref (application);
00075   g_assert (!BAMF_IS_APPLICATION (application));
00076 
00077   application = bamf_application_new_from_desktop_file (DESKTOP_FILE);
00078   g_assert (BAMF_IS_APPLICATION (application));
00079 
00080   g_object_unref (application);
00081   g_assert (!BAMF_IS_APPLICATION (application));
00082 }
00083 
00084 static void
00085 test_desktop_file (void)
00086 {
00087   BamfApplication    *application;
00088 
00089   /* Check it allocates */
00090   application = bamf_application_new ();
00091   g_assert (bamf_application_get_desktop_file (application) == NULL);
00092 
00093   bamf_application_set_desktop_file (application, DESKTOP_FILE);
00094   g_assert (g_strcmp0 (bamf_application_get_desktop_file (application), DESKTOP_FILE) == 0);
00095 
00096   g_object_unref (application);
00097 
00098   application = bamf_application_new_from_desktop_file (DESKTOP_FILE);
00099   g_assert (g_strcmp0 (bamf_application_get_desktop_file (application), DESKTOP_FILE) == 0);
00100 
00101   g_object_unref (application);
00102 }
00103 
00104 static void
00105 on_urgent_changed (BamfApplication *application, gboolean result, gpointer data)
00106 {
00107   signal_seen = TRUE;
00108   signal_result = result;
00109 }
00110 
00111 static void
00112 test_urgent (void)
00113 {
00114   signal_seen = FALSE;
00115   
00116   BamfApplication *application;
00117   BamfWindow *window1, *window2;
00118   BamfLegacyWindowTest *test1, *test2;
00119   
00120   application = bamf_application_new ();
00121   
00122   g_signal_connect (G_OBJECT (application), "urgent-changed", (GCallback) on_urgent_changed, NULL);
00123   
00124   test1 = bamf_legacy_window_test_new (20, "Window X", "class", "exec");
00125   test2 = bamf_legacy_window_test_new (20, "Window Y", "class", "exec");
00126   
00127   window1 = bamf_window_new (BAMF_LEGACY_WINDOW (test1));
00128   window2 = bamf_window_new (BAMF_LEGACY_WINDOW (test2));
00129   
00130   // Ensure we are not visible with no windows
00131   g_assert (!bamf_view_is_urgent (BAMF_VIEW (application)));
00132   
00133   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00134   
00135   // Test that when added, we signaled properly
00136   g_assert (!bamf_view_is_urgent (BAMF_VIEW (application)));
00137   g_assert (!signal_seen);
00138   
00139   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00140   
00141   // Test that we unset and signal properly
00142   g_assert (!bamf_view_is_urgent (BAMF_VIEW (application)));
00143   g_assert (!signal_seen);
00144   
00145   bamf_legacy_window_test_set_attention (test1, TRUE);
00146   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00147   
00148   // Ensure that when adding a skip-tasklist window, we dont set this to visible 
00149   g_assert (bamf_view_is_urgent (BAMF_VIEW (application)));
00150   g_assert (signal_seen);
00151   g_assert (signal_result);
00152   
00153   signal_seen = FALSE;
00154 
00155   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window2));
00156   
00157   g_assert (bamf_view_is_urgent (BAMF_VIEW (application)));
00158   g_assert (!signal_seen);
00159   
00160   bamf_legacy_window_test_set_attention (test1, FALSE);
00161   
00162   g_assert (!bamf_view_is_urgent (BAMF_VIEW (application)));
00163   g_assert (signal_seen);
00164   g_assert (!signal_result);
00165 }
00166 
00167 static void
00168 on_active_changed (BamfApplication *application, gboolean result, gpointer data)
00169 {
00170   signal_seen = TRUE;
00171   signal_result = result;
00172 }
00173 
00174 static void
00175 test_active (void)
00176 {
00177   signal_seen = FALSE;
00178   
00179   BamfApplication *application;
00180   BamfWindow *window1, *window2;
00181   BamfLegacyWindowTest *test1, *test2;
00182   
00183   application = bamf_application_new ();
00184   
00185   g_signal_connect (G_OBJECT (application), "active-changed", (GCallback) on_active_changed, NULL);
00186   
00187   test1 = bamf_legacy_window_test_new (20, "Window X", "class", "exec");
00188   test2 = bamf_legacy_window_test_new (20, "Window Y", "class", "exec");
00189   
00190   window1 = bamf_window_new (BAMF_LEGACY_WINDOW (test1));
00191   window2 = bamf_window_new (BAMF_LEGACY_WINDOW (test2));
00192   
00193   // Ensure we are not active with no windows
00194   g_assert (!bamf_view_is_active (BAMF_VIEW (application)));
00195   
00196   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00197   
00198   // Test that when added, we signaled properly
00199   g_assert (!bamf_view_is_active (BAMF_VIEW (application)));
00200   g_assert (!signal_seen);
00201   
00202   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00203   
00204   // Test that we unset and signal properly
00205   g_assert (!bamf_view_is_active (BAMF_VIEW (application)));
00206   g_assert (!signal_seen);
00207   
00208   bamf_legacy_window_test_set_active (test1, TRUE);
00209   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00210   
00211   // Ensure that when adding a skip-tasklist window, we dont set this to visible 
00212   g_assert (bamf_view_is_active (BAMF_VIEW (application)));
00213   g_assert (signal_seen);
00214   g_assert (signal_result);
00215   
00216   signal_seen = FALSE;
00217 
00218   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window2));
00219   
00220   g_assert (bamf_view_is_active (BAMF_VIEW (application)));
00221   g_assert (!signal_seen);
00222   
00223   bamf_legacy_window_test_set_active (test1, FALSE);
00224   
00225   g_assert (!bamf_view_is_active (BAMF_VIEW (application)));
00226   g_assert (signal_seen);
00227   g_assert (!signal_result);
00228 }
00229 
00230 static void
00231 test_get_xids (void)
00232 {
00233   BamfApplication *application;
00234   BamfWindow *window1, *window2;
00235   BamfLegacyWindowTest *lwin1, *lwin2;
00236   GVariant *container;
00237   GVariantIter *xids;
00238   gboolean found;
00239   guint32 xid;
00240 
00241   application = bamf_application_new ();
00242   
00243   lwin1 = bamf_legacy_window_test_new (25, "window1", "class", "exec");
00244   lwin2 = bamf_legacy_window_test_new (50, "window2", "class", "exec");
00245   window1 = bamf_window_new (BAMF_LEGACY_WINDOW (lwin1));
00246   window2 = bamf_window_new (BAMF_LEGACY_WINDOW (lwin2));
00247 
00248   container = bamf_application_get_xids (application);
00249   g_assert (g_variant_type_equal (g_variant_get_type (container),
00250                                   G_VARIANT_TYPE ("(au)")));
00251   g_assert (g_variant_n_children (container) == 1);
00252   g_variant_get (container, "(au)", &xids);
00253   g_assert (g_variant_iter_n_children (xids) == 0);
00254   g_variant_iter_free (xids);
00255   g_variant_unref (container);
00256 
00257   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00258   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window2));
00259 
00260   container = bamf_application_get_xids (application);
00261   g_assert (g_variant_n_children (container) == 1);
00262   g_variant_get (container, "(au)", &xids);
00263   g_assert (g_variant_iter_n_children (xids) == 2);
00264 
00265   found = FALSE;
00266   while (g_variant_iter_loop (xids, "u", &xid))
00267     {
00268       if (xid == 25)
00269         {
00270           found = TRUE;
00271           break;
00272         }
00273     }
00274 
00275   g_assert (found);
00276 
00277   found = FALSE;
00278   g_variant_get (container, "(au)", &xids);
00279   while (g_variant_iter_loop (xids, "u", &xid))
00280     {
00281       if (xid == 50)
00282         {
00283           found = TRUE;
00284           break;
00285         }
00286     }
00287 
00288   g_assert (found);
00289 
00290   g_variant_iter_free (xids);
00291   g_variant_unref (container);
00292  
00293   g_object_unref (lwin1);
00294   g_object_unref (lwin2);
00295   g_object_unref (window1);
00296   g_object_unref (window2);
00297   g_object_unref (application);
00298 }
00299 
00300 static void
00301 test_manages_xid (void)
00302 {
00303   BamfApplication *application;
00304   BamfLegacyWindowTest *lwin;
00305   BamfWindow *test;
00306 
00307   application = bamf_application_new ();
00308   lwin = bamf_legacy_window_test_new (20, "window", "class", "exec");
00309   test = bamf_window_new (BAMF_LEGACY_WINDOW (lwin));
00310 
00311   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (test));
00312 
00313   g_assert (bamf_application_manages_xid (application, 20));
00314 
00315   g_object_unref (lwin);
00316   g_object_unref (test);
00317   g_object_unref (application);
00318 }
00319 
00320 static void
00321 on_user_visible_changed (BamfApplication *application, gboolean result, gpointer data)
00322 {
00323   signal_seen = TRUE;
00324   signal_result = result;
00325 }
00326 
00327 static void
00328 test_user_visible (void)
00329 {
00330   signal_seen = FALSE;
00331   
00332   BamfApplication *application;
00333   BamfWindow *window1, *window2;
00334   BamfLegacyWindowTest *test1, *test2;
00335   
00336   application = bamf_application_new ();
00337   
00338   g_signal_connect (G_OBJECT (application), "user-visible-changed", (GCallback) on_user_visible_changed, NULL);
00339   
00340   test1 = bamf_legacy_window_test_new (20, "Window X", "class", "exec");
00341   test2 = bamf_legacy_window_test_new (20, "Window Y", "class", "exec");
00342   
00343   window1 = bamf_window_new (BAMF_LEGACY_WINDOW (test1));
00344   window2 = bamf_window_new (BAMF_LEGACY_WINDOW (test2));
00345   
00346   // Ensure we are not visible with no windows
00347   g_assert (!bamf_view_user_visible (BAMF_VIEW (application)));
00348   
00349   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00350   
00351   // Test that when added, we signaled properly
00352   g_assert (bamf_view_user_visible (BAMF_VIEW (application)));
00353   g_assert (signal_seen);
00354   g_assert (signal_result);
00355   
00356   signal_seen = FALSE;
00357   
00358   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00359   
00360   // Test that we unset and signal properly
00361   g_assert (!bamf_view_user_visible (BAMF_VIEW (application)));
00362   g_assert (signal_seen);
00363   g_assert (!signal_result);
00364   
00365   signal_seen = FALSE;
00366   
00367   bamf_legacy_window_test_set_skip (test1, TRUE);
00368   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window1));
00369   
00370   // Ensure that when adding a skip-tasklist window, we dont set this to visible 
00371   g_assert (!bamf_view_user_visible (BAMF_VIEW (application)));
00372   g_assert (!signal_seen);
00373   
00374   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window2));
00375   
00376   g_assert (bamf_view_user_visible (BAMF_VIEW (application)));
00377   g_assert (signal_seen);
00378   g_assert (signal_result);
00379   
00380   signal_seen = FALSE;
00381   
00382   bamf_legacy_window_test_set_skip (test2, TRUE);
00383   
00384   g_assert (!bamf_view_user_visible (BAMF_VIEW (window1)));
00385   g_assert (!bamf_view_user_visible (BAMF_VIEW (application)));
00386   g_assert (signal_seen);
00387   g_assert (!signal_result);
00388 }
00389 
00390 static void
00391 on_window_added (BamfApplication *application, char *window, gpointer data)
00392 {
00393   signal_seen = TRUE;
00394   signal_window = g_strdup (window);
00395 }
00396 
00397 static void
00398 test_window_added (void)
00399 {
00400   signal_seen = FALSE;
00401   
00402   BamfApplication *application;
00403   BamfWindow *window;
00404   BamfLegacyWindowTest *test;
00405   const char *path;
00406   
00407   application = bamf_application_new ();
00408   
00409   g_signal_connect (G_OBJECT (application), "window-added", (GCallback) on_window_added, NULL);
00410   
00411   test = bamf_legacy_window_test_new (20, "Window X", "class", "exec");
00412   window = bamf_window_new (BAMF_LEGACY_WINDOW (test));
00413   
00414   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window));
00415   
00416   // Ensure we dont signal things that are not on the bus
00417   g_assert (!signal_seen);
00418   
00419   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window));
00420   
00421   path = bamf_view_export_on_bus (BAMF_VIEW (window), gdbus_connection);
00422   
00423   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window));
00424   
00425   g_assert (signal_seen);
00426   g_assert_cmpstr (signal_window, ==, path);
00427   
00428   signal_seen = FALSE;
00429   
00430   g_object_unref (window);
00431   g_object_unref (test);
00432 }
00433 
00434 static void
00435 on_window_removed (BamfApplication *application, char *window, gpointer data)
00436 {
00437   signal_seen = TRUE;
00438   signal_window = g_strdup (window);
00439 }
00440 
00441 static void
00442 test_window_removed (void)
00443 {
00444   signal_seen = FALSE;
00445   
00446   BamfApplication *application;
00447   BamfWindow *window;
00448   BamfLegacyWindowTest *test;
00449   const char *path;
00450   
00451   application = bamf_application_new ();
00452   
00453   g_signal_connect (G_OBJECT (application), "window-removed", (GCallback) on_window_removed, NULL);
00454   
00455   test = bamf_legacy_window_test_new (20, "Window X", "class", "exec");
00456   window = bamf_window_new (BAMF_LEGACY_WINDOW (test));
00457   
00458   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window));
00459   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window));
00460   
00461   // Ensure we dont signal things that are not on the bus
00462   g_assert (!signal_seen);
00463   
00464   path = bamf_view_export_on_bus (BAMF_VIEW (window), gdbus_connection);
00465   
00466   bamf_view_add_child (BAMF_VIEW (application), BAMF_VIEW (window));
00467   bamf_view_remove_child (BAMF_VIEW (application), BAMF_VIEW (window));
00468   
00469   g_assert (signal_seen);
00470   g_assert (g_strcmp0 (signal_window, path) == 0);
00471   
00472   signal_seen = FALSE;
00473   
00474   g_object_unref (window);
00475   g_object_unref (test);
00476 }