1 | #include "config.h" |
2 | #include "gdk-pixbuf/gdk-pixbuf.h" |
3 | #include "test-common.h" |
4 | #include <string.h> |
5 | #include <glib.h> |
6 | |
7 | static void |
8 | test_serialize (void) |
9 | { |
10 | GError *error = NULL; |
11 | GdkPixbuf *pixbuf; |
12 | GdkPixbuf *pixbuf2; |
13 | GVariant *data; |
14 | GIcon *icon; |
15 | GInputStream *stream; |
16 | |
17 | if (!format_supported (filename: "png" )) |
18 | { |
19 | g_test_skip (msg: "format not supported" ); |
20 | return; |
21 | } |
22 | |
23 | pixbuf = gdk_pixbuf_new_from_file (filename: g_test_get_filename (file_type: G_TEST_DIST, first_path: "test-image.png" , NULL), error: &error); |
24 | g_assert_no_error (error); |
25 | g_assert (pixbuf != NULL); |
26 | |
27 | /* turn it into a GVariant */ |
28 | data = g_icon_serialize (G_ICON (pixbuf)); |
29 | |
30 | /* back to a GIcon, but this will be a GBytesIcon, not GdkPixbuf */ |
31 | icon = g_icon_deserialize (value: data); |
32 | g_assert (G_IS_BYTES_ICON (icon)); |
33 | |
34 | /* but since that is a GLoadableIcon, we can load it again */ |
35 | stream = g_loadable_icon_load (G_LOADABLE_ICON (icon), size: 0, NULL, NULL, error: &error); |
36 | g_assert_no_error (error); |
37 | pixbuf2 = gdk_pixbuf_new_from_stream (stream, NULL, error: &error); |
38 | g_assert_no_error (error); |
39 | |
40 | /* make sure that the pixels are the same. |
41 | * our _serialize() uses png, so this should be perfect. |
42 | */ |
43 | { |
44 | guchar *pixels_a, *pixels_b; |
45 | guint len_a, len_b; |
46 | pixels_a = gdk_pixbuf_get_pixels_with_length (pixbuf, length: &len_a); |
47 | pixels_b = gdk_pixbuf_get_pixels_with_length (pixbuf: pixbuf2, length: &len_b); |
48 | g_assert (len_a == len_b); |
49 | g_assert (memcmp (pixels_a, pixels_b, len_a) == 0); |
50 | } |
51 | |
52 | g_object_unref (object: pixbuf2); |
53 | g_object_unref (object: pixbuf); |
54 | g_object_unref (object: stream); |
55 | g_variant_unref (value: data); |
56 | |
57 | } |
58 | |
59 | int |
60 | main (int argc, char **argv) |
61 | { |
62 | g_test_init (argc: &argc, argv: &argv, NULL); |
63 | |
64 | g_test_add_func (testpath: "/pixbuf/icon/serialize" , test_func: test_serialize); |
65 | |
66 | return g_test_run (); |
67 | } |
68 | |