[gnome-builder] egg-binding-set: Add tests
- From: Garrett Regier <gregier src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] egg-binding-set: Add tests
- Date: Mon, 18 May 2015 22:44:37 +0000 (UTC)
commit 5a3f0ae7426b3bf359c1551c11c2130c7ab88f7d
Author: Garrett Regier <garrettregier gmail com>
Date: Sun May 17 17:04:16 2015 -0700
egg-binding-set: Add tests
configure.ac | 2 +-
tests/Makefile.am | 6 +
tests/test-egg-binding-set.c | 609 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 616 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index a84d297..c2da5e6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -138,7 +138,7 @@ dnl ***********************************************************************
dnl Check for Required Packages
dnl ***********************************************************************
m4_define([gtk_required_version], [3.17.1])
-m4_define([glib_required_version], [2.45.0])
+m4_define([glib_required_version], [2.45.1])
m4_define([gtksourceview_required_version], [3.17.2])
m4_define([ggit_required_version], [0.22.9])
m4_define([gjs_required_version], [1.42.0])
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 7173d93..e727739 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -116,6 +116,12 @@ test_ide_ctags_CFLAGS = $(tests_cflags)
test_ide_ctags_LDADD = $(tests_libs)
+TESTS += test-egg-binding-set
+test_egg_binding_set_SOURCES = test-egg-binding-set.c
+test_egg_binding_set_CFLAGS = $(egg_cflags)
+test_egg_binding_set_LDADD = $(egg_libs)
+
+
TESTS += test-egg-state-machine
test_egg_state_machine_SOURCES = test-egg-state-machine.c
test_egg_state_machine_CFLAGS = $(egg_cflags)
diff --git a/tests/test-egg-binding-set.c b/tests/test-egg-binding-set.c
new file mode 100644
index 0000000..bccd7b3
--- /dev/null
+++ b/tests/test-egg-binding-set.c
@@ -0,0 +1,609 @@
+
+#define G_LOG_DOMAIN "egg-binding-set"
+
+#include "egg-binding-set.h"
+
+/* Copied from glib */
+typedef struct _BindingSource
+{
+ GObject parent_instance;
+
+ gint foo;
+ gint bar;
+ gdouble value;
+ gboolean toggle;
+} BindingSource;
+
+typedef struct _BindingSourceClass
+{
+ GObjectClass parent_class;
+} BindingSourceClass;
+
+enum
+{
+ PROP_SOURCE_0,
+
+ PROP_SOURCE_FOO,
+ PROP_SOURCE_BAR,
+ PROP_SOURCE_VALUE,
+ PROP_SOURCE_TOGGLE
+};
+
+static GType binding_source_get_type (void);
+G_DEFINE_TYPE (BindingSource, binding_source, G_TYPE_OBJECT);
+
+static void
+binding_source_set_property (GObject *gobject,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ BindingSource *source = (BindingSource *) gobject;
+
+ switch (prop_id)
+ {
+ case PROP_SOURCE_FOO:
+ source->foo = g_value_get_int (value);
+ break;
+
+ case PROP_SOURCE_BAR:
+ source->bar = g_value_get_int (value);
+ break;
+
+ case PROP_SOURCE_VALUE:
+ source->value = g_value_get_double (value);
+ break;
+
+ case PROP_SOURCE_TOGGLE:
+ source->toggle = g_value_get_boolean (value);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+ }
+}
+
+static void
+binding_source_get_property (GObject *gobject,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ BindingSource *source = (BindingSource *) gobject;
+
+ switch (prop_id)
+ {
+ case PROP_SOURCE_FOO:
+ g_value_set_int (value, source->foo);
+ break;
+
+ case PROP_SOURCE_BAR:
+ g_value_set_int (value, source->bar);
+ break;
+
+ case PROP_SOURCE_VALUE:
+ g_value_set_double (value, source->value);
+ break;
+
+ case PROP_SOURCE_TOGGLE:
+ g_value_set_boolean (value, source->toggle);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+ }
+}
+
+static void
+binding_source_class_init (BindingSourceClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ gobject_class->set_property = binding_source_set_property;
+ gobject_class->get_property = binding_source_get_property;
+
+ g_object_class_install_property (gobject_class, PROP_SOURCE_FOO,
+ g_param_spec_int ("foo", "Foo", "Foo",
+ -1, 100,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_SOURCE_BAR,
+ g_param_spec_int ("bar", "Bar", "Bar",
+ -1, 100,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_SOURCE_VALUE,
+ g_param_spec_double ("value", "Value", "Value",
+ -100.0, 200.0,
+ 0.0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_SOURCE_TOGGLE,
+ g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+ FALSE,
+ G_PARAM_READWRITE));
+}
+
+static void
+binding_source_init (BindingSource *self)
+{
+}
+
+typedef struct _BindingTarget
+{
+ GObject parent_instance;
+
+ gint bar;
+ gdouble value;
+ gboolean toggle;
+} BindingTarget;
+
+typedef struct _BindingTargetClass
+{
+ GObjectClass parent_class;
+} BindingTargetClass;
+
+enum
+{
+ PROP_TARGET_0,
+
+ PROP_TARGET_BAR,
+ PROP_TARGET_VALUE,
+ PROP_TARGET_TOGGLE
+};
+
+static GType binding_target_get_type (void);
+G_DEFINE_TYPE (BindingTarget, binding_target, G_TYPE_OBJECT);
+
+static void
+binding_target_set_property (GObject *gobject,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ BindingTarget *target = (BindingTarget *) gobject;
+
+ switch (prop_id)
+ {
+ case PROP_TARGET_BAR:
+ target->bar = g_value_get_int (value);
+ break;
+
+ case PROP_TARGET_VALUE:
+ target->value = g_value_get_double (value);
+ break;
+
+ case PROP_TARGET_TOGGLE:
+ target->toggle = g_value_get_boolean (value);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+ }
+}
+
+static void
+binding_target_get_property (GObject *gobject,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ BindingTarget *target = (BindingTarget *) gobject;
+
+ switch (prop_id)
+ {
+ case PROP_TARGET_BAR:
+ g_value_set_int (value, target->bar);
+ break;
+
+ case PROP_TARGET_VALUE:
+ g_value_set_double (value, target->value);
+ break;
+
+ case PROP_TARGET_TOGGLE:
+ g_value_set_boolean (value, target->toggle);
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
+ }
+}
+
+static void
+binding_target_class_init (BindingTargetClass *klass)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+
+ gobject_class->set_property = binding_target_set_property;
+ gobject_class->get_property = binding_target_get_property;
+
+ g_object_class_install_property (gobject_class, PROP_TARGET_BAR,
+ g_param_spec_int ("bar", "Bar", "Bar",
+ -1, 100,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_TARGET_VALUE,
+ g_param_spec_double ("value", "Value", "Value",
+ -100.0, 200.0,
+ 0.0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (gobject_class, PROP_TARGET_TOGGLE,
+ g_param_spec_boolean ("toggle", "Toggle", "Toggle",
+ FALSE,
+ G_PARAM_READWRITE));
+}
+
+static void
+binding_target_init (BindingTarget *self)
+{
+}
+
+static gboolean
+celsius_to_fahrenheit (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data G_GNUC_UNUSED)
+{
+ gdouble celsius, fahrenheit;
+
+ g_assert (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
+
+ celsius = g_value_get_double (from_value);
+ fahrenheit = (9 * celsius / 5) + 32.0;
+
+ if (g_test_verbose ())
+ g_printerr ("Converting %.2fC to %.2fF\n", celsius, fahrenheit);
+
+ g_value_set_double (to_value, fahrenheit);
+
+ return TRUE;
+}
+
+static gboolean
+fahrenheit_to_celsius (GBinding *binding,
+ const GValue *from_value,
+ GValue *to_value,
+ gpointer user_data G_GNUC_UNUSED)
+{
+ gdouble celsius, fahrenheit;
+
+ g_assert (G_VALUE_HOLDS (from_value, G_TYPE_DOUBLE));
+ g_assert (G_VALUE_HOLDS (to_value, G_TYPE_DOUBLE));
+
+ fahrenheit = g_value_get_double (from_value);
+ celsius = 5 * (fahrenheit - 32.0) / 9;
+
+ if (g_test_verbose ())
+ g_printerr ("Converting %.2fF to %.2fC\n", fahrenheit, celsius);
+
+ g_value_set_double (to_value, celsius);
+
+ return TRUE;
+}
+
+static void
+test_binding_set_invalid (void)
+{
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+ /* Invalid Target Property */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+ "*find_property*target_property*!=*NULL*");
+ egg_binding_set_bind (set, "value",
+ target, "does-not-exist",
+ G_BINDING_DEFAULT);
+ g_test_assert_expected_messages ();
+
+ egg_binding_set_set_source (set, NULL);
+
+ /* Invalid Source Property */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+ "*find_property*source_property*!=*NULL*");
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind (set, "does-not-exist",
+ target, "value",
+ G_BINDING_DEFAULT);
+ g_test_assert_expected_messages ();
+
+ egg_binding_set_set_source (set, NULL);
+
+ /* Invalid Source */
+ g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
+ "*find_property*->source_property*!=*NULL*");
+ egg_binding_set_bind (set, "does-not-exist",
+ target, "value",
+ G_BINDING_DEFAULT);
+ egg_binding_set_set_source (set, source);
+ g_test_assert_expected_messages ();
+
+ g_object_unref (target);
+ g_object_unref (source);
+ g_object_unref (set);
+}
+
+static void
+test_binding_set_default (void)
+{
+ gsize i, j;
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *targets[5];
+
+ for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+ {
+ targets[i] = g_object_new (binding_target_get_type (), NULL);
+ egg_binding_set_bind (set, "foo",
+ targets[i], "bar",
+ G_BINDING_DEFAULT);
+ }
+
+ g_assert_null (egg_binding_set_get_source (set));
+ egg_binding_set_set_source (set, source);
+ g_assert (egg_binding_set_get_source (set) == (GObject *)source);
+
+ for (i = 0; i < 2; ++i)
+ {
+ g_object_set (source, "foo", 42, NULL);
+ for (j = 0; j < G_N_ELEMENTS (targets); ++j)
+ g_assert_cmpint (source->foo, ==, targets[j]->bar);
+
+ g_object_set (targets[0], "bar", 47, NULL);
+ g_assert_cmpint (source->foo, !=, targets[0]->bar);
+
+ /* Check that we transition the source correctly */
+ egg_binding_set_set_source (set, NULL);
+ g_assert_null (egg_binding_set_get_source (set));
+ egg_binding_set_set_source (set, source);
+ g_assert (egg_binding_set_get_source (set) == (GObject *)source);
+ }
+
+ g_object_unref (set);
+
+ g_object_set (source, "foo", 0, NULL);
+ for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+ g_assert_cmpint (source->foo, !=, targets[i]->bar);
+
+ g_object_unref (source);
+ for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+ g_object_unref (targets[i]);
+}
+
+static void
+test_binding_set_bidirectional (void)
+{
+ gsize i, j;
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *targets[5];
+
+ for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+ {
+ targets[i] = g_object_new (binding_target_get_type (), NULL);
+ egg_binding_set_bind (set, "value",
+ targets[i], "value",
+ G_BINDING_BIDIRECTIONAL);
+ }
+
+ g_assert_null (egg_binding_set_get_source (set));
+ egg_binding_set_set_source (set, source);
+ g_assert (egg_binding_set_get_source (set) == (GObject *)source);
+
+ for (i = 0; i < 2; ++i)
+ {
+ g_object_set (source, "value", 42.0, NULL);
+ for (j = 0; j < G_N_ELEMENTS (targets); ++j)
+ g_assert_cmpfloat (source->value, ==, targets[j]->value);
+
+ g_object_set (targets[0], "value", 47.0, NULL);
+ g_assert_cmpfloat (source->value, ==, targets[0]->value);
+
+ /* Check that we transition the source correctly */
+ egg_binding_set_set_source (set, NULL);
+ g_assert_null (egg_binding_set_get_source (set));
+ egg_binding_set_set_source (set, source);
+ g_assert (egg_binding_set_get_source (set) == (GObject *)source);
+ }
+
+ g_object_unref (set);
+
+ g_object_set (targets[0], "value", 0.0, NULL);
+ g_assert_cmpfloat (source->value, !=, targets[0]->value);
+
+ g_object_unref (source);
+ for (i = 0; i < G_N_ELEMENTS (targets); ++i)
+ g_object_unref (targets[i]);
+}
+
+static void
+transform_destroy_notify (gpointer data)
+{
+ gboolean *transform_destroy_called = data;
+
+ *transform_destroy_called = TRUE;
+}
+
+static void
+test_binding_set_transform (void)
+{
+ gboolean transform_destroy_called = FALSE;
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind_full (set, "value",
+ target, "value",
+ G_BINDING_BIDIRECTIONAL,
+ celsius_to_fahrenheit,
+ fahrenheit_to_celsius,
+ &transform_destroy_called,
+ transform_destroy_notify);
+
+ g_object_set (source, "value", 24.0, NULL);
+ g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
+
+ g_object_set (target, "value", 69.0, NULL);
+ g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
+
+ /* The GDestroyNotify should only be called when the
+ * set is freed, not when the various GBindings are freed
+ */
+ egg_binding_set_set_source (set, NULL);
+ g_assert_false (transform_destroy_called);
+
+ g_object_unref (set);
+ g_assert_true (transform_destroy_called);
+
+ g_object_unref (source);
+ g_object_unref (target);
+}
+
+static void
+test_binding_set_transform_closures (void)
+{
+ gboolean transform_destroy_called_1 = FALSE;
+ gboolean transform_destroy_called_2 = FALSE;
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+ GClosure *c2f_closure, *f2c_closure;
+
+ c2f_closure = g_cclosure_new (G_CALLBACK (celsius_to_fahrenheit),
+ &transform_destroy_called_1,
+ (GClosureNotify) transform_destroy_notify);
+ f2c_closure = g_cclosure_new (G_CALLBACK (fahrenheit_to_celsius),
+ &transform_destroy_called_2,
+ (GClosureNotify) transform_destroy_notify);
+
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind_with_closures (set, "value",
+ target, "value",
+ G_BINDING_BIDIRECTIONAL,
+ c2f_closure,
+ f2c_closure);
+
+ g_object_set (source, "value", 24.0, NULL);
+ g_assert_cmpfloat (target->value, ==, ((9 * 24.0 / 5) + 32.0));
+
+ g_object_set (target, "value", 69.0, NULL);
+ g_assert_cmpfloat (source->value, ==, (5 * (69.0 - 32.0) / 9));
+
+ /* The GClsoureNotify should only be called when the
+ * set is freed, not when the various GBindings are freed
+ */
+ egg_binding_set_set_source (set, NULL);
+ g_assert_false (transform_destroy_called_1);
+ g_assert_false (transform_destroy_called_2);
+
+ g_object_unref (set);
+ g_assert_true (transform_destroy_called_1);
+ g_assert_true (transform_destroy_called_2);
+
+ g_object_unref (source);
+ g_object_unref (target);
+}
+
+static void
+test_binding_set_same_object (void)
+{
+ gsize i;
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (),
+ "foo", 100,
+ "bar", 50,
+ NULL);
+
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind (set, "foo",
+ source, "bar",
+ G_BINDING_BIDIRECTIONAL);
+
+ for (i = 0; i < 2; ++i)
+ {
+ g_object_set (source, "foo", 10, NULL);
+ g_assert_cmpint (source->foo, ==, 10);
+ g_assert_cmpint (source->bar, ==, 10);
+
+ g_object_set (source, "bar", 30, NULL);
+ g_assert_cmpint (source->foo, ==, 30);
+ g_assert_cmpint (source->bar, ==, 30);
+
+ /* Check that it is possible both when initially
+ * adding the binding and when changing the source
+ */
+ egg_binding_set_set_source (set, NULL);
+ egg_binding_set_set_source (set, source);
+ }
+
+ g_object_unref (source);
+ g_object_unref (set);
+}
+
+static void
+test_binding_set_weak_ref_source (void)
+{
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind (set, "value",
+ target, "value",
+ G_BINDING_BIDIRECTIONAL);
+
+ g_object_add_weak_pointer (G_OBJECT (source), (gpointer)&source);
+ g_assert (egg_binding_set_get_source (set) == (GObject *)source);
+ g_object_unref (source);
+ g_assert_null (source);
+ g_assert_null (egg_binding_set_get_source (set));
+
+ /* Hopefully this would explode if the binding was still alive */
+ g_object_set (target, "value", 0.0, NULL);
+
+ g_object_unref (target);
+ g_object_unref (set);
+}
+
+static void
+test_binding_set_weak_ref_target (void)
+{
+ EggBindingSet *set = egg_binding_set_new ();
+ BindingSource *source = g_object_new (binding_source_get_type (), NULL);
+ BindingTarget *target = g_object_new (binding_target_get_type (), NULL);
+
+ egg_binding_set_set_source (set, source);
+ egg_binding_set_bind (set, "value",
+ target, "value",
+ G_BINDING_BIDIRECTIONAL);
+
+ g_object_set (source, "value", 47.0, NULL);
+ g_assert_cmpfloat (target->value, ==, 47.0);
+
+ g_object_add_weak_pointer (G_OBJECT (target), (gpointer)&target);
+ g_object_unref (target);
+ g_assert_null (target);
+
+ /* Hopefully this would explode if the binding was still alive */
+ g_object_set (source, "value", 0.0, NULL);
+
+ g_object_unref (source);
+ g_object_unref (set);
+}
+
+gint
+main (gint argc,
+ gchar *argv[])
+{
+ g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/Egg/BindingSet/invalid", test_binding_set_invalid);
+ g_test_add_func ("/Egg/BindingSet/default", test_binding_set_default);
+ g_test_add_func ("/Egg/BindingSet/bidirectional", test_binding_set_bidirectional);
+ g_test_add_func ("/Egg/BindingSet/transform", test_binding_set_transform);
+ g_test_add_func ("/Egg/BindingSet/transform-closures", test_binding_set_transform_closures);
+ g_test_add_func ("/Egg/BindingSet/same-object", test_binding_set_same_object);
+ g_test_add_func ("/Egg/BindingSet/weak-ref-source", test_binding_set_weak_ref_source);
+ g_test_add_func ("/Egg/BindingSet/weak-ref-target", test_binding_set_weak_ref_target);
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]