Ignore:
File:
1 edited

Legend:

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

    r81ec7e1 r1fa6292  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2023 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3434#include <ui/paint.h>
    3535#include <ui/resource.h>
     36#include "../private/testgc.h"
    3637
    3738PCUT_INIT;
     
    3940PCUT_TEST_SUITE(paint);
    4041
    41 static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
    42 static errno_t testgc_set_color(void *, gfx_color_t *);
    43 static errno_t testgc_fill_rect(void *, gfx_rect_t *);
    44 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
    45     gfx_bitmap_alloc_t *, void **);
    46 static errno_t testgc_bitmap_destroy(void *);
    47 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
    48 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    49 
    50 static 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
     42/** Test box characters */
     43static ui_box_chars_t test_box_chars = {
     44        {
     45                { "A", "B", "C" },
     46                { "D", " ", "E" },
     47                { "F", "G", "H" }
     48        }
    5849};
    59 
    60 typedef 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 
    71 typedef struct {
    72         test_gc_t *tgc;
    73         gfx_bitmap_alloc_t alloc;
    74         bool myalloc;
    75 } testgc_bitmap_t;
    7650
    7751/** Paint bevel */
     
    225199        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    226200
     201        center.x = 0;
     202        center.y = 0;
     203
    227204        /* Paint filled circle / upper-left half */
    228205        rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_upleft);
     
    241218}
    242219
     220/** Paint up pointing triangle */
     221PCUT_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 */
     243PCUT_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 */
     265PCUT_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 */
     287PCUT_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) */
     309PCUT_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 */
     331PCUT_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 */
     359PCUT_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 */
     387PCUT_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
    243414/** Paint text box */
    244415PCUT_TEST(text_box)
     
    269440        /* Paint text box */
    270441        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 */
     451PCUT_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);
    271478        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    272479
     
    313520}
    314521
    315 static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
    316 {
    317         (void) arg;
    318         (void) rect;
    319         return EOK;
    320 }
    321 
    322 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    323 {
    324         (void) arg;
    325         (void) color;
    326         return EOK;
    327 }
    328 
    329 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    330 {
    331         (void) arg;
    332         (void) rect;
    333         return EOK;
    334 }
    335 
    336 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    337     gfx_bitmap_alloc_t *alloc, void **rbm)
    338 {
    339         test_gc_t *tgc = (test_gc_t *) arg;
    340         testgc_bitmap_t *tbm;
    341 
    342         tbm = calloc(1, sizeof(testgc_bitmap_t));
    343         if (tbm == NULL)
    344                 return ENOMEM;
    345 
    346         if (alloc == NULL) {
    347                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    348                     sizeof(uint32_t);
    349                 tbm->alloc.off0 = 0;
    350                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    351                     (params->rect.p1.x - params->rect.p0.x) *
    352                     (params->rect.p1.y - params->rect.p0.y));
    353                 tbm->myalloc = true;
    354                 if (tbm->alloc.pixels == NULL) {
    355                         free(tbm);
    356                         return ENOMEM;
    357                 }
    358         } else {
    359                 tbm->alloc = *alloc;
    360         }
    361 
    362         tbm->tgc = tgc;
    363         tgc->bm_created = true;
    364         tgc->bm_params = *params;
    365         tgc->bm_pixels = tbm->alloc.pixels;
    366         *rbm = (void *)tbm;
    367         return EOK;
    368 }
    369 
    370 static errno_t testgc_bitmap_destroy(void *bm)
    371 {
    372         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    373         if (tbm->myalloc)
    374                 free(tbm->alloc.pixels);
    375         tbm->tgc->bm_destroyed = true;
    376         free(tbm);
    377         return EOK;
    378 }
    379 
    380 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    381     gfx_coord2_t *offs)
    382 {
    383         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    384         tbm->tgc->bm_rendered = true;
    385         tbm->tgc->bm_srect = *srect;
    386         tbm->tgc->bm_offs = *offs;
    387         return EOK;
    388 }
    389 
    390 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    391 {
    392         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    393         *alloc = tbm->alloc;
    394         tbm->tgc->bm_got_alloc = true;
    395         return EOK;
     522/** Paint text rectangle */
     523PCUT_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);
    396556}
    397557
Note: See TracChangeset for help on using the changeset viewer.