diff options
author | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2013-04-05 01:13:56 +0900 |
---|---|---|
committer | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2013-04-05 01:13:56 +0900 |
commit | f1543fca9174f965cbe19db8a0fa0e8f5f5a3ff8 (patch) | |
tree | e553fbb0bbb838e248e5b95b2ef9aa53e5ac3615 /tests | |
parent | fc0ea5ea9b33190bc0eef42351957ddee077969a (diff) | |
download | glade-f1543fca9174f965cbe19db8a0fa0e8f5f5a3ff8.tar.gz |
Added Glade's first unit test (about time !)
This patch clears the way for unit testing, some environment variables
are added to ensure unit tests run on code that is not installed but
in tree.
The added test iterates over all widgets in the catalog, uses the
adaptor to create a GladeWidget and asserts that the GladeWidget and
it's internal object is finalized after unreferencing the GladeWidget.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 30 | ||||
-rw-r--r-- | tests/create-widgets.c | 117 |
2 files changed, 147 insertions, 0 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 00000000..e8b026d2 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,30 @@ +include $(top_srcdir)/glade-rules.mk + +noinst_PROGRAMS = $(TEST_PROGS) + +progs_cppflags = \ + $(common_defines) \ + -I$(top_srcdir) \ + -I$(top_builddir) \ + $(GTK_CFLAGS) \ + $(GTK_MAC_BUNDLE_FLAG) \ + $(GTK_MAC_CFLAGS) \ + $(WARN_CFLAGS) \ + $(AM_CPPFLAGS) + +progs_cflags = \ + $(AM_CFLAGS) + +progs_libs = $(GTK_LIBS) $(GTK_MAC_LIBS) +progs_ldadd = $(top_builddir)/gladeui/libgladeui-2.la + +# Namespace expected names and naming exclusivity tests +TEST_PROGS = create-widgets +create_widgets_CPPFLAGS = $(progs_cppflags) +create_widgets_CFLAGS = $(progs_cflags) +create_widgets_LDFLAGS = $(progs_libs) +create_widgets_LDADD = $(progs_ldadd) +create_widgets_SOURCES = create-widgets.c + +TESTS = $(TEST_PROGS) +TESTS_ENVIRONMENT=$(GLADE_TEST_ENVIRONMENT) diff --git a/tests/create-widgets.c b/tests/create-widgets.c new file mode 100644 index 00000000..97e5e1a8 --- /dev/null +++ b/tests/create-widgets.c @@ -0,0 +1,117 @@ +#include <glib.h> +#include <glib-object.h> + +#include <gladeui/glade-app.h> + +/* Avoid warnings from GVFS-RemoteVolumeMonitor */ +static gboolean +ignore_gvfs_warning (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + if (g_strcmp0 (log_domain, "GVFS-RemoteVolumeMonitor") == 0) + return FALSE; + + return TRUE; +} + +static gboolean +main_loop_quit_cb (gpointer data) +{ + gtk_main_quit (); + + return FALSE; +} + +static void +check_finalized (gpointer data, + GObject *where_the_object_was) +{ + gboolean *did_finalize = (gboolean *)data; + + *did_finalize = TRUE; +} + +static void +test_create_widget (gconstpointer data) +{ + GladeWidgetAdaptor *adaptor = (GladeWidgetAdaptor *)data; + GladeWidget *widget; + GObject *object; + gboolean widget_finalized = FALSE; + gboolean object_finalized = FALSE; + + g_test_log_set_fatal_handler (ignore_gvfs_warning, NULL); + + + widget = glade_widget_adaptor_create_widget (adaptor, FALSE, NULL); + g_assert (GLADE_IS_WIDGET (widget)); + + object = glade_widget_get_object (widget); + g_assert (G_IS_OBJECT (object)); + + g_object_weak_ref (G_OBJECT (widget), check_finalized, &widget_finalized); + g_object_weak_ref (G_OBJECT (object), check_finalized, &object_finalized); + + /* filechoosers hold a reference until an async operation is complete */ + if (GTK_IS_FILE_CHOOSER (object)) + { + g_timeout_add (500, main_loop_quit_cb, NULL); + gtk_main(); + } + /* Our plugin code adds an idle when cell renderers are created */ + else if (GTK_IS_CELL_RENDERER (object)) + { + g_timeout_add (50, main_loop_quit_cb, NULL); + gtk_main(); + } + + /* Get rid of the GladeWidget and assert that it finalizes along + * with it's internal object + */ + g_object_unref (widget); + + g_assert (widget_finalized); + g_assert (object_finalized); +} + +int +main (int argc, + char *argv[]) +{ + GList *adaptors, *l; + + gtk_test_init (&argc, &argv, NULL); + + glade_init (); + glade_app_get (); + + adaptors = glade_widget_adaptor_list_adaptors (); + + for (l = adaptors; l; l = l->next) + { + GladeWidgetAdaptor *adaptor = l->data; + GType adaptor_type; + + adaptor_type = glade_widget_adaptor_get_object_type (adaptor); + + if (G_TYPE_IS_INSTANTIATABLE (adaptor_type) && !G_TYPE_IS_ABSTRACT (adaptor_type) && + /* FIXME: Status Icon crashes at dispose time unrealizing it's GtkTrayIcon without a window */ + adaptor_type != GTK_TYPE_STATUS_ICON && + /* FIXME: Icon factory adds itself to the default but never removes itself */ + adaptor_type != GTK_TYPE_ICON_FACTORY && + /* FIXME: Combo box types dont finalize properly for some reason */ + !g_type_is_a (adaptor_type, GTK_TYPE_COMBO_BOX) && + /* FIXME: App choosers leak some async operations after finalization, causing subsequent tests to fail */ + !g_type_is_a (adaptor_type, GTK_TYPE_APP_CHOOSER)) + { + gchar *test_path = g_strdup_printf ("/CreateWidget/%s", glade_widget_adaptor_get_name (adaptor)); + + g_test_add_data_func (test_path, adaptor, test_create_widget); + } + } + g_list_free (adaptors); + + return g_test_run (); +} |