add testcase for GAction
authorRyan Lortie <desrt@desrt.ca>
Wed, 18 Aug 2010 05:55:48 +0000 (01:55 -0400)
committerRyan Lortie <desrt@desrt.ca>
Wed, 18 Aug 2010 05:55:48 +0000 (01:55 -0400)
fix some small bugs it found

gio/gaction.c
gio/tests/.gitignore
gio/tests/Makefile.am
gio/tests/actions.c [new file with mode: 0644]

index 6c865c3..bca7378 100644 (file)
@@ -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));
 }
index 5aecb43..1031dfe 100644 (file)
@@ -1,3 +1,4 @@
+actions
 appinfo
 appinfo-test
 application
index c1d19b9..bb09a86 100644 (file)
@@ -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 (file)
index 0000000..326bb59
--- /dev/null
@@ -0,0 +1,81 @@
+#include <gio/gio.h>
+#include <stdlib.h>
+
+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 ();
+}