doc: update copyright line for 2021
[adg.git] / src / adg / tests / test-entity.c
blobea93a498073418a51fcd26a1f29ee19a1ed018b8
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007-2021 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include <adg-test.h>
22 #include <adg.h>
24 #define ADG_TYPE_DUMMY (adg_dummy_get_type())
27 typedef GObject AdgDummy;
28 typedef GObjectClass AdgDummyClass;
30 static void
31 adg_dummy_class_init(AdgDummyClass *klass)
35 static void
36 adg_dummy_init(AdgDummy *dummy)
40 G_DEFINE_TYPE(AdgDummy, adg_dummy, ADG_TYPE_ENTITY);
43 static void
44 _adg_behavior_misc(void)
46 AdgDummy *dummy = g_object_new(ADG_TYPE_DUMMY, NULL);
47 cairo_t *cr = adg_test_cairo_context();
49 /* Ensure NULL virtual methods do not crash the process */
50 adg_entity_invalidate(ADG_ENTITY(dummy));
51 adg_entity_arrange(ADG_ENTITY(dummy));
52 adg_entity_render(ADG_ENTITY(dummy), cr);
54 cairo_destroy(cr);
55 g_object_unref(dummy);
58 static void
59 _adg_behavior_style(void)
61 AdgEntity *entity;
62 AdgStyle *style, *color_style, *line_style;
64 entity = ADG_ENTITY(adg_logo_new());
65 color_style = ADG_STYLE(adg_color_style_new());
66 line_style = ADG_STYLE(adg_line_style_new());
68 /* Sanity check */
69 adg_entity_set_style(NULL, ADG_DRESS_COLOR, color_style);
70 g_assert_null(adg_entity_get_style(NULL, ADG_DRESS_COLOR));
72 adg_entity_set_style(entity, ADG_DRESS_COLOR, color_style);
74 style = adg_entity_get_style(entity, ADG_DRESS_COLOR);
75 g_assert_nonnull(style);
76 g_assert_true(style == color_style);
78 adg_entity_set_style(entity, ADG_DRESS_COLOR, color_style);
80 style = adg_entity_get_style(entity, ADG_DRESS_COLOR);
81 g_assert_nonnull(style);
82 g_assert_true(style == color_style);
84 adg_entity_set_style(entity, ADG_DRESS_COLOR, NULL);
85 g_assert_null(adg_entity_get_style(NULL, ADG_DRESS_COLOR));
87 /* Try to set an incompatible style */
88 adg_entity_set_style(entity, ADG_DRESS_COLOR, line_style);
90 g_assert_null(adg_entity_get_style(NULL, ADG_DRESS_COLOR));
92 adg_entity_destroy(entity);
93 g_object_unref(color_style);
94 g_object_unref(line_style);
97 static void
98 _adg_behavior_local(void)
100 AdgCanvas *canvas;
101 AdgEntity *entity;
103 /* Sanity check */
104 adg_entity_set_local_mix(NULL, ADG_MIX_UNDEFINED);
105 g_assert_cmpint(adg_entity_get_local_mix(NULL), ==, ADG_MIX_UNDEFINED);
106 adg_entity_local_changed(NULL);
108 canvas = adg_test_canvas();
109 entity = ADG_ENTITY(adg_logo_new());
110 adg_container_add(ADG_CONTAINER(canvas), entity);
112 g_assert_cmpint(adg_entity_get_local_mix(entity), !=, ADG_MIX_UNDEFINED);
114 /* Check any AdgMix value does not crash the process */
115 adg_entity_set_local_mix(entity, ADG_MIX_UNDEFINED);
116 adg_entity_local_changed(entity);
117 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_UNDEFINED);
119 adg_entity_set_local_mix(entity, ADG_MIX_DISABLED);
120 adg_entity_local_changed(entity);
121 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_DISABLED);
123 adg_entity_set_local_mix(entity, ADG_MIX_NONE);
124 adg_entity_local_changed(entity);
125 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_NONE);
127 adg_entity_set_local_mix(entity, ADG_MIX_ANCESTORS);
128 adg_entity_local_changed(entity);
129 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_ANCESTORS);
131 adg_entity_set_local_mix(entity, ADG_MIX_ANCESTORS_NORMALIZED);
132 adg_entity_local_changed(entity);
133 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_ANCESTORS_NORMALIZED);
135 adg_entity_set_local_mix(entity, ADG_MIX_PARENT);
136 adg_entity_local_changed(entity);
137 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_PARENT);
139 adg_entity_set_local_mix(entity, ADG_MIX_PARENT_NORMALIZED);
140 adg_entity_local_changed(entity);
141 g_assert_cmpint(adg_entity_get_local_mix(entity), ==, ADG_MIX_PARENT_NORMALIZED);
143 adg_entity_destroy(ADG_ENTITY(canvas));
146 static void
147 _adg_property_floating(void)
149 AdgEntity *entity;
150 gboolean invalid_boolean;
151 gboolean floating;
153 entity = ADG_ENTITY(adg_logo_new());
154 invalid_boolean = (gboolean) 1234;
156 /* Ensure the default state is false */
157 g_assert_false(adg_entity_has_floating(entity));
159 /* Using the public APIs */
160 adg_entity_switch_floating(entity, invalid_boolean);
161 g_assert_false(adg_entity_has_floating(entity));
163 adg_entity_switch_floating(entity, TRUE);
164 g_assert_true(adg_entity_has_floating(entity));
166 /* Using GObject property methods */
167 g_object_set(entity, "floating", FALSE, NULL);
168 g_object_get(entity, "floating", &floating, NULL);
169 g_assert_false(floating);
171 g_object_set(entity, "floating", invalid_boolean, NULL);
172 g_object_get(entity, "floating", &floating, NULL);
173 g_assert_false(floating);
175 g_object_set(entity, "floating", TRUE, NULL);
176 g_object_get(entity, "floating", &floating, NULL);
177 g_assert_true(floating);
179 adg_entity_destroy(entity);
182 static void
183 _adg_property_parent(void)
185 AdgEntity *entity;
186 AdgEntity *valid_container, *invalid_container;
187 AdgEntity *parent;
189 entity = ADG_ENTITY(adg_logo_new());
190 valid_container = ADG_ENTITY(adg_container_new());
191 invalid_container = adg_test_invalid_pointer();
193 /* Using the public APIs */
194 adg_entity_set_parent(entity, valid_container);
195 parent = adg_entity_get_parent(entity);
196 g_assert_true(parent == valid_container);
198 adg_entity_set_parent(entity, invalid_container);
199 parent = adg_entity_get_parent(entity);
200 g_assert_true(parent == valid_container);
202 adg_entity_set_parent(entity, NULL);
203 parent = adg_entity_get_parent(entity);
204 g_assert_null(parent);
206 /* Using GObject property methods */
207 g_object_set(entity, "parent", valid_container, NULL);
208 g_object_get(entity, "parent", &parent, NULL);
209 g_assert_true(parent == valid_container);
210 adg_entity_destroy(parent);
212 g_object_set(entity, "parent", invalid_container, NULL);
213 g_object_get(entity, "parent", &parent, NULL);
214 g_assert_true(parent == valid_container);
215 adg_entity_destroy(parent);
217 g_object_set(entity, "parent", NULL, NULL);
218 g_object_get(entity, "parent", &parent, NULL);
219 g_assert_null(parent);
221 adg_entity_destroy(entity);
222 adg_entity_destroy(valid_container);
225 static void
226 _adg_property_global_map(void)
228 AdgEntity *entity;
229 const cairo_matrix_t *identity_map;
230 cairo_matrix_t null_map, dummy_map;
231 const cairo_matrix_t *global_map;
232 cairo_matrix_t *global_map_dup;
234 entity = ADG_ENTITY(adg_logo_new());
235 identity_map = adg_matrix_identity();
237 /* A null map is a kind of degenerated matrix: it must be
238 * treated as valid value by the API */
239 cairo_matrix_init(&null_map, 0, 0, 0, 0, 0, 0);
240 /* A general purpose map value without translations */
241 cairo_matrix_init(&dummy_map, 1, 2, 3, 4, 0, 0);
243 /* Using the public APIs */
244 adg_entity_set_global_map(entity, &null_map);
245 global_map = adg_entity_get_global_map(entity);
246 g_assert_true(adg_matrix_equal(global_map, &null_map));
248 adg_entity_transform_global_map(entity, &dummy_map, ADG_TRANSFORM_AFTER);
249 global_map = adg_entity_get_global_map(entity);
250 g_assert_true(adg_matrix_equal(global_map, &null_map));
252 adg_entity_set_global_map(entity, identity_map);
253 global_map = adg_entity_get_global_map(entity);
254 g_assert_true(adg_matrix_equal(global_map, identity_map));
256 adg_entity_set_global_map(entity, NULL);
257 global_map = adg_entity_get_global_map(entity);
258 g_assert_true(adg_matrix_equal(global_map, identity_map));
260 adg_entity_transform_global_map(entity, &dummy_map, ADG_TRANSFORM_BEFORE);
261 global_map = adg_entity_get_global_map(entity);
262 g_assert_true(adg_matrix_equal(global_map, &dummy_map));
264 /* Using GObject property methods */
265 g_object_set(entity, "global-map", &null_map, NULL);
266 g_object_get(entity, "global-map", &global_map_dup, NULL);
267 g_assert_true(adg_matrix_equal(global_map_dup, &null_map));
268 g_free(global_map_dup);
270 g_object_set(entity, "global-map", NULL, NULL);
271 g_object_get(entity, "global-map", &global_map_dup, NULL);
272 g_assert_true(adg_matrix_equal(global_map_dup, &null_map));
273 g_free(global_map_dup);
275 g_object_set(entity, "global-map", identity_map, NULL);
276 g_object_get(entity, "global-map", &global_map_dup, NULL);
277 g_assert_true(adg_matrix_equal(global_map_dup, identity_map));
278 g_free(global_map_dup);
280 adg_entity_destroy(entity);
283 static void
284 _adg_property_local_map(void)
286 AdgEntity *entity;
287 const cairo_matrix_t *identity_map;
288 cairo_matrix_t null_map, dummy_map;
289 const cairo_matrix_t *local_map;
290 cairo_matrix_t *local_map_dup;
292 entity = ADG_ENTITY(adg_logo_new());
293 identity_map = adg_matrix_identity();
295 /* A null map is a kind of degenerated matrix: it must be
296 * treated as valid value by the API */
297 cairo_matrix_init(&null_map, 0, 0, 0, 0, 0, 0);
298 /* A general purpose map value without translations */
299 cairo_matrix_init(&dummy_map, 1, 2, 3, 4, 0, 0);
301 /* Using the public APIs */
302 adg_entity_set_local_map(entity, &null_map);
303 local_map = adg_entity_get_local_map(entity);
304 g_assert_true(adg_matrix_equal(local_map, &null_map));
306 adg_entity_transform_local_map(entity, &dummy_map, ADG_TRANSFORM_AFTER);
307 local_map = adg_entity_get_local_map(entity);
308 g_assert_true(adg_matrix_equal(local_map, &null_map));
310 adg_entity_set_local_map(entity, identity_map);
311 local_map = adg_entity_get_local_map(entity);
312 g_assert_true(adg_matrix_equal(local_map, identity_map));
314 adg_entity_set_local_map(entity, NULL);
315 local_map = adg_entity_get_local_map(entity);
316 g_assert_true(adg_matrix_equal(local_map, identity_map));
318 adg_entity_transform_local_map(entity, &dummy_map, ADG_TRANSFORM_BEFORE);
319 local_map = adg_entity_get_local_map(entity);
320 g_assert_true(adg_matrix_equal(local_map, &dummy_map));
322 /* Using GObject property methods */
323 g_object_set(entity, "local-map", &null_map, NULL);
324 g_object_get(entity, "local-map", &local_map_dup, NULL);
325 g_assert_true(adg_matrix_equal(local_map_dup, &null_map));
326 g_free(local_map_dup);
328 g_object_set(entity, "local-map", NULL, NULL);
329 g_object_get(entity, "local-map", &local_map_dup, NULL);
330 g_assert_true(adg_matrix_equal(local_map_dup, &null_map));
331 g_free(local_map_dup);
333 g_object_set(entity, "local-map", identity_map, NULL);
334 g_object_get(entity, "local-map", &local_map_dup, NULL);
335 g_assert_true(adg_matrix_equal(local_map_dup, identity_map));
336 g_free(local_map_dup);
338 adg_entity_destroy(entity);
341 static void
342 _adg_property_local_mix(void)
344 AdgEntity *entity;
345 AdgMix valid_mix1, valid_mix2, invalid_mix;
346 AdgMix local_mix;
348 entity = ADG_ENTITY(adg_logo_new());
349 valid_mix1 = ADG_MIX_UNDEFINED;
350 valid_mix2 = ADG_MIX_ANCESTORS_NORMALIZED;
351 invalid_mix = G_MAXINT;
353 /* Using the public APIs */
354 adg_entity_set_local_mix(entity, valid_mix1);
355 local_mix = adg_entity_get_local_mix(entity);
356 g_assert_cmpint(local_mix, ==, valid_mix1);
358 adg_entity_set_local_mix(entity, invalid_mix);
359 local_mix = adg_entity_get_local_mix(entity);
360 g_assert_cmpint(local_mix, !=, invalid_mix);
362 adg_entity_set_local_mix(entity, valid_mix2);
363 local_mix = adg_entity_get_local_mix(entity);
364 g_assert_cmpint(local_mix, ==, valid_mix2);
366 /* Using GObject property methods */
367 g_object_set(entity, "local-mix", valid_mix1, NULL);
368 g_object_get(entity, "local-mix", &local_mix, NULL);
369 g_assert_cmpint(local_mix, ==, valid_mix1);
371 g_object_set(entity, "local-mix", invalid_mix, NULL);
372 g_object_get(entity, "local-mix", &local_mix, NULL);
373 g_assert_cmpint(local_mix, ==, valid_mix1);
375 g_object_set(entity, "local-mix", valid_mix2, NULL);
376 g_object_get(entity, "local-mix", &local_mix, NULL);
377 g_assert_cmpint(local_mix, ==, valid_mix2);
379 adg_entity_destroy(entity);
382 static void
383 _adg_property_extents(void)
385 AdgEntity *entity;
386 CpmlExtents new_extents;
387 const CpmlExtents *extents;
389 /* Sanity check */
390 adg_entity_set_extents(NULL, NULL);
391 g_assert_null(adg_entity_get_extents(NULL));
393 entity = ADG_ENTITY(adg_logo_new());
394 new_extents.is_defined = TRUE;
395 new_extents.org.x = 1;
396 new_extents.org.y = 2;
397 new_extents.size.x = 3;
398 new_extents.size.y = 4;
400 extents = adg_entity_get_extents(entity);
401 g_assert_false(extents->is_defined);
403 adg_entity_set_extents(entity, &new_extents);
405 extents = adg_entity_get_extents(entity);
406 g_assert_true(extents->is_defined);
407 adg_assert_isapprox(extents->org.x, 1);
408 adg_assert_isapprox(extents->org.y, 2);
409 adg_assert_isapprox(extents->size.x, 3);
410 adg_assert_isapprox(extents->size.y, 4);
412 adg_entity_set_extents(entity, NULL);
413 g_assert_false(extents->is_defined);
415 adg_entity_destroy(entity);
418 static void
419 _adg_method_get_canvas(void)
421 AdgCanvas *canvas, *response;
422 AdgEntity *entity;
424 /* Sanity check */
425 g_assert_null(adg_entity_get_canvas(NULL));
427 canvas = adg_test_canvas();
428 entity = ADG_ENTITY(adg_logo_new());
430 g_assert_null(adg_entity_get_canvas(entity));
431 adg_container_add(ADG_CONTAINER(canvas), entity);
433 response = adg_entity_get_canvas(entity);
434 g_assert_nonnull(response);
435 g_assert_true(response == canvas);
437 adg_entity_destroy(ADG_ENTITY(canvas));
442 main(int argc, char *argv[])
444 adg_test_init(&argc, &argv);
446 adg_test_add_object_checks("/adg/entity/type/object", ADG_TYPE_ENTITY);
447 adg_test_add_entity_checks("/adg/entity/type/entity", ADG_TYPE_ENTITY);
449 g_test_add_func("/adg/entity/behavior/misc", _adg_behavior_misc);
450 g_test_add_func("/adg/entity/behavior/style", _adg_behavior_style);
451 g_test_add_func("/adg/entity/behavior/local", _adg_behavior_local);
453 g_test_add_func("/adg/entity/property/floating", _adg_property_floating);
454 g_test_add_func("/adg/entity/property/parent", _adg_property_parent);
455 g_test_add_func("/adg/entity/property/global-map", _adg_property_global_map);
456 g_test_add_func("/adg/entity/property/local-map", _adg_property_local_map);
457 g_test_add_func("/adg/entity/property/local-mix", _adg_property_local_mix);
458 g_test_add_func("/adg/entity/property/extents", _adg_property_extents);
460 g_test_add_func("/adg/entity/method/get-canvas", _adg_method_get_canvas);
462 return g_test_run();