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);
55 g_signal_connect (action, "activate", G_CALLBACK (activate), &a);
56 g_assert (!a.did_run);
57 g_action_activate (action, NULL);
61 g_action_set_enabled (action, FALSE);
62 g_action_activate (action, NULL);
63 g_assert (!a.did_run);
65 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
67 g_action_activate (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_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);
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"));
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 (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, };
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);
163 group = g_simple_action_group_new ();
164 g_simple_action_group_insert (group, action);
165 g_object_unref (action);
167 g_assert (!a.did_run);
168 g_action_group_activate (G_ACTION_GROUP (group), "foo", NULL);
169 g_assert (a.did_run);
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);
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);
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);
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"));
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);
212 g_object_unref (group);
213 g_assert (!a.did_run);
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);
232 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
234 g_action_set_state (action, g_variant_new_int32 (123));
237 g_test_trap_assert_failed ();
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);
244 g_object_unref (action);
246 action = g_action_new ("foo", NULL);
247 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
249 g_action_set_state (action, g_variant_new_int32 (123));
252 g_test_trap_assert_failed ();
253 g_object_unref (action);
257 main (int argc, char **argv)
260 g_test_init (&argc, &argv, NULL);
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);
266 return g_test_run ();