source: mainline/uspace/lib/ui/test/checkbox.c@ f0ccb2ab

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f0ccb2ab was f0ccb2ab, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Fix libui unit tests

  • Property mode set to 100644
File size: 14.3 KB
Line 
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 <gfx/coord.h>
31#include <mem.h>
32#include <pcut/pcut.h>
33#include <stdbool.h>
34#include <ui/control.h>
35#include <ui/checkbox.h>
36#include <ui/resource.h>
37#include "../private/checkbox.h"
38
39PCUT_INIT;
40
41PCUT_TEST_SUITE(checkbox);
42
43static errno_t testgc_set_color(void *, gfx_color_t *);
44static errno_t testgc_fill_rect(void *, gfx_rect_t *);
45static errno_t testgc_update(void *);
46static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
47 gfx_bitmap_alloc_t *, void **);
48static errno_t testgc_bitmap_destroy(void *);
49static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
50static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
51
52static gfx_context_ops_t ops = {
53 .set_color = testgc_set_color,
54 .fill_rect = testgc_fill_rect,
55 .update = testgc_update,
56 .bitmap_create = testgc_bitmap_create,
57 .bitmap_destroy = testgc_bitmap_destroy,
58 .bitmap_render = testgc_bitmap_render,
59 .bitmap_get_alloc = testgc_bitmap_get_alloc
60};
61
62static void test_checkbox_switched(ui_checkbox_t *, void *, bool);
63
64static ui_checkbox_cb_t test_checkbox_cb = {
65 .switched = test_checkbox_switched
66};
67
68static ui_checkbox_cb_t dummy_checkbox_cb = {
69};
70
71typedef struct {
72 bool bm_created;
73 bool bm_destroyed;
74 gfx_bitmap_params_t bm_params;
75 void *bm_pixels;
76 gfx_rect_t bm_srect;
77 gfx_coord2_t bm_offs;
78 bool bm_rendered;
79 bool bm_got_alloc;
80} test_gc_t;
81
82typedef struct {
83 test_gc_t *tgc;
84 gfx_bitmap_alloc_t alloc;
85 bool myalloc;
86} testgc_bitmap_t;
87
88typedef struct {
89 bool switched;
90} test_cb_resp_t;
91
92/** Create and destroy check box */
93PCUT_TEST(create_destroy)
94{
95 ui_checkbox_t *checkbox = NULL;
96 errno_t rc;
97
98 rc = ui_checkbox_create(NULL, "Hello", &checkbox);
99 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
100 PCUT_ASSERT_NOT_NULL(checkbox);
101
102 ui_checkbox_destroy(checkbox);
103}
104
105/** ui_checkbox_destroy() can take NULL argument (no-op) */
106PCUT_TEST(destroy_null)
107{
108 ui_checkbox_destroy(NULL);
109}
110
111/** ui_checkbox_ctl() returns control that has a working virtual destructor */
112PCUT_TEST(ctl)
113{
114 ui_checkbox_t *checkbox;
115 ui_control_t *control;
116 errno_t rc;
117
118 rc = ui_checkbox_create(NULL, "Hello", &checkbox);
119 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
120
121 control = ui_checkbox_ctl(checkbox);
122 PCUT_ASSERT_NOT_NULL(control);
123
124 ui_control_destroy(control);
125}
126
127/** Set check box rectangle sets internal field */
128PCUT_TEST(set_rect)
129{
130 ui_checkbox_t *checkbox;
131 gfx_rect_t rect;
132 errno_t rc;
133
134 rc = ui_checkbox_create(NULL, "Hello", &checkbox);
135 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
136
137 rect.p0.x = 1;
138 rect.p0.y = 2;
139 rect.p1.x = 3;
140 rect.p1.y = 4;
141
142 ui_checkbox_set_rect(checkbox, &rect);
143 PCUT_ASSERT_INT_EQUALS(rect.p0.x, checkbox->rect.p0.x);
144 PCUT_ASSERT_INT_EQUALS(rect.p0.y, checkbox->rect.p0.y);
145 PCUT_ASSERT_INT_EQUALS(rect.p1.x, checkbox->rect.p1.x);
146 PCUT_ASSERT_INT_EQUALS(rect.p1.y, checkbox->rect.p1.y);
147
148 ui_checkbox_destroy(checkbox);
149}
150
151/** Paint check box */
152PCUT_TEST(paint)
153{
154 errno_t rc;
155 gfx_context_t *gc = NULL;
156 test_gc_t tgc;
157 ui_resource_t *resource = NULL;
158 ui_checkbox_t *checkbox;
159
160 memset(&tgc, 0, sizeof(tgc));
161 rc = gfx_context_new(&ops, &tgc, &gc);
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
163
164 rc = ui_resource_create(gc, false, &resource);
165 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166 PCUT_ASSERT_NOT_NULL(resource);
167
168 rc = ui_checkbox_create(resource, "Hello", &checkbox);
169 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
170
171 rc = ui_checkbox_paint(checkbox);
172 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
173
174 ui_checkbox_destroy(checkbox);
175 ui_resource_destroy(resource);
176
177 rc = gfx_context_delete(gc);
178 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
179}
180
181/** Test ui_checkbox_switched() */
182PCUT_TEST(switched)
183{
184 errno_t rc;
185 ui_checkbox_t *checkbox;
186 test_cb_resp_t resp;
187
188 rc = ui_checkbox_create(NULL, "Hello", &checkbox);
189 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190
191 /* Switched with no callbacks set */
192 ui_checkbox_switched(checkbox);
193
194 /* Switched with callback not implementing switched */
195 ui_checkbox_set_cb(checkbox, &dummy_checkbox_cb, NULL);
196 ui_checkbox_switched(checkbox);
197
198 /* Switched with real callback set */
199 resp.switched = false;
200 ui_checkbox_set_cb(checkbox, &test_checkbox_cb, &resp);
201 ui_checkbox_switched(checkbox);
202 PCUT_ASSERT_TRUE(resp.switched);
203
204 ui_checkbox_destroy(checkbox);
205}
206
207/** Press and release check box */
208PCUT_TEST(press_release)
209{
210 errno_t rc;
211 gfx_context_t *gc = NULL;
212 test_gc_t tgc;
213 ui_resource_t *resource = NULL;
214 ui_checkbox_t *checkbox;
215 test_cb_resp_t resp;
216
217 memset(&tgc, 0, sizeof(tgc));
218 rc = gfx_context_new(&ops, &tgc, &gc);
219 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
220
221 rc = ui_resource_create(gc, false, &resource);
222 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
223 PCUT_ASSERT_NOT_NULL(resource);
224
225 rc = ui_checkbox_create(resource, "Hello", &checkbox);
226 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
227 PCUT_ASSERT_FALSE(checkbox->checked);
228
229 resp.switched = false;
230 ui_checkbox_set_cb(checkbox, &test_checkbox_cb, &resp);
231
232 PCUT_ASSERT_FALSE(checkbox->held);
233 PCUT_ASSERT_FALSE(checkbox->inside);
234
235 ui_checkbox_press(checkbox);
236 PCUT_ASSERT_TRUE(checkbox->held);
237 PCUT_ASSERT_TRUE(checkbox->inside);
238 PCUT_ASSERT_FALSE(resp.switched);
239 PCUT_ASSERT_FALSE(checkbox->checked);
240
241 ui_checkbox_release(checkbox);
242 PCUT_ASSERT_FALSE(checkbox->held);
243 PCUT_ASSERT_TRUE(checkbox->inside);
244 PCUT_ASSERT_TRUE(resp.switched);
245 PCUT_ASSERT_TRUE(checkbox->checked);
246
247 ui_checkbox_destroy(checkbox);
248 ui_resource_destroy(resource);
249
250 rc = gfx_context_delete(gc);
251 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
252}
253
254/** Press, leave and release check box */
255PCUT_TEST(press_leave_release)
256{
257 errno_t rc;
258 gfx_context_t *gc = NULL;
259 test_gc_t tgc;
260 ui_resource_t *resource = NULL;
261 ui_checkbox_t *checkbox;
262 test_cb_resp_t resp;
263
264 memset(&tgc, 0, sizeof(tgc));
265 rc = gfx_context_new(&ops, &tgc, &gc);
266 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
267
268 rc = ui_resource_create(gc, false, &resource);
269 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
270 PCUT_ASSERT_NOT_NULL(resource);
271
272 rc = ui_checkbox_create(resource, "Hello", &checkbox);
273 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
274
275 resp.switched = false;
276 ui_checkbox_set_cb(checkbox, &test_checkbox_cb, &resp);
277
278 PCUT_ASSERT_FALSE(checkbox->held);
279 PCUT_ASSERT_FALSE(checkbox->inside);
280
281 ui_checkbox_press(checkbox);
282 PCUT_ASSERT_TRUE(checkbox->held);
283 PCUT_ASSERT_TRUE(checkbox->inside);
284 PCUT_ASSERT_FALSE(resp.switched);
285 PCUT_ASSERT_FALSE(checkbox->checked);
286
287 ui_checkbox_leave(checkbox);
288 PCUT_ASSERT_TRUE(checkbox->held);
289 PCUT_ASSERT_FALSE(checkbox->inside);
290 PCUT_ASSERT_FALSE(resp.switched);
291 PCUT_ASSERT_FALSE(checkbox->checked);
292
293 ui_checkbox_release(checkbox);
294 PCUT_ASSERT_FALSE(checkbox->held);
295 PCUT_ASSERT_FALSE(checkbox->inside);
296 PCUT_ASSERT_FALSE(resp.switched);
297 PCUT_ASSERT_FALSE(checkbox->checked);
298
299 ui_checkbox_destroy(checkbox);
300 ui_resource_destroy(resource);
301
302 rc = gfx_context_delete(gc);
303 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
304}
305
306/** Press, leave, enter and release check box */
307PCUT_TEST(press_leave_enter_release)
308{
309 errno_t rc;
310 gfx_context_t *gc = NULL;
311 test_gc_t tgc;
312 ui_resource_t *resource = NULL;
313 ui_checkbox_t *checkbox;
314 test_cb_resp_t resp;
315
316 memset(&tgc, 0, sizeof(tgc));
317 rc = gfx_context_new(&ops, &tgc, &gc);
318 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
319
320 rc = ui_resource_create(gc, false, &resource);
321 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
322 PCUT_ASSERT_NOT_NULL(resource);
323
324 rc = ui_checkbox_create(resource, "Hello", &checkbox);
325 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
326 PCUT_ASSERT_FALSE(checkbox->checked);
327
328 resp.switched = false;
329 ui_checkbox_set_cb(checkbox, &test_checkbox_cb, &resp);
330
331 PCUT_ASSERT_FALSE(checkbox->held);
332 PCUT_ASSERT_FALSE(checkbox->inside);
333
334 ui_checkbox_press(checkbox);
335 PCUT_ASSERT_TRUE(checkbox->held);
336 PCUT_ASSERT_TRUE(checkbox->inside);
337 PCUT_ASSERT_FALSE(resp.switched);
338 PCUT_ASSERT_FALSE(checkbox->checked);
339
340 ui_checkbox_leave(checkbox);
341 PCUT_ASSERT_TRUE(checkbox->held);
342 PCUT_ASSERT_FALSE(checkbox->inside);
343 PCUT_ASSERT_FALSE(resp.switched);
344 PCUT_ASSERT_FALSE(checkbox->checked);
345
346 ui_checkbox_enter(checkbox);
347 PCUT_ASSERT_TRUE(checkbox->held);
348 PCUT_ASSERT_TRUE(checkbox->inside);
349 PCUT_ASSERT_FALSE(resp.switched);
350 PCUT_ASSERT_FALSE(checkbox->checked);
351
352 ui_checkbox_release(checkbox);
353 PCUT_ASSERT_FALSE(checkbox->held);
354 PCUT_ASSERT_TRUE(checkbox->inside);
355 PCUT_ASSERT_TRUE(resp.switched);
356 PCUT_ASSERT_TRUE(checkbox->checked);
357
358 ui_checkbox_destroy(checkbox);
359 ui_resource_destroy(resource);
360
361 rc = gfx_context_delete(gc);
362 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
363}
364
365/** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
366PCUT_TEST(pos_event_press_release)
367{
368 errno_t rc;
369 gfx_context_t *gc = NULL;
370 test_gc_t tgc;
371 ui_resource_t *resource = NULL;
372 ui_checkbox_t *checkbox;
373 ui_evclaim_t claim;
374 pos_event_t event;
375 gfx_rect_t rect;
376
377 memset(&tgc, 0, sizeof(tgc));
378 rc = gfx_context_new(&ops, &tgc, &gc);
379 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
380
381 rc = ui_resource_create(gc, false, &resource);
382 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
383 PCUT_ASSERT_NOT_NULL(resource);
384
385 rc = ui_checkbox_create(resource, "Hello", &checkbox);
386 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
387
388 PCUT_ASSERT_FALSE(checkbox->held);
389
390 rect.p0.x = 10;
391 rect.p0.y = 20;
392 rect.p1.x = 30;
393 rect.p1.y = 40;
394 ui_checkbox_set_rect(checkbox, &rect);
395
396 /* Press outside is not claimed and does nothing */
397 event.type = POS_PRESS;
398 event.hpos = 9;
399 event.vpos = 20;
400 claim = ui_checkbox_pos_event(checkbox, &event);
401 PCUT_ASSERT_FALSE(checkbox->held);
402 PCUT_ASSERT_EQUALS(ui_unclaimed, claim);
403
404 /* Press inside is claimed and depresses check box */
405 event.type = POS_PRESS;
406 event.hpos = 10;
407 event.vpos = 20;
408 claim = ui_checkbox_pos_event(checkbox, &event);
409 PCUT_ASSERT_TRUE(checkbox->held);
410 PCUT_ASSERT_EQUALS(ui_claimed, claim);
411
412 /* Release outside (or anywhere) is claimed and relases check box */
413 event.type = POS_RELEASE;
414 event.hpos = 9;
415 event.vpos = 20;
416 claim = ui_checkbox_pos_event(checkbox, &event);
417 PCUT_ASSERT_FALSE(checkbox->held);
418 PCUT_ASSERT_EQUALS(ui_claimed, claim);
419
420 ui_checkbox_destroy(checkbox);
421 ui_resource_destroy(resource);
422
423 rc = gfx_context_delete(gc);
424 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
425}
426
427/** ui_pos_event() correctly translates POS_UPDATE to enter/leave */
428PCUT_TEST(pos_event_enter_leave)
429{
430 errno_t rc;
431 gfx_context_t *gc = NULL;
432 test_gc_t tgc;
433 ui_resource_t *resource = NULL;
434 ui_checkbox_t *checkbox;
435 pos_event_t event;
436 gfx_rect_t rect;
437
438 memset(&tgc, 0, sizeof(tgc));
439 rc = gfx_context_new(&ops, &tgc, &gc);
440 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
441
442 rc = ui_resource_create(gc, false, &resource);
443 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
444 PCUT_ASSERT_NOT_NULL(resource);
445
446 rc = ui_checkbox_create(resource, "Hello", &checkbox);
447 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
448
449 PCUT_ASSERT_FALSE(checkbox->inside);
450
451 rect.p0.x = 10;
452 rect.p0.y = 20;
453 rect.p1.x = 30;
454 rect.p1.y = 40;
455 ui_checkbox_set_rect(checkbox, &rect);
456
457 /* Moving outside does nothing */
458 event.type = POS_UPDATE;
459 event.hpos = 9;
460 event.vpos = 20;
461 ui_checkbox_pos_event(checkbox, &event);
462 PCUT_ASSERT_FALSE(checkbox->inside);
463
464 /* Moving inside sets inside flag */
465 event.type = POS_UPDATE;
466 event.hpos = 10;
467 event.vpos = 20;
468 ui_checkbox_pos_event(checkbox, &event);
469 PCUT_ASSERT_TRUE(checkbox->inside);
470
471 /* Moving outside clears inside flag */
472 event.type = POS_UPDATE;
473 event.hpos = 9;
474 event.vpos = 20;
475 ui_checkbox_pos_event(checkbox, &event);
476 PCUT_ASSERT_FALSE(checkbox->inside);
477
478 ui_checkbox_destroy(checkbox);
479 ui_resource_destroy(resource);
480
481 rc = gfx_context_delete(gc);
482 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
483}
484
485static errno_t testgc_set_color(void *arg, gfx_color_t *color)
486{
487 (void) arg;
488 (void) color;
489 return EOK;
490}
491
492static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
493{
494 (void) arg;
495 (void) rect;
496 return EOK;
497}
498
499static errno_t testgc_update(void *arg)
500{
501 (void) arg;
502 return EOK;
503}
504
505static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
506 gfx_bitmap_alloc_t *alloc, void **rbm)
507{
508 test_gc_t *tgc = (test_gc_t *) arg;
509 testgc_bitmap_t *tbm;
510
511 tbm = calloc(1, sizeof(testgc_bitmap_t));
512 if (tbm == NULL)
513 return ENOMEM;
514
515 if (alloc == NULL) {
516 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
517 sizeof(uint32_t);
518 tbm->alloc.off0 = 0;
519 tbm->alloc.pixels = calloc(sizeof(uint32_t),
520 (params->rect.p1.x - params->rect.p0.x) *
521 (params->rect.p1.y - params->rect.p0.y));
522 tbm->myalloc = true;
523 if (tbm->alloc.pixels == NULL) {
524 free(tbm);
525 return ENOMEM;
526 }
527 } else {
528 tbm->alloc = *alloc;
529 }
530
531 tbm->tgc = tgc;
532 tgc->bm_created = true;
533 tgc->bm_params = *params;
534 tgc->bm_pixels = tbm->alloc.pixels;
535 *rbm = (void *)tbm;
536 return EOK;
537}
538
539static errno_t testgc_bitmap_destroy(void *bm)
540{
541 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
542 if (tbm->myalloc)
543 free(tbm->alloc.pixels);
544 tbm->tgc->bm_destroyed = true;
545 free(tbm);
546 return EOK;
547}
548
549static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
550 gfx_coord2_t *offs)
551{
552 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
553 tbm->tgc->bm_rendered = true;
554 tbm->tgc->bm_srect = *srect;
555 tbm->tgc->bm_offs = *offs;
556 return EOK;
557}
558
559static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
560{
561 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
562 *alloc = tbm->alloc;
563 tbm->tgc->bm_got_alloc = true;
564 return EOK;
565}
566
567static void test_checkbox_switched(ui_checkbox_t *checkbox, void *arg,
568 bool checked)
569{
570 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
571
572 resp->switched = true;
573}
574
575PCUT_EXPORT(checkbox);
Note: See TracBrowser for help on using the repository browser.