Ignore:
File:
1 edited

Legend:

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

    r1fa6292 r7470d97  
    11/*
    2  * Copyright (c) 2023 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3434#include <ui/paint.h>
    3535#include <ui/resource.h>
    36 #include "../private/testgc.h"
    3736
    3837PCUT_INIT;
     
    4039PCUT_TEST_SUITE(paint);
    4140
    42 /** Test box characters */
    43 static ui_box_chars_t test_box_chars = {
    44         {
    45                 { "A", "B", "C" },
    46                 { "D", " ", "E" },
    47                 { "F", "G", "H" }
    48         }
     41static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
     42static errno_t testgc_set_color(void *, gfx_color_t *);
     43static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     44static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     45    gfx_bitmap_alloc_t *, void **);
     46static errno_t testgc_bitmap_destroy(void *);
     47static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     48static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     49
     50static gfx_context_ops_t ops = {
     51        .set_clip_rect = testgc_set_clip_rect,
     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
    4958};
     59
     60typedef struct {
     61        bool bm_created;
     62        bool bm_destroyed;
     63        gfx_bitmap_params_t bm_params;
     64        void *bm_pixels;
     65        gfx_rect_t bm_srect;
     66        gfx_coord2_t bm_offs;
     67        bool bm_rendered;
     68        bool bm_got_alloc;
     69} test_gc_t;
     70
     71typedef struct {
     72        test_gc_t *tgc;
     73        gfx_bitmap_alloc_t alloc;
     74        bool myalloc;
     75} testgc_bitmap_t;
    5076
    5177/** Paint bevel */
     
    7096        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    7197
    72         rect.p0.x = 10;
    73         rect.p0.y = 20;
    74         rect.p1.x = 30;
    75         rect.p1.y = 40;
    76 
    7798        /* Paint bevel with NULL 'inside' output parameter */
    7899        rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
     
    85106        gfx_color_delete(color2);
    86107        gfx_color_delete(color1);
    87         rc = gfx_context_delete(gc);
    88         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    89 }
    90 
    91 /** Get bevel inside */
    92 PCUT_TEST(get_bevel_inside)
    93 {
    94         errno_t rc;
    95         gfx_context_t *gc = NULL;
    96         test_gc_t tgc;
    97         gfx_rect_t rect;
    98         gfx_rect_t inside;
    99 
    100         memset(&tgc, 0, sizeof(tgc));
    101         rc = gfx_context_new(&ops, &tgc, &gc);
    102         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    103 
    104         rect.p0.x = 10;
    105         rect.p0.y = 20;
    106         rect.p1.x = 30;
    107         rect.p1.y = 40;
    108 
    109         ui_paint_get_bevel_inside(gc, &rect, 2, &inside);
    110         PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
    111         PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
    112         PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
    113         PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
    114 
    115108        rc = gfx_context_delete(gc);
    116109        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     
    135128        PCUT_ASSERT_NOT_NULL(resource);
    136129
    137         rect.p0.x = 10;
    138         rect.p0.y = 20;
    139         rect.p1.x = 30;
    140         rect.p1.y = 40;
    141 
    142130        /* Paint inset frame with NULL 'inside' output parameter */
    143131        rc = ui_paint_inset_frame(resource, &rect, NULL);
     
    147135        rc = ui_paint_inset_frame(resource, &rect, &inside);
    148136        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    149 
    150         ui_resource_destroy(resource);
    151         rc = gfx_context_delete(gc);
    152         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    153 }
    154 
    155 /** Get inset frame inside */
    156 PCUT_TEST(get_inset_frame_inside)
    157 {
    158         errno_t rc;
    159         gfx_context_t *gc = NULL;
    160         ui_resource_t *resource = NULL;
    161         test_gc_t tgc;
    162         gfx_rect_t rect;
    163         gfx_rect_t inside;
    164 
    165         memset(&tgc, 0, sizeof(tgc));
    166         rc = gfx_context_new(&ops, &tgc, &gc);
    167         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    168 
    169         rc = ui_resource_create(gc, false, &resource);
    170         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    171         PCUT_ASSERT_NOT_NULL(resource);
    172 
    173         rect.p0.x = 10;
    174         rect.p0.y = 20;
    175         rect.p1.x = 30;
    176         rect.p1.y = 40;
    177 
    178         ui_paint_get_inset_frame_inside(resource, &rect, &inside);
    179         PCUT_ASSERT_INT_EQUALS(12, inside.p0.x);
    180         PCUT_ASSERT_INT_EQUALS(22, inside.p0.y);
    181         PCUT_ASSERT_INT_EQUALS(28, inside.p1.x);
    182         PCUT_ASSERT_INT_EQUALS(38, inside.p1.y);
    183137
    184138        ui_resource_destroy(resource);
     
    199153        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    200154
    201         center.x = 0;
    202         center.y = 0;
    203 
    204155        /* Paint filled circle / upper-left half */
    205156        rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_upleft);
     
    218169}
    219170
    220 /** Paint up pointing triangle */
    221 PCUT_TEST(up_triangle)
    222 {
    223         errno_t rc;
    224         gfx_context_t *gc = NULL;
    225         test_gc_t tgc;
    226         gfx_coord2_t center;
    227 
    228         memset(&tgc, 0, sizeof(tgc));
    229         rc = gfx_context_new(&ops, &tgc, &gc);
    230         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    231 
    232         center.x = 0;
    233         center.y = 0;
    234 
    235         rc = ui_paint_up_triangle(gc, &center, 5);
    236         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    237 
    238         rc = gfx_context_delete(gc);
    239         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    240 }
    241 
    242 /** Paint down pointing triangle */
    243 PCUT_TEST(down_triangle)
    244 {
    245         errno_t rc;
    246         gfx_context_t *gc = NULL;
    247         test_gc_t tgc;
    248         gfx_coord2_t center;
    249 
    250         memset(&tgc, 0, sizeof(tgc));
    251         rc = gfx_context_new(&ops, &tgc, &gc);
    252         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    253 
    254         center.x = 0;
    255         center.y = 0;
    256 
    257         rc = ui_paint_down_triangle(gc, &center, 5);
    258         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    259 
    260         rc = gfx_context_delete(gc);
    261         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    262 }
    263 
    264 /** Paint left pointing triangle */
    265 PCUT_TEST(left_triangle)
    266 {
    267         errno_t rc;
    268         gfx_context_t *gc = NULL;
    269         test_gc_t tgc;
    270         gfx_coord2_t center;
    271 
    272         memset(&tgc, 0, sizeof(tgc));
    273         rc = gfx_context_new(&ops, &tgc, &gc);
    274         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    275 
    276         center.x = 0;
    277         center.y = 0;
    278 
    279         rc = ui_paint_left_triangle(gc, &center, 5);
    280         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    281 
    282         rc = gfx_context_delete(gc);
    283         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    284 }
    285 
    286 /** Paint right pointing triangle */
    287 PCUT_TEST(right_triangle)
    288 {
    289         errno_t rc;
    290         gfx_context_t *gc = NULL;
    291         test_gc_t tgc;
    292         gfx_coord2_t center;
    293 
    294         memset(&tgc, 0, sizeof(tgc));
    295         rc = gfx_context_new(&ops, &tgc, &gc);
    296         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    297 
    298         center.x = 0;
    299         center.y = 0;
    300 
    301         rc = ui_paint_right_triangle(gc, &center, 5);
    302         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    303 
    304         rc = gfx_context_delete(gc);
    305         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    306 }
    307 
    308 /** Paint diagonal cross (X) */
    309 PCUT_TEST(cross)
    310 {
    311         errno_t rc;
    312         gfx_context_t *gc = NULL;
    313         test_gc_t tgc;
    314         gfx_coord2_t center;
    315 
    316         memset(&tgc, 0, sizeof(tgc));
    317         rc = gfx_context_new(&ops, &tgc, &gc);
    318         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    319 
    320         center.x = 0;
    321         center.y = 0;
    322 
    323         rc = ui_paint_cross(gc, &center, 5, 1, 2);
    324         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    325 
    326         rc = gfx_context_delete(gc);
    327         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    328 }
    329 
    330 /** Paint mimimize icon */
    331 PCUT_TEST(minicon)
    332 {
    333         errno_t rc;
    334         gfx_context_t *gc = NULL;
    335         ui_resource_t *resource = NULL;
    336         test_gc_t tgc;
    337         gfx_coord2_t center;
    338 
    339         memset(&tgc, 0, sizeof(tgc));
    340         rc = gfx_context_new(&ops, &tgc, &gc);
    341         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    342 
    343         rc = ui_resource_create(gc, false, &resource);
    344         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    345         PCUT_ASSERT_NOT_NULL(resource);
    346 
    347         center.x = 0;
    348         center.y = 0;
    349 
    350         rc = ui_paint_minicon(resource, &center, 8, 6);
    351         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    352 
    353         ui_resource_destroy(resource);
    354         rc = gfx_context_delete(gc);
    355         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    356 }
    357 
    358 /** Paint maximize icon */
    359 PCUT_TEST(maxicon)
    360 {
    361         errno_t rc;
    362         gfx_context_t *gc = NULL;
    363         ui_resource_t *resource = NULL;
    364         test_gc_t tgc;
    365         gfx_coord2_t center;
    366 
    367         memset(&tgc, 0, sizeof(tgc));
    368         rc = gfx_context_new(&ops, &tgc, &gc);
    369         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    370 
    371         rc = ui_resource_create(gc, false, &resource);
    372         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    373         PCUT_ASSERT_NOT_NULL(resource);
    374 
    375         center.x = 0;
    376         center.y = 0;
    377 
    378         rc = ui_paint_maxicon(resource, &center, 8, 6);
    379         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    380 
    381         ui_resource_destroy(resource);
    382         rc = gfx_context_delete(gc);
    383         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    384 }
    385 
    386 /** Paint unmaximize icon */
    387 PCUT_TEST(unmaxicon)
    388 {
    389         errno_t rc;
    390         gfx_context_t *gc = NULL;
    391         ui_resource_t *resource = NULL;
    392         test_gc_t tgc;
    393         gfx_coord2_t center;
    394 
    395         memset(&tgc, 0, sizeof(tgc));
    396         rc = gfx_context_new(&ops, &tgc, &gc);
    397         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    398 
    399         rc = ui_resource_create(gc, false, &resource);
    400         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    401         PCUT_ASSERT_NOT_NULL(resource);
    402 
    403         center.x = 0;
    404         center.y = 0;
    405 
    406         rc = ui_paint_unmaxicon(resource, &center, 8, 8, 3, 3);
    407         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    408 
    409         ui_resource_destroy(resource);
    410         rc = gfx_context_delete(gc);
    411         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    412 }
    413 
    414 /** Paint text box */
    415 PCUT_TEST(text_box)
    416 {
    417         errno_t rc;
    418         gfx_context_t *gc = NULL;
    419         ui_resource_t *resource = NULL;
    420         gfx_color_t *color = NULL;
    421         test_gc_t tgc;
    422         gfx_rect_t rect;
    423 
    424         memset(&tgc, 0, sizeof(tgc));
    425         rc = gfx_context_new(&ops, &tgc, &gc);
    426         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    427 
    428         rc = ui_resource_create(gc, false, &resource);
    429         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    430         PCUT_ASSERT_NOT_NULL(resource);
    431 
    432         rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
    433         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    434 
    435         rect.p0.x = 10;
    436         rect.p0.y = 20;
    437         rect.p1.x = 30;
    438         rect.p1.y = 40;
    439 
    440         /* Paint text box */
    441         rc = ui_paint_text_box(resource, &rect, ui_box_single, color);
    442         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    443 
    444         gfx_color_delete(color);
    445         ui_resource_destroy(resource);
    446         rc = gfx_context_delete(gc);
    447         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    448 }
    449 
    450 /** Paint custom text box */
    451 PCUT_TEST(text_box_custom)
    452 {
    453         errno_t rc;
    454         gfx_context_t *gc = NULL;
    455         ui_resource_t *resource = NULL;
    456         gfx_color_t *color = NULL;
    457         test_gc_t tgc;
    458         gfx_rect_t rect;
    459 
    460         memset(&tgc, 0, sizeof(tgc));
    461         rc = gfx_context_new(&ops, &tgc, &gc);
    462         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    463 
    464         rc = ui_resource_create(gc, false, &resource);
    465         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    466         PCUT_ASSERT_NOT_NULL(resource);
    467 
    468         rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
    469         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    470 
    471         rect.p0.x = 10;
    472         rect.p0.y = 20;
    473         rect.p1.x = 30;
    474         rect.p1.y = 40;
    475 
    476         /* Paint text box */
    477         rc = ui_paint_text_box_custom(resource, &rect, &test_box_chars, color);
    478         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    479 
    480         gfx_color_delete(color);
    481         ui_resource_destroy(resource);
    482         rc = gfx_context_delete(gc);
    483         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    484 }
    485 
    486 /** Paint text horizontal brace */
    487 PCUT_TEST(text_hbrace)
    488 {
    489         errno_t rc;
    490         gfx_context_t *gc = NULL;
    491         ui_resource_t *resource = NULL;
    492         gfx_color_t *color = NULL;
    493         test_gc_t tgc;
    494         gfx_rect_t rect;
    495 
    496         memset(&tgc, 0, sizeof(tgc));
    497         rc = gfx_context_new(&ops, &tgc, &gc);
    498         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    499 
    500         rc = ui_resource_create(gc, false, &resource);
    501         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    502         PCUT_ASSERT_NOT_NULL(resource);
    503 
    504         rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
    505         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    506 
    507         rect.p0.x = 10;
    508         rect.p0.y = 20;
    509         rect.p1.x = 30;
    510         rect.p1.y = 40;
    511 
    512         /* Paint text horizontal brace */
    513         rc = ui_paint_text_hbrace(resource, &rect, ui_box_single,
    514             color);
    515 
    516         gfx_color_delete(color);
    517         ui_resource_destroy(resource);
    518         rc = gfx_context_delete(gc);
    519         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    520 }
    521 
    522 /** Paint text rectangle */
    523 PCUT_TEST(text_rect)
    524 {
    525         errno_t rc;
    526         gfx_context_t *gc = NULL;
    527         ui_resource_t *resource = NULL;
    528         gfx_color_t *color = NULL;
    529         test_gc_t tgc;
    530         gfx_rect_t rect;
    531 
    532         memset(&tgc, 0, sizeof(tgc));
    533         rc = gfx_context_new(&ops, &tgc, &gc);
    534         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    535 
    536         rc = ui_resource_create(gc, false, &resource);
    537         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    538         PCUT_ASSERT_NOT_NULL(resource);
    539 
    540         rc = gfx_color_new_rgb_i16(1, 2, 3, &color);
    541         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    542 
    543         rect.p0.x = 10;
    544         rect.p0.y = 20;
    545         rect.p1.x = 30;
    546         rect.p1.y = 40;
    547 
    548         /* Paint text box */
    549         rc = ui_paint_text_rect(resource, &rect, color, "A");
    550         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    551 
    552         gfx_color_delete(color);
    553         ui_resource_destroy(resource);
    554         rc = gfx_context_delete(gc);
    555         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     171static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
     172{
     173        (void) arg;
     174        (void) rect;
     175        return EOK;
     176}
     177
     178static errno_t testgc_set_color(void *arg, gfx_color_t *color)
     179{
     180        (void) arg;
     181        (void) color;
     182        return EOK;
     183}
     184
     185static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
     186{
     187        (void) arg;
     188        (void) rect;
     189        return EOK;
     190}
     191
     192static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     193    gfx_bitmap_alloc_t *alloc, void **rbm)
     194{
     195        test_gc_t *tgc = (test_gc_t *) arg;
     196        testgc_bitmap_t *tbm;
     197
     198        tbm = calloc(1, sizeof(testgc_bitmap_t));
     199        if (tbm == NULL)
     200                return ENOMEM;
     201
     202        if (alloc == NULL) {
     203                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     204                    sizeof(uint32_t);
     205                tbm->alloc.off0 = 0;
     206                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     207                    (params->rect.p1.x - params->rect.p0.x) *
     208                    (params->rect.p1.y - params->rect.p0.y));
     209                tbm->myalloc = true;
     210                if (tbm->alloc.pixels == NULL) {
     211                        free(tbm);
     212                        return ENOMEM;
     213                }
     214        } else {
     215                tbm->alloc = *alloc;
     216        }
     217
     218        tbm->tgc = tgc;
     219        tgc->bm_created = true;
     220        tgc->bm_params = *params;
     221        tgc->bm_pixels = tbm->alloc.pixels;
     222        *rbm = (void *)tbm;
     223        return EOK;
     224}
     225
     226static errno_t testgc_bitmap_destroy(void *bm)
     227{
     228        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     229        if (tbm->myalloc)
     230                free(tbm->alloc.pixels);
     231        tbm->tgc->bm_destroyed = true;
     232        free(tbm);
     233        return EOK;
     234}
     235
     236static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     237    gfx_coord2_t *offs)
     238{
     239        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     240        tbm->tgc->bm_rendered = true;
     241        tbm->tgc->bm_srect = *srect;
     242        tbm->tgc->bm_offs = *offs;
     243        return EOK;
     244}
     245
     246static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     247{
     248        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     249        *alloc = tbm->alloc;
     250        tbm->tgc->bm_got_alloc = true;
     251        return EOK;
    556252}
    557253
Note: See TracChangeset for help on using the changeset viewer.