[ba09d06] | 1 | /*
|
---|
[f0ccb2ab] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[ba09d06] | 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/context.h>
|
---|
| 30 | #include <gfx/coord.h>
|
---|
| 31 | #include <mem.h>
|
---|
| 32 | #include <pcut/pcut.h>
|
---|
| 33 | #include <stdbool.h>
|
---|
[b71c0fc] | 34 | #include <ui/control.h>
|
---|
[ba09d06] | 35 | #include <ui/label.h>
|
---|
[3583ffb] | 36 | #include <ui/resource.h>
|
---|
[ba09d06] | 37 | #include "../private/label.h"
|
---|
| 38 |
|
---|
| 39 | PCUT_INIT;
|
---|
| 40 |
|
---|
| 41 | PCUT_TEST_SUITE(label);
|
---|
| 42 |
|
---|
[7470d97] | 43 | static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
|
---|
[3583ffb] | 44 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
| 45 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
[f0ccb2ab] | 46 | static errno_t testgc_update(void *);
|
---|
[3583ffb] | 47 | static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
|
---|
| 48 | gfx_bitmap_alloc_t *, void **);
|
---|
| 49 | static errno_t testgc_bitmap_destroy(void *);
|
---|
| 50 | static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
|
---|
| 51 | static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
|
---|
| 52 |
|
---|
| 53 | static gfx_context_ops_t ops = {
|
---|
[7470d97] | 54 | .set_clip_rect = testgc_set_clip_rect,
|
---|
[3583ffb] | 55 | .set_color = testgc_set_color,
|
---|
| 56 | .fill_rect = testgc_fill_rect,
|
---|
[f0ccb2ab] | 57 | .update = testgc_update,
|
---|
[3583ffb] | 58 | .bitmap_create = testgc_bitmap_create,
|
---|
| 59 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
| 60 | .bitmap_render = testgc_bitmap_render,
|
---|
| 61 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | typedef struct {
|
---|
| 65 | bool bm_created;
|
---|
| 66 | bool bm_destroyed;
|
---|
| 67 | gfx_bitmap_params_t bm_params;
|
---|
| 68 | void *bm_pixels;
|
---|
| 69 | gfx_rect_t bm_srect;
|
---|
| 70 | gfx_coord2_t bm_offs;
|
---|
| 71 | bool bm_rendered;
|
---|
| 72 | bool bm_got_alloc;
|
---|
| 73 | } test_gc_t;
|
---|
| 74 |
|
---|
| 75 | typedef struct {
|
---|
| 76 | test_gc_t *tgc;
|
---|
| 77 | gfx_bitmap_alloc_t alloc;
|
---|
| 78 | bool myalloc;
|
---|
| 79 | } testgc_bitmap_t;
|
---|
| 80 |
|
---|
[ba09d06] | 81 | typedef struct {
|
---|
| 82 | bool clicked;
|
---|
| 83 | } test_cb_resp_t;
|
---|
| 84 |
|
---|
[f93e4e3] | 85 | /** Create and destroy label */
|
---|
[ba09d06] | 86 | PCUT_TEST(create_destroy)
|
---|
| 87 | {
|
---|
| 88 | ui_label_t *label = NULL;
|
---|
| 89 | errno_t rc;
|
---|
| 90 |
|
---|
| 91 | rc = ui_label_create(NULL, "Hello", &label);
|
---|
| 92 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 93 | PCUT_ASSERT_NOT_NULL(label);
|
---|
| 94 |
|
---|
| 95 | ui_label_destroy(label);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /** ui_label_destroy() can take NULL argument (no-op) */
|
---|
| 99 | PCUT_TEST(destroy_null)
|
---|
| 100 | {
|
---|
| 101 | ui_label_destroy(NULL);
|
---|
| 102 | }
|
---|
| 103 |
|
---|
[b71c0fc] | 104 | /** ui_label_ctl() returns control that has a working virtual destructor */
|
---|
| 105 | PCUT_TEST(ctl)
|
---|
| 106 | {
|
---|
| 107 | ui_label_t *label;
|
---|
| 108 | ui_control_t *control;
|
---|
| 109 | errno_t rc;
|
---|
| 110 |
|
---|
| 111 | rc = ui_label_create(NULL, "Hello", &label);
|
---|
| 112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 113 |
|
---|
| 114 | control = ui_label_ctl(label);
|
---|
| 115 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 116 |
|
---|
| 117 | ui_control_destroy(control);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[f93e4e3] | 120 | /** Set label rectangle sets internal field */
|
---|
[ba09d06] | 121 | PCUT_TEST(set_rect)
|
---|
| 122 | {
|
---|
| 123 | ui_label_t *label;
|
---|
| 124 | gfx_rect_t rect;
|
---|
| 125 | errno_t rc;
|
---|
| 126 |
|
---|
| 127 | rc = ui_label_create(NULL, "Hello", &label);
|
---|
| 128 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 129 |
|
---|
| 130 | rect.p0.x = 1;
|
---|
| 131 | rect.p0.y = 2;
|
---|
| 132 | rect.p1.x = 3;
|
---|
| 133 | rect.p1.y = 4;
|
---|
| 134 |
|
---|
| 135 | ui_label_set_rect(label, &rect);
|
---|
| 136 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
|
---|
| 137 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
|
---|
| 138 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
|
---|
| 139 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
|
---|
| 140 |
|
---|
| 141 | ui_label_destroy(label);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[f93e4e3] | 144 | /** Set label text horizontal alignment sets internal field */
|
---|
[58a67050] | 145 | PCUT_TEST(set_halign)
|
---|
| 146 | {
|
---|
| 147 | ui_label_t *label;
|
---|
| 148 | errno_t rc;
|
---|
| 149 |
|
---|
| 150 | rc = ui_label_create(NULL, "Hello", &label);
|
---|
| 151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 152 |
|
---|
| 153 | ui_label_set_halign(label, gfx_halign_left);
|
---|
| 154 | PCUT_ASSERT_EQUALS(gfx_halign_left, label->halign);
|
---|
| 155 | ui_label_set_halign(label, gfx_halign_center);
|
---|
| 156 | PCUT_ASSERT_EQUALS(gfx_halign_center, label->halign);
|
---|
| 157 |
|
---|
| 158 | ui_label_destroy(label);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[f93e4e3] | 161 | /** Set label rectangle sets internal field */
|
---|
[ba09d06] | 162 | PCUT_TEST(set_text)
|
---|
| 163 | {
|
---|
| 164 | ui_label_t *label;
|
---|
| 165 | gfx_rect_t rect;
|
---|
| 166 | errno_t rc;
|
---|
| 167 |
|
---|
| 168 | rc = ui_label_create(NULL, "Hello", &label);
|
---|
| 169 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 170 |
|
---|
| 171 | rect.p0.x = 1;
|
---|
| 172 | rect.p0.y = 2;
|
---|
| 173 | rect.p1.x = 3;
|
---|
| 174 | rect.p1.y = 4;
|
---|
| 175 |
|
---|
| 176 | ui_label_set_rect(label, &rect);
|
---|
| 177 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, label->rect.p0.x);
|
---|
| 178 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, label->rect.p0.y);
|
---|
| 179 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, label->rect.p1.x);
|
---|
| 180 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, label->rect.p1.y);
|
---|
| 181 |
|
---|
| 182 | ui_label_destroy(label);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[f93e4e3] | 185 | /** Paint label */
|
---|
[ba09d06] | 186 | PCUT_TEST(paint)
|
---|
| 187 | {
|
---|
| 188 | errno_t rc;
|
---|
[3583ffb] | 189 | gfx_context_t *gc = NULL;
|
---|
| 190 | test_gc_t tgc;
|
---|
| 191 | ui_resource_t *resource = NULL;
|
---|
[ba09d06] | 192 | ui_label_t *label;
|
---|
| 193 |
|
---|
[3583ffb] | 194 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 195 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 196 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 197 |
|
---|
[9c7dc8e] | 198 | rc = ui_resource_create(gc, false, &resource);
|
---|
[ba09d06] | 199 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 200 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[ba09d06] | 201 |
|
---|
[3583ffb] | 202 | rc = ui_label_create(resource, "Hello", &label);
|
---|
[ba09d06] | 203 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 204 |
|
---|
| 205 | rc = ui_label_paint(label);
|
---|
| 206 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 207 |
|
---|
| 208 | ui_label_destroy(label);
|
---|
[3583ffb] | 209 | ui_resource_destroy(resource);
|
---|
| 210 |
|
---|
| 211 | rc = gfx_context_delete(gc);
|
---|
| 212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[7470d97] | 215 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
| 216 | {
|
---|
| 217 | (void) arg;
|
---|
| 218 | (void) rect;
|
---|
| 219 | return EOK;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[3583ffb] | 222 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
| 223 | {
|
---|
| 224 | (void) arg;
|
---|
| 225 | (void) color;
|
---|
| 226 | return EOK;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 230 | {
|
---|
| 231 | (void) arg;
|
---|
| 232 | (void) rect;
|
---|
| 233 | return EOK;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[f0ccb2ab] | 236 | static errno_t testgc_update(void *arg)
|
---|
| 237 | {
|
---|
| 238 | (void) arg;
|
---|
| 239 | return EOK;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[3583ffb] | 242 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 243 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 244 | {
|
---|
| 245 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 246 | testgc_bitmap_t *tbm;
|
---|
| 247 |
|
---|
| 248 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 249 | if (tbm == NULL)
|
---|
| 250 | return ENOMEM;
|
---|
| 251 |
|
---|
| 252 | if (alloc == NULL) {
|
---|
| 253 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 254 | sizeof(uint32_t);
|
---|
| 255 | tbm->alloc.off0 = 0;
|
---|
| 256 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
| 257 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 258 | (params->rect.p1.y - params->rect.p0.y));
|
---|
| 259 | tbm->myalloc = true;
|
---|
| 260 | if (tbm->alloc.pixels == NULL) {
|
---|
| 261 | free(tbm);
|
---|
| 262 | return ENOMEM;
|
---|
| 263 | }
|
---|
| 264 | } else {
|
---|
| 265 | tbm->alloc = *alloc;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | tbm->tgc = tgc;
|
---|
| 269 | tgc->bm_created = true;
|
---|
| 270 | tgc->bm_params = *params;
|
---|
| 271 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 272 | *rbm = (void *)tbm;
|
---|
| 273 | return EOK;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 277 | {
|
---|
| 278 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 279 | if (tbm->myalloc)
|
---|
| 280 | free(tbm->alloc.pixels);
|
---|
| 281 | tbm->tgc->bm_destroyed = true;
|
---|
| 282 | free(tbm);
|
---|
| 283 | return EOK;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 287 | gfx_coord2_t *offs)
|
---|
| 288 | {
|
---|
| 289 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 290 | tbm->tgc->bm_rendered = true;
|
---|
| 291 | tbm->tgc->bm_srect = *srect;
|
---|
| 292 | tbm->tgc->bm_offs = *offs;
|
---|
| 293 | return EOK;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 297 | {
|
---|
| 298 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 299 | *alloc = tbm->alloc;
|
---|
| 300 | tbm->tgc->bm_got_alloc = true;
|
---|
| 301 | return EOK;
|
---|
[ba09d06] | 302 | }
|
---|
| 303 |
|
---|
| 304 | PCUT_EXPORT(label);
|
---|