2 * Copyright (c) 2021 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <gfx/context.h>
30 #include <gfx/coord.h>
32 #include <pcut/pcut.h>
34 #include <ui/control.h>
36 #include <ui/resource.h>
38 #include "../private/dummygc.h"
39 #include "../private/image.h"
43 PCUT_TEST_SUITE(image
);
45 /** Create and destroy image */
46 PCUT_TEST(create_destroy
)
48 ui_image_t
*image
= NULL
;
52 rc
= ui_image_create(NULL
, NULL
, &brect
, &image
);
53 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
54 PCUT_ASSERT_NOT_NULL(image
);
56 ui_image_destroy(image
);
59 /** ui_image_destroy() can take NULL argument (no-op) */
60 PCUT_TEST(destroy_null
)
62 ui_image_destroy(NULL
);
65 /** ui_image_ctl() returns control that has a working virtual destructor */
68 ui_image_t
*image
= NULL
;
69 ui_control_t
*control
;
73 rc
= ui_image_create(NULL
, NULL
, &brect
, &image
);
74 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
75 PCUT_ASSERT_NOT_NULL(image
);
77 control
= ui_image_ctl(image
);
78 PCUT_ASSERT_NOT_NULL(control
);
80 ui_control_destroy(control
);
83 /** Set image rectangle sets internal field */
89 ui_resource_t
*resource
= NULL
;
90 ui_image_t
*image
= NULL
;
94 rc
= dummygc_create(&dgc
);
95 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
97 gc
= dummygc_get_ctx(dgc
);
99 rc
= ui_resource_create(gc
, false, &resource
);
100 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
101 PCUT_ASSERT_NOT_NULL(resource
);
103 rc
= ui_image_create(resource
, NULL
, &brect
, &image
);
104 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
105 PCUT_ASSERT_NOT_NULL(image
);
112 ui_image_set_rect(image
, &rect
);
113 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, image
->rect
.p0
.x
);
114 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, image
->rect
.p0
.y
);
115 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, image
->rect
.p1
.x
);
116 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, image
->rect
.p1
.y
);
118 ui_image_destroy(image
);
119 ui_resource_destroy(resource
);
120 dummygc_destroy(dgc
);
123 /** Set image flags sets internal field */
126 ui_image_t
*image
= NULL
;
130 rc
= ui_image_create(NULL
, NULL
, &brect
, &image
);
131 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
132 PCUT_ASSERT_NOT_NULL(image
);
134 PCUT_ASSERT_INT_EQUALS(0, image
->flags
);
136 ui_image_set_flags(image
, ui_imgf_frame
);
137 PCUT_ASSERT_INT_EQUALS(ui_imgf_frame
, image
->flags
);
139 ui_image_destroy(image
);
142 /** Set image bitmap */
145 ui_image_t
*image
= NULL
;
148 gfx_bitmap_t
*bitmap
;
149 gfx_bitmap_params_t params
;
152 ui_resource_t
*resource
= NULL
;
155 rc
= dummygc_create(&dgc
);
156 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
158 gc
= dummygc_get_ctx(dgc
);
160 rc
= ui_resource_create(gc
, false, &resource
);
161 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
162 PCUT_ASSERT_NOT_NULL(resource
);
164 rc
= ui_image_create(resource
, NULL
, &brect
, &image
);
165 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
166 PCUT_ASSERT_NOT_NULL(image
);
173 ui_image_set_rect(image
, &rect
);
174 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, image
->rect
.p0
.x
);
175 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, image
->rect
.p0
.y
);
176 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, image
->rect
.p1
.x
);
177 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, image
->rect
.p1
.y
);
179 gfx_bitmap_params_init(¶ms
);
180 rc
= gfx_bitmap_create(gc
, ¶ms
, NULL
, &bitmap
);
181 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
183 ui_image_set_bmp(image
, bitmap
, &brect
);
184 PCUT_ASSERT_EQUALS(bitmap
, image
->bitmap
);
186 rc
= ui_image_paint(image
);
187 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
189 ui_image_destroy(image
);
190 ui_resource_destroy(resource
);
191 dummygc_destroy(dgc
);
199 gfx_bitmap_params_t params
;
200 gfx_bitmap_t
*bitmap
;
201 ui_resource_t
*resource
= NULL
;
202 ui_image_t
*image
= NULL
;
206 rc
= dummygc_create(&dgc
);
207 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
209 gc
= dummygc_get_ctx(dgc
);
211 rc
= ui_resource_create(gc
, false, &resource
);
212 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
213 PCUT_ASSERT_NOT_NULL(resource
);
215 gfx_bitmap_params_init(¶ms
);
216 rc
= gfx_bitmap_create(gc
, ¶ms
, NULL
, &bitmap
);
217 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
219 rc
= ui_image_create(resource
, bitmap
, &brect
, &image
);
220 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
221 PCUT_ASSERT_NOT_NULL(image
);
223 rc
= ui_image_paint(image
);
224 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
226 /* Check that we can paint image after setting bitmap to NULL */
228 ui_image_set_bmp(image
, NULL
, &brect
);
230 rc
= ui_image_paint(image
);
231 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
233 ui_image_destroy(image
);
234 ui_resource_destroy(resource
);
235 dummygc_destroy(dgc
);