commit fb6b4e5afa30768b57d7c642266b6dae55c43f0f Author: Dan Williams Date: Thu Dec 18 12:47:09 2008 -0500 [test] add testcase for GetAll with interfaces diff --git a/test/core/Makefile.am b/test/core/Makefile.am index aeb0e89..be39ddb 100644 --- a/test/core/Makefile.am +++ b/test/core/Makefile.am @@ -17,7 +17,7 @@ else TESTS= endif -EXTRA_DIST=run-test.sh run-peer-test.sh test-service-glib.xml my-object-marshal.list test-service-glib.xml +EXTRA_DIST=run-test.sh run-peer-test.sh test-service-glib.xml my-object-marshal.list test-service-glib-subclass.xml if DBUS_BUILD_TESTS @@ -51,11 +51,13 @@ test_variant_recursion_SOURCES=test-variant-recursion.c test_variant_recursion_LDADD= $(DBUS_GLIB_TOOL_LIBS) $(top_builddir)/dbus/libdbus-glib-1.la $(top_builddir)/dbus/libdbus-gtool.la -BUILT_SOURCES = test-service-glib-glue.h test-service-glib-bindings.h my-object-marshal.c my-object-marshal.h +BUILT_SOURCES = test-service-glib-glue.h test-service-glib-subclass-glue.h test-service-glib-bindings.h my-object-marshal.c my-object-marshal.h test_service_glib_SOURCES= \ my-object.c \ my-object.h \ + my-object-subclass.c \ + my-object-subclass.h \ my-object-marshal.c \ test-service-glib.c test_service_glib_LDADD= $(top_builddir)/dbus/libdbus-glib-1.la $(DBUS_GLIB_THREADS_LIBS) @@ -63,6 +65,9 @@ test_service_glib_LDADD= $(top_builddir)/dbus/libdbus-glib-1.la $(DBUS_GLIB_THRE test-service-glib-glue.h: test-service-glib.xml $(top_builddir)/dbus/dbus-binding-tool $(top_builddir)/dbus/dbus-binding-tool --prefix=my_object --mode=glib-server --output=test-service-glib-glue.h $(srcdir)/test-service-glib.xml +test-service-glib-subclass-glue.h: test-service-glib-subclass.xml $(top_builddir)/dbus/dbus-binding-tool + $(top_builddir)/dbus/dbus-binding-tool --prefix=my_object_subclass --mode=glib-server --output=test-service-glib-subclass-glue.h $(srcdir)/test-service-glib-subclass.xml + test-service-glib-bindings.h: test-service-glib.xml $(top_builddir)/dbus/dbus-binding-tool $(top_builddir)/dbus/dbus-binding-tool --prefix=my_object --mode=glib-client --output=test-service-glib-bindings.h $(srcdir)/test-service-glib.xml @@ -76,6 +81,8 @@ my-object-marshal.h: Makefile my-object-marshal.list peer_server_SOURCES = \ my-object.c \ my-object.h \ + my-object-subclass.c \ + my-object-subclass.h \ my-object-marshal.c \ peer-server.c peer_server_LDADD= $(top_builddir)/dbus/libdbus-glib-1.la $(DBUS_GLIB_THREADS_LIBS) diff --git a/test/core/my-object-subclass.c b/test/core/my-object-subclass.c new file mode 100644 index 0000000..7b4ae9e --- /dev/null +++ b/test/core/my-object-subclass.c @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include "my-object-subclass.h" + +#include "test-service-glib-subclass-glue.h" + +/* Properties */ +enum +{ + PROP_0, + PROP_THIS_IS_A_SUBCLASS_STRING, + PROP_THIS_IS_A_SUBCLASS_UINT +}; + +G_DEFINE_TYPE(MyObjectSubclass, my_object_subclass, MY_TYPE_OBJECT) + +static void +my_object_subclass_finalize (GObject *object) +{ + MyObjectSubclass *mobject = MY_OBJECT_SUBCLASS (object); + + g_free (mobject->this_is_a_subclass_string); + + (G_OBJECT_CLASS (my_object_subclass_parent_class)->finalize) (object); +} + +static void +my_object_subclass_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + MyObjectSubclass *mobject; + + mobject = MY_OBJECT_SUBCLASS (object); + + switch (prop_id) + { + case PROP_THIS_IS_A_SUBCLASS_STRING: + g_free (mobject->this_is_a_subclass_string); + mobject->this_is_a_subclass_string = g_value_dup_string (value); + break; + + case PROP_THIS_IS_A_SUBCLASS_UINT: + mobject->this_is_a_subclass_uint = g_value_get_uint (value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +my_object_subclass_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + MyObjectSubclass *mobject; + + mobject = MY_OBJECT_SUBCLASS (object); + + switch (prop_id) + { + case PROP_THIS_IS_A_SUBCLASS_STRING: + g_value_set_string (value, mobject->this_is_a_subclass_string); + break; + + case PROP_THIS_IS_A_SUBCLASS_UINT: + g_value_set_uint (value, mobject->this_is_a_subclass_uint); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +my_object_subclass_init (MyObjectSubclass *obj) +{ +} + +static void +my_object_subclass_class_init (MyObjectSubclassClass *mobject_class) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (mobject_class); + + dbus_g_object_type_install_info (MY_TYPE_OBJECT_SUBCLASS, + &dbus_glib_my_object_subclass_object_info); + + gobject_class->finalize = my_object_subclass_finalize; + gobject_class->set_property = my_object_subclass_set_property; + gobject_class->get_property = my_object_subclass_get_property; + + g_object_class_install_property (gobject_class, + PROP_THIS_IS_A_SUBCLASS_STRING, + g_param_spec_string ("this_is_a_subclass_string", + _("Sample string"), + _("Example of a string property"), + "default subclass value", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + + g_object_class_install_property (gobject_class, + PROP_THIS_IS_A_SUBCLASS_UINT, + g_param_spec_uint ("this_is_a_subclass_uint", + _("Sample uint"), + _("Example of a uint property"), + 0, G_MAXUINT32, 1234567, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); +} + diff --git a/test/core/my-object-subclass.h b/test/core/my-object-subclass.h new file mode 100644 index 0000000..edb03cd --- /dev/null +++ b/test/core/my-object-subclass.h @@ -0,0 +1,33 @@ +#ifndef __MY_OBJECT_SUBCLASS_H__ +#define __MY_OBJECT_SUBCLASS_H__ + +#include +#include + +#include "my-object.h" + +typedef struct MyObjectSubclass MyObjectSubclass; +typedef struct MyObjectSubclassClass MyObjectSubclassClass; + +GType my_object_subclass_get_type (void); + +struct MyObjectSubclass +{ + MyObject parent; + char *this_is_a_subclass_string; + guint this_is_a_subclass_uint; +}; + +struct MyObjectSubclassClass +{ + MyObjectClass parent; +}; + +#define MY_TYPE_OBJECT_SUBCLASS (my_object_subclass_get_type ()) +#define MY_OBJECT_SUBCLASS(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), MY_TYPE_OBJECT_SUBCLASS, MyObjectSubclass)) +#define MY_OBJECT_SUBCLASS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), MY_TYPE_OBJECT_SUBCLASS, MyObjectSubclassClass)) +#define MY_IS_OBJECT_SUBCLASS(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), MY_TYPE_OBJECT_SUBCLASS)) +#define MY_IS_OBJECT_SUBCLASS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), MY_TYPE_OBJECT_SUBCLASS)) +#define MY_OBJECT_SUBCLASS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), MY_TYPE_OBJECT_SUBCLASS, MyObjectSubclassClass)) + +#endif diff --git a/test/core/test-dbus-glib.c b/test/core/test-dbus-glib.c index d659ff3..1800aa1 100644 --- a/test/core/test-dbus-glib.c +++ b/test/core/test-dbus-glib.c @@ -268,6 +268,175 @@ increment_async_cb (DBusGProxy *proxy, guint val, GError *error, gpointer data) g_source_remove (exit_timeout); } +#define DBUS_TYPE_G_MAP_OF_VARIANT (dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE)) +static gboolean +test_base_class_get_all (DBusGConnection *connection, + const char *object_path, + const char *expected_string_value) +{ + DBusGProxy *proxy; + GError *error = NULL; + GHashTable *hash = NULL; + + g_assert (expected_string_value != NULL); + g_assert (object_path != NULL); + + /* Test GetAll with interfaces on the base class */ + + proxy = dbus_g_proxy_new_for_name (connection, + "org.freedesktop.DBus.GLib.TestService", + object_path, + DBUS_INTERFACE_PROPERTIES); + g_assert (proxy != NULL); + + g_print ("%s: Calling GetAll for unknown interface\n", object_path); + { + if (dbus_g_proxy_call (proxy, "GetAll", &error, + G_TYPE_STRING, "org.freedesktop.DBus.foobar.blahblah", + G_TYPE_INVALID, + DBUS_TYPE_G_MAP_OF_VARIANT, &hash, G_TYPE_INVALID)) + lose ("Unexpected success for GetAll call of unknown interface\n"); + g_clear_error (&error); + hash = NULL; + } + + g_print ("%s: Calling GetAll for base class interface\n", object_path); + { + GValue *value; + const char *foo = NULL; + + if (!dbus_g_proxy_call (proxy, "GetAll", &error, + G_TYPE_STRING, "org.freedesktop.DBus.GLib.Tests.MyObject", + G_TYPE_INVALID, + DBUS_TYPE_G_MAP_OF_VARIANT, &hash, G_TYPE_INVALID)) + lose_gerror ("Unexpected error for GetProperty call of base class interface\n", error); + g_clear_error (&error); + + if (!hash) + { + lose ("%s: Unexpected NULL hash table returned for GetAll call of base " + "class interface\n", object_path); + } + + if (g_hash_table_size (hash) != 1) + { + lose ("%s: Unexpected hash table size %d (expected 1) returned for GetAll " + " call of base class interface\n", object_path, g_hash_table_size (hash)); + } + value = g_hash_table_lookup (hash, "this_is_a_string"); + if (!value) + { + lose ("%s: Unexpected missing 'this_is_a_string' property for GetAll " + "call of base class interface\n", object_path); + } + if (!G_VALUE_HOLDS_STRING (value)) + { + lose ("%s: Unexpected wrong type for 'this_is_a_string' property for " + "GetAll call of base class interface\n", object_path); + } + foo = g_value_get_string (value); + if (!foo || strcmp (foo, expected_string_value)) + { + lose ("%s: Unexpected value for 'this_is_a_string' property for GetAll " + "call of base class interface\n", object_path); + } + + g_hash_table_destroy (hash); + hash = NULL; + } + + g_object_unref (proxy); + return TRUE; +} + +static gboolean +test_subclass_get_all (DBusGConnection *connection, + const char *object_path) +{ + DBusGProxy *proxy; + GError *error = NULL; + GHashTable *hash = NULL; + + g_assert (object_path != NULL); + + /* Test GetAll with interfaces on the subclass */ + + proxy = dbus_g_proxy_new_for_name (connection, + "org.freedesktop.DBus.GLib.TestService", + object_path, + DBUS_INTERFACE_PROPERTIES); + g_assert (proxy != NULL); + + g_print ("%s: Calling GetAll for subclass interface\n", object_path); + { + GValue *value; + const char *string = NULL; + guint num = 0; + + if (!dbus_g_proxy_call (proxy, "GetAll", &error, + G_TYPE_STRING, "org.freedesktop.DBus.GLib.Tests.MyObjectSubclass", + G_TYPE_INVALID, + DBUS_TYPE_G_MAP_OF_VARIANT, &hash, G_TYPE_INVALID)) + lose_gerror ("Unexpected error for GetProperty call of base subclass interface\n", error); + g_clear_error (&error); + + if (!hash) + { + lose ("%s: Unexpected NULL hash table returned for GetAll call of " + "subclass interface\n", object_path); + } + + if (g_hash_table_size (hash) != 2) + { + lose ("%s: Unexpected hash table size %d (expected 2) returned for GetAll " + " call of subclass interface\n", object_path, g_hash_table_size (hash)); + } + + /* Test the string property */ + value = g_hash_table_lookup (hash, "this_is_a_subclass_string"); + if (!value) + { + lose ("%s: Unexpected missing 'this_is_a_subclass_string' property for " + "GetAll call of subclass interface\n", object_path); + } + if (!G_VALUE_HOLDS_STRING (value)) + { + lose ("%s: Unexpected wrong type for 'this_is_a_subclass_string' " + "property for GetAll call of subclass interface\n", object_path); + } + string = g_value_get_string (value); + if (!string || strcmp (string, "default subclass value")) + { + lose ("%s: Unexpected value for 'this_is_a_subclass_string' property " + "for GetAll call of subclass interface\n", object_path); + } + + /* Test the uint property */ + value = g_hash_table_lookup (hash, "this_is_a_subclass_uint"); + if (!value) + { + lose ("%s: Unexpected missing 'this_is_a_subclass_uint' property for " + "GetAll call of subclass interface\n", object_path); + } + if (!G_VALUE_HOLDS_UINT (value)) + { + lose ("%s: Unexpected wrong type for 'this_is_a_subclass_uint' " + "property for GetAll call of subclass interface\n", object_path); + } + num = g_value_get_uint (value); + if (num != 1234567) + { + lose ("%s: Unexpected value for 'this_is_a_subclass_uint' property " + "for GetAll call of subclass interface\n", object_path); + } + + g_hash_table_destroy (hash); + hash = NULL; + } + + g_object_unref (proxy); + return TRUE; +} static void lose (const char *str, ...) @@ -1839,6 +2008,24 @@ main (int argc, char **argv) g_object_unref (property_proxy); property_proxy = NULL; + /* Test GetAll */ + /* 'testing value' set earlier by the SetProperty tests */ + test_base_class_get_all (connection, + "/org/freedesktop/DBus/GLib/Tests/MyTestObject", + "testing value"); + + /* "" is base class default for this_is_a_string property since the + * property isn't marked with G_PARAM_CONSTRUT. + */ + test_base_class_get_all (connection, + "/org/freedesktop/DBus/GLib/Tests/MyTestObjectSubclass", + ""); + + /* Finally test GetAll of a subclass on a different interface to ensure that + * the right properties are returned (fdo #19145) + */ + test_subclass_get_all (connection, "/org/freedesktop/DBus/GLib/Tests/MyTestObjectSubclass"); + test_terminate_proxy1 = dbus_g_proxy_new_for_name_owner (connection, "org.freedesktop.DBus.GLib.TestService", "/org/freedesktop/DBus/GLib/Tests/MyTestObject", diff --git a/test/core/test-service-glib-subclass.xml b/test/core/test-service-glib-subclass.xml new file mode 100644 index 0000000..ffa34de --- /dev/null +++ b/test/core/test-service-glib-subclass.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/test/core/test-service-glib.c b/test/core/test-service-glib.c index bb9157e..1cdb0ac 100644 --- a/test/core/test-service-glib.c +++ b/test/core/test-service-glib.c @@ -12,9 +12,11 @@ #include #include "my-object.h" +#include "my-object-subclass.h" static GObject *obj; static GObject *obj2; +static GObject *subobj; GMainLoop *loop; #define TEST_SERVICE_NAME "org.freedesktop.DBus.GLib.TestService" @@ -59,6 +61,7 @@ main (int argc, char **argv) obj = g_object_new (MY_TYPE_OBJECT, NULL); obj2 = g_object_new (MY_TYPE_OBJECT, NULL); + subobj = g_object_new (MY_TYPE_OBJECT_SUBCLASS, NULL); dbus_g_connection_register_g_object (connection, "/org/freedesktop/DBus/GLib/Tests/MyTestObject", @@ -67,6 +70,10 @@ main (int argc, char **argv) "/org/freedesktop/DBus/GLib/Tests/MyTestObject2", obj2); + dbus_g_connection_register_g_object (connection, + "/org/freedesktop/DBus/GLib/Tests/MyTestObjectSubclass", + subobj); + driver_proxy = dbus_g_proxy_new_for_name (connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, diff --git a/test/core/test-service-glib.xml b/test/core/test-service-glib.xml index ae2ea18..56071ab 100644 --- a/test/core/test-service-glib.xml +++ b/test/core/test-service-glib.xml @@ -2,6 +2,8 @@ + +