1 | /*
|
---|
2 | * Copyright (c) 2020 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 <gfx/coord.h>
|
---|
31 | #include <mem.h>
|
---|
32 | #include <pcut/pcut.h>
|
---|
33 | #include <stdbool.h>
|
---|
34 | #include <ui/resource.h>
|
---|
35 | #include <ui/wdecor.h>
|
---|
36 | #include "../private/wdecor.h"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(wdecor);
|
---|
41 |
|
---|
42 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
43 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
44 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
45 | gfx_bitmap_alloc_t *, void **);
|
---|
46 | static errno_t testgc_bitmap_destroy(void *);
|
---|
47 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
48 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
49 |
|
---|
50 | static gfx_context_ops_t ops = {
|
---|
51 | .set_color = testgc_set_color,
|
---|
52 | .fill_rect = testgc_fill_rect,
|
---|
53 | .bitmap_create = testgc_bitmap_create,
|
---|
54 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
55 | .bitmap_render = testgc_bitmap_render,
|
---|
56 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
57 | };
|
---|
58 |
|
---|
59 | static void test_wdecor_move(ui_wdecor_t *, void *, gfx_coord2_t *);
|
---|
60 |
|
---|
61 | static ui_wdecor_cb_t test_wdecor_cb = {
|
---|
62 | .move = test_wdecor_move
|
---|
63 | };
|
---|
64 |
|
---|
65 | static ui_wdecor_cb_t dummy_wdecor_cb = {
|
---|
66 | };
|
---|
67 |
|
---|
68 | typedef struct {
|
---|
69 | bool bm_created;
|
---|
70 | bool bm_destroyed;
|
---|
71 | gfx_bitmap_params_t bm_params;
|
---|
72 | void *bm_pixels;
|
---|
73 | gfx_rect_t bm_srect;
|
---|
74 | gfx_coord2_t bm_offs;
|
---|
75 | bool bm_rendered;
|
---|
76 | bool bm_got_alloc;
|
---|
77 | } test_gc_t;
|
---|
78 |
|
---|
79 | typedef struct {
|
---|
80 | test_gc_t *tgc;
|
---|
81 | gfx_bitmap_alloc_t alloc;
|
---|
82 | bool myalloc;
|
---|
83 | } testgc_bitmap_t;
|
---|
84 |
|
---|
85 | typedef struct {
|
---|
86 | bool move;
|
---|
87 | gfx_coord2_t pos;
|
---|
88 | } test_cb_resp_t;
|
---|
89 |
|
---|
90 | /** Create and destroy button */
|
---|
91 | PCUT_TEST(create_destroy)
|
---|
92 | {
|
---|
93 | ui_wdecor_t *wdecor = NULL;
|
---|
94 | errno_t rc;
|
---|
95 |
|
---|
96 | rc = ui_wdecor_create(NULL, "Hello", &wdecor);
|
---|
97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
98 | PCUT_ASSERT_NOT_NULL(wdecor);
|
---|
99 |
|
---|
100 | ui_wdecor_destroy(wdecor);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** ui_wdecor_destroy() can take NULL argument (no-op) */
|
---|
104 | PCUT_TEST(destroy_null)
|
---|
105 | {
|
---|
106 | ui_wdecor_destroy(NULL);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /** Set window decoration rectangle sets internal field */
|
---|
110 | PCUT_TEST(set_rect)
|
---|
111 | {
|
---|
112 | ui_wdecor_t *wdecor;
|
---|
113 | gfx_rect_t rect;
|
---|
114 | errno_t rc;
|
---|
115 |
|
---|
116 | rc = ui_wdecor_create(NULL, "Hello", &wdecor);
|
---|
117 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
118 |
|
---|
119 | rect.p0.x = 1;
|
---|
120 | rect.p0.y = 2;
|
---|
121 | rect.p1.x = 3;
|
---|
122 | rect.p1.y = 4;
|
---|
123 |
|
---|
124 | ui_wdecor_set_rect(wdecor, &rect);
|
---|
125 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, wdecor->rect.p0.x);
|
---|
126 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, wdecor->rect.p0.y);
|
---|
127 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, wdecor->rect.p1.x);
|
---|
128 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, wdecor->rect.p1.y);
|
---|
129 |
|
---|
130 | ui_wdecor_destroy(wdecor);
|
---|
131 | }
|
---|
132 |
|
---|
133 | /** Set window decoration active sets internal field */
|
---|
134 | PCUT_TEST(set_active)
|
---|
135 | {
|
---|
136 | ui_wdecor_t *wdecor;
|
---|
137 | errno_t rc;
|
---|
138 |
|
---|
139 | rc = ui_wdecor_create(NULL, "Hello", &wdecor);
|
---|
140 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
141 |
|
---|
142 | PCUT_ASSERT_TRUE(wdecor->active);
|
---|
143 |
|
---|
144 | ui_wdecor_set_active(wdecor, false);
|
---|
145 | PCUT_ASSERT_FALSE(wdecor->active);
|
---|
146 |
|
---|
147 | ui_wdecor_set_active(wdecor, true);
|
---|
148 | PCUT_ASSERT_TRUE(wdecor->active);
|
---|
149 |
|
---|
150 | ui_wdecor_destroy(wdecor);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /** Paint button */
|
---|
154 | PCUT_TEST(paint)
|
---|
155 | {
|
---|
156 | errno_t rc;
|
---|
157 | gfx_context_t *gc = NULL;
|
---|
158 | test_gc_t tgc;
|
---|
159 | ui_resource_t *resource = NULL;
|
---|
160 | ui_wdecor_t *wdecor;
|
---|
161 |
|
---|
162 | memset(&tgc, 0, sizeof(tgc));
|
---|
163 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
164 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
165 |
|
---|
166 | rc = ui_resource_create(gc, &resource);
|
---|
167 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
168 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
169 |
|
---|
170 | rc = ui_wdecor_create(resource, "Hello", &wdecor);
|
---|
171 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
172 |
|
---|
173 | rc = ui_wdecor_paint(wdecor);
|
---|
174 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
175 |
|
---|
176 | ui_wdecor_destroy(wdecor);
|
---|
177 | ui_resource_destroy(resource);
|
---|
178 |
|
---|
179 | rc = gfx_context_delete(gc);
|
---|
180 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
181 | }
|
---|
182 |
|
---|
183 | /** Test ui_wdecor_move() */
|
---|
184 | PCUT_TEST(move)
|
---|
185 | {
|
---|
186 | errno_t rc;
|
---|
187 | ui_wdecor_t *wdecor;
|
---|
188 | test_cb_resp_t resp;
|
---|
189 | gfx_coord2_t pos;
|
---|
190 |
|
---|
191 | rc = ui_wdecor_create(NULL, "Hello", &wdecor);
|
---|
192 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
193 |
|
---|
194 | pos.x = 3;
|
---|
195 | pos.y = 4;
|
---|
196 |
|
---|
197 | /* Move callback with no callbacks set */
|
---|
198 | ui_wdecor_move(wdecor, &pos);
|
---|
199 |
|
---|
200 | /* Move callback with move callback not implemented */
|
---|
201 | ui_wdecor_set_cb(wdecor, &dummy_wdecor_cb, NULL);
|
---|
202 | ui_wdecor_move(wdecor, &pos);
|
---|
203 |
|
---|
204 | /* Move callback with real callback set */
|
---|
205 | resp.move = false;
|
---|
206 | resp.pos.x = 0;
|
---|
207 | resp.pos.y = 0;
|
---|
208 | ui_wdecor_set_cb(wdecor, &test_wdecor_cb, &resp);
|
---|
209 | ui_wdecor_move(wdecor, &pos);
|
---|
210 | PCUT_ASSERT_TRUE(resp.move);
|
---|
211 | PCUT_ASSERT_INT_EQUALS(pos.x, resp.pos.x);
|
---|
212 | PCUT_ASSERT_INT_EQUALS(pos.y, resp.pos.y);
|
---|
213 |
|
---|
214 | ui_wdecor_destroy(wdecor);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Button press on title bar generates move callback */
|
---|
218 | PCUT_TEST(pos_event_move)
|
---|
219 | {
|
---|
220 | ui_wdecor_t *wdecor;
|
---|
221 | gfx_rect_t rect;
|
---|
222 | pos_event_t event;
|
---|
223 | test_cb_resp_t resp;
|
---|
224 | errno_t rc;
|
---|
225 |
|
---|
226 | rc = ui_wdecor_create(NULL, "Hello", &wdecor);
|
---|
227 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
228 |
|
---|
229 | rect.p0.x = 10;
|
---|
230 | rect.p0.y = 20;
|
---|
231 | rect.p1.x = 100;
|
---|
232 | rect.p1.y = 200;
|
---|
233 |
|
---|
234 | ui_wdecor_set_rect(wdecor, &rect);
|
---|
235 |
|
---|
236 | ui_wdecor_set_cb(wdecor, &test_wdecor_cb, (void *) &resp);
|
---|
237 |
|
---|
238 | resp.move = false;
|
---|
239 | resp.pos.x = 0;
|
---|
240 | resp.pos.y = 0;
|
---|
241 |
|
---|
242 | event.type = POS_PRESS;
|
---|
243 | event.hpos = 50;
|
---|
244 | event.vpos = 25;
|
---|
245 | ui_wdecor_pos_event(wdecor, &event);
|
---|
246 |
|
---|
247 | PCUT_ASSERT_TRUE(resp.move);
|
---|
248 | PCUT_ASSERT_INT_EQUALS(event.hpos, resp.pos.x);
|
---|
249 | PCUT_ASSERT_INT_EQUALS(event.vpos, resp.pos.y);
|
---|
250 |
|
---|
251 | ui_wdecor_destroy(wdecor);
|
---|
252 | }
|
---|
253 |
|
---|
254 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
255 | {
|
---|
256 | (void) arg;
|
---|
257 | (void) color;
|
---|
258 | return EOK;
|
---|
259 | }
|
---|
260 |
|
---|
261 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
262 | {
|
---|
263 | (void) arg;
|
---|
264 | (void) rect;
|
---|
265 | return EOK;
|
---|
266 | }
|
---|
267 |
|
---|
268 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
269 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
270 | {
|
---|
271 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
272 | testgc_bitmap_t *tbm;
|
---|
273 |
|
---|
274 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
275 | if (tbm == NULL)
|
---|
276 | return ENOMEM;
|
---|
277 |
|
---|
278 | if (alloc == NULL) {
|
---|
279 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
280 | sizeof(uint32_t);
|
---|
281 | tbm->alloc.off0 = 0;
|
---|
282 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
283 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
284 | (params->rect.p1.y - params->rect.p0.y));
|
---|
285 | tbm->myalloc = true;
|
---|
286 | if (tbm->alloc.pixels == NULL) {
|
---|
287 | free(tbm);
|
---|
288 | return ENOMEM;
|
---|
289 | }
|
---|
290 | } else {
|
---|
291 | tbm->alloc = *alloc;
|
---|
292 | }
|
---|
293 |
|
---|
294 | tbm->tgc = tgc;
|
---|
295 | tgc->bm_created = true;
|
---|
296 | tgc->bm_params = *params;
|
---|
297 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
298 | *rbm = (void *)tbm;
|
---|
299 | return EOK;
|
---|
300 | }
|
---|
301 |
|
---|
302 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
303 | {
|
---|
304 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
305 | if (tbm->myalloc)
|
---|
306 | free(tbm->alloc.pixels);
|
---|
307 | tbm->tgc->bm_destroyed = true;
|
---|
308 | free(tbm);
|
---|
309 | return EOK;
|
---|
310 | }
|
---|
311 |
|
---|
312 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
313 | gfx_coord2_t *offs)
|
---|
314 | {
|
---|
315 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
316 | tbm->tgc->bm_rendered = true;
|
---|
317 | tbm->tgc->bm_srect = *srect;
|
---|
318 | tbm->tgc->bm_offs = *offs;
|
---|
319 | return EOK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
323 | {
|
---|
324 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
325 | *alloc = tbm->alloc;
|
---|
326 | tbm->tgc->bm_got_alloc = true;
|
---|
327 | return EOK;
|
---|
328 | }
|
---|
329 |
|
---|
330 | static void test_wdecor_move(ui_wdecor_t *wdecor, void *arg, gfx_coord2_t *pos)
|
---|
331 | {
|
---|
332 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
333 |
|
---|
334 | resp->move = true;
|
---|
335 | resp->pos = *pos;
|
---|
336 | }
|
---|
337 |
|
---|
338 | PCUT_EXPORT(wdecor);
|
---|