Ignore:
File:
1 edited

Legend:

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

    rde9992c r1fa6292  
    11/*
    2  * Copyright (c) 2020 Jiri Svoboda
     2 * Copyright (c) 2023 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3333#include <stdbool.h>
    3434#include <ui/paint.h>
     35#include <ui/resource.h>
     36#include "../private/testgc.h"
    3537
    3638PCUT_INIT;
     
    3840PCUT_TEST_SUITE(paint);
    3941
    40 static errno_t testgc_set_color(void *, gfx_color_t *);
    41 static errno_t testgc_fill_rect(void *, gfx_rect_t *);
    42 static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
    43     gfx_bitmap_alloc_t *, void **);
    44 static errno_t testgc_bitmap_destroy(void *);
    45 static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
    46 static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    47 
    48 static gfx_context_ops_t ops = {
    49         .set_color = testgc_set_color,
    50         .fill_rect = testgc_fill_rect,
    51         .bitmap_create = testgc_bitmap_create,
    52         .bitmap_destroy = testgc_bitmap_destroy,
    53         .bitmap_render = testgc_bitmap_render,
    54         .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        }
    5549};
    56 
    57 typedef struct {
    58         bool bm_created;
    59         bool bm_destroyed;
    60         gfx_bitmap_params_t bm_params;
    61         void *bm_pixels;
    62         gfx_rect_t bm_srect;
    63         gfx_coord2_t bm_offs;
    64         bool bm_rendered;
    65         bool bm_got_alloc;
    66 } test_gc_t;
    67 
    68 typedef struct {
    69         test_gc_t *tgc;
    70         gfx_bitmap_alloc_t alloc;
    71         bool myalloc;
    72 } testgc_bitmap_t;
    7350
    7451/** Paint bevel */
     
    9370        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    9471
     72        rect.p0.x = 10;
     73        rect.p0.y = 20;
     74        rect.p1.x = 30;
     75        rect.p1.y = 40;
     76
    9577        /* Paint bevel with NULL 'inside' output parameter */
    9678        rc = ui_paint_bevel(gc, &rect, color1, color2, 2, NULL);
     
    10789}
    10890
    109 static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    110 {
    111         (void) arg;
    112         (void) color;
    113         return EOK;
    114 }
    115 
    116 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
    117 {
    118         (void) arg;
    119         (void) rect;
    120         return EOK;
    121 }
    122 
    123 static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
    124     gfx_bitmap_alloc_t *alloc, void **rbm)
    125 {
    126         test_gc_t *tgc = (test_gc_t *) arg;
    127         testgc_bitmap_t *tbm;
    128 
    129         tbm = calloc(1, sizeof(testgc_bitmap_t));
    130         if (tbm == NULL)
    131                 return ENOMEM;
    132 
    133         if (alloc == NULL) {
    134                 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
    135                     sizeof(uint32_t);
    136                 tbm->alloc.off0 = 0;
    137                 tbm->alloc.pixels = calloc(sizeof(uint32_t),
    138                     (params->rect.p1.x - params->rect.p0.x) *
    139                     (params->rect.p1.y - params->rect.p0.y));
    140                 tbm->myalloc = true;
    141                 if (tbm->alloc.pixels == NULL) {
    142                         free(tbm);
    143                         return ENOMEM;
    144                 }
    145         } else {
    146                 tbm->alloc = *alloc;
    147         }
    148 
    149         tbm->tgc = tgc;
    150         tgc->bm_created = true;
    151         tgc->bm_params = *params;
    152         tgc->bm_pixels = tbm->alloc.pixels;
    153         *rbm = (void *)tbm;
    154         return EOK;
    155 }
    156 
    157 static errno_t testgc_bitmap_destroy(void *bm)
    158 {
    159         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    160         if (tbm->myalloc)
    161                 free(tbm->alloc.pixels);
    162         tbm->tgc->bm_destroyed = true;
    163         free(tbm);
    164         return EOK;
    165 }
    166 
    167 static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
    168     gfx_coord2_t *offs)
    169 {
    170         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    171         tbm->tgc->bm_rendered = true;
    172         tbm->tgc->bm_srect = *srect;
    173         tbm->tgc->bm_offs = *offs;
    174         return EOK;
    175 }
    176 
    177 static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
    178 {
    179         testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
    180         *alloc = tbm->alloc;
    181         tbm->tgc->bm_got_alloc = true;
    182         return EOK;
     91/** Get bevel inside */
     92PCUT_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
     115        rc = gfx_context_delete(gc);
     116        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     117}
     118
     119/** Paint inset frame */
     120PCUT_TEST(inset_frame)
     121{
     122        errno_t rc;
     123        gfx_context_t *gc = NULL;
     124        ui_resource_t *resource = NULL;
     125        test_gc_t tgc;
     126        gfx_rect_t rect;
     127        gfx_rect_t inside;
     128
     129        memset(&tgc, 0, sizeof(tgc));
     130        rc = gfx_context_new(&ops, &tgc, &gc);
     131        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     132
     133        rc = ui_resource_create(gc, false, &resource);
     134        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     135        PCUT_ASSERT_NOT_NULL(resource);
     136
     137        rect.p0.x = 10;
     138        rect.p0.y = 20;
     139        rect.p1.x = 30;
     140        rect.p1.y = 40;
     141
     142        /* Paint inset frame with NULL 'inside' output parameter */
     143        rc = ui_paint_inset_frame(resource, &rect, NULL);
     144        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     145
     146        /* Paint inset frame with valid 'inside' output parameter */
     147        rc = ui_paint_inset_frame(resource, &rect, &inside);
     148        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 */
     156PCUT_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);
     183
     184        ui_resource_destroy(resource);
     185        rc = gfx_context_delete(gc);
     186        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     187}
     188
     189/** Paint filled circle */
     190PCUT_TEST(filled_circle)
     191{
     192        errno_t rc;
     193        gfx_context_t *gc = NULL;
     194        test_gc_t tgc;
     195        gfx_coord2_t center;
     196
     197        memset(&tgc, 0, sizeof(tgc));
     198        rc = gfx_context_new(&ops, &tgc, &gc);
     199        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     200
     201        center.x = 0;
     202        center.y = 0;
     203
     204        /* Paint filled circle / upper-left half */
     205        rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_upleft);
     206        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     207
     208        /* Paint filled circle / lower-right half */
     209        rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_lowright);
     210        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     211
     212        /* Paint entire filled circle */
     213        rc = ui_paint_filled_circle(gc, &center, 10, ui_fcircle_entire);
     214        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     215
     216        rc = gfx_context_delete(gc);
     217        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     218}
     219
     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
     414/** Paint text box */
     415PCUT_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 */
     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);
     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 */
     487PCUT_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 */
     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);
    183556}
    184557
Note: See TracChangeset for help on using the changeset viewer.