Ignore:
File:
1 edited

Legend:

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

    r1fa6292 r297b1b3  
    11/*
    2  * Copyright (c) 2024 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3636#include <ui/resource.h>
    3737#include "../private/checkbox.h"
    38 #include "../private/testgc.h"
    3938
    4039PCUT_INIT;
    4140
    4241PCUT_TEST_SUITE(checkbox);
     42
     43static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
     44static errno_t testgc_set_color(void *, gfx_color_t *);
     45static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     46static errno_t testgc_update(void *);
     47static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     48    gfx_bitmap_alloc_t *, void **);
     49static errno_t testgc_bitmap_destroy(void *);
     50static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     51static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     52
     53static 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};
    4363
    4464static void test_checkbox_switched(ui_checkbox_t *, void *, bool);
     
    5070static ui_checkbox_cb_t dummy_checkbox_cb = {
    5171};
     72
     73typedef 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
     84typedef struct {
     85        test_gc_t *tgc;
     86        gfx_bitmap_alloc_t alloc;
     87        bool myalloc;
     88} testgc_bitmap_t;
    5289
    5390typedef struct {
     
    110147        PCUT_ASSERT_INT_EQUALS(rect.p1.x, checkbox->rect.p1.x);
    111148        PCUT_ASSERT_INT_EQUALS(rect.p1.y, checkbox->rect.p1.y);
    112 
    113         ui_checkbox_destroy(checkbox);
    114 }
    115 
    116 /** Get check box checked returns internal field */
    117 PCUT_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 */
    134 PCUT_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);
    146149
    147150        ui_checkbox_destroy(checkbox);
     
    512515}
    513516
     517static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
     518{
     519        (void) arg;
     520        (void) rect;
     521        return EOK;
     522}
     523
     524static errno_t testgc_set_color(void *arg, gfx_color_t *color)
     525{
     526        (void) arg;
     527        (void) color;
     528        return EOK;
     529}
     530
     531static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
     532{
     533        (void) arg;
     534        (void) rect;
     535        return EOK;
     536}
     537
     538static errno_t testgc_update(void *arg)
     539{
     540        (void) arg;
     541        return EOK;
     542}
     543
     544static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     545    gfx_bitmap_alloc_t *alloc, void **rbm)
     546{
     547        test_gc_t *tgc = (test_gc_t *) arg;
     548        testgc_bitmap_t *tbm;
     549
     550        tbm = calloc(1, sizeof(testgc_bitmap_t));
     551        if (tbm == NULL)
     552                return ENOMEM;
     553
     554        if (alloc == NULL) {
     555                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     556                    sizeof(uint32_t);
     557                tbm->alloc.off0 = 0;
     558                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     559                    (params->rect.p1.x - params->rect.p0.x) *
     560                    (params->rect.p1.y - params->rect.p0.y));
     561                tbm->myalloc = true;
     562                if (tbm->alloc.pixels == NULL) {
     563                        free(tbm);
     564                        return ENOMEM;
     565                }
     566        } else {
     567                tbm->alloc = *alloc;
     568        }
     569
     570        tbm->tgc = tgc;
     571        tgc->bm_created = true;
     572        tgc->bm_params = *params;
     573        tgc->bm_pixels = tbm->alloc.pixels;
     574        *rbm = (void *)tbm;
     575        return EOK;
     576}
     577
     578static errno_t testgc_bitmap_destroy(void *bm)
     579{
     580        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     581        if (tbm->myalloc)
     582                free(tbm->alloc.pixels);
     583        tbm->tgc->bm_destroyed = true;
     584        free(tbm);
     585        return EOK;
     586}
     587
     588static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     589    gfx_coord2_t *offs)
     590{
     591        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     592        tbm->tgc->bm_rendered = true;
     593        tbm->tgc->bm_srect = *srect;
     594        tbm->tgc->bm_offs = *offs;
     595        return EOK;
     596}
     597
     598static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     599{
     600        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     601        *alloc = tbm->alloc;
     602        tbm->tgc->bm_got_alloc = true;
     603        return EOK;
     604}
     605
    514606static void test_checkbox_switched(ui_checkbox_t *checkbox, void *arg,
    515607    bool checked)
Note: See TracChangeset for help on using the changeset viewer.