Merge remote branch 'gvdb/master'
[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   GSimpleAction *action;
29   gchar *name;
30   GVariantType *parameter_type;
31   gboolean enabled;
32   GVariantType *state_type;
33   GVariant *state;
34
35   action = g_simple_action_new ("foo", NULL);
36   g_assert (g_action_get_enabled (G_ACTION (action)));
37   g_assert (g_action_get_parameter_type (G_ACTION (action)) == NULL);
38   g_assert (g_action_get_state_type (G_ACTION (action)) == NULL);
39   g_assert (g_action_get_state_hint (G_ACTION (action)) == NULL);
40   g_assert (g_action_get_state (G_ACTION (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 (G_ACTION (action), NULL);
58   g_assert (a.did_run);
59   a.did_run = FALSE;
60
61   g_simple_action_set_enabled (action, FALSE);
62   g_action_activate (G_ACTION (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 (G_ACTION (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_simple_action_new ("foo", G_VARIANT_TYPE_STRING);
76   g_assert (g_action_get_enabled (G_ACTION (action)));
77   g_assert (g_variant_type_equal (g_action_get_parameter_type (G_ACTION (action)), G_VARIANT_TYPE_STRING));
78   g_assert (g_action_get_state_type (G_ACTION (action)) == NULL);
79   g_assert (g_action_get_state_hint (G_ACTION (action)) == NULL);
80   g_assert (g_action_get_state (G_ACTION (action)) == NULL);
81
82   g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
83   g_assert (!a.did_run);
84   g_action_activate (G_ACTION (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 (G_ACTION (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   GSimpleAction *simple;
153   GAction *action;
154   gchar **actions;
155   GVariant *state;
156
157   simple = g_simple_action_new ("foo", NULL);
158   g_signal_connect (simple, "activate", G_CALLBACK (activate), &a);
159   g_assert (!a.did_run);
160   g_action_activate (G_ACTION (simple), NULL);
161   g_assert (a.did_run);
162   a.did_run = FALSE;
163
164   group = g_simple_action_group_new ();
165   g_simple_action_group_insert (group, G_ACTION (simple));
166   g_object_unref (simple);
167
168   g_assert (!a.did_run);
169   g_action_group_activate (G_ACTION_GROUP (group), "foo", NULL);
170   g_assert (a.did_run);
171
172   simple = g_simple_action_new_stateful ("bar", G_VARIANT_TYPE_STRING, g_variant_new_string ("hihi"));
173   g_simple_action_group_insert (group, G_ACTION (simple));
174   g_object_unref (simple);
175
176   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "foo"));
177   g_assert (g_action_group_has_action (G_ACTION_GROUP (group), "bar"));
178   g_assert (!g_action_group_has_action (G_ACTION_GROUP (group), "baz"));
179   actions = g_action_group_list_actions (G_ACTION_GROUP (group));
180   g_assert_cmpint (g_strv_length (actions), ==, 2);
181   g_assert (strv_set_equal (actions, "foo", "bar", NULL));
182   g_strfreev (actions);
183   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "foo"));
184   g_assert (g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
185   g_assert (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "foo") == NULL);
186   g_assert (g_variant_type_equal (g_action_group_get_parameter_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
187   g_assert (g_action_group_get_state_type (G_ACTION_GROUP (group), "foo") == NULL);
188   g_assert (g_variant_type_equal (g_action_group_get_state_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
189   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "foo") == NULL);
190   g_assert (g_action_group_get_state_hint (G_ACTION_GROUP (group), "bar") == NULL);
191   g_assert (g_action_group_get_state (G_ACTION_GROUP (group), "foo") == NULL);
192   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
193   g_assert (g_variant_type_equal (g_variant_get_type (state), G_VARIANT_TYPE_STRING));
194   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hihi");
195   g_variant_unref (state);
196
197   g_action_group_set_state (G_ACTION_GROUP (group), "bar", g_variant_new_string ("boo"));
198   state = g_action_group_get_state (G_ACTION_GROUP (group), "bar");
199   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "boo");
200   g_variant_unref (state);
201
202   action = g_simple_action_group_lookup (group, "bar");
203   g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
204   g_assert (!g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
205
206   g_simple_action_group_remove (group, "bar");
207   action = g_simple_action_group_lookup (group, "foo");
208   g_assert_cmpstr (g_action_get_name (action), ==, "foo");
209   action = g_simple_action_group_lookup (group, "bar");
210   g_assert (action == NULL);
211
212   a.did_run = FALSE;
213   g_object_unref (group);
214   g_assert (!a.did_run);
215 }
216
217 static void
218 test_stateful (void)
219 {
220   GSimpleAction *action;
221   GVariant *state;
222
223   action = g_simple_action_new_stateful ("foo", NULL, g_variant_new_string ("hihi"));
224   g_assert (g_action_get_enabled (G_ACTION (action)));
225   g_assert (g_action_get_parameter_type (G_ACTION (action)) == NULL);
226   g_assert (g_action_get_state_hint (G_ACTION (action)) == NULL);
227   g_assert (g_variant_type_equal (g_action_get_state_type (G_ACTION (action)),
228                                   G_VARIANT_TYPE_STRING));
229   state = g_action_get_state (G_ACTION (action));
230   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hihi");
231   g_variant_unref (state);
232
233   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
234     {
235       g_action_set_state (G_ACTION (action), g_variant_new_int32 (123));
236       exit (0);
237     }
238   g_test_trap_assert_failed ();
239
240   g_action_set_state (G_ACTION (action), g_variant_new_string ("hello"));
241   state = g_action_get_state (G_ACTION (action));
242   g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "hello");
243   g_variant_unref (state);
244
245   g_object_unref (action);
246
247   action = g_simple_action_new ("foo", NULL);
248   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
249     {
250       g_action_set_state (G_ACTION (action), g_variant_new_int32 (123));
251       exit (0);
252     }
253   g_test_trap_assert_failed ();
254   g_object_unref (action);
255 }
256
257 int
258 main (int argc, char **argv)
259 {
260   g_type_init ();
261   g_test_init (&argc, &argv, NULL);
262
263   g_test_add_func ("/actions/basic", test_basic);
264   g_test_add_func ("/actions/simplegroup", test_simple_group);
265   g_test_add_func ("/actions/stateful", test_stateful);
266
267   return g_test_run ();
268 }