| 1 | /*
|
|---|
| 2 | * Copyright (c) 2021 Jiri Svoboda
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <gfx/color.h>
|
|---|
| 30 | #include <gfx/context.h>
|
|---|
| 31 | #include <gfx/render.h>
|
|---|
| 32 | #include <pcut/pcut.h>
|
|---|
| 33 | #include <mem.h>
|
|---|
| 34 | #include <stdbool.h>
|
|---|
| 35 |
|
|---|
| 36 | PCUT_INIT;
|
|---|
| 37 |
|
|---|
| 38 | PCUT_TEST_SUITE(render);
|
|---|
| 39 |
|
|---|
| 40 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
|---|
| 41 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
|---|
| 42 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
|---|
| 43 | static errno_t testgc_update(void *);
|
|---|
| 44 |
|
|---|
| 45 | static gfx_context_ops_t ops = {
|
|---|
| 46 | .set_clip_rect = testgc_set_clip_rect,
|
|---|
| 47 | .set_color = testgc_set_color,
|
|---|
| 48 | .fill_rect = testgc_fill_rect,
|
|---|
| 49 | .update = testgc_update
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | /** Test graphics context data */
|
|---|
| 53 | typedef 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;
|
|---|
| 61 | gfx_color_t *dclr;
|
|---|
| 62 |
|
|---|
| 63 | bool fill_rect;
|
|---|
| 64 | gfx_rect_t frect;
|
|---|
| 65 |
|
|---|
| 66 | bool update;
|
|---|
| 67 | } test_gc_t;
|
|---|
| 68 |
|
|---|
| 69 | /** Set clipping rectangle */
|
|---|
| 70 | PCUT_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 */
|
|---|
| 104 | PCUT_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 */
|
|---|
| 128 | PCUT_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 */
|
|---|
| 150 | PCUT_TEST(set_color)
|
|---|
| 151 | {
|
|---|
| 152 | errno_t rc;
|
|---|
| 153 | gfx_color_t *color;
|
|---|
| 154 | gfx_context_t *gc = NULL;
|
|---|
| 155 | uint16_t r, g, b;
|
|---|
| 156 | test_gc_t tgc;
|
|---|
| 157 |
|
|---|
| 158 | memset(&tgc, 0, sizeof(tgc));
|
|---|
| 159 |
|
|---|
| 160 | rc = gfx_context_new(&ops, &tgc, &gc);
|
|---|
| 161 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 162 |
|
|---|
| 163 | rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
|
|---|
| 164 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 165 |
|
|---|
| 166 | PCUT_ASSERT_FALSE(tgc.set_color);
|
|---|
| 167 |
|
|---|
| 168 | tgc.rc = EOK;
|
|---|
| 169 |
|
|---|
| 170 | rc = gfx_set_color(gc, color);
|
|---|
| 171 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 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 */
|
|---|
| 186 | PCUT_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);
|
|---|
| 207 |
|
|---|
| 208 | gfx_color_delete(color);
|
|---|
| 209 |
|
|---|
| 210 | rc = gfx_context_delete(gc);
|
|---|
| 211 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** Fill rectangle */
|
|---|
| 215 | PCUT_TEST(fill_rect)
|
|---|
| 216 | {
|
|---|
| 217 | errno_t rc;
|
|---|
| 218 | gfx_rect_t rect;
|
|---|
| 219 | gfx_context_t *gc = NULL;
|
|---|
| 220 | test_gc_t tgc;
|
|---|
| 221 |
|
|---|
| 222 | memset(&tgc, 0, sizeof(tgc));
|
|---|
| 223 |
|
|---|
| 224 | rc = gfx_context_new(&ops, &tgc, &gc);
|
|---|
| 225 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 226 |
|
|---|
| 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;
|
|---|
| 235 |
|
|---|
| 236 | rc = gfx_fill_rect(gc, &rect);
|
|---|
| 237 | 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 */
|
|---|
| 250 | PCUT_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 */
|
|---|
| 279 | PCUT_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 */
|
|---|
| 302 | PCUT_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 |
|
|---|
| 322 | static 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;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
|---|
| 338 | {
|
|---|
| 339 | test_gc_t *tgc = (test_gc_t *) arg;
|
|---|
| 340 |
|
|---|
| 341 | tgc->set_color = true;
|
|---|
| 342 |
|
|---|
| 343 | /* Technically we should copy the data */
|
|---|
| 344 | tgc->dclr = color;
|
|---|
| 345 | return tgc->rc;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
|---|
| 349 | {
|
|---|
| 350 | test_gc_t *tgc = (test_gc_t *) arg;
|
|---|
| 351 |
|
|---|
| 352 | tgc->fill_rect = true;
|
|---|
| 353 | tgc->frect = *rect;
|
|---|
| 354 | return tgc->rc;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | static 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;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | PCUT_EXPORT(render);
|
|---|