1 | /*
|
---|
2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #include <gfx/context.h>
|
---|
30 | #include <mem.h>
|
---|
31 | #include <pcut/pcut.h>
|
---|
32 | #include <stdbool.h>
|
---|
33 | #include <ui/resource.h>
|
---|
34 | #include "../private/resource.h"
|
---|
35 |
|
---|
36 | PCUT_INIT;
|
---|
37 |
|
---|
38 | PCUT_TEST_SUITE(resource);
|
---|
39 |
|
---|
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 *);
|
---|
45 |
|
---|
46 | static void test_expose(void *);
|
---|
47 |
|
---|
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
|
---|
53 | };
|
---|
54 |
|
---|
55 | typedef struct {
|
---|
56 | bool bm_created;
|
---|
57 | bool bm_destroyed;
|
---|
58 | gfx_bitmap_params_t bm_params;
|
---|
59 | void *bm_pixels;
|
---|
60 | gfx_rect_t bm_srect;
|
---|
61 | gfx_coord2_t bm_offs;
|
---|
62 | bool bm_rendered;
|
---|
63 | bool bm_got_alloc;
|
---|
64 | } test_gc_t;
|
---|
65 |
|
---|
66 | typedef struct {
|
---|
67 | test_gc_t *tgc;
|
---|
68 | gfx_bitmap_alloc_t alloc;
|
---|
69 | bool myalloc;
|
---|
70 | } testgc_bitmap_t;
|
---|
71 |
|
---|
72 | typedef struct {
|
---|
73 | bool expose;
|
---|
74 | } test_resp_t;
|
---|
75 |
|
---|
76 | /** Create and destroy UI resource */
|
---|
77 | PCUT_TEST(create_destroy)
|
---|
78 | {
|
---|
79 | errno_t rc;
|
---|
80 | gfx_context_t *gc = NULL;
|
---|
81 | test_gc_t tgc;
|
---|
82 | ui_resource_t *resource = NULL;
|
---|
83 |
|
---|
84 | memset(&tgc, 0, sizeof(tgc));
|
---|
85 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
86 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
87 |
|
---|
88 | rc = ui_resource_create(gc, false, &resource);
|
---|
89 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
90 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
91 |
|
---|
92 | PCUT_ASSERT_NOT_NULL(resource->tface);
|
---|
93 | PCUT_ASSERT_NOT_NULL(resource->font);
|
---|
94 |
|
---|
95 | ui_resource_destroy(resource);
|
---|
96 |
|
---|
97 | rc = gfx_context_delete(gc);
|
---|
98 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** ui_resource_destroy() can take NULL argument (no-op) */
|
---|
102 | PCUT_TEST(destroy_null)
|
---|
103 | {
|
---|
104 | ui_resource_destroy(NULL);
|
---|
105 | }
|
---|
106 |
|
---|
107 | /** ui_resource_set_expose_cb() / ui_resource_expose() */
|
---|
108 | PCUT_TEST(set_expose_cb_expose)
|
---|
109 | {
|
---|
110 | errno_t rc;
|
---|
111 | gfx_context_t *gc = NULL;
|
---|
112 | test_gc_t tgc;
|
---|
113 | ui_resource_t *resource = NULL;
|
---|
114 | test_resp_t resp;
|
---|
115 |
|
---|
116 | memset(&tgc, 0, sizeof(tgc));
|
---|
117 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
118 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
119 |
|
---|
120 | rc = ui_resource_create(gc, false, &resource);
|
---|
121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
122 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
123 |
|
---|
124 | ui_resource_set_expose_cb(resource, test_expose, &resp);
|
---|
125 |
|
---|
126 | resp.expose = false;
|
---|
127 | ui_resource_expose(resource);
|
---|
128 | PCUT_ASSERT_TRUE(resp.expose);
|
---|
129 |
|
---|
130 | ui_resource_destroy(resource);
|
---|
131 |
|
---|
132 | rc = gfx_context_delete(gc);
|
---|
133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /** ui_resource_get_font() returns the font */
|
---|
137 | PCUT_TEST(get_font)
|
---|
138 | {
|
---|
139 | errno_t rc;
|
---|
140 | gfx_context_t *gc = NULL;
|
---|
141 | test_gc_t tgc;
|
---|
142 | ui_resource_t *resource = NULL;
|
---|
143 | gfx_font_t *font;
|
---|
144 |
|
---|
145 | memset(&tgc, 0, sizeof(tgc));
|
---|
146 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
147 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
148 |
|
---|
149 | rc = ui_resource_create(gc, false, &resource);
|
---|
150 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
151 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
152 |
|
---|
153 | font = ui_resource_get_font(resource);
|
---|
154 | PCUT_ASSERT_EQUALS(resource->font, font);
|
---|
155 |
|
---|
156 | ui_resource_destroy(resource);
|
---|
157 |
|
---|
158 | rc = gfx_context_delete(gc);
|
---|
159 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** ui_resource_is_textmode() returns the textmode flag */
|
---|
163 | PCUT_TEST(is_textmode)
|
---|
164 | {
|
---|
165 | errno_t rc;
|
---|
166 | gfx_context_t *gc = NULL;
|
---|
167 | test_gc_t tgc;
|
---|
168 | ui_resource_t *resource = NULL;
|
---|
169 |
|
---|
170 | memset(&tgc, 0, sizeof(tgc));
|
---|
171 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
172 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
173 |
|
---|
174 | rc = ui_resource_create(gc, false, &resource);
|
---|
175 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
176 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
177 |
|
---|
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));
|
---|
183 |
|
---|
184 | ui_resource_destroy(resource);
|
---|
185 |
|
---|
186 | rc = gfx_context_delete(gc);
|
---|
187 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** ui_resource_get_wnd_face_color() returns window face color */
|
---|
191 | PCUT_TEST(get_wnd_face_color)
|
---|
192 | {
|
---|
193 | errno_t rc;
|
---|
194 | gfx_context_t *gc = NULL;
|
---|
195 | test_gc_t tgc;
|
---|
196 | ui_resource_t *resource = NULL;
|
---|
197 | gfx_color_t *color;
|
---|
198 |
|
---|
199 | memset(&tgc, 0, sizeof(tgc));
|
---|
200 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
201 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
202 |
|
---|
203 | rc = ui_resource_create(gc, false, &resource);
|
---|
204 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
205 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
206 |
|
---|
207 | color = ui_resource_get_wnd_face_color(resource);
|
---|
208 | PCUT_ASSERT_EQUALS(resource->wnd_face_color, color);
|
---|
209 |
|
---|
210 | ui_resource_destroy(resource);
|
---|
211 |
|
---|
212 | rc = gfx_context_delete(gc);
|
---|
213 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** ui_resource_get_wnd_text_color() returns window text color */
|
---|
217 | PCUT_TEST(get_wnd_text_color)
|
---|
218 | {
|
---|
219 | errno_t rc;
|
---|
220 | gfx_context_t *gc = NULL;
|
---|
221 | test_gc_t tgc;
|
---|
222 | ui_resource_t *resource = NULL;
|
---|
223 | gfx_color_t *color;
|
---|
224 |
|
---|
225 | memset(&tgc, 0, sizeof(tgc));
|
---|
226 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
227 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
228 |
|
---|
229 | rc = ui_resource_create(gc, false, &resource);
|
---|
230 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
231 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
232 |
|
---|
233 | color = ui_resource_get_wnd_text_color(resource);
|
---|
234 | PCUT_ASSERT_EQUALS(resource->wnd_text_color, color);
|
---|
235 |
|
---|
236 | ui_resource_destroy(resource);
|
---|
237 |
|
---|
238 | rc = gfx_context_delete(gc);
|
---|
239 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
240 | }
|
---|
241 |
|
---|
242 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
243 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
244 | {
|
---|
245 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
246 | testgc_bitmap_t *tbm;
|
---|
247 |
|
---|
248 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
249 | if (tbm == NULL)
|
---|
250 | return ENOMEM;
|
---|
251 |
|
---|
252 | if (alloc == NULL) {
|
---|
253 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
254 | sizeof(uint32_t);
|
---|
255 | tbm->alloc.off0 = 0;
|
---|
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));
|
---|
259 | tbm->myalloc = true;
|
---|
260 | if (tbm->alloc.pixels == NULL) {
|
---|
261 | free(tbm);
|
---|
262 | return ENOMEM;
|
---|
263 | }
|
---|
264 | } else {
|
---|
265 | tbm->alloc = *alloc;
|
---|
266 | }
|
---|
267 |
|
---|
268 | tbm->tgc = tgc;
|
---|
269 | tgc->bm_created = true;
|
---|
270 | tgc->bm_params = *params;
|
---|
271 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
272 | *rbm = (void *)tbm;
|
---|
273 | return EOK;
|
---|
274 | }
|
---|
275 |
|
---|
276 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
277 | {
|
---|
278 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
279 | if (tbm->myalloc)
|
---|
280 | free(tbm->alloc.pixels);
|
---|
281 | tbm->tgc->bm_destroyed = true;
|
---|
282 | free(tbm);
|
---|
283 | return EOK;
|
---|
284 | }
|
---|
285 |
|
---|
286 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
287 | gfx_coord2_t *offs)
|
---|
288 | {
|
---|
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;
|
---|
293 | return EOK;
|
---|
294 | }
|
---|
295 |
|
---|
296 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
297 | {
|
---|
298 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
299 | *alloc = tbm->alloc;
|
---|
300 | tbm->tgc->bm_got_alloc = true;
|
---|
301 | return EOK;
|
---|
302 | }
|
---|
303 |
|
---|
304 | static void test_expose(void *arg)
|
---|
305 | {
|
---|
306 | test_resp_t *resp = (test_resp_t *) arg;
|
---|
307 |
|
---|
308 | resp->expose = true;
|
---|
309 | }
|
---|
310 |
|
---|
311 | PCUT_EXPORT(resource);
|
---|