1 | /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | /* GdkPixbuf library - tests for GdkPixbuf constructors |
3 | * |
4 | * Copyright (C) 2018 Federico Mena Quintero |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
18 | */ |
19 | #include "config.h" |
20 | #include "gdk-pixbuf/gdk-pixbuf.h" |
21 | #include "test-common.h" |
22 | |
23 | static void |
24 | test_no_construct_properties (void) |
25 | { |
26 | GdkPixbuf *pixbuf = g_object_new (GDK_TYPE_PIXBUF, NULL); |
27 | GBytes *bytes; |
28 | guchar *pixels; |
29 | |
30 | g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, 1); |
31 | g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, 1); |
32 | |
33 | g_object_get (object: pixbuf, first_property_name: "pixel-bytes" , &bytes, NULL); |
34 | g_assert (bytes != NULL); |
35 | g_bytes_unref (bytes); |
36 | |
37 | g_object_get (object: pixbuf, first_property_name: "pixels" , &pixels, NULL); |
38 | g_assert (pixels != NULL); |
39 | } |
40 | |
41 | #define WIDTH 10 |
42 | #define HEIGHT 20 |
43 | #define BUFFER_SIZE (WIDTH * HEIGHT * 4) |
44 | #define ROWSTRIDE (WIDTH * 4) |
45 | |
46 | static void |
47 | test_pixels (void) |
48 | { |
49 | guchar *pixels = g_new0 (guchar, BUFFER_SIZE); |
50 | |
51 | GdkPixbuf *pixbuf = g_object_new (GDK_TYPE_PIXBUF, |
52 | first_property_name: "width" , WIDTH, |
53 | "height" , HEIGHT, |
54 | "rowstride" , ROWSTRIDE, |
55 | "bits-per-sample" , 8, |
56 | "n-channels" , 3, |
57 | "has-alpha" , TRUE, |
58 | "pixels" , pixels, |
59 | NULL); |
60 | |
61 | g_assert (gdk_pixbuf_get_pixels (pixbuf) == pixels); |
62 | g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, WIDTH); |
63 | g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, HEIGHT); |
64 | g_assert_cmpint (gdk_pixbuf_get_rowstride (pixbuf), ==, ROWSTRIDE); |
65 | |
66 | g_object_unref (object: pixbuf); |
67 | g_free (mem: pixels); |
68 | } |
69 | |
70 | static void |
71 | test_bytes (void) |
72 | { |
73 | guchar *pixels = g_new0 (guchar, BUFFER_SIZE); |
74 | GBytes *bytes = g_bytes_new_take (data: pixels, BUFFER_SIZE); |
75 | GdkPixbuf *pixbuf = g_object_new (GDK_TYPE_PIXBUF, |
76 | first_property_name: "width" , WIDTH, |
77 | "height" , HEIGHT, |
78 | "rowstride" , ROWSTRIDE, |
79 | "bits-per-sample" , 8, |
80 | "n-channels" , 3, |
81 | "has-alpha" , TRUE, |
82 | "pixel-bytes" , bytes, |
83 | NULL); |
84 | |
85 | GBytes *read_bytes = gdk_pixbuf_read_pixel_bytes (pixbuf); |
86 | |
87 | g_assert (read_bytes == bytes); |
88 | g_assert_cmpint (gdk_pixbuf_get_width (pixbuf), ==, WIDTH); |
89 | g_assert_cmpint (gdk_pixbuf_get_height (pixbuf), ==, HEIGHT); |
90 | g_assert_cmpint (gdk_pixbuf_get_rowstride (pixbuf), ==, ROWSTRIDE); |
91 | |
92 | g_bytes_unref (bytes: read_bytes); |
93 | g_object_unref (object: pixbuf); |
94 | g_bytes_unref (bytes); |
95 | } |
96 | |
97 | int |
98 | main (int argc, char **argv) |
99 | { |
100 | g_test_init (argc: &argc, argv: &argv, NULL); |
101 | |
102 | g_test_add_func (testpath: "/pixbuf/construction/no_construct_properties" , test_func: test_no_construct_properties); |
103 | g_test_add_func (testpath: "/pixbuf/construction/pixels" , test_func: test_pixels); |
104 | g_test_add_func (testpath: "/pixbuf/construction/bytes" , test_func: test_bytes); |
105 | |
106 | return g_test_run (); |
107 | } |
108 | |