[glib] Add testcase for GSimpleAction::change-state



commit d5915a4be3e790daf99c306c2d6651328b8dbb66
Author: Ryan Lortie <desrt desrt ca>
Date:   Wed Jun 29 14:44:58 2011 +0100

    Add testcase for GSimpleAction::change-state

 gio/tests/actions.c |   73 ++++++++++++++++++++++++++++++---------------------
 1 files changed, 43 insertions(+), 30 deletions(-)
---
diff --git a/gio/tests/actions.c b/gio/tests/actions.c
index 27f2428..757ecc9 100644
--- a/gio/tests/actions.c
+++ b/gio/tests/actions.c
@@ -328,37 +328,49 @@ test_entries (void)
   g_object_unref (actions);
 }
 
- static void
- activate_quit (GSimpleAction *simple,
-                GVariant      *parameter,
-                gpointer       user_data)
- {
-   exit (0);
- }
-
- static void
- activate_print_string (GSimpleAction *simple,
-                        GVariant      *parameter,
-                        gpointer       user_data)
- {
-   g_print ("%s\n", g_variant_get_string (parameter, NULL));
- }
-
- static GActionGroup *
- create_action_group (void)
- {
-   const GActionEntry entries[] = {
-     { "quit",         activate_quit              },
-     { "print-string", activate_print_string, "s" }
-   };
-   GSimpleActionGroup *group;
-
-   group = g_simple_action_group_new ();
-   g_simple_action_group_add_entries (group, entries, G_N_ELEMENTS (entries), NULL);
-
-   return G_ACTION_GROUP (group);
- }
 
+/* /actions/change-state */
+static void
+change_volume_state (GSimpleAction *action,
+                     GVariant      *value,
+                     gpointer       user_data)
+{
+  gint requested;
+
+  requested = g_variant_get_int32 (value);
+
+  /* Volume only goes from 0 to 10 */
+  if (0 <= requested && requested <= 10)
+    g_simple_action_set_state (action, value);
+}
+
+static void
+test_change_state (void)
+{
+  GSimpleAction *action;
+  GVariant *state;
+
+  action = g_simple_action_new_stateful ("volume", NULL,
+                                         g_variant_new_int32 (0));
+  g_signal_connect (action, "change-state",
+                    G_CALLBACK (change_volume_state), NULL);
+
+  state = g_action_get_state (G_ACTION (action));
+  g_assert_cmpint (g_variant_get_int32 (state), ==, 0);
+  g_variant_unref (state);
+
+  /* should change */
+  g_action_change_state (G_ACTION (action), g_variant_new_int32 (7));
+  state = g_action_get_state (G_ACTION (action));
+  g_assert_cmpint (g_variant_get_int32 (state), ==, 7);
+  g_variant_unref (state);
+
+  /* should not change */
+  g_action_change_state (G_ACTION (action), g_variant_new_int32 (11));
+  state = g_action_get_state (G_ACTION (action));
+  g_assert_cmpint (g_variant_get_int32 (state), ==, 7);
+  g_variant_unref (state);
+}
 
 int
 main (int argc, char **argv)
@@ -370,6 +382,7 @@ main (int argc, char **argv)
   g_test_add_func ("/actions/simplegroup", test_simple_group);
   g_test_add_func ("/actions/stateful", test_stateful);
   g_test_add_func ("/actions/entries", test_entries);
+  g_test_add_func ("/actions/change-state", test_change_state);
 
   return g_test_run ();
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]