11 activate (GAction *action,
15 Activation *activation = user_data;
18 activation->params = g_variant_ref (parameter);
20 activation->params = NULL;
21 activation->did_run = TRUE;
27 Activation a = { 0, };
28 GSimpleAction *action;
30 GVariantType *parameter_type;
32 GVariantType *state_type;
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);
43 "parameter-type", ¶meter_type,
45 "state-type", &state_type,
48 g_assert_cmpstr (name, ==, "foo");
49 g_assert (parameter_type == NULL);
51 g_assert (state_type == NULL);
52 g_assert (state == NULL);
55 g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
56 g_assert (!a.did_run);
57 g_action_activate (G_ACTION (action), NULL);
61 g_simple_action_set_enabled (action, FALSE);
62 g_action_activate (G_ACTION (action), NULL);
63 g_assert (!a.did_run);
65 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
67 g_action_activate (G_ACTION (action), g_variant_new_string ("xxx"));
70 g_test_trap_assert_failed ();
72 g_object_unref (action);
73 g_assert (!a.did_run);
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);
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"));
86 g_assert_cmpstr (g_variant_get_string (a.params, NULL), ==, "Hello world");
87 g_variant_unref (a.params);
90 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
92 g_action_activate (G_ACTION (action), NULL);
96 g_test_trap_assert_failed ();
98 g_object_unref (action);
99 g_assert (!a.did_run);
103 strv_has_string (gchar **haystack,
108 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
110 if (g_strcmp0 (haystack[n], needle) == 0)
117 strv_set_equal (gchar **strv, ...)
126 va_start (list, strv);
129 str = va_arg (list, const gchar *);
132 if (!strv_has_string (strv, str))
142 res = g_strv_length ((gchar**)strv) == count;
148 test_simple_group (void)
150 GSimpleActionGroup *group;
151 Activation a = { 0, };
152 GSimpleAction *simple;
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);
164 group = g_simple_action_group_new ();
165 g_simple_action_group_insert (group, G_ACTION (simple));
166 g_object_unref (simple);
168 g_assert (!a.did_run);
169 g_action_group_activate_action (G_ACTION_GROUP (group), "foo", NULL);
170 g_assert (a.did_run);
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);
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_action_enabled (G_ACTION_GROUP (group), "foo"));
184 g_assert (g_action_group_get_action_enabled (G_ACTION_GROUP (group), "bar"));
185 g_assert (g_action_group_get_action_parameter_type (G_ACTION_GROUP (group), "foo") == NULL);
186 g_assert (g_variant_type_equal (g_action_group_get_action_parameter_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
187 g_assert (g_action_group_get_action_state_type (G_ACTION_GROUP (group), "foo") == NULL);
188 g_assert (g_variant_type_equal (g_action_group_get_action_state_type (G_ACTION_GROUP (group), "bar"), G_VARIANT_TYPE_STRING));
189 g_assert (g_action_group_get_action_state_hint (G_ACTION_GROUP (group), "foo") == NULL);
190 g_assert (g_action_group_get_action_state_hint (G_ACTION_GROUP (group), "bar") == NULL);
191 g_assert (g_action_group_get_action_state (G_ACTION_GROUP (group), "foo") == NULL);
192 state = g_action_group_get_action_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);
197 g_action_group_change_action_state (G_ACTION_GROUP (group), "bar", g_variant_new_string ("boo"));
198 state = g_action_group_get_action_state (G_ACTION_GROUP (group), "bar");
199 g_assert_cmpstr (g_variant_get_string (state, NULL), ==, "boo");
200 g_variant_unref (state);
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_action_enabled (G_ACTION_GROUP (group), "bar"));
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);
213 g_object_unref (group);
214 g_assert (!a.did_run);
220 GSimpleAction *action;
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);
233 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
235 g_simple_action_set_state (action, g_variant_new_int32 (123));
238 g_test_trap_assert_failed ();
240 g_simple_action_set_state (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);
245 g_object_unref (action);
247 action = g_simple_action_new ("foo", NULL);
248 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
250 g_simple_action_set_state (action, g_variant_new_int32 (123));
253 g_test_trap_assert_failed ();
254 g_object_unref (action);
258 main (int argc, char **argv)
261 g_test_init (&argc, &argv, NULL);
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);
267 return g_test_run ();