GActionGroup is now an interface
[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   g_free (name);
54
55   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
56   g_assert (!a.did_run);
57   g_action_activate (action, NULL);
58   g_assert (a.did_run);
59   a.did_run = FALSE;
60
61   g_action_set_enabled (action, FALSE);
62   g_action_activate (action, NULL);
63   g_assert (!a.did_run);
64
65   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
66     {
67       g_action_activate (action, g_variant_new_string ("xxx"));
68       exit (0);
69     }
70   g_test_trap_assert_failed ();
71
72   g_object_unref (action);
73   g_assert (!a.did_run);
74
75   action = g_action_new ("foo", G_VARIANT_TYPE_STRING);
76   g_assert (g_action_get_enabled (action));
77   g_assert (g_variant_type_equal (g_action_get_parameter_type (action), G_VARIANT_TYPE_STRING));
78   g_assert (g_action_get_state_type (action) == NULL);
79   g_assert (g_action_get_state_hint (action) == NULL);
80   g_assert (g_action_get_state (action) == NULL);
81
82   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
83   g_assert (!a.did_run);
84   g_action_activate (action, g_variant_new_string ("Hello world"));
85   g_assert (a.did_run);
86   g_assert_cmpstr (g_variant_get_string (a.params, NULL), ==, "Hello world");
87   g_variant_unref (a.params);
88   a.did_run = FALSE;
89
90   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
91     {
92       g_action_activate (action, NULL);
93       exit (0);
94     }
95
96   g_test_trap_assert_failed ();
97
98   g_object_unref (action);
99   g_assert (!a.did_run);
100 }
101
102 static gboolean
103 strv_has_string (gchar       **haystack,
104                  const gchar  *needle)
105 {
106   guint n;
107
108   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
109     {
110       if (g_strcmp0 (haystack[n], needle) == 0)
111         return TRUE;
112     }
113   return FALSE;
114 }
115
116 static gboolean
117 strv_set_equal (gchar **strv, ...)
118 {
119   gint count;
120   va_list list;
121   const gchar *str;
122   gboolean res;
123
124   res = TRUE;
125   count = 0;
126   va_start (list, strv);
127   while (1)
128     {
129       str = va_arg (list, const gchar *);
130       if (str == NULL)
131         break;
132       if (!strv_has_string (strv, str))
133         {
134           res = FALSE;
135           break;
136         }
137       count++;
138     }
139   va_end (list);
140
141   if (res)
142     res = g_strv_length ((gchar**)strv) == count;
143
144   return res;
145 }
146
147 static void
148 test_simple_group (void)
149 {
150   GSimpleActionGroup *group;
151   Activation a = { 0, };
152   GAction *action;
153   gchar **actions;
154   GVariant *state;
155
156   action = g_action_new ("foo", NULL);
157   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
158   g_assert (!a.did_run);
159   g_action_activate (action, NULL);
160   g_assert (a.did_run);
161   a.did_run = FALSE;
162
163   group = g_simple_action_group_new ();
164   g_simple_action_group_insert (group, action);
165   g_object_unref (action);
166
167   g_assert (!a.did_run);
168   g_action_group_activate (G_ACTION_GROUP (group), "foo", NULL);
169   g_assert (a.did_run);
170
171   action = g_action_new_stateful ("bar", G_VARIANT_TYPE_STRING, g_variant_new_string ("hihi"));
172   g_simple_action_group_insert (group, action);
173   g_object_unref (action);
174
175   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "foo"));
176   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "bar"));
177   g_assert (!g_action_group_has_action (G_ACTION_GROUP (group), "baz"));
178   actions = g_action_group_list_actions (G_ACTION_GROUP (group));
179   g_assert_cmpint (g_strv_length (actions), ==, 2);
180   g_assert (strv_set_equal (actions, "foo", "bar", NULL));
181   g_strfreev (actions);
182   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "foo"));
183   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
184   g_assert (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "foo") == NULL);
185   g_assert (g_variant_type_equal (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
186   g_assert (g_action_group_get_state_type (G_ACTION_GROUP (group), "foo") == NULL);
187   g_assert (g_variant_type_equal (g_action_group_get_state_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
188   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "foo") == NULL);
189   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "bar") == NULL);
190   g_assert (g_action_group_get_state (G_ACTION_GROUP (group), "foo") == NULL);
191   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
192   g_assert (g_variant_type_equal (g_variant_get_type (state), G_VARIANT_TYPE_STRING));
193   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hihi");
194   g_variant_unref (state);
195
196   g_action_group_set_state (G_ACTION_GROUP (group), "bar", g_variant_new_string ("boo"));
197   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
198   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "boo");
199   g_variant_unref (state);
200
201   action = g_simple_action_group_lookup (group, "bar");
202   g_action_set_enabled (action, FALSE);
203   g_assert (!g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
204
205   g_simple_action_group_remove (group, "bar");
206   action = g_simple_action_group_lookup (group, "foo");
207   g_assert_cmpstr (g_action_get_name (action), ==, "foo");
208   action = g_simple_action_group_lookup (group, "bar");
209   g_assert (action == NULL);
210
211   a.did_run = FALSE;
212   g_object_unref (group);
213   g_assert (!a.did_run);
214 }
215
216 static void
217 test_stateful (void)
218 {
219   GVariant *state;
220   GAction *action;
221
222   action = g_action_new_stateful ("foo", NULL, g_variant_new_string ("hihi"));
223   g_assert (g_action_get_enabled (action));
224   g_assert (g_action_get_parameter_type (action) == NULL);
225   g_assert (g_action_get_state_hint (action) == NULL);
226   g_assert (g_variant_type_equal (g_action_get_state_type (action),
227                                   G_VARIANT_TYPE_STRING));
228   state = g_action_get_state (action);
229   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hihi");
230   g_variant_unref (state);
231
232   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
233     {
234       g_action_set_state (action, g_variant_new_int32 (123));
235       exit (0);
236     }
237   g_test_trap_assert_failed ();
238
239   g_action_set_state (action, g_variant_new_string ("hello"));
240   state = g_action_get_state (action);
241   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hello");
242   g_variant_unref (state);
243
244   g_object_unref (action);
245
246   action = g_action_new ("foo", NULL);
247   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
248     {
249       g_action_set_state (action, g_variant_new_int32 (123));
250       exit (0);
251     }
252   g_test_trap_assert_failed ();
253   g_object_unref (action);
254 }
255
256 int
257 main (int argc, char **argv)
258 {
259   g_type_init ();
260   g_test_init (&argc, &argv, NULL);
261
262   g_test_add_func ("/actions/basic", test_basic);
263   g_test_add_func ("/actions/simplegroup", test_simple_group);
264   g_test_add_func ("/actions/stateful", test_stateful);
265
266   return g_test_run ();
267 }