From 8014e9c6e67958103ef490e1bc753e4426751751 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Wed, 18 Aug 2010 01:55:48 -0400 Subject: [PATCH] add testcase for GAction fix some small bugs it found --- gio/gaction.c | 9 ++++-- gio/tests/.gitignore | 1 + gio/tests/Makefile.am | 3 ++ gio/tests/actions.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 gio/tests/actions.c diff --git a/gio/gaction.c b/gio/gaction.c index 6c865c3..bca7378 100644 --- a/gio/gaction.c +++ b/gio/gaction.c @@ -141,7 +141,8 @@ g_action_set_property (GObject *object, guint prop_id, break; case PROP_STATE: - g_action_set_state (action, g_value_get_variant (value)); + if (g_value_get_variant (value)) + g_action_set_state (action, g_value_get_variant (value)); break; default: @@ -270,7 +271,8 @@ g_action_class_init (GActionClass *class) g_object_class_install_property (object_class, PROP_ENABLED, g_param_spec_boolean ("enabled", "enabled", "if the action can be activated", TRUE, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); /** * GAction:state-type: @@ -292,7 +294,8 @@ g_action_class_init (GActionClass *class) g_object_class_install_property (object_class, PROP_STATE, g_param_spec_variant ("state", "state", "the state the action is in", G_VARIANT_TYPE_ANY, NULL, - G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + G_PARAM_CONSTRUCT | G_PARAM_READWRITE | + G_PARAM_STATIC_STRINGS)); g_type_class_add_private (class, sizeof (GActionPrivate)); } diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore index 5aecb43..1031dfe 100644 --- a/gio/tests/.gitignore +++ b/gio/tests/.gitignore @@ -1,3 +1,4 @@ +actions appinfo appinfo-test application diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am index c1d19b9..bb09a86 100644 --- a/gio/tests/Makefile.am +++ b/gio/tests/Makefile.am @@ -18,6 +18,7 @@ progs_ldadd = \ $(top_builddir)/gio/libgio-2.0.la TEST_PROGS += \ + actions \ memory-input-stream \ memory-output-stream \ readwrite \ @@ -94,6 +95,8 @@ if OS_WIN32 TEST_PROGS += win32-streams endif +actions_LDADD = $(progs_ldadd) + memory_input_stream_SOURCES = memory-input-stream.c memory_input_stream_LDADD = $(progs_ldadd) diff --git a/gio/tests/actions.c b/gio/tests/actions.c new file mode 100644 index 0000000..326bb59 --- /dev/null +++ b/gio/tests/actions.c @@ -0,0 +1,81 @@ +#include +#include + +typedef struct +{ + GVariant *params; + gboolean did_run; +} Activation; + +static void +activate (GAction *action, + GVariant *parameter, + gpointer user_data) +{ + Activation *activation = user_data; + + if (parameter) + activation->params = g_variant_ref (parameter); + else + activation->params = NULL; + activation->did_run = TRUE; +} + +static void +test_basic (void) +{ + Activation a = { 0, }; + GAction *action; + + action = g_action_new ("foo", NULL); + g_signal_connect (action, "activate", G_CALLBACK (activate), &a); + g_assert (!a.did_run); + g_action_activate (action, NULL); + g_assert (a.did_run); + a.did_run = FALSE; + + g_action_set_enabled (action, FALSE); + g_action_activate (action, NULL); + g_assert (!a.did_run); + + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) + { + g_action_activate (action, g_variant_new_string ("xxx")); + exit (0); + } + g_test_trap_assert_failed (); + + g_object_unref (action); + g_assert (!a.did_run); + + action = g_action_new ("foo", G_VARIANT_TYPE_STRING); + g_signal_connect (action, "activate", G_CALLBACK (activate), &a); + g_assert (!a.did_run); + g_action_activate (action, g_variant_new_string ("Hello world")); + g_assert (a.did_run); + g_assert_cmpstr (g_variant_get_string (a.params, NULL), ==, "Hello world"); + g_variant_unref (a.params); + a.did_run = FALSE; + + if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) + { + g_action_activate (action, NULL); + exit (0); + } + + g_test_trap_assert_failed (); + + g_object_unref (action); + g_assert (!a.did_run); +} + +int +main (int argc, char **argv) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/actions/basic", test_basic); + + return g_test_run (); +} -- 2.7.4