Debian Codegraph @

librsvg-c/tests-c/api.c

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