Debian Codegraph @

librsvg-c/tests-c/api.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim: set ts=4 nowrap ai expandtab sw=4: */
3
4 /* These are the C API tests for librsvg. These test the complete C
5 * API, especially its historical peculiarities to ensure ABI
6 * compatibility.
7 *
8 * These tests are not meant to exhaustively test librsvg's features.
9 * For those, you should look at the Rust integration tests. See
10 * tests/README.md for details.
11 */
12
13 #include <stdio.h>
14 #include <glib.h>
15 #include <cairo.h>
16
17 #define RSVG_DISABLE_DEPRECATION_WARNINGS /* so we can test deprecated API */
18 #include <librsvg/rsvg.h>
19 #include "test-utils.h"
20
21 /*
22 Untested:
23 rsvg_handle_internal_set_testing
24 */
25
26 static void
27 handle_has_correct_type_info (void)
28 {
29 GTypeQuery q;
30 RsvgHandle *handle;
31
32 g_type_query (RSVG_TYPE_HANDLE, &q);
33 g_assert (q.type == RSVG_TYPE_HANDLE);
34 g_assert (q.type == rsvg_handle_get_type ());
35
36 g_assert_cmpstr (q.type_name, ==, "RsvgHandle");
37
38 /* These test that the sizes of the structs in the header file actually match the
39 * sizes of structs and the glib-subclass machinery in the Rust side.
40 */
41 g_assert (sizeof (RsvgHandleClass) == (gsize) q.class_size);
42 g_assert (sizeof (RsvgHandle) == (gsize) q.instance_size);
43
44 handle = rsvg_handle_new();
45 g_assert (G_OBJECT_TYPE (handle) == RSVG_TYPE_HANDLE);
46 g_object_unref (handle);
47 }
48
49 static void
50 assert_flags_value_matches (GFlagsValue *v,
51 guint value,
52 const char *value_name,
53 const char *value_nick)
54 {
55 g_assert_cmpint(v->value, ==, value);
56 g_assert_cmpstr(v->value_name, ==, value_name);
57 g_assert_cmpstr(v->value_nick, ==, value_nick);
58 }
59
60 static void
61 flags_registration (void)
62 {
63 GType ty;
64 GTypeQuery q;
65 GTypeClass *type_class;
66 GFlagsClass *flags_class;
67
68 ty = RSVG_TYPE_HANDLE_FLAGS;
69
70 g_assert (ty != G_TYPE_INVALID);
71
72 g_type_query (RSVG_TYPE_HANDLE_FLAGS, &q);
73 g_assert (q.type == ty);
74 g_assert (G_TYPE_IS_FLAGS (q.type));
75 g_assert_cmpstr (q.type_name, ==, "RsvgHandleFlags");
76
77 type_class = g_type_class_ref (ty);
78 g_assert (G_IS_FLAGS_CLASS (type_class));
79 g_assert (G_FLAGS_CLASS_TYPE (type_class) == ty);
80
81 flags_class = G_FLAGS_CLASS (type_class);
82 g_assert_cmpint (flags_class->n_values, ==, 3);
83
84 assert_flags_value_matches(&flags_class->values[0],
85 RSVG_HANDLE_FLAGS_NONE,
86 "RSVG_HANDLE_FLAGS_NONE",
87 "flags-none");
88
89 assert_flags_value_matches(&flags_class->values[1],
90 RSVG_HANDLE_FLAG_UNLIMITED,
91 "RSVG_HANDLE_FLAG_UNLIMITED",
92 "flag-unlimited");
93
94 assert_flags_value_matches(&flags_class->values[2],
95 RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA,
96 "RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA",
97 "flag-keep-image-data");
98
99 g_type_class_unref (type_class);
100 }
101
102 static void
103 assert_enum_value_matches (GEnumValue *v,
104 gint value,
105 const char *value_name,
106 const char *value_nick)
107 {
108 g_assert_cmpint (v->value, ==, value);
109 g_assert_cmpstr (v->value_name, ==, value_name);
110 g_assert_cmpstr (v->value_nick, ==, value_nick);
111 }
112
113 static void
114 error_registration (void)
115 {
116 GType ty;
117 GTypeQuery q;
118 GTypeClass *type_class;
119 GEnumClass *enum_class;
120
121 g_assert_cmpint (RSVG_ERROR, !=, 0);
122
123 ty = RSVG_TYPE_ERROR;
124
125 g_assert (ty != G_TYPE_INVALID);
126
127 g_type_query (ty, &q);
128 g_assert (q.type == ty);
129 g_assert (G_TYPE_IS_ENUM (q.type));
130 g_assert_cmpstr (q.type_name, ==, "RsvgError");
131
132 type_class = g_type_class_ref (ty);
133 g_assert (G_IS_ENUM_CLASS (type_class));
134 g_assert (G_ENUM_CLASS_TYPE (type_class) == ty);
135
136 enum_class = G_ENUM_CLASS (type_class);
137 g_assert_cmpint (enum_class->n_values, ==, 1);
138
139 assert_enum_value_matches (&enum_class->values[0],
140 RSVG_ERROR_FAILED,
141 "RSVG_ERROR_FAILED",
142 "failed");
143
144 g_type_class_unref (type_class);
145 }
146
147 static char *
148 get_test_filename (const char *basename) {
149 return g_build_filename (test_utils_get_test_data_path (),
150 "api",
151 basename,
152 NULL);
153 }
154
155 static RsvgHandle *
156 load_test_document (const char *basename) {
157 char *filename = get_test_filename (basename);
158 GError *error = NULL;
159
160 RsvgHandle *handle = rsvg_handle_new_from_file (filename, &error);
161 g_free (filename);
162
163 g_assert_nonnull (handle);
164 g_assert_no_error (error);
165
166 return handle;
167 }
168
169 #define EXAMPLE_WIDTH 100
170 #define EXAMPLE_HEIGHT 400
171
172 #define XZOOM 2
173 #define YZOOM 3
174
175 #define MAX_WIDTH 10
176 #define MAX_HEIGHT 40
177
178 #define MAX_ZOOMED_WIDTH 20
179 #define MAX_ZOOMED_HEIGHT 120
180
181 #define EXAMPLE_ONE_ID "#one"
182 #define EXAMPLE_TWO_ID "#two"
183 #define EXAMPLE_NONEXISTENT_ID "#nonexistent"
184
185 #define EXAMPLE_ONE_X 0
186 #define EXAMPLE_ONE_Y 0
187 #define EXAMPLE_ONE_W 100
188 #define EXAMPLE_ONE_H 200
189
190 #define EXAMPLE_TWO_X 0
191 #define EXAMPLE_TWO_Y 200
192 #define EXAMPLE_TWO_W 100
193 #define EXAMPLE_TWO_H 200
194
195 #ifdef HAVE_PIXBUF
196 static GdkPixbuf *
197 pixbuf_from_file (const char *filename, GError **error)
198 {
199 return rsvg_pixbuf_from_file (filename, error);
200 }
201
202 static GdkPixbuf *
203 pixbuf_from_file_at_zoom (const char *filename, GError **error)
204 {
205 return rsvg_pixbuf_from_file_at_zoom (filename, (double) XZOOM, (double) YZOOM, error);
206 }
207
208 static GdkPixbuf *
209 pixbuf_from_file_at_size (const char *filename, GError **error)
210 {
211 return rsvg_pixbuf_from_file_at_size (filename, EXAMPLE_WIDTH * XZOOM, EXAMPLE_HEIGHT * YZOOM, error);
212 }
213
214 static GdkPixbuf *
215 pixbuf_from_file_at_max_size (const char *filename, GError **error)
216 {
217 return rsvg_pixbuf_from_file_at_max_size (filename, MAX_WIDTH, MAX_HEIGHT, error);
218 }
219
220 static GdkPixbuf *
221 pixbuf_from_file_at_zoom_with_max (const char *filename, GError **error)
222 {
223 return rsvg_pixbuf_from_file_at_zoom_with_max (filename,
224 XZOOM, YZOOM,
225 MAX_ZOOMED_WIDTH, MAX_ZOOMED_HEIGHT,
226 error);
227 }
228
229 typedef GdkPixbuf *(* PixbufCreateFn) (const char *filename, GError **error);
230
231 typedef struct {
232 const char *test_name;
233 PixbufCreateFn pixbuf_create_fn;
234 int expected_width;
235 int expected_height;
236 } PixbufTest;
237
238 static const PixbufTest pixbuf_tests[] = {
239 {
240 "/api/pixbuf_from_file",
241 pixbuf_from_file,
242 EXAMPLE_WIDTH,
243 EXAMPLE_HEIGHT
244 },
245 {
246 "/api/pixbuf_from_file_at_zoom",
247 pixbuf_from_file_at_zoom,
248 EXAMPLE_WIDTH * XZOOM,
249 EXAMPLE_HEIGHT * YZOOM
250 },
251 {
252 "/api/pixbuf_from_file_at_size",
253 pixbuf_from_file_at_size,
254 EXAMPLE_WIDTH * XZOOM,
255 EXAMPLE_HEIGHT * YZOOM
256 },
257 {
258 "/api/pixbuf_from_file_at_max_size",
259 pixbuf_from_file_at_max_size,
260 MAX_WIDTH,
261 MAX_HEIGHT
262 },
263 {
264 "/api/pixbuf_from_file_at_zoom_with_max",
265 pixbuf_from_file_at_zoom_with_max,
266 MAX_ZOOMED_WIDTH,
267 MAX_ZOOMED_HEIGHT
268 },
269 };
270
271 static void
272 test_pixbuf (gconstpointer data)
273 {
274 const PixbufTest *test = data;
275
276 char *filename = get_test_filename ("example.svg");
277 GError *error = NULL;
278
279 GdkPixbuf *pixbuf = test->pixbuf_create_fn (filename, &error);
280
281 g_free (filename);
282
283 g_assert_nonnull (pixbuf);
284 g_assert_no_error (error);
285 g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, test->expected_width);
286 g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, test->expected_height);
287
288 g_object_unref (pixbuf);
289 }
290
291 static void
292 pixbuf_overflow (void)
293 {
294 char *filename = get_test_filename ("example.svg");
295 GError *error = NULL;
296
297 g_assert (!rsvg_pixbuf_from_file_at_zoom (filename, 1000000.0, 1000000.0, &error));
298 g_assert_error (error, RSVG_ERROR, RSVG_ERROR_FAILED);
299 g_error_free (error);
300 g_free (filename);
301 }
302 #endif /* defined(HAVE_PIXBUF) */
303
304 static void
305 noops (void)
306 {
307 /* Just to test that these functions are present in the binary, I guess */
308 rsvg_init ();
309 rsvg_term ();
310 rsvg_cleanup ();
311 }
312
313 static void
314 noops_return_null (void)
315 {
316 RsvgHandle *handle = rsvg_handle_new ();
317
318 g_assert_null (rsvg_handle_get_title (handle));
319 g_assert_null (rsvg_handle_get_desc (handle));
320 g_assert_null (rsvg_handle_get_metadata (handle));
321
322 g_object_unref (handle);
323 }
324
325 static void
326 set_dpi (void)
327 {
328 RsvgHandle *handle;
329 RsvgDimensionData dim;
330
331 rsvg_set_default_dpi (100.0);
332
333 handle = load_test_document ("dpi.svg");
334
335 rsvg_handle_get_dimensions (handle, &dim);
336 g_assert_cmpint (dim.width, ==, 100);
337 g_assert_cmpint (dim.height, ==, 400);
338
339 rsvg_handle_set_dpi (handle, 200.0);
340 rsvg_handle_get_dimensions (handle, &dim);
341 g_assert_cmpint (dim.width, ==, 200);
342 g_assert_cmpint (dim.height, ==, 800);
343 g_object_unref (handle);
344
345 handle = load_test_document ("dpi.svg");
346
347 rsvg_handle_set_dpi_x_y (handle, 400.0, 300.0);
348 rsvg_handle_get_dimensions (handle, &dim);
349 g_assert_cmpint (dim.width, ==, 400);
350 g_assert_cmpint (dim.height, ==, 1200);
351 g_object_unref (handle);
352 }
353
354 static void
355 base_uri (void)
356 {
357 RsvgHandle *handle = rsvg_handle_new ();
358 const char *uri;
359
360 uri = rsvg_handle_get_base_uri (handle);
361 g_assert_null (uri);
362
363 rsvg_handle_set_base_uri (handle, "file:///foo/bar.svg");
364 uri = rsvg_handle_get_base_uri (handle);
365
366 g_assert_cmpstr (uri, ==, "file:///foo/bar.svg");
367
368 g_object_unref (handle);
369 }
370
371 static void
372 base_gfile (void)
373 {
374 RsvgHandle *handle = rsvg_handle_new ();
375 GFile *file;
376 const char *uri;
377
378 uri = rsvg_handle_get_base_uri (handle);
379 g_assert_null (uri);
380
381 file = g_file_new_for_uri ("file:///foo/bar.svg");
382
383 rsvg_handle_set_base_gfile (handle, file);
384 uri = rsvg_handle_get_base_uri (handle);
385
386 g_assert_cmpstr (uri, ==, "file:///foo/bar.svg");
387
388 g_object_unref (file);
389 g_object_unref (handle);
390 }
391
392 static void
393 handle_write_close_free (void)
394 {
395 char *filename = get_test_filename ("dpi.svg");
396 char *data;
397 gsize length;
398 gsize i;
399 GError *error = NULL;
400
401 g_assert (g_file_get_contents (filename, &data, &length, &error));
402 g_free (filename);
403
404 g_assert_nonnull (data);
405 g_assert_no_error (error);
406
407 RsvgHandle *handle = rsvg_handle_new_with_flags (RSVG_HANDLE_FLAGS_NONE);
408
409 for (i = 0; i < length; i++) {
410 g_assert (rsvg_handle_write (handle, (guchar *) &data[i], 1, &error));
411 g_assert_no_error (error);
412 }
413
414 g_assert (rsvg_handle_close (handle, &error));
415 g_assert_no_error (error);
416
417 /* Test that close() is idempotent in the happy case */
418 g_assert (rsvg_handle_close (handle, &error));
419 g_assert_no_error (error);
420
421 rsvg_handle_free (handle);
422 g_free (data);
423 }
424
425 static void
426 handle_new_from_file (void)
427 {
428 char *filename = get_test_filename ("dpi.svg");
429 char *abs_path = g_canonicalize_filename(filename, NULL);
430 char *uri = g_strconcat ("file://", abs_path, NULL);
431
432 RsvgHandle *handle;
433 GError *error = NULL;
434
435 /* rsvg_handle_new_from_file() can take both filenames and URIs */
436
437 handle = rsvg_handle_new_from_file (filename, &error);
438 g_assert_nonnull (handle);
439 g_assert_no_error (error);
440 g_object_unref (handle);
441
442 handle = rsvg_handle_new_from_file (uri, &error);
443 g_assert_nonnull (handle);
444 g_assert_no_error (error);
445 g_object_unref (handle);
446
447 g_free (filename);
448 g_free (abs_path);
449 g_free (uri);
450 }
451
452 static void
453 handle_new_from_data (void)
454 {
455 char *filename = get_test_filename ("dpi.svg");
456 char *data;
457 gsize length;
458 GError *error = NULL;
459
460 g_assert (g_file_get_contents (filename, &data, &length, &error));
461 g_free (filename);
462
463 g_assert_nonnull (data);
464 g_assert_no_error (error);
465
466 RsvgHandle *handle = rsvg_handle_new_from_data ((guint8 *) data, length, &error);
467 g_assert_nonnull (handle);
468 g_assert_no_error (error);
469
470 g_object_unref (handle);
471 g_free (data);
472 }
473
474 static void
475 handle_new_from_gfile_sync (void)
476 {
477 char *filename = get_test_filename ("dpi.svg");
478 GError *error = NULL;
479 GFile *file = g_file_new_for_path (filename);
480 g_assert_nonnull (file);
481
482 g_free (filename);
483
484 RsvgHandle *handle = rsvg_handle_new_from_gfile_sync (file,
485 RSVG_HANDLE_FLAGS_NONE,
486 NULL,
487 &error);
488
489 g_assert_nonnull (handle);
490 g_assert_no_error (error);
491
492 g_object_unref (handle);
493 g_object_unref (file);
494 }
495
496 static void
497 handle_new_from_stream_sync (void)
498 {
499 char *filename = get_test_filename ("dpi.svg");
500 GError *error = NULL;
501 GFile *file = g_file_new_for_path (filename);
502 g_assert_nonnull (file);
503
504 g_free (filename);
505
506 GFileInputStream *stream = g_file_read (file, NULL, &error);
507 g_assert (stream != NULL);
508 g_assert_no_error (error);
509
510 RsvgHandle *handle = rsvg_handle_new_from_stream_sync (G_INPUT_STREAM (stream),
511 file,
512 RSVG_HANDLE_FLAGS_NONE,
513 NULL,
514 &error);
515
516 g_assert_nonnull (handle);
517 g_assert_no_error (error);
518
519 g_object_unref (handle);
520 g_object_unref (file);
521 g_object_unref (stream);
522 }
523
524 static void
525 handle_read_stream_sync (void)
526 {
527 char *filename = get_test_filename ("dpi.svg");
528 GError *error = NULL;
529 GFile *file = g_file_new_for_path (filename);
530 g_assert_nonnull (file);
531
532 g_free (filename);
533
534 GFileInputStream *stream = g_file_read (file, NULL, &error);
535 g_assert_nonnull (stream);
536 g_assert_no_error (error);
537
538 RsvgHandle *handle = rsvg_handle_new ();
539
540 g_assert (rsvg_handle_read_stream_sync (handle, G_INPUT_STREAM (stream), NULL, &error));
541 g_assert_no_error (error);
542
543 g_object_unref (handle);
544 g_object_unref (file);
545 g_object_unref (stream);
546 }
547
548 static void
549 handle_has_sub (void)
550 {
551 RsvgHandle *handle = load_test_document ("example.svg");
552
553 g_assert (rsvg_handle_has_sub (handle, EXAMPLE_ONE_ID));
554 g_assert (rsvg_handle_has_sub (handle, EXAMPLE_TWO_ID));
555 g_assert (!rsvg_handle_has_sub (handle, "#foo"));
556
557 g_object_unref (handle);
558 }
559
560 #ifdef HAVE_PIXBUF
561 static void
562 test_get_pixbuf (gboolean sub)
563 {
564 RsvgHandle *handle = load_test_document ("example.svg");
565
566 GdkPixbuf *pixbuf;
567 if (sub) {
568 pixbuf = rsvg_handle_get_pixbuf_sub (handle, EXAMPLE_ONE_ID);
569 } else {
570 pixbuf = rsvg_handle_get_pixbuf (handle);
571 }
572
573 g_assert_nonnull (pixbuf);
574
575 /* Note that rsvg_handle_get_pixbuf_sub() creates a surface the size of the
576 * whole SVG, not just the size of the sub-element.
577 */
578 g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, EXAMPLE_WIDTH);
579 g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, EXAMPLE_HEIGHT);
580
581 cairo_surface_t *surface_a = test_utils_cairo_surface_from_pixbuf (pixbuf);
582 cairo_surface_t *surface_b = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, EXAMPLE_WIDTH, EXAMPLE_HEIGHT);
583 cairo_surface_t *surface_diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, EXAMPLE_WIDTH, EXAMPLE_HEIGHT);
584
585 g_object_unref (pixbuf);
586
587 g_assert_nonnull (surface_a);
588 g_assert_nonnull (surface_b);
589 g_assert_nonnull (surface_diff);
590
591 cairo_t *cr = cairo_create (surface_b);
592 if (sub) {
593 g_assert (rsvg_handle_render_cairo_sub (handle, cr, EXAMPLE_ONE_ID));
594 } else {
595 g_assert (rsvg_handle_render_cairo (handle, cr));
596 }
597 cairo_destroy (cr);
598
599 g_object_unref (handle);
600
601 TestUtilsBufferDiffResult result = {0, 0};
602 test_utils_compare_surfaces (surface_a, surface_b, surface_diff, &result);
603
604 if (result.pixels_changed && result.max_diff > 0) {
605 g_test_fail ();
606 }
607
608 cairo_surface_destroy (surface_a);
609 cairo_surface_destroy (surface_b);
610 cairo_surface_destroy (surface_diff);
611 }
612
613 static void
614 handle_get_pixbuf (void)
615 {
616 test_get_pixbuf (FALSE);
617 }
618
619 static void
620 handle_get_pixbuf_sub (void)
621 {
622 test_get_pixbuf (TRUE);
623 }
624
625 /* Test that calling rsvg_handle_get_pixbuf() will produce a g_warning if there is a rendering error.
626 * This is for the benefit of the C-based gdk-pixbuf loader, which uses rsvg_handle_get_pixbuf() --- with
627 * the warning, calling code will at least have a clue that something went wrong, since that function
628 * does not return a GError.
629 */
630 static void
631 handle_get_pixbuf_produces_g_warning (void)
632 {
633 if (g_test_subprocess ()) {
634 RsvgHandle *handle = load_test_document ("too-big.svg");
635
636 GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf (handle);
637 g_assert_null (pixbuf);
638 }
639
640 g_test_trap_subprocess (NULL, 0, 0);
641 g_test_trap_assert_stderr ("*WARNING*could not render*");
642 }
643 #endif /* defined(HAVE_PIXBUF) */
644
645 static void
646 dimensions_and_position (void)
647 {
648 RsvgHandle *handle = load_test_document ("example.svg");
649 RsvgDimensionData dim;
650
651 g_assert (rsvg_handle_get_dimensions_sub (handle, &dim, EXAMPLE_TWO_ID));
652 g_assert_cmpint (dim.width, ==, EXAMPLE_TWO_W);
653 g_assert_cmpint (dim.height, ==, EXAMPLE_TWO_H);
654
655 RsvgPositionData pos;
656 g_assert (rsvg_handle_get_position_sub (handle, &pos, EXAMPLE_TWO_ID));
657 g_assert_cmpint (pos.x, ==, EXAMPLE_TWO_X);
658 g_assert_cmpint (pos.y, ==, EXAMPLE_TWO_Y);
659
660 g_assert_false (rsvg_handle_get_position_sub (handle, &pos, EXAMPLE_NONEXISTENT_ID));
661 g_assert_false (rsvg_handle_get_dimensions_sub (handle, &dim, EXAMPLE_NONEXISTENT_ID));
662
663 /* Asking for "position of the whole SVG" (id=NULL) always returns (0, 0) */
664 g_assert (rsvg_handle_get_position_sub (handle, &pos, NULL));
665 g_assert_cmpint (pos.x, ==, 0);
666 g_assert_cmpint (pos.y, ==, 0);
667
668 g_object_unref (handle);
669 }
670
671 struct size_func_data
672 {
673 gboolean called;
674 gboolean destroyed;
675 gboolean testing_size_func_calls;
676 };
677
678 static void
679 size_func (gint *width, gint *height, gpointer user_data)
680 {
681 struct size_func_data *data = user_data;
682
683 if (data->testing_size_func_calls) {
684 g_assert_false (data->called);
685 data->called = TRUE;
686
687 g_assert_false (data->destroyed);
688 }
689
690 *width = 42;
691 *height = 43;
692 }
693
694 static void
695 size_func_destroy (gpointer user_data)
696 {
697 struct size_func_data *data = user_data;
698
699 if (data->testing_size_func_calls) {
700 g_assert_false (data->destroyed);
701 data->destroyed = TRUE;
702 }
703 }
704
705 static void
706 set_size_callback (void)
707 {
708 RsvgHandle *handle;
709 struct size_func_data data;
710 RsvgDimensionData dim;
711
712 handle = load_test_document ("example.svg");
713
714 data.called = FALSE;
715 data.destroyed = FALSE;
716 data.testing_size_func_calls = TRUE;
717
718 rsvg_handle_set_size_callback (handle, size_func, &data, size_func_destroy);
719
720 rsvg_handle_get_dimensions (handle, &dim);
721 g_assert_cmpint (dim.width, ==, 42);
722 g_assert_cmpint (dim.height, ==, 43);
723
724 g_object_unref (handle);
725
726 g_assert_true (data.called);
727 g_assert_true (data.destroyed);
728 }
729
730 static void
731 reset_size_callback (void)
732 {
733 RsvgHandle *handle;
734 struct size_func_data data_1;
735 struct size_func_data data_2;
736
737 handle = load_test_document ("example.svg");
738
739 data_1.called = FALSE;
740 data_1.destroyed = FALSE;
741 data_1.testing_size_func_calls = TRUE;
742
743 rsvg_handle_set_size_callback (handle, size_func, &data_1, size_func_destroy);
744
745 data_2.called = FALSE;
746 data_2.destroyed = FALSE;
747 data_2.testing_size_func_calls = TRUE;
748
749 rsvg_handle_set_size_callback (handle, size_func, &data_2, size_func_destroy);
750 g_assert_true (data_1.destroyed);
751
752 g_object_unref (handle);
753
754 g_assert_true (data_2.destroyed);
755 }
756
757 #ifdef HAVE_PIXBUF
758 static void
759 zero_size_func (gint *width, gint *height, gpointer user_data)
760 {
761 (void) user_data;
762
763 *width = 0;
764 *height = 0;
765 }
766
767 static void
768 render_with_zero_size_callback (void)
769 {
770 /* gdk_pixbuf_get_file_info() uses a GdkPixbufLoader, but in its
771 * "size-prepared" callback it saves the computed size, and then calls
772 * gdk_pixbuf_loader_set_size(loader, 0, 0). Presumably it does to tell
773 * loaders that it only wanted to know the size, but that they shouldn't
774 * decode or render the image to a pixbuf buffer.
775 *
776 * Librsvg used to panic when getting (0, 0) from the size_callback; this
777 * test is to check that there is no such crash now. Instead, librsvg
778 * will return a 1x1 transparent pixbuf.
779 */
780 RsvgHandle *handle;
781 GdkPixbuf *pixbuf;
782
783 handle = load_test_document ("example.svg");
784
785 rsvg_handle_set_size_callback (handle, zero_size_func, NULL, NULL);
786
787 pixbuf = rsvg_handle_get_pixbuf (handle);
788 g_assert_nonnull (pixbuf);
789 g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 1);
790 g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 1);
791
792 g_object_unref (pixbuf);
793 g_object_unref (handle);
794 }
795
796 static void
797 pixbuf_size_func (gint *width, gint *height, gpointer user_data)
798 {
799 (void) user_data;
800
801 *width = 420;
802 *height = 430;
803 }
804
805 static void
806 get_pixbuf_with_size_callback (void)
807 {
808 RsvgHandle *handle = rsvg_handle_new ();
809
810 rsvg_handle_set_size_callback (handle, pixbuf_size_func, NULL, NULL);
811
812 char *filename = get_test_filename ("example.svg");
813 guchar *data = NULL;
814 gsize length;
815 GError *error = NULL;
816
817 g_assert (g_file_get_contents (filename, (gchar **) &data, &length, &error));
818 g_assert_nonnull (data);
819
820 g_free (filename);
821
822 g_assert (rsvg_handle_write (handle, data, length, &error));
823 g_assert_no_error (error);
824
825 g_assert (rsvg_handle_close (handle, &error));
826 g_assert_no_error (error);
827
828 GdkPixbuf *pixbuf = rsvg_handle_get_pixbuf (handle);
829 g_assert_nonnull (pixbuf);
830 g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 420);
831 g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 430);
832
833 g_object_unref (pixbuf);
834 g_free (data);
835 g_object_unref (handle);
836 }
837 #endif /* defined(HAVE_PIXBUF) */
838
839 static void
840 detects_cairo_context_in_error (void)
841 {
842 if (g_test_subprocess ()) {
843 RsvgHandle *handle = load_test_document ("example.svg");
844
845 /* this is wrong; it is to simulate creating a surface and a cairo_t in error */
846 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, -1, -1);
847 cairo_t *cr = cairo_create (surf);
848 /* rsvg_handle_render_cairo() should return FALSE when it gets a cr in an error state */
849 g_assert_false (rsvg_handle_render_cairo (handle, cr));
850
851 return;
852 }
853
854 g_test_trap_subprocess (NULL, 0, 0);
855 g_test_trap_assert_failed ();
856 g_test_trap_assert_stderr ("*WARNING*cannot render on a cairo_t with a failure status*");
857 }
858
859 static gboolean
860 matrixes_are_equal (cairo_matrix_t *a, cairo_matrix_t *b)
861 {
862 return (a->xx == b->xx &&
863 a->yx == b->yx &&
864 a->xy == b->xy &&
865 a->yy == b->yy &&
866 a->x0 == b->x0 &&
867 a->y0 == b->y0);
868 }
869
870 static void
871 can_draw_to_non_image_surface (void)
872 {
873 cairo_rectangle_t rect;
874 cairo_surface_t *surface;
875 cairo_t *cr;
876
877 RsvgHandle *handle = load_test_document ("example.svg");
878
879 rect.x = 0.0;
880 rect.y = 0.0;
881 rect.width = 100.0;
882 rect.height = 100.0;
883
884 /* We create a surface that is not a Cairo image surface,
885 * so we can test that in fact we can render to non-image surfaces.
886 */
887 surface = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &rect);
888 cr = cairo_create (surface);
889
890 cairo_translate (cr, 42.0, 42.0);
891
892 cairo_matrix_t original_affine;
893 cairo_get_matrix (cr, &original_affine);
894
895 g_assert (rsvg_handle_render_cairo (handle, cr));
896
897 cairo_matrix_t new_affine;
898 cairo_get_matrix (cr, &new_affine);
899
900 g_assert (matrixes_are_equal (&original_affine, &new_affine));
901
902 g_object_unref (handle);
903
904 cairo_destroy (cr);
905 cairo_surface_destroy (surface);
906 }
907
908 /* Test that we preserve the affine transformation in the cr during a call
909 * to rsvg_handle_render_cairo_sub().
910 */
911 static void
912 render_cairo_sub (void)
913 {
914 RsvgHandle *handle = load_test_document ("bug334-element-positions.svg");
915
916 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 200, 200);
917 cairo_t *cr = cairo_create (surf);
918
919 cairo_translate (cr, 42.0, 42.0);
920
921 cairo_matrix_t original_affine;
922 cairo_get_matrix (cr, &original_affine);
923
924 g_assert (rsvg_handle_render_cairo_sub (handle, cr, "#button5-leader"));
925
926 cairo_matrix_t new_affine;
927 cairo_get_matrix (cr, &new_affine);
928
929 g_assert (matrixes_are_equal (&original_affine, &new_affine));
930
931 g_object_unref (handle);
932 cairo_destroy (cr);
933 cairo_surface_destroy (surf);
934 }
935
936 static void
937 get_intrinsic_dimensions (void)
938 {
939 RsvgHandle *handle = load_test_document ("example.svg");
940
941 gboolean has_width;
942 RsvgLength width;
943 gboolean has_height;
944 RsvgLength height;
945 gboolean has_viewbox;
946 RsvgRectangle viewbox;
947
948 rsvg_handle_get_intrinsic_dimensions (handle, &has_width, &width, &has_height, &height, &has_viewbox, &viewbox);
949
950 g_assert (has_width);
951 g_assert_cmpfloat (width.length, ==, 100.0);
952 g_assert (width.unit == RSVG_UNIT_PX);
953
954 g_assert (has_height);
955 g_assert_cmpfloat (height.length, ==, 400.0);
956 g_assert (height.unit == RSVG_UNIT_PX);
957
958 g_assert (has_viewbox);
959 g_assert_cmpfloat (viewbox.x, ==, 0.0);
960 g_assert_cmpfloat (viewbox.y, ==, 0.0);
961 g_assert_cmpfloat (viewbox.width, ==, 100.0);
962 g_assert_cmpfloat (viewbox.height, ==, 400.0);
963
964 g_object_unref (handle);
965 }
966
967 static void
968 get_intrinsic_dimensions_missing_values (void)
969 {
970 RsvgHandle *handle = load_test_document ("no-viewbox.svg");
971
972 gboolean has_width;
973 RsvgLength width;
974 gboolean has_height;
975 RsvgLength height;
976 gboolean has_viewbox;
977 RsvgRectangle viewbox;
978
979 rsvg_handle_get_intrinsic_dimensions (handle, &has_width, &width, &has_height, &height, &has_viewbox, &viewbox);
980 g_assert_true (has_width);
981 g_assert_true (has_height);
982 g_assert_false (has_viewbox);
983 g_object_unref (handle);
984 }
985
986 static void
987 get_intrinsic_size_in_pixels_yes (void)
988 {
989 RsvgHandle *handle = load_test_document ("size.svg");
990 gdouble width, height;
991
992 rsvg_handle_set_dpi (handle, 96.0);
993
994 /* Test optional parameters */
995 g_assert (rsvg_handle_get_intrinsic_size_in_pixels (handle, NULL, NULL));
996
997 /* Test the actual result */
998 g_assert (rsvg_handle_get_intrinsic_size_in_pixels (handle, &width, &height));
999 g_assert_cmpfloat (width, ==, 192.0);
1000 g_assert_cmpfloat (height, ==, 288.0);
1001
1002 g_object_unref (handle);
1003 }
1004
1005 static void
1006 get_intrinsic_size_in_pixels_no (void)
1007 {
1008 RsvgHandle *handle = load_test_document ("no-size.svg");
1009 gdouble width, height;
1010
1011 rsvg_handle_set_dpi (handle, 96.0);
1012 g_assert (!rsvg_handle_get_intrinsic_size_in_pixels (handle, &width, &height));
1013 g_assert_cmpfloat (width, ==, 0.0);
1014 g_assert_cmpfloat (height, ==, 0.0);
1015
1016 g_object_unref (handle);
1017 }
1018
1019 static void
1020 set_stylesheet (void)
1021 {
1022 const char *css = "rect { fill: #00ff00; }";
1023
1024 RsvgHandle *handle = load_test_document ("stylesheet.svg");
1025 RsvgHandle *ref_handle = load_test_document ("stylesheet-ref.svg");
1026
1027 cairo_surface_t *output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100);
1028 cairo_surface_t *reference = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100);
1029
1030 RsvgRectangle viewport = { 0.0, 0.0, 100.0, 100.0 };
1031
1032 cairo_t *output_cr = cairo_create (output);
1033 cairo_t *ref_cr = cairo_create (reference);
1034
1035 GError *error = NULL;
1036 g_assert (rsvg_handle_set_stylesheet (handle, (const guint8 *) css, strlen (css), &error));
1037 g_assert_no_error (error);
1038
1039 g_assert (rsvg_handle_render_document (handle, output_cr, &viewport, &error));
1040 g_assert_no_error (error);
1041
1042 g_assert (rsvg_handle_render_document (ref_handle, ref_cr, &viewport, &error));
1043 g_assert_no_error (error);
1044
1045 cairo_surface_t *diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 100, 100);
1046
1047 TestUtilsBufferDiffResult result = {0, 0};
1048 test_utils_compare_surfaces (output, reference, diff, &result);
1049
1050 if (result.pixels_changed && result.max_diff > 0) {
1051 g_test_fail ();
1052 }
1053
1054 cairo_surface_destroy (diff);
1055 cairo_destroy (ref_cr);
1056 cairo_destroy (output_cr);
1057 cairo_surface_destroy (reference);
1058 cairo_surface_destroy (output);
1059 g_object_unref (ref_handle);
1060 g_object_unref (handle);
1061 }
1062
1063 static void
1064 render_document (void)
1065 {
1066 RsvgHandle *handle = load_test_document ("document.svg");
1067
1068 cairo_surface_t *output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 150, 150);
1069 cairo_t *cr = cairo_create (output);
1070
1071 RsvgRectangle viewport = { 50.0, 50.0, 50.0, 50.0 };
1072
1073 GError *error = NULL;
1074 g_assert (rsvg_handle_render_document (handle, cr, &viewport, &error));
1075 g_assert_no_error (error);
1076
1077 cairo_destroy (cr);
1078
1079 cairo_surface_t *expected = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 150, 150);
1080 cr = cairo_create (expected);
1081
1082 cairo_translate (cr, 50.0, 50.0);
1083 cairo_rectangle (cr, 10.0, 10.0, 30.0, 30.0);
1084 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 0.5);
1085 cairo_fill (cr);
1086 cairo_destroy (cr);
1087
1088 cairo_surface_t *diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 150, 150);
1089
1090 TestUtilsBufferDiffResult result = {0, 0};
1091 test_utils_compare_surfaces (output, expected, diff, &result);
1092
1093 if (result.pixels_changed && result.max_diff > 0) {
1094 g_test_fail ();
1095 }
1096
1097 cairo_surface_destroy (diff);
1098 cairo_surface_destroy (expected);
1099 cairo_surface_destroy (output);
1100 g_object_unref (handle);
1101 }
1102
1103 static void
1104 get_geometry_for_layer (void)
1105 {
1106 RsvgHandle *handle = load_test_document ("geometry.svg");
1107
1108 RsvgRectangle viewport = { 0.0, 0.0, 100.0, 400.0 };
1109 RsvgRectangle ink_rect;
1110 RsvgRectangle logical_rect;
1111
1112 GError *error = NULL;
1113
1114 g_assert_false (rsvg_handle_get_geometry_for_layer (handle, "#nonexistent", &viewport,
1115 &ink_rect, &logical_rect, &error));
1116 g_assert_nonnull (error);
1117
1118 g_clear_error (&error);
1119
1120 g_assert (rsvg_handle_get_geometry_for_layer (handle, "#two", &viewport,
1121 &ink_rect, &logical_rect, &error));
1122 g_assert_no_error (error);
1123
1124 g_assert_cmpfloat (ink_rect.x, ==, 5.0);
1125 g_assert_cmpfloat (ink_rect.y, ==, 195.0);
1126 g_assert_cmpfloat (ink_rect.width, ==, 90.0);
1127 g_assert_cmpfloat (ink_rect.height, ==, 110.0);
1128
1129 g_assert_cmpfloat (logical_rect.x, ==, 10.0);
1130 g_assert_cmpfloat (logical_rect.y, ==, 200.0);
1131 g_assert_cmpfloat (logical_rect.width, ==, 80.0);
1132 g_assert_cmpfloat (logical_rect.height, ==, 100.0);
1133
1134 g_object_unref (handle);
1135 }
1136
1137 static void
1138 render_layer (void)
1139 {
1140 RsvgHandle *handle = load_test_document ("layers.svg");
1141
1142 cairo_surface_t *output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1143 cairo_t *cr = cairo_create (output);
1144
1145 RsvgRectangle viewport = { 100.0, 100.0, 100.0, 100.0 };
1146
1147 GError *error = NULL;
1148
1149 g_assert (rsvg_handle_render_layer (handle, cr, "#bar", &viewport, &error));
1150 g_assert_no_error (error);
1151
1152 cairo_destroy (cr);
1153
1154 cairo_surface_t *expected = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1155 cr = cairo_create (expected);
1156
1157 cairo_translate (cr, 100.0, 100.0);
1158 cairo_rectangle (cr, 20.0, 20.0, 30.0, 30.0);
1159 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 1.0);
1160 cairo_fill (cr);
1161 cairo_destroy (cr);
1162
1163 cairo_surface_t *diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1164
1165 TestUtilsBufferDiffResult result = {0, 0};
1166 test_utils_compare_surfaces (output, expected, diff, &result);
1167
1168 if (result.pixels_changed && result.max_diff > 0) {
1169 g_test_fail ();
1170 }
1171
1172 cairo_surface_destroy (diff);
1173 cairo_surface_destroy (expected);
1174 cairo_surface_destroy (output);
1175 g_object_unref (handle);
1176 }
1177
1178 static void
1179 set_cancellable_for_rendering (void)
1180 {
1181 RsvgHandle *handle = load_test_document ("layers.svg");
1182
1183 cairo_surface_t *output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1184 cairo_t *cr = cairo_create (output);
1185
1186 RsvgRectangle viewport = { 100.0, 100.0, 100.0, 100.0 };
1187
1188 GError *error = NULL;
1189
1190 /* Same as in the Rust API test, we cancel immediately and then start rendering. */
1191 GCancellable *cancellable = g_cancellable_new ();
1192 g_cancellable_cancel (cancellable);
1193
1194 rsvg_handle_set_cancellable_for_rendering (handle, cancellable);
1195
1196 g_assert_false (rsvg_handle_render_layer (handle, cr, "#bar", &viewport, &error));
1197 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1198
1199 cairo_destroy (cr);
1200 cairo_surface_destroy (output);
1201 g_object_unref (handle);
1202 }
1203
1204 static void
1205 untransformed_element (void)
1206 {
1207 RsvgHandle *handle = load_test_document ("geometry-element.svg");
1208
1209 RsvgRectangle ink_rect;
1210 RsvgRectangle logical_rect;
1211
1212 GError *error = NULL;
1213
1214 g_assert (!rsvg_handle_get_geometry_for_element (handle, "#nonexistent",
1215 &ink_rect, &logical_rect, &error));
1216 g_assert_nonnull (error);
1217
1218 g_clear_error (&error);
1219
1220 g_assert (rsvg_handle_get_geometry_for_element (handle, "#foo",
1221 &ink_rect, &logical_rect, &error));
1222 g_assert_no_error (error);
1223
1224 g_assert_cmpfloat (ink_rect.x, ==, 0.0);
1225 g_assert_cmpfloat (ink_rect.y, ==, 0.0);
1226 g_assert_cmpfloat (ink_rect.width, ==, 40.0);
1227 g_assert_cmpfloat (ink_rect.height, ==, 50.0);
1228
1229 g_assert_cmpfloat (logical_rect.x, ==, 5.0);
1230 g_assert_cmpfloat (logical_rect.y, ==, 5.0);
1231 g_assert_cmpfloat (logical_rect.width, ==, 30.0);
1232 g_assert_cmpfloat (logical_rect.height, ==, 40.0);
1233
1234 cairo_surface_t *output = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1235 cairo_t *cr = cairo_create (output);
1236
1237 RsvgRectangle viewport = { 100.0, 100.0, 100.0, 100.0 };
1238
1239 g_assert (rsvg_handle_render_element (handle, cr, "#foo", &viewport, &error));
1240 g_assert_no_error (error);
1241
1242 cairo_destroy (cr);
1243
1244 cairo_surface_t *expected = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1245 cr = cairo_create (expected);
1246
1247 cairo_translate (cr, 100.0, 100.0);
1248 cairo_rectangle (cr, 10.0, 10.0, 60.0, 80.0);
1249 cairo_set_source_rgba (cr, 0.0, 0.0, 1.0, 1.0);
1250 cairo_fill_preserve (cr);
1251
1252 cairo_set_line_width (cr, 20.0);
1253 cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
1254 cairo_stroke (cr);
1255
1256 cairo_destroy (cr);
1257
1258 cairo_surface_t *diff = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 300, 300);
1259
1260 TestUtilsBufferDiffResult result = {0, 0};
1261 test_utils_compare_surfaces (output, expected, diff, &result);
1262
1263 if (result.pixels_changed && result.max_diff > 0) {
1264 g_test_fail ();
1265 }
1266
1267 cairo_surface_destroy (diff);
1268 cairo_surface_destroy (expected);
1269 cairo_surface_destroy (output);
1270 g_object_unref (handle);
1271 }
1272
1273 /* https://gitlab.gnome.org/GNOME/librsvg/issues/385 */
1274 static void
1275 no_write_before_close (void)
1276 {
1277 RsvgHandle *handle = rsvg_handle_new();
1278 GError *error = NULL;
1279
1280 g_assert_false (rsvg_handle_close (handle, &error));
1281 g_assert_error (error, RSVG_ERROR, RSVG_ERROR_FAILED);
1282 g_error_free (error);
1283 error = NULL;
1284
1285 /* Test that close() is idempotent in the error case */
1286 g_assert (rsvg_handle_close (handle, &error));
1287 g_assert_no_error (error);
1288
1289 g_object_unref (handle);
1290 }
1291
1292 static void
1293 empty_write_close (void)
1294 {
1295 RsvgHandle *handle = rsvg_handle_new();
1296 GError *error = NULL;
1297 guchar buf = 0;
1298
1299 g_assert_true (rsvg_handle_write (handle, &buf, 0, &error));
1300 g_assert_no_error (error);
1301
1302 g_assert_false (rsvg_handle_close (handle, &error));
1303 g_assert_error (error, RSVG_ERROR, RSVG_ERROR_FAILED);
1304
1305 g_error_free (error);
1306
1307 g_object_unref (handle);
1308 }
1309
1310 /* Test that trying to render a handle that has not been loaded yet results in a g_critical */
1311 static void
1312 ordering_render_before_load (void)
1313 {
1314 if (g_test_subprocess ()) {
1315 RsvgHandle *handle = rsvg_handle_new ();
1316
1317 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
1318 cairo_t *cr = cairo_create (surf);
1319
1320 g_assert_false (rsvg_handle_render_cairo (handle, cr));
1321
1322 return;
1323 }
1324
1325 g_test_trap_subprocess (NULL, 0, 0);
1326 g_test_trap_assert_failed ();
1327 g_test_trap_assert_stderr ("*CRITICAL*Handle has not been loaded*");
1328 }
1329
1330 /* Test that trying to render a handle that is in the middle of loading results in a g_critical */
1331 static void
1332 ordering_render_while_loading (void)
1333 {
1334 if (g_test_subprocess ()) {
1335 RsvgHandle *handle = rsvg_handle_new ();
1336 GError *error = NULL;
1337 guchar buf = '<'; /* as if we started writing some XML */
1338
1339 /* push a single byte to the handle to start its loading process */
1340 g_assert_true (rsvg_handle_write (handle, &buf, 1, &error));
1341 g_assert_no_error (error);
1342
1343 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
1344 cairo_t *cr = cairo_create (surf);
1345
1346 g_assert_false (rsvg_handle_render_cairo (handle, cr));
1347
1348 return;
1349 }
1350
1351 g_test_trap_subprocess (NULL, 0, 0);
1352 g_test_trap_assert_failed ();
1353 g_test_trap_assert_stderr ("*CRITICAL*Handle is still loading*");
1354 }
1355
1356 /* Test that trying to render a handle that was closed with an error results in a g_critical */
1357 static void
1358 rendering_after_close_error (void)
1359 {
1360 if (g_test_subprocess ()) {
1361 RsvgHandle *handle = rsvg_handle_new ();
1362 GError *error = NULL;
1363 guchar buf = 0;
1364
1365 /* Write nothing to start the loading process */
1366 g_assert_true (rsvg_handle_write (handle, &buf, 0, &error));
1367 g_assert_no_error (error);
1368
1369 /* Close the handle */
1370 g_assert_false (rsvg_handle_close (handle, &error));
1371 g_assert_error (error, RSVG_ERROR, RSVG_ERROR_FAILED);
1372
1373 g_error_free (error);
1374
1375 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
1376 cairo_t *cr = cairo_create (surf);
1377
1378 g_assert_false (rsvg_handle_render_cairo (handle, cr));
1379
1380 return;
1381 }
1382
1383 g_test_trap_subprocess (NULL, 0, 0);
1384 g_test_trap_assert_failed ();
1385 g_test_trap_assert_stderr ("*CRITICAL*did you check for errors during the loading stage*");
1386 }
1387
1388 static void
1389 render_cairo_produces_g_warning (void)
1390 {
1391 if (g_test_subprocess ()) {
1392 RsvgHandle *handle = load_test_document ("instancing-limit.svg");
1393
1394 cairo_surface_t *surf = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 10, 10);
1395 cairo_t *cr = cairo_create (surf);
1396
1397 g_assert_false (rsvg_handle_render_cairo (handle, cr));
1398 return;
1399 }
1400
1401 g_test_trap_subprocess (NULL, 0, 0);
1402 g_test_trap_assert_stderr ("*WARNING*exceeded*");
1403 }
1404
1405 static void
1406 cannot_request_external_elements (void)
1407 {
1408 /* We want to test that using one of the _sub() functions will fail
1409 * if the element's id is within an external file.
1410 */
1411
1412 RsvgHandle *handle = load_test_document ("example.svg");
1413 RsvgPositionData pos;
1414
1415 g_assert_false (rsvg_handle_get_position_sub (handle, &pos, "dpi.svg#one"));
1416
1417 g_object_unref (handle);
1418 }
1419
1420 static void
1421 test_flags (RsvgHandleFlags flags)
1422 {
1423 guint read_flags;
1424
1425 RsvgHandle *handle = g_object_new (RSVG_TYPE_HANDLE,
1426 "flags", flags,
1427 NULL);
1428 g_object_get (handle, "flags", &read_flags, NULL);
1429 g_assert (read_flags == flags);
1430
1431 g_object_unref (handle);
1432 }
1433
1434 static void
1435 property_flags (void)
1436 {
1437 test_flags (RSVG_HANDLE_FLAGS_NONE);
1438 test_flags (RSVG_HANDLE_FLAG_UNLIMITED);
1439 test_flags (RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA);
1440 test_flags (RSVG_HANDLE_FLAG_UNLIMITED | RSVG_HANDLE_FLAG_KEEP_IMAGE_DATA);
1441 }
1442
1443 static void
1444 property_dpi (void)
1445 {
1446 RsvgHandle *handle = g_object_new (RSVG_TYPE_HANDLE,
1447 "dpi-x", 42.0,
1448 "dpi-y", 43.0,
1449 NULL);
1450 double x, y;
1451
1452 g_object_get (handle,
1453 "dpi-x", &x,
1454 "dpi-y", &y,
1455 NULL);
1456
1457 g_assert_cmpfloat (x, ==, 42.0);
1458 g_assert_cmpfloat (y, ==, 43.0);
1459
1460 g_object_unref (handle);
1461 }
1462
1463 static void
1464 property_base_uri (void)
1465 {
1466 RsvgHandle *handle = g_object_new (RSVG_TYPE_HANDLE,
1467 "base-uri", "file:///foo/bar.svg",
1468 NULL);
1469 char *uri;
1470
1471 g_object_get (handle,
1472 "base-uri", &uri,
1473 NULL);
1474
1475 g_assert_cmpstr (uri, ==, "file:///foo/bar.svg");
1476 g_free (uri);
1477
1478 g_object_unref (handle);
1479 }
1480
1481 static void
1482 property_dimensions (void)
1483 {
1484 RsvgHandle *handle = load_test_document ("example.svg");
1485
1486 int width;
1487 int height;
1488 double em;
1489 double ex;
1490
1491 g_object_get (handle,
1492 "width", &width,
1493 "height", &height,
1494 "em", &em,
1495 "ex", &ex,
1496 NULL);
1497
1498 g_assert_cmpint (width, ==, EXAMPLE_WIDTH);
1499 g_assert_cmpint (height, ==, EXAMPLE_HEIGHT);
1500
1501 g_assert_cmpfloat (em, ==, (double) EXAMPLE_WIDTH);
1502 g_assert_cmpfloat (ex, ==, (double) EXAMPLE_HEIGHT);
1503
1504 g_object_unref (handle);
1505 }
1506
1507 static void
1508 property_deprecated (void)
1509 {
1510 RsvgHandle *handle = load_test_document ("example.svg");
1511
1512 char *title;
1513 char *desc;
1514 char *metadata;
1515
1516 g_object_get (handle,
1517 "title", &title,
1518 "desc", &desc,
1519 "metadata", &metadata,
1520 NULL);
1521
1522 g_assert_null (title);
1523 g_assert_null (desc);
1524 g_assert_null (metadata);
1525
1526 g_object_unref (handle);
1527 }
1528
1529 static void
1530 return_if_fail (void)
1531 {
1532 if (g_test_subprocess ()) {
1533 RsvgHandle *handle;
1534
1535 handle = rsvg_handle_new();
1536 g_assert_nonnull (handle);
1537
1538 /* NULL is an invalid argument... */
1539 rsvg_handle_set_base_uri (handle, NULL);
1540 g_object_unref (handle);
1541 }
1542
1543 g_test_trap_subprocess (NULL, 0, 0);
1544 /* ... and here we catch that it was validated */
1545 g_test_trap_assert_stderr ("*rsvg_handle_set_base_uri*assertion*failed*");
1546 }
1547
1548 static void
1549 return_if_fail_null_check (void)
1550 {
1551 if (g_test_subprocess ()) {
1552 /* Pass NULL as an argument, incorrectly... */
1553 g_assert_null (rsvg_handle_get_base_uri (NULL));
1554 }
1555
1556 g_test_trap_subprocess (NULL, 0, 0);
1557 /* ... and here we catch that it was validated */
1558 g_test_trap_assert_stderr ("*rsvg_handle_get_base_uri*assertion*handle*failed*");
1559 }
1560
1561 static void
1562 return_if_fail_type_check (void)
1563 {
1564 if (g_test_subprocess ()) {
1565 /* Create a random GObject that is not an RsvgHandle... */
1566 GInputStream *stream = g_memory_input_stream_new();
1567
1568 /* Feed it to an RsvgHandle function so it will bail out */
1569 g_assert_null (rsvg_handle_get_base_uri ((RsvgHandle *) stream));
1570
1571 g_object_unref (stream);
1572 }
1573
1574 g_test_trap_subprocess (NULL, 0, 0);
1575 /* ... and here we catch that it was validated */
1576 g_test_trap_assert_stderr ("*rsvg_handle_get_base_uri*assertion*handle*failed*");
1577 }
1578
1579 static void
1580 library_version_defines (void)
1581 {
1582 gchar *version = g_strdup_printf ("%u.%u.%u",
1583 LIBRSVG_MAJOR_VERSION, LIBRSVG_MINOR_VERSION, LIBRSVG_MICRO_VERSION);
1584 g_assert_cmpstr (version, ==, LIBRSVG_VERSION);
1585 g_free (version);
1586 }
1587
1588 static void
1589 library_version_check (void)
1590 {
1591 g_assert_true(LIBRSVG_CHECK_VERSION(1, 99, 9));
1592 g_assert_true(LIBRSVG_CHECK_VERSION(2, 0, 0));
1593 g_assert_true(LIBRSVG_CHECK_VERSION(2, 50, 7));
1594 g_assert_false(LIBRSVG_CHECK_VERSION(2, 99, 0));
1595 g_assert_false(LIBRSVG_CHECK_VERSION(3, 0, 0));
1596 }
1597
1598 static void
1599 library_version_constants (void)
1600 {
1601 g_assert_cmpuint (rsvg_major_version, ==, LIBRSVG_MAJOR_VERSION);
1602 g_assert_cmpuint (rsvg_minor_version, ==, LIBRSVG_MINOR_VERSION);
1603 g_assert_cmpuint (rsvg_micro_version, ==, LIBRSVG_MICRO_VERSION);
1604 }
1605
1606 typedef struct
1607 {
1608 const gchar *test_name;
1609 const gchar *file_path;
1610 const gchar *id;
1611 gdouble x;
1612 gdouble y;
1613 gdouble width;
1614 gdouble height;
1615 gboolean has_position;
1616 gboolean has_dimensions;
1617 } DimensionsFixtureData;
1618
1619 static void
1620 test_dimensions (gconstpointer user_data)
1621 {
1622 const DimensionsFixtureData *fixture = user_data;
1623 RsvgHandle *handle;
1624 RsvgPositionData position;
1625 RsvgDimensionData dimension;
1626 gchar *target_file;
1627 GError *error = NULL;
1628
1629 target_file = g_build_filename (test_utils_get_test_data_path (),
1630 fixture->file_path, NULL);
1631 handle = rsvg_handle_new_from_file (target_file, &error);
1632 g_free (target_file);
1633 g_assert_no_error (error);
1634
1635 if (fixture->id) {
1636 g_assert (rsvg_handle_has_sub (handle, fixture->id));
1637 g_assert (rsvg_handle_get_position_sub (handle, &position, fixture->id));
1638 g_assert (rsvg_handle_get_dimensions_sub (handle, &dimension, fixture->id));
1639 } else {
1640 rsvg_handle_get_dimensions (handle, &dimension);
1641 }
1642
1643 if (fixture->has_position) {
1644 g_assert_cmpint (fixture->x, ==, position.x);
1645 g_assert_cmpint (fixture->y, ==, position.y);
1646 }
1647
1648 if (fixture->has_dimensions) {
1649 g_assert_cmpint (fixture->width, ==, dimension.width);
1650 g_assert_cmpint (fixture->height, ==, dimension.height);
1651 }
1652
1653 g_object_unref (handle);
1654 }
1655
1656 static DimensionsFixtureData dimensions_fixtures[] =
1657 {
1658 {
1659 "/dimensions/viewbox_only",
1660 "dimensions/bug608102.svg",
1661 NULL,
1662 0, 0, 16, 16,
1663 FALSE, TRUE
1664 },
1665 {
1666 "/dimensions/hundred_percent_width_and_height",
1667 "dimensions/bug612951.svg",
1668 NULL,
1669 0, 0, 47, 47.14,
1670 FALSE, TRUE
1671 },
1672 {
1673 "/dimensions/viewbox_only_2",
1674 "dimensions/bug614018.svg",
1675 NULL,
1676 0, 0, 972, 546,
1677 FALSE, TRUE
1678 },
1679 {
1680 "/dimensions/sub/rect_no_unit",
1681 "dimensions/sub-rect-no-unit.svg",
1682 "#rect-no-unit",
1683 0, 0, 44, 45,
1684 FALSE, TRUE
1685 },
1686 {
1687 "/dimensions/with_viewbox",
1688 "dimensions/bug521-with-viewbox.svg",
1689 "#foo",
1690 50.0, 60.0, 70.0, 80.0,
1691 TRUE, TRUE
1692 },
1693 {
1694 "/dimensions/sub/823",
1695 "dimensions/bug823-position-sub.svg",
1696 "#pad_width",
1697 444.0, 139.0, 0.0, 0.0,
1698 TRUE, FALSE
1699 },
1700 };
1701
1702 typedef struct
1703 {
1704 const char *test_name;
1705 const char *fixture;
1706 size_t buf_size;
1707 } LoadingTestData;
1708
1709 static void
1710 load_n_bytes_at_a_time (gconstpointer data)
1711 {
1712 const LoadingTestData *fixture_data = data;
1713 char *filename = g_build_filename (test_utils_get_test_data_path (), fixture_data->fixture, NULL);
1714 guchar *buf = g_new (guchar, fixture_data->buf_size);
1715 gboolean done;
1716
1717 RsvgHandle *handle;
1718 FILE *file;
1719
1720 file = fopen (filename, "rb");
1721 g_assert_nonnull (file);
1722
1723 handle = rsvg_handle_new_with_flags (RSVG_HANDLE_FLAGS_NONE);
1724
1725 done = FALSE;
1726
1727 do {
1728 size_t num_read;
1729
1730 num_read = fread (buf, 1, fixture_data->buf_size, file);
1731
1732 if (num_read > 0) {
1733 g_assert_true (rsvg_handle_write (handle, buf, num_read, NULL));
1734 } else {
1735 g_assert_cmpint (ferror (file), ==, 0);
1736
1737 if (feof (file)) {
1738 done = TRUE;
1739 }
1740 }
1741 } while (!done);
1742
1743 fclose (file);
1744 g_free (filename);
1745
1746 g_assert_true (rsvg_handle_close (handle, NULL));
1747
1748 g_object_unref (handle);
1749
1750 g_free (buf);
1751 }
1752
1753 static LoadingTestData loading_tests[] = {
1754 { "/loading/one-byte-at-a-time", "loading/gnome-cool.svg", 1 },
1755 { "/loading/compressed-one-byte-at-a-time", "loading/gnome-cool.svgz", 1 },
1756 { "/loading/compressed-two-bytes-at-a-time", "loading/gnome-cool.svgz", 2 } /* to test reading the entire gzip header */
1757 };
1758
1759 #ifdef HAVE_PIXBUF
1760 static void
1761 add_pixbuf_tests (void)
1762 {
1763 gsize i;
1764
1765 /* Tests for rsvg_handle_get_pixbuf() and rsvg_handle_get_pixbuf_sub() */
1766 g_test_add_func ("/api/handle_get_pixbuf", handle_get_pixbuf);
1767 g_test_add_func ("/api/handle_get_pixbuf_sub", handle_get_pixbuf_sub);
1768 g_test_add_func ("/api/handle_get_pixbuf_produces_g_warning", handle_get_pixbuf_produces_g_warning);
1769 g_test_add_func ("/api/get_pixbuf_with_size_callback", get_pixbuf_with_size_callback);
1770
1771 /* Tests for the deprecated GdkPixbuf-based API */
1772 for (i = 0; i < G_N_ELEMENTS (pixbuf_tests); i++) {
1773 g_test_add_data_func (pixbuf_tests[i].test_name, &pixbuf_tests[i], test_pixbuf);
1774 }
1775
1776 g_test_add_func ("/api/pixbuf_overflow", pixbuf_overflow);
1777 }
1778 #endif /* defined(HAVE_PIXBUF) */
1779
1780 /* Tests for the C API of librsvg*/
1781 static void
1782 add_api_tests (void)
1783 {
1784 g_test_add_func ("/api/handle_has_correct_type_info", handle_has_correct_type_info);
1785 g_test_add_func ("/api/flags_registration", flags_registration);
1786 g_test_add_func ("/api/error_registration", error_registration);
1787 g_test_add_func ("/api/noops", noops);
1788 g_test_add_func ("/api/noops_return_null", noops_return_null);
1789 g_test_add_func ("/api/set_dpi", set_dpi);
1790 g_test_add_func ("/api/base_uri", base_uri);
1791 g_test_add_func ("/api/base_gfile", base_gfile);
1792 g_test_add_func ("/api/handle_write_close_free", handle_write_close_free);
1793 g_test_add_func ("/api/handle_new_from_file", handle_new_from_file);
1794 g_test_add_func ("/api/handle_new_from_data", handle_new_from_data);
1795 g_test_add_func ("/api/handle_new_from_gfile_sync", handle_new_from_gfile_sync);
1796 g_test_add_func ("/api/handle_new_from_stream_sync", handle_new_from_stream_sync);
1797 g_test_add_func ("/api/handle_read_stream_sync", handle_read_stream_sync);
1798 g_test_add_func ("/api/handle_has_sub", handle_has_sub);
1799 g_test_add_func ("/api/dimensions_and_position", dimensions_and_position);
1800 g_test_add_func ("/api/set_size_callback", set_size_callback);
1801 g_test_add_func ("/api/reset_size_callback", reset_size_callback);
1802 #ifdef HAVE_PIXBUF
1803 g_test_add_func ("/api/render_with_zero_size_callback", render_with_zero_size_callback);
1804 #endif
1805 g_test_add_func ("/api/detects_cairo_context_in_error", detects_cairo_context_in_error);
1806 g_test_add_func ("/api/can_draw_to_non_image_surface", can_draw_to_non_image_surface);
1807 g_test_add_func ("/api/render_cairo_sub", render_cairo_sub);
1808 g_test_add_func ("/api/get_intrinsic_dimensions", get_intrinsic_dimensions);
1809 g_test_add_func ("/api/get_intrinsic_dimensions_missing_values", get_intrinsic_dimensions_missing_values);
1810 g_test_add_func ("/api/get_intrinsic_size_in_pixels/yes", get_intrinsic_size_in_pixels_yes);
1811 g_test_add_func ("/api/get_intrinsic_size_in_pixels/no", get_intrinsic_size_in_pixels_no);
1812 g_test_add_func ("/api/set_stylesheet", set_stylesheet);
1813 g_test_add_func ("/api/render_document", render_document);
1814 g_test_add_func ("/api/get_geometry_for_layer", get_geometry_for_layer);
1815 g_test_add_func ("/api/render_layer", render_layer);
1816 g_test_add_func ("/api/set_cancellable_for_rendering", set_cancellable_for_rendering);
1817 g_test_add_func ("/api/untransformed_element", untransformed_element);
1818 g_test_add_func ("/api/no_write_before_close", no_write_before_close);
1819 g_test_add_func ("/api/empty_write_close", empty_write_close);
1820 g_test_add_func ("/api/ordering_render_before_load", ordering_render_before_load);
1821 g_test_add_func ("/api/ordering_render_while_loading", ordering_render_while_loading);
1822 g_test_add_func ("/api/rendering_after_close_error", rendering_after_close_error);
1823 g_test_add_func ("/api/render_cairo_produces_g_warning", render_cairo_produces_g_warning);
1824 g_test_add_func ("/api/cannot_request_external_elements", cannot_request_external_elements);
1825 g_test_add_func ("/api/property_flags", property_flags);
1826 g_test_add_func ("/api/property_dpi", property_dpi);
1827 g_test_add_func ("/api/property_base_uri", property_base_uri);
1828 g_test_add_func ("/api/property_dimensions", property_dimensions);
1829 g_test_add_func ("/api/property_deprecated", property_deprecated);
1830 g_test_add_func ("/api/return_if_fail", return_if_fail);
1831 g_test_add_func ("/api/return_if_fail_null_check", return_if_fail_null_check);
1832 g_test_add_func ("/api/return_if_fail_type_check", return_if_fail_type_check);
1833 g_test_add_func ("/api/library_version_defines", library_version_defines);
1834 g_test_add_func ("/api/library_version_check", library_version_check);
1835 g_test_add_func ("/api/library_version_constants", library_version_constants);
1836 }
1837
1838 /* Tests for the deprecated APIs to get geometries */
1839 static void
1840 add_geometry_tests (void)
1841 {
1842 gsize i;
1843
1844 for (i = 0; i < G_N_ELEMENTS (dimensions_fixtures); i++)
1845 g_test_add_data_func (dimensions_fixtures[i].test_name, &dimensions_fixtures[i], test_dimensions);
1846 }
1847
1848 /* Tests for the deprecated API for loading bytes at a time */
1849 static void
1850 add_loading_tests (void)
1851 {
1852 gsize i;
1853
1854 for (i = 0; i < G_N_ELEMENTS (loading_tests); i++) {
1855 g_test_add_data_func (loading_tests[i].test_name, &loading_tests[i], load_n_bytes_at_a_time);
1856 }
1857 }
1858
1859 int
1860 main (int argc, char **argv)
1861 {
1862 g_test_init (&argc, &argv, NULL);
1863
1864 test_utils_print_dependency_versions ();
1865
1866 #ifdef HAVE_PIXBUF
1867 add_pixbuf_tests ();
1868 #endif
1869 add_api_tests ();
1870 add_geometry_tests ();
1871 add_loading_tests ();
1872
1873 return g_test_run ();
1874 }