unit: Add gdbus/client_set_string_property
Luiz Augusto von Dentz
maintainerPATCH v2 1/3 unit: Add gdbus/client_set_string_property
From: Luiz Augusto von Dentz <[email protected]>
--- unit/test-gdbus-client.c | 112 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 3 deletions(-)
unit/test-gdbus-client.c +109 -3
@@ -35,6 +35,7 @@ struct context {GMainLoop *main_loop;DBusConnection *dbus_conn;GDBusClient *dbus_client; +void *data;guint timeout_source; };
@@ -98,6 +99,7 @@ static void destroy_context(struct context *context)g_main_loop_unref (context->main_loop); +g_free (context->data);g_free (context); }
@@ -353,9 +355,9 @@ static void proxy_get_string(GDBusProxy *proxy, void *user_data)static gboolean get_string (const GDBusPropertyTable *property,DBusMessageIter *iter,void *data) { -const char *string ="value" ; +struct context *context = data; -dbus_message_iter_append_basic (iter,DBUS_TYPE_STRING , &string); +dbus_message_iter_append_basic (iter,DBUS_TYPE_STRING , &context->data);return TRUE; }
@@ -371,10 +373,11 @@ static void client_get_string_property(void)if (context ==NULL )return ; + context->data =g_strdup ("value" );g_dbus_register_interface (context->dbus_conn,SERVICE_PATH ,SERVICE_NAME , methods, signals, string_properties, -NULL ,NULL ); + context,NULL ); context->dbus_client =g_dbus_client_new (context->dbus_conn,SERVICE_NAME ,SERVICE_PATH );
@@ -592,6 +595,106 @@ static void client_get_uint64_property(void)destroy_context (context); } +static void property_set_success (const DBusError *err,void *user_data) +{ +g_assert (!dbus_error_is_set (err)); +} + +static void proxy_set_string (GDBusProxy *proxy,void *user_data) +{ +DBusMessageIter iter; +const char *string; + +if (g_test_verbose ()) +g_print ("proxy %s found\n" , +g_dbus_proxy_get_interface (proxy)); + +g_assert (g_dbus_proxy_get_property (proxy,"String" , &iter)); +g_assert (dbus_message_iter_get_arg_type (&iter) ==DBUS_TYPE_STRING ); + +dbus_message_iter_get_basic (&iter, &string); +g_assert (g_strcmp0 (string,"value" ) ==0 ); + + string ="value1" ; +g_assert (g_dbus_proxy_set_property_basic (proxy,"String" , +DBUS_TYPE_STRING , &string, + property_set_success, user_data, +NULL )); +} + +static void property_string_changed (GDBusProxy *proxy,const char *name, +DBusMessageIter *iter,void *user_data) +{ +struct context *context = user_data; +const char *string; + +if (g_test_verbose ()) +g_print ("property %s changed\n" , name); + +g_assert (g_strcmp0 (name,"String" ) ==0 ); +g_assert (dbus_message_iter_get_arg_type (iter) ==DBUS_TYPE_STRING ); + +dbus_message_iter_get_basic (iter, &string); +g_assert (g_strcmp0 (string,"value1" ) ==0 ); + +g_dbus_client_unref (context->dbus_client); +} + +static void set_string (const GDBusPropertyTable *property, +DBusMessageIter *iter,GDBusPendingPropertySet id, +void *data) +{ +struct context *context = data; +const char *string; + +g_assert (dbus_message_iter_get_arg_type (iter) ==DBUS_TYPE_STRING ); + +dbus_message_iter_get_basic (iter, &string); +g_assert (g_strcmp0 (string,"value1" ) ==0 ); + +g_free (context->data); + context->data =g_strdup (string); + +g_dbus_emit_property_changed (context->dbus_conn,SERVICE_PATH , +SERVICE_NAME ,"String" ); + +g_dbus_pending_property_success (id); +} + +static void client_set_string_property (void ) +{ +struct context *context =create_context (); +static const GDBusPropertyTable string_properties[] = { + {"String" ,"s" , get_string, set_string }, + { }, + }; + +if (context ==NULL ) +return ; + + context->data =g_strdup ("value" ); +g_dbus_register_interface (context->dbus_conn, +SERVICE_PATH ,SERVICE_NAME , + methods, signals, string_properties, + context,NULL ); + + context->dbus_client =g_dbus_client_new (context->dbus_conn, +SERVICE_NAME ,SERVICE_PATH ); + +g_dbus_client_set_disconnect_watch (context->dbus_client, + disconnect_handler, context); +g_dbus_client_set_proxy_handlers (context->dbus_client, proxy_set_string, +NULL , property_string_changed, + context); + +g_main_loop_run (context->main_loop); + +g_dbus_unregister_interface (context->dbus_conn, +SERVICE_PATH ,SERVICE_NAME ); + +destroy_context (context); +} +int main (int argc,char *argv[]) {g_test_init (&argc, &argv,NULL );
@@ -616,5 +719,8 @@ int main(int argc, char *argv[])g_test_add_func ("/gdbus/client_get_dict_property" , client_get_dict_property); +g_test_add_func ("/gdbus/client_set_string_property" , + client_set_string_property); +return g_test_run (); }
Signature · 1 line
1.8.1.2
Luiz Augusto von Dentz
maintainerPATCH v2 2/3 unit: Add gdbus/client_string_changed
From: Luiz Augusto von Dentz <[email protected]>
---
v2: Emit property change on idle to better emulate the async nature and add a timeout so the test doesn't hang forever.
unit/test-gdbus-client.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+)
unit/test-gdbus-client.c +73 -0
@@ -695,6 +695,76 @@ static void client_set_string_property(void)destroy_context (context); } +static gboolean string_exists (const GDBusPropertyTable *property,void *data) +{ +struct context *context = data; + +return context->data !=NULL ; +} + +static gboolean emit_string_change (void *user_data) +{ +struct context *context = user_data; + + context->data =g_strdup ("value1" ); + +g_dbus_emit_property_changed (context->dbus_conn,SERVICE_PATH , +SERVICE_NAME ,"String" ); + + context->timeout_source =g_timeout_add_seconds (2 , timeout_handler, + context); + +return FALSE; +} + +static void proxy_string_changed (GDBusProxy *proxy,void *user_data) +{ +struct context *context = user_data; +DBusMessageIter iter; + +if (g_test_verbose ()) +g_print ("proxy %s found\n" , +g_dbus_proxy_get_interface (proxy)); + +g_assert (!g_dbus_proxy_get_property (proxy,"String" , &iter)); + +g_idle_add (emit_string_change, context); +} + +static void client_string_changed (void ) +{ +struct context *context =create_context (); +static const GDBusPropertyTable string_properties[] = { + {"String" ,"s" , get_string,NULL , string_exists }, + { }, + }; + +if (context ==NULL ) +return ; + +g_dbus_register_interface (context->dbus_conn, +SERVICE_PATH ,SERVICE_NAME , + methods, signals, string_properties, + context,NULL ); + + context->dbus_client =g_dbus_client_new (context->dbus_conn, +SERVICE_NAME ,SERVICE_PATH ); + +g_dbus_client_set_disconnect_watch (context->dbus_client, + disconnect_handler, context); +g_dbus_client_set_proxy_handlers (context->dbus_client, + proxy_string_changed,NULL , + property_string_changed, + context); + +g_main_loop_run (context->main_loop); + +g_dbus_unregister_interface (context->dbus_conn, +SERVICE_PATH ,SERVICE_NAME ); + +destroy_context (context); +} +int main (int argc,char *argv[]) {g_test_init (&argc, &argv,NULL );
@@ -722,5 +792,8 @@ int main(int argc, char *argv[])g_test_add_func ("/gdbus/client_set_string_property" , client_set_string_property); +g_test_add_func ("/gdbus/client_string_changed" , + client_string_changed); +return g_test_run (); }
Signature · 1 line
1.8.1.2
Luiz Augusto von Dentz
maintainerPATCH v2 3/3 gdbus: Fix not calling property_changed callback
In reply to Luiz Augusto von DentzFrom: Luiz Augusto von Dentz <[email protected]>
In case a property did not appear by the time proxy_added was called property_changed has to be called if it appear latter otherwise the application will be unaware of it.
--- gdbus/client.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
gdbus/client.c +10 -13
@@ -205,6 +205,7 @@ static void prop_entry_free(gpointer data)static void add_property (GDBusProxy *proxy,const char *name,DBusMessageIter *iter,gboolean send_changed) { +GDBusClient *client = proxy->client;DBusMessageIter value;struct prop_entry *prop;
@@ -215,20 +216,8 @@ static void add_property(GDBusProxy *proxy, const char *name, prop =g_hash_table_lookup (proxy->prop_list, name);if (prop !=NULL ) { -GDBusClient *client = proxy->client; -prop_entry_update (prop, &value); - -if (proxy->prop_func) - proxy->prop_func (proxy, name, &value, proxy->prop_data); - -if (client ==NULL || send_changed == FALSE) -return ; - -if (client->property_changed) - client->property_changed (proxy, name, &value, - client->user_data); -return ; + goto done; } prop =prop_entry_new (name, &value);
@@ -237,8 +226,16 @@ static void add_property(GDBusProxy *proxy, const char *name,g_hash_table_replace (proxy->prop_list, prop->name, prop); +done:if (proxy->prop_func) proxy->prop_func (proxy, name, &value, proxy->prop_data); + +if (client ==NULL || send_changed == FALSE) +return ; + +if (client->property_changed) + client->property_changed (proxy, name, &value, + client->user_data); } static void update_properties(GDBusProxy *proxy, DBusMessageIter *iter,
Signature · 1 line
1.8.1.2