Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ui/test/pbutton.c

    r1fa6292 r3583ffb  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <ui/resource.h>
    3737#include "../private/pbutton.h"
    38 #include "../private/testgc.h"
    3938
    4039PCUT_INIT;
     
    4241PCUT_TEST_SUITE(pbutton);
    4342
     43static errno_t testgc_set_color(void *, gfx_color_t *);
     44static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     45static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     46    gfx_bitmap_alloc_t *, void **);
     47static errno_t testgc_bitmap_destroy(void *);
     48static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     49static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     50
     51static gfx_context_ops_t ops = {
     52        .set_color = testgc_set_color,
     53        .fill_rect = testgc_fill_rect,
     54        .bitmap_create = testgc_bitmap_create,
     55        .bitmap_destroy = testgc_bitmap_destroy,
     56        .bitmap_render = testgc_bitmap_render,
     57        .bitmap_get_alloc = testgc_bitmap_get_alloc
     58};
     59
    4460static void test_pbutton_clicked(ui_pbutton_t *, void *);
    45 static void test_pbutton_down(ui_pbutton_t *, void *);
    46 static void test_pbutton_up(ui_pbutton_t *, void *);
    4761
    4862static ui_pbutton_cb_t test_pbutton_cb = {
    49         .clicked = test_pbutton_clicked,
    50         .down = test_pbutton_down,
    51         .up = test_pbutton_up
     63        .clicked = test_pbutton_clicked
    5264};
    5365
     
    5668
    5769typedef struct {
     70        bool bm_created;
     71        bool bm_destroyed;
     72        gfx_bitmap_params_t bm_params;
     73        void *bm_pixels;
     74        gfx_rect_t bm_srect;
     75        gfx_coord2_t bm_offs;
     76        bool bm_rendered;
     77        bool bm_got_alloc;
     78} test_gc_t;
     79
     80typedef struct {
     81        test_gc_t *tgc;
     82        gfx_bitmap_alloc_t alloc;
     83        bool myalloc;
     84} testgc_bitmap_t;
     85
     86typedef struct {
    5887        bool clicked;
    59         bool down;
    60         bool up;
    6188} test_cb_resp_t;
    6289
     
    94121
    95122        ui_control_destroy(control);
    96 }
    97 
    98 /** Set flags sets internal field */
    99 PCUT_TEST(set_flags)
    100 {
    101         ui_pbutton_t *pbutton;
    102         errno_t rc;
    103 
    104         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    105         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    106 
    107         ui_pbutton_set_flags(pbutton, ui_pbf_no_text_depress);
    108         PCUT_ASSERT_INT_EQUALS(ui_pbf_no_text_depress, pbutton->flags);
    109 
    110         ui_pbutton_destroy(pbutton);
    111123}
    112124
     
    153165}
    154166
    155 /** Get light gets internal field */
    156 PCUT_TEST(get_light)
    157 {
    158         ui_pbutton_t *pbutton;
    159         errno_t rc;
    160 
    161         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    162         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    163 
    164         pbutton->light = true;
    165         PCUT_ASSERT_TRUE(ui_pbutton_get_light(pbutton));
    166 
    167         pbutton->light = false;
    168         PCUT_ASSERT_FALSE(ui_pbutton_get_light(pbutton));
    169 
    170         ui_pbutton_destroy(pbutton);
    171 }
    172 
    173 /** Set light sets internal field */
    174 PCUT_TEST(set_light)
    175 {
    176         ui_pbutton_t *pbutton;
    177         errno_t rc;
    178 
    179         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    180         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    181 
    182         ui_pbutton_set_light(pbutton, true);
    183         PCUT_ASSERT_TRUE(pbutton->light);
    184 
    185         ui_pbutton_set_light(pbutton, false);
    186         PCUT_ASSERT_FALSE(pbutton->light);
    187 
    188         ui_pbutton_destroy(pbutton);
    189 }
    190 
    191 /** Set caption sets internal field */
    192 PCUT_TEST(set_caption)
    193 {
    194         ui_pbutton_t *pbutton;
    195         errno_t rc;
    196 
    197         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    198         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    199 
    200         PCUT_ASSERT_STR_EQUALS("Hello", pbutton->caption);
    201 
    202         rc = ui_pbutton_set_caption(pbutton, "World");
    203         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    204 
    205         PCUT_ASSERT_STR_EQUALS("World", pbutton->caption);
    206 
    207         ui_pbutton_destroy(pbutton);
    208 }
    209 
    210167/** Paint button */
    211168PCUT_TEST(paint)
     
    221178        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    222179
    223         rc = ui_resource_create(gc, false, &resource);
     180        rc = ui_resource_create(gc, &resource);
    224181        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    225182        PCUT_ASSERT_NOT_NULL(resource);
     
    264221}
    265222
    266 /** Test ui_pbutton_down() */
    267 PCUT_TEST(down)
    268 {
    269         errno_t rc;
    270         ui_pbutton_t *pbutton;
    271         test_cb_resp_t resp;
    272 
    273         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    274         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    275 
    276         /* Down with no callbacks set */
    277         ui_pbutton_clicked(pbutton);
    278 
    279         /* Down with callback not implementing down */
    280         ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
    281         ui_pbutton_down(pbutton);
    282 
    283         /* Down with real callback set */
    284         resp.down = false;
    285         ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
    286         ui_pbutton_down(pbutton);
    287         PCUT_ASSERT_TRUE(resp.down);
    288 
    289         ui_pbutton_destroy(pbutton);
    290 }
    291 
    292 /** Test ui_pbutton_up() */
    293 PCUT_TEST(up)
    294 {
    295         errno_t rc;
    296         ui_pbutton_t *pbutton;
    297         test_cb_resp_t resp;
    298 
    299         rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    300         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    301 
    302         /* Up with no callbacks set */
    303         ui_pbutton_clicked(pbutton);
    304 
    305         /* Up with callback not implementing up */
    306         ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
    307         ui_pbutton_up(pbutton);
    308 
    309         /* Up with real callback set */
    310         resp.up = false;
    311         ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
    312         ui_pbutton_up(pbutton);
    313         PCUT_ASSERT_TRUE(resp.up);
    314 
    315         ui_pbutton_destroy(pbutton);
    316 }
    317 
    318223/** Press and release button */
    319224PCUT_TEST(press_release)
     
    330235        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    331236
    332         rc = ui_resource_create(gc, false, &resource);
     237        rc = ui_resource_create(gc, &resource);
    333238        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    334239        PCUT_ASSERT_NOT_NULL(resource);
     
    338243
    339244        resp.clicked = false;
    340         resp.down = false;
    341         resp.up = false;
    342245        ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
    343246
     
    348251        PCUT_ASSERT_TRUE(pbutton->held);
    349252        PCUT_ASSERT_TRUE(pbutton->inside);
    350         PCUT_ASSERT_TRUE(resp.down);
    351         PCUT_ASSERT_FALSE(resp.up);
    352253        PCUT_ASSERT_FALSE(resp.clicked);
    353254
     
    355256        PCUT_ASSERT_FALSE(pbutton->held);
    356257        PCUT_ASSERT_TRUE(pbutton->inside);
    357         PCUT_ASSERT_TRUE(resp.up);
    358258        PCUT_ASSERT_TRUE(resp.clicked);
    359259
     
    379279        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    380280
    381         rc = ui_resource_create(gc, false, &resource);
     281        rc = ui_resource_create(gc, &resource);
    382282        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    383283        PCUT_ASSERT_NOT_NULL(resource);
     
    428328        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    429329
    430         rc = ui_resource_create(gc, false, &resource);
     330        rc = ui_resource_create(gc, &resource);
    431331        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    432332        PCUT_ASSERT_NOT_NULL(resource);
     
    484384        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    485385
    486         rc = ui_resource_create(gc, false, &resource);
     386        rc = ui_resource_create(gc, &resource);
    487387        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    488388        PCUT_ASSERT_NOT_NULL(resource);
     
    545445        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    546446
    547         rc = ui_resource_create(gc, false, &resource);
     447        rc = ui_resource_create(gc, &resource);
    548448        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    549449        PCUT_ASSERT_NOT_NULL(resource);
     
    588488}
    589489
     490static errno_t testgc_set_color(void *arg, gfx_color_t *color)
     491{
     492        (void) arg;
     493        (void) color;
     494        return EOK;
     495}
     496
     497static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
     498{
     499        (void) arg;
     500        (void) rect;
     501        return EOK;
     502}
     503
     504static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     505    gfx_bitmap_alloc_t *alloc, void **rbm)
     506{
     507        test_gc_t *tgc = (test_gc_t *) arg;
     508        testgc_bitmap_t *tbm;
     509
     510        tbm = calloc(1, sizeof(testgc_bitmap_t));
     511        if (tbm == NULL)
     512                return ENOMEM;
     513
     514        if (alloc == NULL) {
     515                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     516                    sizeof(uint32_t);
     517                tbm->alloc.off0 = 0;
     518                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     519                    (params->rect.p1.x - params->rect.p0.x) *
     520                    (params->rect.p1.y - params->rect.p0.y));
     521                tbm->myalloc = true;
     522                if (tbm->alloc.pixels == NULL) {
     523                        free(tbm);
     524                        return ENOMEM;
     525                }
     526        } else {
     527                tbm->alloc = *alloc;
     528        }
     529
     530        tbm->tgc = tgc;
     531        tgc->bm_created = true;
     532        tgc->bm_params = *params;
     533        tgc->bm_pixels = tbm->alloc.pixels;
     534        *rbm = (void *)tbm;
     535        return EOK;
     536}
     537
     538static errno_t testgc_bitmap_destroy(void *bm)
     539{
     540        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     541        if (tbm->myalloc)
     542                free(tbm->alloc.pixels);
     543        tbm->tgc->bm_destroyed = true;
     544        free(tbm);
     545        return EOK;
     546}
     547
     548static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     549    gfx_coord2_t *offs)
     550{
     551        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     552        tbm->tgc->bm_rendered = true;
     553        tbm->tgc->bm_srect = *srect;
     554        tbm->tgc->bm_offs = *offs;
     555        return EOK;
     556}
     557
     558static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     559{
     560        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     561        *alloc = tbm->alloc;
     562        tbm->tgc->bm_got_alloc = true;
     563        return EOK;
     564}
     565
    590566static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
    591567{
     
    595571}
    596572
    597 static void test_pbutton_down(ui_pbutton_t *pbutton, void *arg)
    598 {
    599         test_cb_resp_t *resp = (test_cb_resp_t *) arg;
    600 
    601         resp->down = true;
    602 }
    603 
    604 static void test_pbutton_up(ui_pbutton_t *pbutton, void *arg)
    605 {
    606         test_cb_resp_t *resp = (test_cb_resp_t *) arg;
    607 
    608         resp->up = true;
    609 }
    610 
    611573PCUT_EXPORT(pbutton);
Note: See TracChangeset for help on using the changeset viewer.