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, };
30 GVariantType *parameter_type;
32 GVariantType *state_type;
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);
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);
54 g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
55 g_assert (!a.did_run);
56 g_action_activate (action, NULL);
60 g_action_set_enabled (action, FALSE);
61 g_action_activate (action, NULL);
62 g_assert (!a.did_run);
64 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
66 g_action_activate (action, g_variant_new_string ("xxx"));
69 g_test_trap_assert_failed ();
71 g_object_unref (action);
72 g_assert (!a.did_run);
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);
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"));
85 g_assert_cmpstr (g_variant_get_string (a.params, NULL), ==, "Hello world");
86 g_variant_unref (a.params);
89 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
91 g_action_activate (action, NULL);
95 g_test_trap_assert_failed ();
97 g_object_unref (action);
98 g_assert (!a.did_run);
102 strv_has_string (const gchar **haystack,
107 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
109 if (g_strcmp0 (haystack[n], needle) == 0)
116 strv_set_equal (const gchar **strv, ...)
125 va_start (list, strv);
128 str = va_arg (list, const gchar *);
131 if (!strv_has_string (strv, str))
141 res = g_strv_length ((gchar**)strv) == count;
147 test_simple_group (void)
149 GSimpleActionGroup *group;
150 Activation a = { 0, };
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);
162 group = g_simple_action_group_new ();
163 g_simple_action_group_insert (group, action);
164 g_object_unref (action);
166 g_assert (!a.did_run);
167 g_action_group_activate (G_ACTION_GROUP (group), "foo", NULL);
168 g_assert (a.did_run);
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);
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");
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");
198 g_simple_action_group_set_enabled (group, "bar", FALSE);
199 g_assert (!g_action_group_get_enabled (G_ACTION_GROUP (group), "bar"));
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);
208 g_object_unref (group);
209 g_assert (!a.did_run);
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),
226 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
228 g_action_set_state (action, g_variant_new_int32 (123));
231 g_test_trap_assert_failed ();
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),
237 g_object_unref (action);
239 action = g_action_new ("foo", NULL);
240 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
242 g_action_set_state (action, g_variant_new_int32 (123));
245 g_test_trap_assert_failed ();
246 g_object_unref (action);
250 main (int argc, char **argv)
253 g_test_init (&argc, &argv, NULL);
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);
259 return g_test_run ();