Debian Codegraph @

tests/conform/actor-graph.c

raw
1 #include <clutter/clutter.h>
2
3 static void
4 actor_add_child (void)
5 {
6 ClutterActor *actor = clutter_actor_new ();
7 ClutterActor *iter;
8
9 g_object_ref_sink (actor);
10 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
11
12 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
13 "name", "foo",
14 NULL));
15 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
16 "name", "bar",
17 NULL));
18 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
19 "name", "baz",
20 NULL));
21
22 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 3);
23
24 iter = clutter_actor_get_first_child (actor);
25 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
26
27 iter = clutter_actor_get_next_sibling (iter);
28 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
29
30 iter = clutter_actor_get_next_sibling (iter);
31 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
32 g_assert (iter == clutter_actor_get_last_child (actor));
33 g_assert (clutter_actor_get_next_sibling (iter) == NULL);
34
35 iter = clutter_actor_get_last_child (actor);
36 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
37
38 iter = clutter_actor_get_previous_sibling (iter);
39 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
40
41 iter = clutter_actor_get_previous_sibling (iter);
42 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
43 g_assert (iter == clutter_actor_get_first_child (actor));
44 g_assert (clutter_actor_get_previous_sibling (iter) == NULL);
45
46 clutter_actor_destroy (actor);
47 g_assert (actor == NULL);
48 }
49
50 static void
51 actor_insert_child (void)
52 {
53 ClutterActor *actor = clutter_actor_new ();
54 ClutterActor *iter;
55
56 g_object_ref_sink (actor);
57 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
58
59 clutter_actor_insert_child_at_index (actor,
60 g_object_new (CLUTTER_TYPE_ACTOR,
61 "name", "foo",
62 NULL),
63 0);
64
65 iter = clutter_actor_get_first_child (actor);
66 g_assert (iter != NULL);
67 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
68 g_assert (iter == clutter_actor_get_child_at_index (actor, 0));
69
70 clutter_actor_insert_child_below (actor,
71 g_object_new (CLUTTER_TYPE_ACTOR,
72 "name", "bar",
73 NULL),
74 iter);
75
76 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 2);
77
78 iter = clutter_actor_get_first_child (actor);
79 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
80 iter = clutter_actor_get_next_sibling (iter);
81 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
82 g_assert (iter == clutter_actor_get_child_at_index (actor, 1));
83
84 iter = clutter_actor_get_first_child (actor);
85 clutter_actor_insert_child_above (actor,
86 g_object_new (CLUTTER_TYPE_ACTOR,
87 "name", "baz",
88 NULL),
89 iter);
90
91 iter = clutter_actor_get_last_child (actor);
92 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
93
94 iter = clutter_actor_get_previous_sibling (iter);
95 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
96
97 iter = clutter_actor_get_previous_sibling (iter);
98 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
99
100 clutter_actor_remove_all_children (actor);
101
102 clutter_actor_insert_child_at_index (actor,
103 g_object_new (CLUTTER_TYPE_ACTOR,
104 "name", "1",
105 NULL),
106 0);
107 iter = clutter_actor_get_child_at_index (actor, 0);
108 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "1");
109 g_assert (clutter_actor_get_first_child (actor) == iter);
110 g_assert (clutter_actor_get_last_child (actor) == iter);
111
112 clutter_actor_insert_child_at_index (actor,
113 g_object_new (CLUTTER_TYPE_ACTOR,
114 "name", "2",
115 NULL),
116 0);
117 iter = clutter_actor_get_child_at_index (actor, 0);
118 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "2");
119 g_assert (clutter_actor_get_first_child (actor) == iter);
120 iter = clutter_actor_get_child_at_index (actor, 1);
121 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "1");
122 g_assert (clutter_actor_get_last_child (actor) == iter);
123
124 clutter_actor_insert_child_at_index (actor,
125 g_object_new (CLUTTER_TYPE_ACTOR,
126 "name", "3",
127 NULL),
128 -1);
129 iter = clutter_actor_get_child_at_index (actor, 2);
130 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "3");
131 g_assert (clutter_actor_get_last_child (actor) == iter);
132
133 clutter_actor_destroy (actor);
134 g_assert (actor == NULL);
135 }
136
137 static void
138 actor_remove_child (void)
139 {
140 ClutterActor *actor = clutter_actor_new ();
141 ClutterActor *iter;
142
143 g_object_ref_sink (actor);
144 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
145
146 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
147 "name", "foo",
148 NULL));
149 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
150 "name", "bar",
151 NULL));
152
153 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 2);
154
155 g_assert (clutter_actor_get_first_child (actor) != clutter_actor_get_last_child (actor));
156
157 iter = clutter_actor_get_first_child (actor);
158 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
159
160 iter = clutter_actor_get_last_child (actor);
161 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
162
163 clutter_actor_remove_child (actor, clutter_actor_get_first_child (actor));
164
165 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 1);
166
167 iter = clutter_actor_get_first_child (actor);
168 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
169 g_assert (clutter_actor_get_first_child (actor) == clutter_actor_get_last_child (actor));
170
171 clutter_actor_remove_child (actor, clutter_actor_get_first_child (actor));
172
173 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 0);
174 g_assert (clutter_actor_get_first_child (actor) == NULL);
175 g_assert (clutter_actor_get_last_child (actor) == NULL);
176
177 clutter_actor_destroy (actor);
178 g_assert (actor == NULL);
179 }
180
181 static void
182 actor_raise_child (void)
183 {
184 ClutterActor *actor = clutter_actor_new ();
185 ClutterActor *iter;
186 gboolean show_on_set_parent;
187
188 g_object_ref_sink (actor);
189 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
190
191 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
192 "name", "foo",
193 "visible", FALSE,
194 NULL));
195 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
196 "name", "bar",
197 "visible", FALSE,
198 NULL));
199 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
200 "name", "baz",
201 "visible", FALSE,
202 NULL));
203
204 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 3);
205
206 iter = clutter_actor_get_child_at_index (actor, 1);
207 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
208
209 clutter_actor_set_child_above_sibling (actor, iter,
210 clutter_actor_get_child_at_index (actor, 2));
211
212 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 0)),
213 ==,
214 "foo");
215 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 1)),
216 ==,
217 "baz");
218 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 2)),
219 ==,
220 "bar");
221 g_assert (!clutter_actor_is_visible (iter));
222 g_object_get (iter, "show-on-set-parent", &show_on_set_parent, NULL);
223 g_assert (!show_on_set_parent);
224
225 iter = clutter_actor_get_child_at_index (actor, 0);
226 clutter_actor_set_child_above_sibling (actor, iter, NULL);
227 g_object_add_weak_pointer (G_OBJECT (iter), (gpointer *) &iter);
228
229 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 0)),
230 ==,
231 "baz");
232 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 1)),
233 ==,
234 "bar");
235 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 2)),
236 ==,
237 "foo");
238 g_assert (!clutter_actor_is_visible (iter));
239 g_object_get (iter, "show-on-set-parent", &show_on_set_parent, NULL);
240 g_assert (!show_on_set_parent);
241
242 clutter_actor_destroy (actor);
243 g_assert (actor == NULL);
244 g_assert (iter == NULL);
245 }
246
247 static void
248 actor_lower_child (void)
249 {
250 ClutterActor *actor = clutter_actor_new ();
251 ClutterActor *iter;
252 gboolean show_on_set_parent;
253
254 g_object_ref_sink (actor);
255 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
256
257 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
258 "name", "foo",
259 "visible", FALSE,
260 NULL));
261 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
262 "name", "bar",
263 "visible", FALSE,
264 NULL));
265 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
266 "name", "baz",
267 "visible", FALSE,
268 NULL));
269
270 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 3);
271
272 iter = clutter_actor_get_child_at_index (actor, 1);
273 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
274
275 clutter_actor_set_child_below_sibling (actor, iter,
276 clutter_actor_get_child_at_index (actor, 0));
277
278 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 0)),
279 ==,
280 "bar");
281 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 1)),
282 ==,
283 "foo");
284 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 2)),
285 ==,
286 "baz");
287 g_assert (!clutter_actor_is_visible (iter));
288 g_object_get (iter, "show-on-set-parent", &show_on_set_parent, NULL);
289 g_assert (!show_on_set_parent);
290
291 iter = clutter_actor_get_child_at_index (actor, 2);
292 clutter_actor_set_child_below_sibling (actor, iter, NULL);
293
294 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 0)),
295 ==,
296 "baz");
297 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 1)),
298 ==,
299 "bar");
300 g_assert_cmpstr (clutter_actor_get_name (clutter_actor_get_child_at_index (actor, 2)),
301 ==,
302 "foo");
303 g_assert (!clutter_actor_is_visible (iter));
304 g_object_get (iter, "show-on-set-parent", &show_on_set_parent, NULL);
305 g_assert (!show_on_set_parent);
306
307 clutter_actor_destroy (actor);
308 g_assert (actor == NULL);
309 }
310
311 static void
312 actor_replace_child (void)
313 {
314 ClutterActor *actor = clutter_actor_new ();
315 ClutterActor *iter;
316
317 g_object_ref_sink (actor);
318 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
319
320 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
321 "name", "foo",
322 NULL));
323 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
324 "name", "bar",
325 NULL));
326
327 iter = clutter_actor_get_child_at_index (actor, 0);
328 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
329
330 clutter_actor_replace_child (actor, iter,
331 g_object_new (CLUTTER_TYPE_ACTOR,
332 "name", "baz",
333 NULL));
334
335 iter = clutter_actor_get_child_at_index (actor, 0);
336 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
337
338 iter = clutter_actor_get_child_at_index (actor, 1);
339 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
340
341 clutter_actor_replace_child (actor, iter,
342 g_object_new (CLUTTER_TYPE_ACTOR,
343 "name", "qux",
344 NULL));
345
346 iter = clutter_actor_get_child_at_index (actor, 0);
347 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
348
349 iter = clutter_actor_get_child_at_index (actor, 1);
350 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "qux");
351
352 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
353 "name", "foo",
354 NULL));
355
356 clutter_actor_replace_child (actor, iter,
357 g_object_new (CLUTTER_TYPE_ACTOR,
358 "name", "bar",
359 NULL));
360
361 iter = clutter_actor_get_last_child (actor);
362 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "foo");
363 iter = clutter_actor_get_previous_sibling (iter);
364 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "bar");
365 iter = clutter_actor_get_previous_sibling (iter);
366 g_assert_cmpstr (clutter_actor_get_name (iter), ==, "baz");
367
368 clutter_actor_destroy (actor);
369 g_assert (actor == NULL);
370 }
371
372 static void
373 actor_remove_all (void)
374 {
375 ClutterActor *actor = clutter_actor_new ();
376
377 g_object_ref_sink (actor);
378 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
379
380 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
381 "name", "foo",
382 NULL));
383 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
384 "name", "bar",
385 NULL));
386 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
387 "name", "baz",
388 NULL));
389
390 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 3);
391
392 clutter_actor_remove_all_children (actor);
393
394 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 0);
395
396 clutter_actor_destroy (actor);
397 g_assert (actor == NULL);
398 }
399
400 static void
401 actor_added (ClutterContainer *container,
402 ClutterActor *child,
403 gpointer data)
404 {
405 ClutterActor *actor = CLUTTER_ACTOR (container);
406 int *counter = data;
407 ClutterActor *old_child;
408
409 if (g_test_verbose ())
410 g_print ("Adding actor '%s'\n", clutter_actor_get_name (child));
411
412 old_child = clutter_actor_get_child_at_index (actor, 0);
413 if (old_child != child)
414 clutter_actor_remove_child (actor, old_child);
415
416 *counter += 1;
417 }
418
419 static void
420 actor_removed (ClutterContainer *container,
421 ClutterActor *child,
422 gpointer data)
423 {
424 int *counter = data;
425
426 if (g_test_verbose ())
427 g_print ("Removing actor '%s'\n", clutter_actor_get_name (child));
428
429 *counter += 1;
430 }
431
432 static void
433 actor_container_signals (void)
434 {
435 ClutterActor *actor = clutter_actor_new ();
436 int add_count, remove_count;
437
438 g_object_ref_sink (actor);
439 g_object_add_weak_pointer (G_OBJECT (actor), (gpointer *) &actor);
440
441 add_count = remove_count = 0;
442 g_signal_connect (actor,
443 "actor-added", G_CALLBACK (actor_added),
444 &add_count);
445 g_signal_connect (actor,
446 "actor-removed", G_CALLBACK (actor_removed),
447 &remove_count);
448
449 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
450 "name", "foo",
451 NULL));
452
453 g_assert_cmpint (add_count, ==, 1);
454 g_assert_cmpint (remove_count, ==, 0);
455 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 1);
456
457 clutter_actor_add_child (actor, g_object_new (CLUTTER_TYPE_ACTOR,
458 "name", "bar",
459 NULL));
460
461 g_assert_cmpint (add_count, ==, 2);
462 g_assert_cmpint (remove_count, ==, 1);
463 g_assert_cmpint (clutter_actor_get_n_children (actor), ==, 1);
464
465 g_signal_handlers_disconnect_by_func (actor, G_CALLBACK (actor_added),
466 &add_count);
467 g_signal_handlers_disconnect_by_func (actor, G_CALLBACK (actor_removed),
468 &remove_count);
469
470 clutter_actor_destroy (actor);
471 g_assert (actor == NULL);
472 }
473
474 static void
475 actor_contains (void)
476 {
477 /* This build up the following tree:
478 *
479 * a
480 * ╱ │ ╲
481 * ╱ │ ╲
482 * b c d
483 * ╱ ╲ ╱ ╲ ╱ ╲
484 * e f g h i j
485 */
486 struct {
487 ClutterActor *actor_a, *actor_b, *actor_c, *actor_d, *actor_e;
488 ClutterActor *actor_f, *actor_g, *actor_h, *actor_i, *actor_j;
489 } d;
490 int x, y;
491 ClutterActor **actor_array = &d.actor_a;
492
493 /* Matrix of expected results */
494 static const gboolean expected_results[] =
495 { /* a, b, c, d, e, f, g, h, i, j */
496 /* a */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
497 /* b */ 0, 1, 0, 0, 1, 1, 0, 0, 0, 0,
498 /* c */ 0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
499 /* d */ 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
500 /* e */ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
501 /* f */ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
502 /* g */ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
503 /* h */ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
504 /* i */ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
505 /* j */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
506 };
507
508 d.actor_a = clutter_actor_new ();
509 d.actor_b = clutter_actor_new ();
510 d.actor_c = clutter_actor_new ();
511 d.actor_d = clutter_actor_new ();
512 d.actor_e = clutter_actor_new ();
513 d.actor_f = clutter_actor_new ();
514 d.actor_g = clutter_actor_new ();
515 d.actor_h = clutter_actor_new ();
516 d.actor_i = clutter_actor_new ();
517 d.actor_j = clutter_actor_new ();
518
519 clutter_actor_add_child (d.actor_a, d.actor_b);
520 clutter_actor_add_child (d.actor_a, d.actor_c);
521 clutter_actor_add_child (d.actor_a, d.actor_d);
522
523 clutter_actor_add_child (d.actor_b, d.actor_e);
524 clutter_actor_add_child (d.actor_b, d.actor_f);
525
526 clutter_actor_add_child (d.actor_c, d.actor_g);
527 clutter_actor_add_child (d.actor_c, d.actor_h);
528
529 clutter_actor_add_child (d.actor_d, d.actor_i);
530 clutter_actor_add_child (d.actor_d, d.actor_j);
531
532 for (y = 0; y < 10; y++)
533 for (x = 0; x < 10; x++)
534 g_assert_cmpint (clutter_actor_contains (actor_array[x],
535 actor_array[y]),
536 ==,
537 expected_results[x * 10 + y]);
538 }
539
540 CLUTTER_TEST_SUITE (
541 CLUTTER_TEST_UNIT ("/actor/graph/add-child", actor_add_child)
542 CLUTTER_TEST_UNIT ("/actor/graph/insert-child", actor_insert_child)
543 CLUTTER_TEST_UNIT ("/actor/graph/remove-child", actor_remove_child)
544 CLUTTER_TEST_UNIT ("/actor/graph/raise-child", actor_raise_child)
545 CLUTTER_TEST_UNIT ("/actor/graph/lower-child", actor_lower_child)
546 CLUTTER_TEST_UNIT ("/actor/graph/replace-child", actor_replace_child)
547 CLUTTER_TEST_UNIT ("/actor/graph/remove-all", actor_remove_all)
548 CLUTTER_TEST_UNIT ("/actor/graph/container-signals", actor_container_signals)
549 CLUTTER_TEST_UNIT ("/actor/graph/contains", actor_contains)
550 )