Improve test coverage for actions and action groups
[platform/upstream/glib.git] / gio / tests / actions.c
1 #include <gio/gio.h>
2 #include <stdlib.h>
3
4 typedef struct
5 {
6   GVariant *params;
7   gboolean did_run;
8 } Activation;
9
10 static void
11 activate (GAction  *action,
12           GVariant *parameter,
13           gpointer  user_data)
14 {
15   Activation *activation = user_data;
16
17   if (parameter)
18     activation->params = g_variant_ref (parameter);
19   else
20     activation->params = NULL;
21   activation->did_run = TRUE;
22 }
23
24 static void
25 test_basic (void)
26 {
27   Activation a = { 0, };
28   GAction *action;
29   const gchar *name;
30   GVariantType *parameter_type;
31   gboolean enabled;
32   GVariantType *state_type;
33   GVariant *state;
34
35   action = g_action_new ("foo", NULL);
36   g_assert (g_action_get_enabled (action));
37   g_assert (g_action_get_parameter_type (action) == NULL);
38   g_assert (g_action_get_state_type (action) == NULL);
39   g_assert (g_action_get_state_hint (action) == NULL);
40   g_assert (g_action_get_state (action) == NULL);
41   g_object_get (action,
42                 "name", &name,
43                 "parameter-type", &parameter_type,
44                 "enabled", &enabled,
45                 "state-type", &state_type,
46                 "state", &state,
47                  NULL);
48   g_assert_cmpstr (name, ==, "foo");
49   g_assert (parameter_type == NULL);
50   g_assert (enabled);
51   g_assert (state_type == NULL);
52   g_assert (state == NULL);
53
54   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
55   g_assert (!a.did_run);
56   g_action_activate (action, NULL);
57   g_assert (a.did_run);
58   a.did_run = FALSE;
59
60   g_action_set_enabled (action, FALSE);
61   g_action_activate (action, NULL);
62   g_assert (!a.did_run);
63
64   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
65     {
66       g_action_activate (action, g_variant_new_string ("xxx"));
67       exit (0);
68     }
69   g_test_trap_assert_failed ();
70
71   g_object_unref (action);
72   g_assert (!a.did_run);
73
74   action = g_action_new ("foo", G_VARIANT_TYPE_STRING);
75   g_assert (g_action_get_enabled (action));
76   g_assert (g_variant_type_equal (g_action_get_parameter_type (action), G_VARIANT_TYPE_STRING));
77   g_assert (g_action_get_state_type (action) == NULL);
78   g_assert (g_action_get_state_hint (action) == NULL);
79   g_assert (g_action_get_state (action) == NULL);
80
81   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
82   g_assert (!a.did_run);
83   g_action_activate (action, g_variant_new_string ("Hello world"));
84   g_assert (a.did_run);
85   g_assert_cmpstr (g_variant_get_string (a.params, NULL), ==, "Hello world");
86   g_variant_unref (a.params);
87   a.did_run = FALSE;
88
89   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
90     {
91       g_action_activate (action, NULL);
92       exit (0);
93     }
94
95   g_test_trap_assert_failed ();
96
97   g_object_unref (action);
98   g_assert (!a.did_run);
99 }
100
101 static gboolean
102 strv_has_string (const gchar **haystack,
103                  const gchar  *needle)
104 {
105   guint n;
106
107   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
108     {
109       if (g_strcmp0 (haystack[n], needle) == 0)
110         return TRUE;
111     }
112   return FALSE;
113 }
114
115 static gboolean
116 strv_set_equal (const gchar **strv, ...)
117 {
118   gint count;
119   va_list list;
120   const gchar *str;
121   gboolean res;
122
123   res = TRUE;
124   count = 0;
125   va_start (list, strv);
126   while (1)
127     {
128       str = va_arg (list, const gchar *);
129       if (str == NULL)
130         break;
131       if (!strv_has_string (strv, str))
132         {
133           res = FALSE;
134           break;
135         }
136       count++;
137     }
138   va_end (list);
139
140   if (res)
141     res = g_strv_length ((gchar**)strv) == count;
142
143   return res;
144 }
145
146 static void
147 test_simple_group (void)
148 {
149   GSimpleActionGroup *group;
150   Activation a = { 0, };
151   GAction *action;
152   gchar **actions;
153   GVariant *state;
154
155   action = g_action_new ("foo", NULL);
156   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
157   g_assert (!a.did_run);
158   g_action_activate (action, NULL);
159   g_assert (a.did_run);
160   a.did_run = FALSE;
161
162   group = g_simple_action_group_new ();
163   g_simple_action_group_insert (group, action);
164   g_object_unref (action);
165
166   g_assert (!a.did_run);
167   g_action_group_activate (G_ACTION_GROUP (group), "foo", NULL);
168   g_assert (a.did_run);
169
170   action = g_action_new_stateful ("bar", G_VARIANT_TYPE_STRING, g_variant_new_string ("hihi"));
171   g_simple_action_group_insert (group, action);
172   g_object_unref (action);
173
174   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "foo"));
175   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "bar"));
176   g_assert (!g_action_group_has_action (G_ACTION_GROUP (group), "baz"));
177   actions = g_action_group_list_actions (G_ACTION_GROUP (group));
178   g_assert_cmpint (g_strv_length (actions), ==, 2);
179   g_assert (strv_set_equal (actions, "foo", "bar", NULL));
180   g_strfreev (actions);
181   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "foo"));
182   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
183   g_assert (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "foo") == NULL);
184   g_assert (g_variant_type_equal (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
185   g_assert (g_action_group_get_state_type (G_ACTION_GROUP (group), "foo") == NULL);
186   g_assert (g_variant_type_equal (g_action_group_get_state_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
187   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "foo") == NULL);
188   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "bar") == NULL);
189   g_assert (g_action_group_get_state (G_ACTION_GROUP (group), "foo") == NULL);
190   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
191   g_assert (g_variant_type_equal (g_variant_get_type (state), G_VARIANT_TYPE_STRING));
192   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hihi");
193
194   g_action_group_set_state (G_ACTION_GROUP (group), "bar", g_variant_new_string ("boo"));
195   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
196   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "boo");
197
198   g_simple_action_group_set_enabled (group, "bar", FALSE);
199   g_assert (!g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
200
201   g_simple_action_group_remove (group, "bar");
202   action = g_simple_action_group_lookup (group, "foo");
203   g_assert_cmpstr (g_action_get_name (action), ==, "foo");
204   action = g_simple_action_group_lookup (group, "bar");
205   g_assert (action == NULL);
206
207   a.did_run = FALSE;
208   g_object_unref (group);
209   g_assert (!a.did_run);
210 }
211
212 static void
213 test_stateful (void)
214 {
215   GAction *action;
216
217   action = g_action_new_stateful ("foo", NULL, g_variant_new_string ("hihi"));
218   g_assert (g_action_get_enabled (action));
219   g_assert (g_action_get_parameter_type (action) == NULL);
220   g_assert (g_action_get_state_hint (action) == NULL);
221   g_assert (g_variant_type_equal (g_action_get_state_type (action),
222                                   G_VARIANT_TYPE_STRING));
223   g_assert_cmpstr (g_variant_get_string (g_action_get_state (action), NULL),
224                    ==, "hihi");
225
226   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
227     {
228       g_action_set_state (action, g_variant_new_int32 (123));
229       exit (0);
230     }
231   g_test_trap_assert_failed ();
232
233   g_action_set_state (action, g_variant_new_string ("hello"));
234   g_assert_cmpstr (g_variant_get_string (g_action_get_state (action), NULL),
235                    ==, "hello");
236
237   g_object_unref (action);
238
239   action = g_action_new ("foo", NULL);
240   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
241     {
242       g_action_set_state (action, g_variant_new_int32 (123));
243       exit (0);
244     }
245   g_test_trap_assert_failed ();
246   g_object_unref (action);
247 }
248
249 int
250 main (int argc, char **argv)
251 {
252   g_type_init ();
253   g_test_init (&argc, &argv, NULL);
254
255   g_test_add_func ("/actions/basic", test_basic);
256   g_test_add_func ("/actions/simplegroup", test_simple_group);
257   g_test_add_func ("/actions/stateful", test_stateful);
258
259   return g_test_run ();
260 }