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>
31 #include <pcut/pcut.h>
33 #include <ui/resource.h>
34 #include "../private/resource.h"
38 PCUT_TEST_SUITE(resource
);
40 static errno_t
testgc_bitmap_create(void *, gfx_bitmap_params_t
*,
41 gfx_bitmap_alloc_t
*, void **);
42 static errno_t
testgc_bitmap_destroy(void *);
43 static errno_t
testgc_bitmap_render(void *, gfx_rect_t
*, gfx_coord2_t
*);
44 static errno_t
testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t
*);
46 static void test_expose(void *);
48 static gfx_context_ops_t ops
= {
49 .bitmap_create
= testgc_bitmap_create
,
50 .bitmap_destroy
= testgc_bitmap_destroy
,
51 .bitmap_render
= testgc_bitmap_render
,
52 .bitmap_get_alloc
= testgc_bitmap_get_alloc
58 gfx_bitmap_params_t bm_params
;
68 gfx_bitmap_alloc_t alloc
;
76 /** Create and destroy UI resource */
77 PCUT_TEST(create_destroy
)
80 gfx_context_t
*gc
= NULL
;
82 ui_resource_t
*resource
= NULL
;
84 memset(&tgc
, 0, sizeof(tgc
));
85 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
86 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
88 rc
= ui_resource_create(gc
, false, &resource
);
89 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
90 PCUT_ASSERT_NOT_NULL(resource
);
92 PCUT_ASSERT_NOT_NULL(resource
->tface
);
93 PCUT_ASSERT_NOT_NULL(resource
->font
);
95 ui_resource_destroy(resource
);
97 rc
= gfx_context_delete(gc
);
98 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
101 /** ui_resource_destroy() can take NULL argument (no-op) */
102 PCUT_TEST(destroy_null
)
104 ui_resource_destroy(NULL
);
107 /** ui_resource_set_expose_cb() / ui_resource_expose() */
108 PCUT_TEST(set_expose_cb_expose
)
111 gfx_context_t
*gc
= NULL
;
113 ui_resource_t
*resource
= NULL
;
116 memset(&tgc
, 0, sizeof(tgc
));
117 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
118 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
120 rc
= ui_resource_create(gc
, false, &resource
);
121 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
122 PCUT_ASSERT_NOT_NULL(resource
);
124 ui_resource_set_expose_cb(resource
, test_expose
, &resp
);
127 ui_resource_expose(resource
);
128 PCUT_ASSERT_TRUE(resp
.expose
);
130 ui_resource_destroy(resource
);
132 rc
= gfx_context_delete(gc
);
133 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
136 /** ui_resource_get_font() returns the font */
140 gfx_context_t
*gc
= NULL
;
142 ui_resource_t
*resource
= NULL
;
145 memset(&tgc
, 0, sizeof(tgc
));
146 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
147 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
149 rc
= ui_resource_create(gc
, false, &resource
);
150 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
151 PCUT_ASSERT_NOT_NULL(resource
);
153 font
= ui_resource_get_font(resource
);
154 PCUT_ASSERT_EQUALS(resource
->font
, font
);
156 ui_resource_destroy(resource
);
158 rc
= gfx_context_delete(gc
);
159 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
162 /** ui_resource_is_textmode() returns the textmode flag */
163 PCUT_TEST(is_textmode
)
166 gfx_context_t
*gc
= NULL
;
168 ui_resource_t
*resource
= NULL
;
170 memset(&tgc
, 0, sizeof(tgc
));
171 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
172 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
174 rc
= ui_resource_create(gc
, false, &resource
);
175 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
176 PCUT_ASSERT_NOT_NULL(resource
);
178 /* To make sure let's test both true and false case */
179 resource
->textmode
= true;
180 PCUT_ASSERT_TRUE(ui_resource_is_textmode(resource
));
181 resource
->textmode
= false;
182 PCUT_ASSERT_FALSE(ui_resource_is_textmode(resource
));
184 ui_resource_destroy(resource
);
186 rc
= gfx_context_delete(gc
);
187 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
190 /** ui_resource_get_wnd_face_color() returns window face color */
191 PCUT_TEST(get_wnd_face_color
)
194 gfx_context_t
*gc
= NULL
;
196 ui_resource_t
*resource
= NULL
;
199 memset(&tgc
, 0, sizeof(tgc
));
200 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
201 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
203 rc
= ui_resource_create(gc
, false, &resource
);
204 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
205 PCUT_ASSERT_NOT_NULL(resource
);
207 color
= ui_resource_get_wnd_face_color(resource
);
208 PCUT_ASSERT_EQUALS(resource
->wnd_face_color
, color
);
210 ui_resource_destroy(resource
);
212 rc
= gfx_context_delete(gc
);
213 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
216 /** ui_resource_get_wnd_text_color() returns window text color */
217 PCUT_TEST(get_wnd_text_color
)
220 gfx_context_t
*gc
= NULL
;
222 ui_resource_t
*resource
= NULL
;
225 memset(&tgc
, 0, sizeof(tgc
));
226 rc
= gfx_context_new(&ops
, &tgc
, &gc
);
227 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
229 rc
= ui_resource_create(gc
, false, &resource
);
230 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
231 PCUT_ASSERT_NOT_NULL(resource
);
233 color
= ui_resource_get_wnd_text_color(resource
);
234 PCUT_ASSERT_EQUALS(resource
->wnd_text_color
, color
);
236 ui_resource_destroy(resource
);
238 rc
= gfx_context_delete(gc
);
239 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
242 static errno_t
testgc_bitmap_create(void *arg
, gfx_bitmap_params_t
*params
,
243 gfx_bitmap_alloc_t
*alloc
, void **rbm
)
245 test_gc_t
*tgc
= (test_gc_t
*) arg
;
246 testgc_bitmap_t
*tbm
;
248 tbm
= calloc(1, sizeof(testgc_bitmap_t
));
253 tbm
->alloc
.pitch
= (params
->rect
.p1
.x
- params
->rect
.p0
.x
) *
256 tbm
->alloc
.pixels
= calloc(sizeof(uint32_t),
257 (params
->rect
.p1
.x
- params
->rect
.p0
.x
) *
258 (params
->rect
.p1
.y
- params
->rect
.p0
.y
));
260 if (tbm
->alloc
.pixels
== NULL
) {
269 tgc
->bm_created
= true;
270 tgc
->bm_params
= *params
;
271 tgc
->bm_pixels
= tbm
->alloc
.pixels
;
276 static errno_t
testgc_bitmap_destroy(void *bm
)
278 testgc_bitmap_t
*tbm
= (testgc_bitmap_t
*)bm
;
280 free(tbm
->alloc
.pixels
);
281 tbm
->tgc
->bm_destroyed
= true;
286 static errno_t
testgc_bitmap_render(void *bm
, gfx_rect_t
*srect
,
289 testgc_bitmap_t
*tbm
= (testgc_bitmap_t
*)bm
;
290 tbm
->tgc
->bm_rendered
= true;
291 tbm
->tgc
->bm_srect
= *srect
;
292 tbm
->tgc
->bm_offs
= *offs
;
296 static errno_t
testgc_bitmap_get_alloc(void *bm
, gfx_bitmap_alloc_t
*alloc
)
298 testgc_bitmap_t
*tbm
= (testgc_bitmap_t
*)bm
;
300 tbm
->tgc
->bm_got_alloc
= true;
304 static void test_expose(void *arg
)
306 test_resp_t
*resp
= (test_resp_t
*) arg
;
311 PCUT_EXPORT(resource
);