1 | /*
|
---|
2 | * Copyright (c) 2023 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/control.h>
|
---|
35 | #include <ui/pbutton.h>
|
---|
36 | #include <ui/resource.h>
|
---|
37 | #include "../private/pbutton.h"
|
---|
38 |
|
---|
39 | PCUT_INIT;
|
---|
40 |
|
---|
41 | PCUT_TEST_SUITE(pbutton);
|
---|
42 |
|
---|
43 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
---|
44 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
45 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
46 | static errno_t testgc_update(void *);
|
---|
47 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
48 | gfx_bitmap_alloc_t *, void **);
|
---|
49 | static errno_t testgc_bitmap_destroy(void *);
|
---|
50 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
51 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
52 |
|
---|
53 | static gfx_context_ops_t ops = {
|
---|
54 | .set_clip_rect = testgc_set_clip_rect,
|
---|
55 | .set_color = testgc_set_color,
|
---|
56 | .fill_rect = testgc_fill_rect,
|
---|
57 | .update = testgc_update,
|
---|
58 | .bitmap_create = testgc_bitmap_create,
|
---|
59 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
60 | .bitmap_render = testgc_bitmap_render,
|
---|
61 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
62 | };
|
---|
63 |
|
---|
64 | static void test_pbutton_clicked(ui_pbutton_t *, void *);
|
---|
65 | static void test_pbutton_down(ui_pbutton_t *, void *);
|
---|
66 | static void test_pbutton_up(ui_pbutton_t *, void *);
|
---|
67 |
|
---|
68 | static ui_pbutton_cb_t test_pbutton_cb = {
|
---|
69 | .clicked = test_pbutton_clicked,
|
---|
70 | .down = test_pbutton_down,
|
---|
71 | .up = test_pbutton_up
|
---|
72 | };
|
---|
73 |
|
---|
74 | static ui_pbutton_cb_t dummy_pbutton_cb = {
|
---|
75 | };
|
---|
76 |
|
---|
77 | typedef struct {
|
---|
78 | bool bm_created;
|
---|
79 | bool bm_destroyed;
|
---|
80 | gfx_bitmap_params_t bm_params;
|
---|
81 | void *bm_pixels;
|
---|
82 | gfx_rect_t bm_srect;
|
---|
83 | gfx_coord2_t bm_offs;
|
---|
84 | bool bm_rendered;
|
---|
85 | bool bm_got_alloc;
|
---|
86 | } test_gc_t;
|
---|
87 |
|
---|
88 | typedef struct {
|
---|
89 | test_gc_t *tgc;
|
---|
90 | gfx_bitmap_alloc_t alloc;
|
---|
91 | bool myalloc;
|
---|
92 | } testgc_bitmap_t;
|
---|
93 |
|
---|
94 | typedef struct {
|
---|
95 | bool clicked;
|
---|
96 | bool down;
|
---|
97 | bool up;
|
---|
98 | } test_cb_resp_t;
|
---|
99 |
|
---|
100 | /** Create and destroy button */
|
---|
101 | PCUT_TEST(create_destroy)
|
---|
102 | {
|
---|
103 | ui_pbutton_t *pbutton = NULL;
|
---|
104 | errno_t rc;
|
---|
105 |
|
---|
106 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
107 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
108 | PCUT_ASSERT_NOT_NULL(pbutton);
|
---|
109 |
|
---|
110 | ui_pbutton_destroy(pbutton);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /** ui_pbutton_destroy() can take NULL argument (no-op) */
|
---|
114 | PCUT_TEST(destroy_null)
|
---|
115 | {
|
---|
116 | ui_pbutton_destroy(NULL);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** ui_pbutton_ctl() returns control that has a working virtual destructor */
|
---|
120 | PCUT_TEST(ctl)
|
---|
121 | {
|
---|
122 | ui_pbutton_t *pbutton;
|
---|
123 | ui_control_t *control;
|
---|
124 | errno_t rc;
|
---|
125 |
|
---|
126 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
127 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
128 |
|
---|
129 | control = ui_pbutton_ctl(pbutton);
|
---|
130 | PCUT_ASSERT_NOT_NULL(control);
|
---|
131 |
|
---|
132 | ui_control_destroy(control);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Set flags sets internal field */
|
---|
136 | PCUT_TEST(set_flags)
|
---|
137 | {
|
---|
138 | ui_pbutton_t *pbutton;
|
---|
139 | errno_t rc;
|
---|
140 |
|
---|
141 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
142 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
143 |
|
---|
144 | ui_pbutton_set_flags(pbutton, ui_pbf_no_text_depress);
|
---|
145 | PCUT_ASSERT_INT_EQUALS(ui_pbf_no_text_depress, pbutton->flags);
|
---|
146 |
|
---|
147 | ui_pbutton_destroy(pbutton);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Set button rectangle sets internal field */
|
---|
151 | PCUT_TEST(set_rect)
|
---|
152 | {
|
---|
153 | ui_pbutton_t *pbutton;
|
---|
154 | gfx_rect_t rect;
|
---|
155 | errno_t rc;
|
---|
156 |
|
---|
157 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
158 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
159 |
|
---|
160 | rect.p0.x = 1;
|
---|
161 | rect.p0.y = 2;
|
---|
162 | rect.p1.x = 3;
|
---|
163 | rect.p1.y = 4;
|
---|
164 |
|
---|
165 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
166 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, pbutton->rect.p0.x);
|
---|
167 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, pbutton->rect.p0.y);
|
---|
168 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, pbutton->rect.p1.x);
|
---|
169 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, pbutton->rect.p1.y);
|
---|
170 |
|
---|
171 | ui_pbutton_destroy(pbutton);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Set default flag sets internal field */
|
---|
175 | PCUT_TEST(set_default)
|
---|
176 | {
|
---|
177 | ui_pbutton_t *pbutton;
|
---|
178 | errno_t rc;
|
---|
179 |
|
---|
180 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
181 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
182 |
|
---|
183 | ui_pbutton_set_default(pbutton, true);
|
---|
184 | PCUT_ASSERT_TRUE(pbutton->isdefault);
|
---|
185 |
|
---|
186 | ui_pbutton_set_default(pbutton, false);
|
---|
187 | PCUT_ASSERT_FALSE(pbutton->isdefault);
|
---|
188 |
|
---|
189 | ui_pbutton_destroy(pbutton);
|
---|
190 | }
|
---|
191 |
|
---|
192 | /** Get light gets internal field */
|
---|
193 | PCUT_TEST(get_light)
|
---|
194 | {
|
---|
195 | ui_pbutton_t *pbutton;
|
---|
196 | errno_t rc;
|
---|
197 |
|
---|
198 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
199 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
200 |
|
---|
201 | pbutton->light = true;
|
---|
202 | PCUT_ASSERT_TRUE(ui_pbutton_get_light(pbutton));
|
---|
203 |
|
---|
204 | pbutton->light = false;
|
---|
205 | PCUT_ASSERT_FALSE(ui_pbutton_get_light(pbutton));
|
---|
206 |
|
---|
207 | ui_pbutton_destroy(pbutton);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Set light sets internal field */
|
---|
211 | PCUT_TEST(set_light)
|
---|
212 | {
|
---|
213 | ui_pbutton_t *pbutton;
|
---|
214 | errno_t rc;
|
---|
215 |
|
---|
216 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
217 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
218 |
|
---|
219 | ui_pbutton_set_light(pbutton, true);
|
---|
220 | PCUT_ASSERT_TRUE(pbutton->light);
|
---|
221 |
|
---|
222 | ui_pbutton_set_light(pbutton, false);
|
---|
223 | PCUT_ASSERT_FALSE(pbutton->light);
|
---|
224 |
|
---|
225 | ui_pbutton_destroy(pbutton);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Set caption sets internal field */
|
---|
229 | PCUT_TEST(set_caption)
|
---|
230 | {
|
---|
231 | ui_pbutton_t *pbutton;
|
---|
232 | errno_t rc;
|
---|
233 |
|
---|
234 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
235 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
236 |
|
---|
237 | PCUT_ASSERT_STR_EQUALS("Hello", pbutton->caption);
|
---|
238 |
|
---|
239 | rc = ui_pbutton_set_caption(pbutton, "World");
|
---|
240 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
241 |
|
---|
242 | PCUT_ASSERT_STR_EQUALS("World", pbutton->caption);
|
---|
243 |
|
---|
244 | ui_pbutton_destroy(pbutton);
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Paint button */
|
---|
248 | PCUT_TEST(paint)
|
---|
249 | {
|
---|
250 | errno_t rc;
|
---|
251 | gfx_context_t *gc = NULL;
|
---|
252 | test_gc_t tgc;
|
---|
253 | ui_resource_t *resource = NULL;
|
---|
254 | ui_pbutton_t *pbutton;
|
---|
255 |
|
---|
256 | memset(&tgc, 0, sizeof(tgc));
|
---|
257 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
258 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
259 |
|
---|
260 | rc = ui_resource_create(gc, false, &resource);
|
---|
261 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
262 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
263 |
|
---|
264 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
265 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
266 |
|
---|
267 | rc = ui_pbutton_paint(pbutton);
|
---|
268 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
269 |
|
---|
270 | ui_pbutton_destroy(pbutton);
|
---|
271 | ui_resource_destroy(resource);
|
---|
272 |
|
---|
273 | rc = gfx_context_delete(gc);
|
---|
274 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
275 | }
|
---|
276 |
|
---|
277 | /** Test ui_pbutton_clicked() */
|
---|
278 | PCUT_TEST(clicked)
|
---|
279 | {
|
---|
280 | errno_t rc;
|
---|
281 | ui_pbutton_t *pbutton;
|
---|
282 | test_cb_resp_t resp;
|
---|
283 |
|
---|
284 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
285 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
286 |
|
---|
287 | /* Clicked with no callbacks set */
|
---|
288 | ui_pbutton_clicked(pbutton);
|
---|
289 |
|
---|
290 | /* Clicked with callback not implementing clicked */
|
---|
291 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
292 | ui_pbutton_clicked(pbutton);
|
---|
293 |
|
---|
294 | /* Clicked with real callback set */
|
---|
295 | resp.clicked = false;
|
---|
296 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
297 | ui_pbutton_clicked(pbutton);
|
---|
298 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
299 |
|
---|
300 | ui_pbutton_destroy(pbutton);
|
---|
301 | }
|
---|
302 |
|
---|
303 | /** Test ui_pbutton_down() */
|
---|
304 | PCUT_TEST(down)
|
---|
305 | {
|
---|
306 | errno_t rc;
|
---|
307 | ui_pbutton_t *pbutton;
|
---|
308 | test_cb_resp_t resp;
|
---|
309 |
|
---|
310 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
311 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
312 |
|
---|
313 | /* Down with no callbacks set */
|
---|
314 | ui_pbutton_clicked(pbutton);
|
---|
315 |
|
---|
316 | /* Down with callback not implementing down */
|
---|
317 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
318 | ui_pbutton_down(pbutton);
|
---|
319 |
|
---|
320 | /* Down with real callback set */
|
---|
321 | resp.down = false;
|
---|
322 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
323 | ui_pbutton_down(pbutton);
|
---|
324 | PCUT_ASSERT_TRUE(resp.down);
|
---|
325 |
|
---|
326 | ui_pbutton_destroy(pbutton);
|
---|
327 | }
|
---|
328 |
|
---|
329 | /** Test ui_pbutton_up() */
|
---|
330 | PCUT_TEST(up)
|
---|
331 | {
|
---|
332 | errno_t rc;
|
---|
333 | ui_pbutton_t *pbutton;
|
---|
334 | test_cb_resp_t resp;
|
---|
335 |
|
---|
336 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
337 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
338 |
|
---|
339 | /* Up with no callbacks set */
|
---|
340 | ui_pbutton_clicked(pbutton);
|
---|
341 |
|
---|
342 | /* Up with callback not implementing up */
|
---|
343 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
344 | ui_pbutton_up(pbutton);
|
---|
345 |
|
---|
346 | /* Up with real callback set */
|
---|
347 | resp.up = false;
|
---|
348 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
349 | ui_pbutton_up(pbutton);
|
---|
350 | PCUT_ASSERT_TRUE(resp.up);
|
---|
351 |
|
---|
352 | ui_pbutton_destroy(pbutton);
|
---|
353 | }
|
---|
354 |
|
---|
355 | /** Press and release button */
|
---|
356 | PCUT_TEST(press_release)
|
---|
357 | {
|
---|
358 | errno_t rc;
|
---|
359 | gfx_context_t *gc = NULL;
|
---|
360 | test_gc_t tgc;
|
---|
361 | ui_resource_t *resource = NULL;
|
---|
362 | ui_pbutton_t *pbutton;
|
---|
363 | test_cb_resp_t resp;
|
---|
364 |
|
---|
365 | memset(&tgc, 0, sizeof(tgc));
|
---|
366 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
367 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
368 |
|
---|
369 | rc = ui_resource_create(gc, false, &resource);
|
---|
370 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
371 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
372 |
|
---|
373 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
374 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
375 |
|
---|
376 | resp.clicked = false;
|
---|
377 | resp.down = false;
|
---|
378 | resp.up = false;
|
---|
379 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
380 |
|
---|
381 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
382 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
383 |
|
---|
384 | ui_pbutton_press(pbutton);
|
---|
385 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
386 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
387 | PCUT_ASSERT_TRUE(resp.down);
|
---|
388 | PCUT_ASSERT_FALSE(resp.up);
|
---|
389 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
390 |
|
---|
391 | ui_pbutton_release(pbutton);
|
---|
392 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
393 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
394 | PCUT_ASSERT_TRUE(resp.up);
|
---|
395 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
396 |
|
---|
397 | ui_pbutton_destroy(pbutton);
|
---|
398 | ui_resource_destroy(resource);
|
---|
399 |
|
---|
400 | rc = gfx_context_delete(gc);
|
---|
401 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
402 | }
|
---|
403 |
|
---|
404 | /** Press, leave and release button */
|
---|
405 | PCUT_TEST(press_leave_release)
|
---|
406 | {
|
---|
407 | errno_t rc;
|
---|
408 | gfx_context_t *gc = NULL;
|
---|
409 | test_gc_t tgc;
|
---|
410 | ui_resource_t *resource = NULL;
|
---|
411 | ui_pbutton_t *pbutton;
|
---|
412 | test_cb_resp_t resp;
|
---|
413 |
|
---|
414 | memset(&tgc, 0, sizeof(tgc));
|
---|
415 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
416 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
417 |
|
---|
418 | rc = ui_resource_create(gc, false, &resource);
|
---|
419 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
420 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
421 |
|
---|
422 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
423 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
424 |
|
---|
425 | resp.clicked = false;
|
---|
426 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
427 |
|
---|
428 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
429 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
430 |
|
---|
431 | ui_pbutton_press(pbutton);
|
---|
432 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
433 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
434 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
435 |
|
---|
436 | ui_pbutton_leave(pbutton);
|
---|
437 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
438 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
439 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
440 |
|
---|
441 | ui_pbutton_release(pbutton);
|
---|
442 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
443 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
444 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
445 |
|
---|
446 | ui_pbutton_destroy(pbutton);
|
---|
447 | ui_resource_destroy(resource);
|
---|
448 |
|
---|
449 | rc = gfx_context_delete(gc);
|
---|
450 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
451 | }
|
---|
452 |
|
---|
453 | /** Press, leave, enter and release button */
|
---|
454 | PCUT_TEST(press_leave_enter_release)
|
---|
455 | {
|
---|
456 | errno_t rc;
|
---|
457 | gfx_context_t *gc = NULL;
|
---|
458 | test_gc_t tgc;
|
---|
459 | ui_resource_t *resource = NULL;
|
---|
460 | ui_pbutton_t *pbutton;
|
---|
461 | test_cb_resp_t resp;
|
---|
462 |
|
---|
463 | memset(&tgc, 0, sizeof(tgc));
|
---|
464 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
465 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
466 |
|
---|
467 | rc = ui_resource_create(gc, false, &resource);
|
---|
468 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
469 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
470 |
|
---|
471 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
472 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
473 |
|
---|
474 | resp.clicked = false;
|
---|
475 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
476 |
|
---|
477 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
478 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
479 |
|
---|
480 | ui_pbutton_press(pbutton);
|
---|
481 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
482 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
483 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
484 |
|
---|
485 | ui_pbutton_leave(pbutton);
|
---|
486 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
487 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
488 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
489 |
|
---|
490 | ui_pbutton_enter(pbutton);
|
---|
491 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
492 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
493 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
494 |
|
---|
495 | ui_pbutton_release(pbutton);
|
---|
496 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
497 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
498 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
499 |
|
---|
500 | ui_pbutton_destroy(pbutton);
|
---|
501 | ui_resource_destroy(resource);
|
---|
502 |
|
---|
503 | rc = gfx_context_delete(gc);
|
---|
504 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
505 | }
|
---|
506 |
|
---|
507 | /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
|
---|
508 | PCUT_TEST(pos_event_press_release)
|
---|
509 | {
|
---|
510 | errno_t rc;
|
---|
511 | gfx_context_t *gc = NULL;
|
---|
512 | test_gc_t tgc;
|
---|
513 | ui_resource_t *resource = NULL;
|
---|
514 | ui_pbutton_t *pbutton;
|
---|
515 | ui_evclaim_t claim;
|
---|
516 | pos_event_t event;
|
---|
517 | gfx_rect_t rect;
|
---|
518 |
|
---|
519 | memset(&tgc, 0, sizeof(tgc));
|
---|
520 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
521 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
522 |
|
---|
523 | rc = ui_resource_create(gc, false, &resource);
|
---|
524 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
525 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
526 |
|
---|
527 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
528 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
529 |
|
---|
530 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
531 |
|
---|
532 | rect.p0.x = 10;
|
---|
533 | rect.p0.y = 20;
|
---|
534 | rect.p1.x = 30;
|
---|
535 | rect.p1.y = 40;
|
---|
536 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
537 |
|
---|
538 | /* Press outside is not claimed and does nothing */
|
---|
539 | event.type = POS_PRESS;
|
---|
540 | event.hpos = 9;
|
---|
541 | event.vpos = 20;
|
---|
542 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
543 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
544 | PCUT_ASSERT_EQUALS(ui_unclaimed, claim);
|
---|
545 |
|
---|
546 | /* Press inside is claimed and depresses button */
|
---|
547 | event.type = POS_PRESS;
|
---|
548 | event.hpos = 10;
|
---|
549 | event.vpos = 20;
|
---|
550 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
551 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
552 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
553 |
|
---|
554 | /* Release outside (or anywhere) is claimed and relases button */
|
---|
555 | event.type = POS_RELEASE;
|
---|
556 | event.hpos = 9;
|
---|
557 | event.vpos = 20;
|
---|
558 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
559 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
560 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
561 |
|
---|
562 | ui_pbutton_destroy(pbutton);
|
---|
563 | ui_resource_destroy(resource);
|
---|
564 |
|
---|
565 | rc = gfx_context_delete(gc);
|
---|
566 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
567 | }
|
---|
568 |
|
---|
569 | /** ui_pos_event() correctly translates POS_UPDATE to enter/leave */
|
---|
570 | PCUT_TEST(pos_event_enter_leave)
|
---|
571 | {
|
---|
572 | errno_t rc;
|
---|
573 | gfx_context_t *gc = NULL;
|
---|
574 | test_gc_t tgc;
|
---|
575 | ui_resource_t *resource = NULL;
|
---|
576 | ui_pbutton_t *pbutton;
|
---|
577 | pos_event_t event;
|
---|
578 | gfx_rect_t rect;
|
---|
579 |
|
---|
580 | memset(&tgc, 0, sizeof(tgc));
|
---|
581 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
582 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
583 |
|
---|
584 | rc = ui_resource_create(gc, false, &resource);
|
---|
585 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
586 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
587 |
|
---|
588 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
589 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
590 |
|
---|
591 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
592 |
|
---|
593 | rect.p0.x = 10;
|
---|
594 | rect.p0.y = 20;
|
---|
595 | rect.p1.x = 30;
|
---|
596 | rect.p1.y = 40;
|
---|
597 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
598 |
|
---|
599 | /* Moving outside does nothing */
|
---|
600 | event.type = POS_UPDATE;
|
---|
601 | event.hpos = 9;
|
---|
602 | event.vpos = 20;
|
---|
603 | ui_pbutton_pos_event(pbutton, &event);
|
---|
604 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
605 |
|
---|
606 | /* Moving inside sets inside flag */
|
---|
607 | event.type = POS_UPDATE;
|
---|
608 | event.hpos = 10;
|
---|
609 | event.vpos = 20;
|
---|
610 | ui_pbutton_pos_event(pbutton, &event);
|
---|
611 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
612 |
|
---|
613 | /* Moving outside clears inside flag */
|
---|
614 | event.type = POS_UPDATE;
|
---|
615 | event.hpos = 9;
|
---|
616 | event.vpos = 20;
|
---|
617 | ui_pbutton_pos_event(pbutton, &event);
|
---|
618 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
619 |
|
---|
620 | ui_pbutton_destroy(pbutton);
|
---|
621 | ui_resource_destroy(resource);
|
---|
622 |
|
---|
623 | rc = gfx_context_delete(gc);
|
---|
624 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
625 | }
|
---|
626 |
|
---|
627 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
628 | {
|
---|
629 | (void) arg;
|
---|
630 | (void) rect;
|
---|
631 | return EOK;
|
---|
632 | }
|
---|
633 |
|
---|
634 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
635 | {
|
---|
636 | (void) arg;
|
---|
637 | (void) color;
|
---|
638 | return EOK;
|
---|
639 | }
|
---|
640 |
|
---|
641 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
642 | {
|
---|
643 | (void) arg;
|
---|
644 | (void) rect;
|
---|
645 | return EOK;
|
---|
646 | }
|
---|
647 |
|
---|
648 | static errno_t testgc_update(void *arg)
|
---|
649 | {
|
---|
650 | (void) arg;
|
---|
651 | return EOK;
|
---|
652 | }
|
---|
653 |
|
---|
654 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
655 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
656 | {
|
---|
657 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
658 | testgc_bitmap_t *tbm;
|
---|
659 |
|
---|
660 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
661 | if (tbm == NULL)
|
---|
662 | return ENOMEM;
|
---|
663 |
|
---|
664 | if (alloc == NULL) {
|
---|
665 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
666 | sizeof(uint32_t);
|
---|
667 | tbm->alloc.off0 = 0;
|
---|
668 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
669 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
670 | (params->rect.p1.y - params->rect.p0.y));
|
---|
671 | tbm->myalloc = true;
|
---|
672 | if (tbm->alloc.pixels == NULL) {
|
---|
673 | free(tbm);
|
---|
674 | return ENOMEM;
|
---|
675 | }
|
---|
676 | } else {
|
---|
677 | tbm->alloc = *alloc;
|
---|
678 | }
|
---|
679 |
|
---|
680 | tbm->tgc = tgc;
|
---|
681 | tgc->bm_created = true;
|
---|
682 | tgc->bm_params = *params;
|
---|
683 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
684 | *rbm = (void *)tbm;
|
---|
685 | return EOK;
|
---|
686 | }
|
---|
687 |
|
---|
688 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
689 | {
|
---|
690 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
691 | if (tbm->myalloc)
|
---|
692 | free(tbm->alloc.pixels);
|
---|
693 | tbm->tgc->bm_destroyed = true;
|
---|
694 | free(tbm);
|
---|
695 | return EOK;
|
---|
696 | }
|
---|
697 |
|
---|
698 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
699 | gfx_coord2_t *offs)
|
---|
700 | {
|
---|
701 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
702 | tbm->tgc->bm_rendered = true;
|
---|
703 | tbm->tgc->bm_srect = *srect;
|
---|
704 | tbm->tgc->bm_offs = *offs;
|
---|
705 | return EOK;
|
---|
706 | }
|
---|
707 |
|
---|
708 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
709 | {
|
---|
710 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
711 | *alloc = tbm->alloc;
|
---|
712 | tbm->tgc->bm_got_alloc = true;
|
---|
713 | return EOK;
|
---|
714 | }
|
---|
715 |
|
---|
716 | static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
717 | {
|
---|
718 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
719 |
|
---|
720 | resp->clicked = true;
|
---|
721 | }
|
---|
722 |
|
---|
723 | static void test_pbutton_down(ui_pbutton_t *pbutton, void *arg)
|
---|
724 | {
|
---|
725 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
726 |
|
---|
727 | resp->down = true;
|
---|
728 | }
|
---|
729 |
|
---|
730 | static void test_pbutton_up(ui_pbutton_t *pbutton, void *arg)
|
---|
731 | {
|
---|
732 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
733 |
|
---|
734 | resp->up = true;
|
---|
735 | }
|
---|
736 |
|
---|
737 | PCUT_EXPORT(pbutton);
|
---|