Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfx/test/render.c

    r9259d20 r7470d97  
    11/*
    2  * Copyright (c) 2019 Jiri Svoboda
     2 * Copyright (c) 2021 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3232#include <pcut/pcut.h>
    3333#include <mem.h>
     34#include <stdbool.h>
    3435
    3536PCUT_INIT;
     
    3738PCUT_TEST_SUITE(render);
    3839
     40static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
    3941static errno_t testgc_set_color(void *, gfx_color_t *);
    4042static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     43static errno_t testgc_update(void *);
    4144
    4245static gfx_context_ops_t ops = {
     46        .set_clip_rect = testgc_set_clip_rect,
    4347        .set_color = testgc_set_color,
    44         .fill_rect = testgc_fill_rect
     48        .fill_rect = testgc_fill_rect,
     49        .update = testgc_update
    4550};
    4651
    4752/** Test graphics context data */
    4853typedef struct {
     54        errno_t rc;
     55
     56        bool set_clip_rect;
     57        gfx_rect_t crect;
     58        bool do_clip;
     59
     60        bool set_color;
    4961        gfx_color_t *dclr;
    50         gfx_rect_t *rect;
     62
     63        bool fill_rect;
     64        gfx_rect_t frect;
     65
     66        bool update;
    5167} test_gc_t;
    5268
     69/** Set clipping rectangle */
     70PCUT_TEST(set_clip_rect)
     71{
     72        errno_t rc;
     73        gfx_rect_t rect;
     74        gfx_context_t *gc = NULL;
     75        test_gc_t tgc;
     76
     77        memset(&tgc, 0, sizeof(tgc));
     78
     79        rc = gfx_context_new(&ops, &tgc, &gc);
     80        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     81
     82        rect.p0.x = 1;
     83        rect.p0.y = 2;
     84        rect.p1.x = 3;
     85        rect.p1.y = 4;
     86
     87        tgc.rc = EOK;
     88
     89        rc = gfx_set_clip_rect(gc, &rect);
     90        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     91
     92        PCUT_ASSERT_TRUE(tgc.set_clip_rect);
     93        PCUT_ASSERT_TRUE(tgc.do_clip);
     94        PCUT_ASSERT_INT_EQUALS(rect.p0.x, tgc.crect.p0.x);
     95        PCUT_ASSERT_INT_EQUALS(rect.p0.y, tgc.crect.p0.y);
     96        PCUT_ASSERT_INT_EQUALS(rect.p1.x, tgc.crect.p1.x);
     97        PCUT_ASSERT_INT_EQUALS(rect.p1.y, tgc.crect.p1.y);
     98
     99        rc = gfx_context_delete(gc);
     100        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     101}
     102
     103/** Set null clipping rectangle */
     104PCUT_TEST(set_clip_rect_null)
     105{
     106        errno_t rc;
     107        gfx_context_t *gc = NULL;
     108        test_gc_t tgc;
     109
     110        memset(&tgc, 0, sizeof(tgc));
     111
     112        rc = gfx_context_new(&ops, &tgc, &gc);
     113        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     114
     115        tgc.rc = EOK;
     116
     117        rc = gfx_set_clip_rect(gc, NULL);
     118        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     119
     120        PCUT_ASSERT_TRUE(tgc.set_clip_rect);
     121        PCUT_ASSERT_FALSE(tgc.do_clip);
     122
     123        rc = gfx_context_delete(gc);
     124        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     125}
     126
     127/** Set clipping rectangle with error return */
     128PCUT_TEST(set_clip_rect_failure)
     129{
     130        errno_t rc;
     131        gfx_rect_t rect;
     132        gfx_context_t *gc = NULL;
     133        test_gc_t tgc;
     134
     135        memset(&tgc, 0, sizeof(tgc));
     136
     137        rc = gfx_context_new(&ops, &tgc, &gc);
     138        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     139
     140        tgc.rc = EIO;
     141
     142        rc = gfx_set_clip_rect(gc, &rect);
     143        PCUT_ASSERT_ERRNO_VAL(EIO, rc);
     144
     145        rc = gfx_context_delete(gc);
     146        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     147}
     148
     149/** Set drawing color */
    53150PCUT_TEST(set_color)
    54151{
     
    56153        gfx_color_t *color;
    57154        gfx_context_t *gc = NULL;
     155        uint16_t r, g, b;
    58156        test_gc_t tgc;
    59157
     
    66164        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    67165
     166        PCUT_ASSERT_FALSE(tgc.set_color);
     167
     168        tgc.rc = EOK;
     169
    68170        rc = gfx_set_color(gc, color);
    69171        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    70         PCUT_ASSERT_EQUALS(color, tgc.dclr);
    71         PCUT_ASSERT_NULL(tgc.rect);
     172
     173        PCUT_ASSERT_TRUE(tgc.set_color);
     174
     175        gfx_color_get_rgb_i16(tgc.dclr, &r, &g, &b);
     176
     177        PCUT_ASSERT_INT_EQUALS(0xffff, r);
     178        PCUT_ASSERT_INT_EQUALS(0xffff, g);
     179        PCUT_ASSERT_INT_EQUALS(0xffff, b);
     180
     181        rc = gfx_context_delete(gc);
     182        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     183}
     184
     185/** Set drawing color with error return */
     186PCUT_TEST(set_color_failure)
     187{
     188        errno_t rc;
     189        gfx_color_t *color;
     190        gfx_context_t *gc = NULL;
     191        test_gc_t tgc;
     192
     193        memset(&tgc, 0, sizeof(tgc));
     194
     195        rc = gfx_context_new(&ops, &tgc, &gc);
     196        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     197
     198        rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     199        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     200
     201        PCUT_ASSERT_FALSE(tgc.set_color);
     202
     203        tgc.rc = EIO;
     204
     205        rc = gfx_set_color(gc, color);
     206        PCUT_ASSERT_ERRNO_VAL(EIO, rc);
    72207
    73208        gfx_color_delete(color);
     
    77212}
    78213
     214/** Fill rectangle */
    79215PCUT_TEST(fill_rect)
    80216{
    81217        errno_t rc;
    82         gfx_color_t *color;
    83218        gfx_rect_t rect;
    84219        gfx_context_t *gc = NULL;
     
    90225        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    91226
    92         rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
    93         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    94 
    95         rc = gfx_set_color(gc, color);
    96         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    97         PCUT_ASSERT_EQUALS(color, tgc.dclr);
     227        rect.p0.x = 1;
     228        rect.p0.y = 2;
     229        rect.p1.x = 3;
     230        rect.p1.y = 4;
     231
     232        PCUT_ASSERT_FALSE(tgc.fill_rect);
     233
     234        tgc.rc = EOK;
    98235
    99236        rc = gfx_fill_rect(gc, &rect);
    100237        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    101         PCUT_ASSERT_EQUALS(&rect, tgc.rect);
    102 
    103         gfx_color_delete(color);
    104 
    105         rc = gfx_context_delete(gc);
    106         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     238
     239        PCUT_ASSERT_TRUE(tgc.fill_rect);
     240        PCUT_ASSERT_INT_EQUALS(rect.p0.x, tgc.frect.p0.x);
     241        PCUT_ASSERT_INT_EQUALS(rect.p0.y, tgc.frect.p0.y);
     242        PCUT_ASSERT_INT_EQUALS(rect.p1.x, tgc.frect.p1.x);
     243        PCUT_ASSERT_INT_EQUALS(rect.p1.y, tgc.frect.p1.y);
     244
     245        rc = gfx_context_delete(gc);
     246        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     247}
     248
     249/** Fill rectangle with error return */
     250PCUT_TEST(fill_rect_failure)
     251{
     252        errno_t rc;
     253        gfx_rect_t rect;
     254        gfx_context_t *gc = NULL;
     255        test_gc_t tgc;
     256
     257        memset(&tgc, 0, sizeof(tgc));
     258
     259        rc = gfx_context_new(&ops, &tgc, &gc);
     260        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     261
     262        rect.p0.x = 1;
     263        rect.p0.y = 2;
     264        rect.p1.x = 3;
     265        rect.p1.y = 4;
     266
     267        PCUT_ASSERT_FALSE(tgc.fill_rect);
     268
     269        tgc.rc = EIO;
     270
     271        rc = gfx_fill_rect(gc, &rect);
     272        PCUT_ASSERT_ERRNO_VAL(EIO, rc);
     273
     274        rc = gfx_context_delete(gc);
     275        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     276}
     277
     278/** Update GC */
     279PCUT_TEST(update)
     280{
     281        errno_t rc;
     282        gfx_context_t *gc = NULL;
     283        test_gc_t tgc;
     284
     285        memset(&tgc, 0, sizeof(tgc));
     286
     287        rc = gfx_context_new(&ops, &tgc, &gc);
     288        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     289
     290        tgc.rc = EOK;
     291
     292        PCUT_ASSERT_FALSE(tgc.update);
     293        rc = gfx_update(gc);
     294        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     295        PCUT_ASSERT_TRUE(tgc.update);
     296
     297        rc = gfx_context_delete(gc);
     298        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     299}
     300
     301/** Update GC with error return */
     302PCUT_TEST(update_failure)
     303{
     304        errno_t rc;
     305        gfx_context_t *gc = NULL;
     306        test_gc_t tgc;
     307
     308        memset(&tgc, 0, sizeof(tgc));
     309
     310        rc = gfx_context_new(&ops, &tgc, &gc);
     311        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     312
     313        tgc.rc = EIO;
     314        rc = gfx_update(gc);
     315
     316        PCUT_ASSERT_ERRNO_VAL(EIO, rc);
     317
     318        rc = gfx_context_delete(gc);
     319        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     320}
     321
     322static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
     323{
     324        test_gc_t *tgc = (test_gc_t *) arg;
     325
     326        tgc->set_clip_rect = true;
     327        if (rect != NULL) {
     328                tgc->do_clip = true;
     329                tgc->crect = *rect;
     330        } else {
     331                tgc->do_clip = false;
     332        }
     333
     334        return tgc->rc;
    107335}
    108336
     
    110338{
    111339        test_gc_t *tgc = (test_gc_t *) arg;
     340
     341        tgc->set_color = true;
    112342
    113343        /* Technically we should copy the data */
    114344        tgc->dclr = color;
    115         return EOK;
     345        return tgc->rc;
    116346}
    117347
     
    120350        test_gc_t *tgc = (test_gc_t *) arg;
    121351
    122         /* Technically we should copy the data */
    123         tgc->rect = rect;
    124         return EOK;
     352        tgc->fill_rect = true;
     353        tgc->frect = *rect;
     354        return tgc->rc;
     355}
     356
     357static errno_t testgc_update(void *arg)
     358{
     359        test_gc_t *tgc = (test_gc_t *) arg;
     360
     361        tgc->update = true;
     362        return tgc->rc;
    125363}
    126364
Note: See TracChangeset for help on using the changeset viewer.