Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / lib / ui / test / pbutton.c
blob7ecf02af59dec270d111a8ce4a3b663c1d3c29aa
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
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"
39 PCUT_INIT;
41 PCUT_TEST_SUITE(pbutton);
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 *);
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
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 *);
68 static ui_pbutton_cb_t test_pbutton_cb = {
69 .clicked = test_pbutton_clicked,
70 .down = test_pbutton_down,
71 .up = test_pbutton_up
74 static ui_pbutton_cb_t dummy_pbutton_cb = {
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;
88 typedef struct {
89 test_gc_t *tgc;
90 gfx_bitmap_alloc_t alloc;
91 bool myalloc;
92 } testgc_bitmap_t;
94 typedef struct {
95 bool clicked;
96 bool down;
97 bool up;
98 } test_cb_resp_t;
100 /** Create and destroy button */
101 PCUT_TEST(create_destroy)
103 ui_pbutton_t *pbutton = NULL;
104 errno_t rc;
106 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
107 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
108 PCUT_ASSERT_NOT_NULL(pbutton);
110 ui_pbutton_destroy(pbutton);
113 /** ui_pbutton_destroy() can take NULL argument (no-op) */
114 PCUT_TEST(destroy_null)
116 ui_pbutton_destroy(NULL);
119 /** ui_pbutton_ctl() returns control that has a working virtual destructor */
120 PCUT_TEST(ctl)
122 ui_pbutton_t *pbutton;
123 ui_control_t *control;
124 errno_t rc;
126 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
127 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
129 control = ui_pbutton_ctl(pbutton);
130 PCUT_ASSERT_NOT_NULL(control);
132 ui_control_destroy(control);
135 /** Set flags sets internal field */
136 PCUT_TEST(set_flags)
138 ui_pbutton_t *pbutton;
139 errno_t rc;
141 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
144 ui_pbutton_set_flags(pbutton, ui_pbf_no_text_depress);
145 PCUT_ASSERT_INT_EQUALS(ui_pbf_no_text_depress, pbutton->flags);
147 ui_pbutton_destroy(pbutton);
150 /** Set button rectangle sets internal field */
151 PCUT_TEST(set_rect)
153 ui_pbutton_t *pbutton;
154 gfx_rect_t rect;
155 errno_t rc;
157 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
158 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
160 rect.p0.x = 1;
161 rect.p0.y = 2;
162 rect.p1.x = 3;
163 rect.p1.y = 4;
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);
171 ui_pbutton_destroy(pbutton);
174 /** Set default flag sets internal field */
175 PCUT_TEST(set_default)
177 ui_pbutton_t *pbutton;
178 errno_t rc;
180 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
183 ui_pbutton_set_default(pbutton, true);
184 PCUT_ASSERT_TRUE(pbutton->isdefault);
186 ui_pbutton_set_default(pbutton, false);
187 PCUT_ASSERT_FALSE(pbutton->isdefault);
189 ui_pbutton_destroy(pbutton);
192 /** Get light gets internal field */
193 PCUT_TEST(get_light)
195 ui_pbutton_t *pbutton;
196 errno_t rc;
198 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
199 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
201 pbutton->light = true;
202 PCUT_ASSERT_TRUE(ui_pbutton_get_light(pbutton));
204 pbutton->light = false;
205 PCUT_ASSERT_FALSE(ui_pbutton_get_light(pbutton));
207 ui_pbutton_destroy(pbutton);
210 /** Set light sets internal field */
211 PCUT_TEST(set_light)
213 ui_pbutton_t *pbutton;
214 errno_t rc;
216 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
217 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
219 ui_pbutton_set_light(pbutton, true);
220 PCUT_ASSERT_TRUE(pbutton->light);
222 ui_pbutton_set_light(pbutton, false);
223 PCUT_ASSERT_FALSE(pbutton->light);
225 ui_pbutton_destroy(pbutton);
228 /** Set caption sets internal field */
229 PCUT_TEST(set_caption)
231 ui_pbutton_t *pbutton;
232 errno_t rc;
234 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
235 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
237 PCUT_ASSERT_STR_EQUALS("Hello", pbutton->caption);
239 rc = ui_pbutton_set_caption(pbutton, "World");
240 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242 PCUT_ASSERT_STR_EQUALS("World", pbutton->caption);
244 ui_pbutton_destroy(pbutton);
247 /** Paint button */
248 PCUT_TEST(paint)
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;
256 memset(&tgc, 0, sizeof(tgc));
257 rc = gfx_context_new(&ops, &tgc, &gc);
258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
260 rc = ui_resource_create(gc, false, &resource);
261 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
262 PCUT_ASSERT_NOT_NULL(resource);
264 rc = ui_pbutton_create(resource, "Hello", &pbutton);
265 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
267 rc = ui_pbutton_paint(pbutton);
268 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
270 ui_pbutton_destroy(pbutton);
271 ui_resource_destroy(resource);
273 rc = gfx_context_delete(gc);
274 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
277 /** Test ui_pbutton_clicked() */
278 PCUT_TEST(clicked)
280 errno_t rc;
281 ui_pbutton_t *pbutton;
282 test_cb_resp_t resp;
284 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
285 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
287 /* Clicked with no callbacks set */
288 ui_pbutton_clicked(pbutton);
290 /* Clicked with callback not implementing clicked */
291 ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
292 ui_pbutton_clicked(pbutton);
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);
300 ui_pbutton_destroy(pbutton);
303 /** Test ui_pbutton_down() */
304 PCUT_TEST(down)
306 errno_t rc;
307 ui_pbutton_t *pbutton;
308 test_cb_resp_t resp;
310 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
311 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
313 /* Down with no callbacks set */
314 ui_pbutton_clicked(pbutton);
316 /* Down with callback not implementing down */
317 ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
318 ui_pbutton_down(pbutton);
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);
326 ui_pbutton_destroy(pbutton);
329 /** Test ui_pbutton_up() */
330 PCUT_TEST(up)
332 errno_t rc;
333 ui_pbutton_t *pbutton;
334 test_cb_resp_t resp;
336 rc = ui_pbutton_create(NULL, "Hello", &pbutton);
337 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
339 /* Up with no callbacks set */
340 ui_pbutton_clicked(pbutton);
342 /* Up with callback not implementing up */
343 ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
344 ui_pbutton_up(pbutton);
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);
352 ui_pbutton_destroy(pbutton);
355 /** Press and release button */
356 PCUT_TEST(press_release)
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;
365 memset(&tgc, 0, sizeof(tgc));
366 rc = gfx_context_new(&ops, &tgc, &gc);
367 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
369 rc = ui_resource_create(gc, false, &resource);
370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
371 PCUT_ASSERT_NOT_NULL(resource);
373 rc = ui_pbutton_create(resource, "Hello", &pbutton);
374 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
376 resp.clicked = false;
377 resp.down = false;
378 resp.up = false;
379 ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
381 PCUT_ASSERT_FALSE(pbutton->held);
382 PCUT_ASSERT_FALSE(pbutton->inside);
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);
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);
397 ui_pbutton_destroy(pbutton);
398 ui_resource_destroy(resource);
400 rc = gfx_context_delete(gc);
401 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
404 /** Press, leave and release button */
405 PCUT_TEST(press_leave_release)
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;
414 memset(&tgc, 0, sizeof(tgc));
415 rc = gfx_context_new(&ops, &tgc, &gc);
416 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
418 rc = ui_resource_create(gc, false, &resource);
419 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
420 PCUT_ASSERT_NOT_NULL(resource);
422 rc = ui_pbutton_create(resource, "Hello", &pbutton);
423 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
425 resp.clicked = false;
426 ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
428 PCUT_ASSERT_FALSE(pbutton->held);
429 PCUT_ASSERT_FALSE(pbutton->inside);
431 ui_pbutton_press(pbutton);
432 PCUT_ASSERT_TRUE(pbutton->held);
433 PCUT_ASSERT_TRUE(pbutton->inside);
434 PCUT_ASSERT_FALSE(resp.clicked);
436 ui_pbutton_leave(pbutton);
437 PCUT_ASSERT_TRUE(pbutton->held);
438 PCUT_ASSERT_FALSE(pbutton->inside);
439 PCUT_ASSERT_FALSE(resp.clicked);
441 ui_pbutton_release(pbutton);
442 PCUT_ASSERT_FALSE(pbutton->held);
443 PCUT_ASSERT_FALSE(pbutton->inside);
444 PCUT_ASSERT_FALSE(resp.clicked);
446 ui_pbutton_destroy(pbutton);
447 ui_resource_destroy(resource);
449 rc = gfx_context_delete(gc);
450 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
453 /** Press, leave, enter and release button */
454 PCUT_TEST(press_leave_enter_release)
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;
463 memset(&tgc, 0, sizeof(tgc));
464 rc = gfx_context_new(&ops, &tgc, &gc);
465 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
467 rc = ui_resource_create(gc, false, &resource);
468 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
469 PCUT_ASSERT_NOT_NULL(resource);
471 rc = ui_pbutton_create(resource, "Hello", &pbutton);
472 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
474 resp.clicked = false;
475 ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
477 PCUT_ASSERT_FALSE(pbutton->held);
478 PCUT_ASSERT_FALSE(pbutton->inside);
480 ui_pbutton_press(pbutton);
481 PCUT_ASSERT_TRUE(pbutton->held);
482 PCUT_ASSERT_TRUE(pbutton->inside);
483 PCUT_ASSERT_FALSE(resp.clicked);
485 ui_pbutton_leave(pbutton);
486 PCUT_ASSERT_TRUE(pbutton->held);
487 PCUT_ASSERT_FALSE(pbutton->inside);
488 PCUT_ASSERT_FALSE(resp.clicked);
490 ui_pbutton_enter(pbutton);
491 PCUT_ASSERT_TRUE(pbutton->held);
492 PCUT_ASSERT_TRUE(pbutton->inside);
493 PCUT_ASSERT_FALSE(resp.clicked);
495 ui_pbutton_release(pbutton);
496 PCUT_ASSERT_FALSE(pbutton->held);
497 PCUT_ASSERT_TRUE(pbutton->inside);
498 PCUT_ASSERT_TRUE(resp.clicked);
500 ui_pbutton_destroy(pbutton);
501 ui_resource_destroy(resource);
503 rc = gfx_context_delete(gc);
504 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
507 /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
508 PCUT_TEST(pos_event_press_release)
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;
519 memset(&tgc, 0, sizeof(tgc));
520 rc = gfx_context_new(&ops, &tgc, &gc);
521 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
523 rc = ui_resource_create(gc, false, &resource);
524 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
525 PCUT_ASSERT_NOT_NULL(resource);
527 rc = ui_pbutton_create(resource, "Hello", &pbutton);
528 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
530 PCUT_ASSERT_FALSE(pbutton->held);
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);
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);
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);
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);
562 ui_pbutton_destroy(pbutton);
563 ui_resource_destroy(resource);
565 rc = gfx_context_delete(gc);
566 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
569 /** ui_pos_event() correctly translates POS_UPDATE to enter/leave */
570 PCUT_TEST(pos_event_enter_leave)
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;
580 memset(&tgc, 0, sizeof(tgc));
581 rc = gfx_context_new(&ops, &tgc, &gc);
582 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
584 rc = ui_resource_create(gc, false, &resource);
585 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
586 PCUT_ASSERT_NOT_NULL(resource);
588 rc = ui_pbutton_create(resource, "Hello", &pbutton);
589 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
591 PCUT_ASSERT_FALSE(pbutton->inside);
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);
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);
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);
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);
620 ui_pbutton_destroy(pbutton);
621 ui_resource_destroy(resource);
623 rc = gfx_context_delete(gc);
624 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
627 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
629 (void) arg;
630 (void) rect;
631 return EOK;
634 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
636 (void) arg;
637 (void) color;
638 return EOK;
641 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
643 (void) arg;
644 (void) rect;
645 return EOK;
648 static errno_t testgc_update(void *arg)
650 (void) arg;
651 return EOK;
654 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
655 gfx_bitmap_alloc_t *alloc, void **rbm)
657 test_gc_t *tgc = (test_gc_t *) arg;
658 testgc_bitmap_t *tbm;
660 tbm = calloc(1, sizeof(testgc_bitmap_t));
661 if (tbm == NULL)
662 return ENOMEM;
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;
676 } else {
677 tbm->alloc = *alloc;
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;
688 static errno_t testgc_bitmap_destroy(void *bm)
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;
698 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
699 gfx_coord2_t *offs)
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;
708 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
710 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
711 *alloc = tbm->alloc;
712 tbm->tgc->bm_got_alloc = true;
713 return EOK;
716 static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
718 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
720 resp->clicked = true;
723 static void test_pbutton_down(ui_pbutton_t *pbutton, void *arg)
725 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
727 resp->down = true;
730 static void test_pbutton_up(ui_pbutton_t *pbutton, void *arg)
732 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
734 resp->up = true;
737 PCUT_EXPORT(pbutton);