Ignore:
File:
1 edited

Legend:

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

    r7470d97 r1fa6292  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2023 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <ui/resource.h>
    3737#include "../private/pbutton.h"
     38#include "../private/testgc.h"
    3839
    3940PCUT_INIT;
     
    4142PCUT_TEST_SUITE(pbutton);
    4243
    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 
    6444static void test_pbutton_clicked(ui_pbutton_t *, void *);
     45static void test_pbutton_down(ui_pbutton_t *, void *);
     46static void test_pbutton_up(ui_pbutton_t *, void *);
    6547
    6648static ui_pbutton_cb_t test_pbutton_cb = {
    67         .clicked = test_pbutton_clicked
     49        .clicked = test_pbutton_clicked,
     50        .down = test_pbutton_down,
     51        .up = test_pbutton_up
    6852};
    6953
     
    7256
    7357typedef struct {
    74         bool bm_created;
    75         bool bm_destroyed;
    76         gfx_bitmap_params_t bm_params;
    77         void *bm_pixels;
    78         gfx_rect_t bm_srect;
    79         gfx_coord2_t bm_offs;
    80         bool bm_rendered;
    81         bool bm_got_alloc;
    82 } test_gc_t;
    83 
    84 typedef struct {
    85         test_gc_t *tgc;
    86         gfx_bitmap_alloc_t alloc;
    87         bool myalloc;
    88 } testgc_bitmap_t;
    89 
    90 typedef struct {
    9158        bool clicked;
     59        bool down;
     60        bool up;
    9261} test_cb_resp_t;
    9362
     
    12594
    12695        ui_control_destroy(control);
     96}
     97
     98/** Set flags sets internal field */
     99PCUT_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);
    127111}
    128112
     
    169153}
    170154
     155/** Get light gets internal field */
     156PCUT_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 */
     174PCUT_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 */
     192PCUT_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
    171210/** Paint button */
    172211PCUT_TEST(paint)
     
    225264}
    226265
     266/** Test ui_pbutton_down() */
     267PCUT_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() */
     293PCUT_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
    227318/** Press and release button */
    228319PCUT_TEST(press_release)
     
    247338
    248339        resp.clicked = false;
     340        resp.down = false;
     341        resp.up = false;
    249342        ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
    250343
     
    255348        PCUT_ASSERT_TRUE(pbutton->held);
    256349        PCUT_ASSERT_TRUE(pbutton->inside);
     350        PCUT_ASSERT_TRUE(resp.down);
     351        PCUT_ASSERT_FALSE(resp.up);
    257352        PCUT_ASSERT_FALSE(resp.clicked);
    258353
     
    260355        PCUT_ASSERT_FALSE(pbutton->held);
    261356        PCUT_ASSERT_TRUE(pbutton->inside);
     357        PCUT_ASSERT_TRUE(resp.up);
    262358        PCUT_ASSERT_TRUE(resp.clicked);
    263359
     
    492588}
    493589
    494 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
    495 {
    496         (void) arg;
    497         (void) rect;
    498         return EOK;
    499 }
    500 
    501 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    502 {
    503         (void) arg;
    504         (void) color;
    505         return EOK;
    506 }
    507 
    508 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    509 {
    510         (void) arg;
    511         (void) rect;
    512         return EOK;
    513 }
    514 
    515 static errno_t testgc_update(void *arg)
    516 {
    517         (void) arg;
    518         return EOK;
    519 }
    520 
    521 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    522     gfx_bitmap_alloc_t *alloc, void **rbm)
    523 {
    524         test_gc_t *tgc = (test_gc_t *) arg;
    525         testgc_bitmap_t *tbm;
    526 
    527         tbm = calloc(1, sizeof(testgc_bitmap_t));
    528         if (tbm == NULL)
    529                 return ENOMEM;
    530 
    531         if (alloc == NULL) {
    532                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    533                     sizeof(uint32_t);
    534                 tbm->alloc.off0 = 0;
    535                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    536                     (params->rect.p1.x - params->rect.p0.x) *
    537                     (params->rect.p1.y - params->rect.p0.y));
    538                 tbm->myalloc = true;
    539                 if (tbm->alloc.pixels == NULL) {
    540                         free(tbm);
    541                         return ENOMEM;
    542                 }
    543         } else {
    544                 tbm->alloc = *alloc;
    545         }
    546 
    547         tbm->tgc = tgc;
    548         tgc->bm_created = true;
    549         tgc->bm_params = *params;
    550         tgc->bm_pixels = tbm->alloc.pixels;
    551         *rbm = (void *)tbm;
    552         return EOK;
    553 }
    554 
    555 static errno_t testgc_bitmap_destroy(void *bm)
    556 {
    557         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    558         if (tbm->myalloc)
    559                 free(tbm->alloc.pixels);
    560         tbm->tgc->bm_destroyed = true;
    561         free(tbm);
    562         return EOK;
    563 }
    564 
    565 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    566     gfx_coord2_t *offs)
    567 {
    568         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    569         tbm->tgc->bm_rendered = true;
    570         tbm->tgc->bm_srect = *srect;
    571         tbm->tgc->bm_offs = *offs;
    572         return EOK;
    573 }
    574 
    575 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    576 {
    577         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    578         *alloc = tbm->alloc;
    579         tbm->tgc->bm_got_alloc = true;
    580         return EOK;
    581 }
    582 
    583590static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
    584591{
     
    588595}
    589596
     597static 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
     604static 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
    590611PCUT_EXPORT(pbutton);
Note: See TracChangeset for help on using the changeset viewer.