Ignore:
File:
1 edited

Legend:

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

    r7470d97 r1fa6292  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2024 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <ui/resource.h>
    3737#include "../private/checkbox.h"
     38#include "../private/testgc.h"
    3839
    3940PCUT_INIT;
    4041
    4142PCUT_TEST_SUITE(checkbox);
    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 };
    6343
    6444static void test_checkbox_switched(ui_checkbox_t *, void *, bool);
     
    7050static ui_checkbox_cb_t dummy_checkbox_cb = {
    7151};
    72 
    73 typedef 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;
    8952
    9053typedef struct {
     
    151114}
    152115
    153 /** Paint check box */
    154 PCUT_TEST(paint)
    155 {
    156         errno_t rc;
    157         gfx_context_t *gc = NULL;
    158         test_gc_t tgc;
    159         ui_resource_t *resource = NULL;
    160         ui_checkbox_t *checkbox;
    161 
    162         memset(&tgc, 0, sizeof(tgc));
    163         rc = gfx_context_new(&ops, &tgc, &gc);
    164         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    165 
    166         rc = ui_resource_create(gc, false, &resource);
    167         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    168         PCUT_ASSERT_NOT_NULL(resource);
    169 
    170         rc = ui_checkbox_create(resource, "Hello", &checkbox);
    171         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    172 
    173         rc = ui_checkbox_paint(checkbox);
     116/** Get check box checked returns internal field */
     117PCUT_TEST(get_checked)
     118{
     119        ui_checkbox_t *checkbox;
     120        errno_t rc;
     121
     122        rc = ui_checkbox_create(NULL, "Hello", &checkbox);
     123        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     124
     125        checkbox->checked = false;
     126        PCUT_ASSERT_FALSE(ui_checkbox_get_checked(checkbox));
     127        checkbox->checked = true;
     128        PCUT_ASSERT_TRUE(ui_checkbox_get_checked(checkbox));
     129
     130        ui_checkbox_destroy(checkbox);
     131}
     132
     133/** Set check box checked sets internal field */
     134PCUT_TEST(set_checked)
     135{
     136        ui_checkbox_t *checkbox;
     137        errno_t rc;
     138
     139        rc = ui_checkbox_create(NULL, "Hello", &checkbox);
     140        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     141
     142        ui_checkbox_set_checked(checkbox, true);
     143        PCUT_ASSERT_TRUE(checkbox->checked);
     144        ui_checkbox_set_checked(checkbox, false);
     145        PCUT_ASSERT_FALSE(checkbox->checked);
     146
     147        ui_checkbox_destroy(checkbox);
     148}
     149
     150/** Paint check box in graphics mode */
     151PCUT_TEST(paint_gfx)
     152{
     153        errno_t rc;
     154        gfx_context_t *gc = NULL;
     155        test_gc_t tgc;
     156        ui_resource_t *resource = NULL;
     157        ui_checkbox_t *checkbox;
     158
     159        memset(&tgc, 0, sizeof(tgc));
     160        rc = gfx_context_new(&ops, &tgc, &gc);
     161        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     162
     163        rc = ui_resource_create(gc, false, &resource);
     164        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     165        PCUT_ASSERT_NOT_NULL(resource);
     166
     167        rc = ui_checkbox_create(resource, "Hello", &checkbox);
     168        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     169
     170        rc = ui_checkbox_paint_gfx(checkbox);
     171        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     172
     173        ui_checkbox_destroy(checkbox);
     174        ui_resource_destroy(resource);
     175
     176        rc = gfx_context_delete(gc);
     177        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     178}
     179
     180/** Paint check box in text mode */
     181PCUT_TEST(paint_text)
     182{
     183        errno_t rc;
     184        gfx_context_t *gc = NULL;
     185        test_gc_t tgc;
     186        ui_resource_t *resource = NULL;
     187        ui_checkbox_t *checkbox;
     188
     189        memset(&tgc, 0, sizeof(tgc));
     190        rc = gfx_context_new(&ops, &tgc, &gc);
     191        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     192
     193        rc = ui_resource_create(gc, false, &resource);
     194        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     195        PCUT_ASSERT_NOT_NULL(resource);
     196
     197        rc = ui_checkbox_create(resource, "Hello", &checkbox);
     198        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     199
     200        rc = ui_checkbox_paint_text(checkbox);
    174201        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    175202
     
    485512}
    486513
    487 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
    488 {
    489         (void) arg;
    490         (void) rect;
    491         return EOK;
    492 }
    493 
    494 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    495 {
    496         (void) arg;
    497         (void) color;
    498         return EOK;
    499 }
    500 
    501 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    502 {
    503         (void) arg;
    504         (void) rect;
    505         return EOK;
    506 }
    507 
    508 static errno_t testgc_update(void *arg)
    509 {
    510         (void) arg;
    511         return EOK;
    512 }
    513 
    514 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    515     gfx_bitmap_alloc_t *alloc, void **rbm)
    516 {
    517         test_gc_t *tgc = (test_gc_t *) arg;
    518         testgc_bitmap_t *tbm;
    519 
    520         tbm = calloc(1, sizeof(testgc_bitmap_t));
    521         if (tbm == NULL)
    522                 return ENOMEM;
    523 
    524         if (alloc == NULL) {
    525                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    526                     sizeof(uint32_t);
    527                 tbm->alloc.off0 = 0;
    528                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    529                     (params->rect.p1.x - params->rect.p0.x) *
    530                     (params->rect.p1.y - params->rect.p0.y));
    531                 tbm->myalloc = true;
    532                 if (tbm->alloc.pixels == NULL) {
    533                         free(tbm);
    534                         return ENOMEM;
    535                 }
    536         } else {
    537                 tbm->alloc = *alloc;
    538         }
    539 
    540         tbm->tgc = tgc;
    541         tgc->bm_created = true;
    542         tgc->bm_params = *params;
    543         tgc->bm_pixels = tbm->alloc.pixels;
    544         *rbm = (void *)tbm;
    545         return EOK;
    546 }
    547 
    548 static errno_t testgc_bitmap_destroy(void *bm)
    549 {
    550         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    551         if (tbm->myalloc)
    552                 free(tbm->alloc.pixels);
    553         tbm->tgc->bm_destroyed = true;
    554         free(tbm);
    555         return EOK;
    556 }
    557 
    558 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    559     gfx_coord2_t *offs)
    560 {
    561         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    562         tbm->tgc->bm_rendered = true;
    563         tbm->tgc->bm_srect = *srect;
    564         tbm->tgc->bm_offs = *offs;
    565         return EOK;
    566 }
    567 
    568 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    569 {
    570         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    571         *alloc = tbm->alloc;
    572         tbm->tgc->bm_got_alloc = true;
    573         return EOK;
    574 }
    575 
    576514static void test_checkbox_switched(ui_checkbox_t *checkbox, void *arg,
    577515    bool checked)
Note: See TracChangeset for help on using the changeset viewer.