[f80690a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2020 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 |
|
---|
[4ed00d3] | 29 | #include <gfx/context.h>
|
---|
| 30 | #include <gfx/coord.h>
|
---|
| 31 | #include <mem.h>
|
---|
[f80690a] | 32 | #include <pcut/pcut.h>
|
---|
[4ed00d3] | 33 | #include <stdbool.h>
|
---|
[f80690a] | 34 | #include <ui/pbutton.h>
|
---|
[4ed00d3] | 35 | #include <ui/resource.h>
|
---|
| 36 | #include "../private/pbutton.h"
|
---|
[f80690a] | 37 |
|
---|
| 38 | PCUT_INIT;
|
---|
| 39 |
|
---|
| 40 | PCUT_TEST_SUITE(pbutton);
|
---|
| 41 |
|
---|
[4ed00d3] | 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_color = testgc_set_color,
|
---|
| 52 | .fill_rect = testgc_fill_rect,
|
---|
| 53 | .bitmap_create = testgc_bitmap_create,
|
---|
| 54 | .bitmap_destroy = testgc_bitmap_destroy,
|
---|
| 55 | .bitmap_render = testgc_bitmap_render,
|
---|
| 56 | .bitmap_get_alloc = testgc_bitmap_get_alloc
|
---|
| 57 | };
|
---|
| 58 |
|
---|
| 59 | typedef struct {
|
---|
| 60 | bool bm_created;
|
---|
| 61 | bool bm_destroyed;
|
---|
| 62 | gfx_bitmap_params_t bm_params;
|
---|
| 63 | void *bm_pixels;
|
---|
| 64 | gfx_rect_t bm_srect;
|
---|
| 65 | gfx_coord2_t bm_offs;
|
---|
| 66 | bool bm_rendered;
|
---|
| 67 | bool bm_got_alloc;
|
---|
| 68 | } test_gc_t;
|
---|
| 69 |
|
---|
| 70 | typedef struct {
|
---|
| 71 | test_gc_t *tgc;
|
---|
| 72 | gfx_bitmap_alloc_t alloc;
|
---|
| 73 | bool myalloc;
|
---|
| 74 | } testgc_bitmap_t;
|
---|
| 75 |
|
---|
| 76 | /** Create and destroy button */
|
---|
[f80690a] | 77 | PCUT_TEST(create_destroy)
|
---|
[4ed00d3] | 78 | {
|
---|
| 79 | ui_pbutton_t *pbutton = NULL;
|
---|
| 80 | errno_t rc;
|
---|
| 81 |
|
---|
| 82 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 83 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 84 | PCUT_ASSERT_NOT_NULL(pbutton);
|
---|
| 85 |
|
---|
| 86 | ui_pbutton_destroy(pbutton);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /** ui_pbutton_destroy() can take NULL argument (no-op) */
|
---|
| 90 | PCUT_TEST(destroy_null)
|
---|
| 91 | {
|
---|
| 92 | ui_pbutton_destroy(NULL);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /** Set button rectangle sets internal field */
|
---|
| 96 | PCUT_TEST(set_rect)
|
---|
[f80690a] | 97 | {
|
---|
| 98 | ui_pbutton_t *pbutton;
|
---|
[4ed00d3] | 99 | gfx_rect_t rect;
|
---|
[f80690a] | 100 | errno_t rc;
|
---|
| 101 |
|
---|
[47728678] | 102 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
[f80690a] | 103 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 104 |
|
---|
[4ed00d3] | 105 | rect.p0.x = 1;
|
---|
| 106 | rect.p0.y = 2;
|
---|
| 107 | rect.p1.x = 3;
|
---|
| 108 | rect.p1.y = 4;
|
---|
| 109 |
|
---|
| 110 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
| 111 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, pbutton->rect.p0.x);
|
---|
| 112 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, pbutton->rect.p0.y);
|
---|
| 113 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, pbutton->rect.p1.x);
|
---|
| 114 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, pbutton->rect.p1.y);
|
---|
| 115 |
|
---|
[f80690a] | 116 | ui_pbutton_destroy(pbutton);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[4ed00d3] | 119 | /** Set default flag sets internal field */
|
---|
| 120 | PCUT_TEST(set_default)
|
---|
| 121 | {
|
---|
| 122 | ui_pbutton_t *pbutton;
|
---|
| 123 | errno_t rc;
|
---|
| 124 |
|
---|
| 125 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 126 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 127 |
|
---|
| 128 | ui_pbutton_set_default(pbutton, true);
|
---|
| 129 | PCUT_ASSERT_TRUE(pbutton->isdefault);
|
---|
| 130 |
|
---|
| 131 | ui_pbutton_set_default(pbutton, false);
|
---|
| 132 | PCUT_ASSERT_FALSE(pbutton->isdefault);
|
---|
| 133 |
|
---|
| 134 | ui_pbutton_destroy(pbutton);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /** Paint button */
|
---|
| 138 | PCUT_TEST(paint)
|
---|
| 139 | {
|
---|
| 140 | errno_t rc;
|
---|
| 141 | gfx_context_t *gc = NULL;
|
---|
| 142 | test_gc_t tgc;
|
---|
| 143 | ui_resource_t *resource = NULL;
|
---|
| 144 | ui_pbutton_t *pbutton;
|
---|
| 145 |
|
---|
| 146 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 147 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 148 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 149 |
|
---|
| 150 | rc = ui_resource_create(gc, &resource);
|
---|
| 151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 152 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 153 |
|
---|
| 154 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
| 155 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 156 |
|
---|
| 157 | rc = ui_pbutton_paint(pbutton);
|
---|
| 158 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 159 |
|
---|
| 160 | ui_pbutton_destroy(pbutton);
|
---|
| 161 | ui_resource_destroy(resource);
|
---|
| 162 |
|
---|
| 163 | rc = gfx_context_delete(gc);
|
---|
| 164 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /** ui_pbutton_press()/release() sets/clears internal held flag */
|
---|
| 168 | PCUT_TEST(press_release)
|
---|
| 169 | {
|
---|
| 170 | ui_pbutton_t *pbutton;
|
---|
| 171 | errno_t rc;
|
---|
| 172 |
|
---|
| 173 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 174 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 175 |
|
---|
| 176 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 177 |
|
---|
| 178 | ui_pbutton_press(pbutton);
|
---|
| 179 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 180 |
|
---|
| 181 | ui_pbutton_release(pbutton);
|
---|
| 182 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 183 |
|
---|
| 184 | ui_pbutton_destroy(pbutton);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[faca61b8] | 187 | /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
|
---|
| 188 | PCUT_TEST(pos_event_press_release)
|
---|
| 189 | {
|
---|
| 190 | ui_pbutton_t *pbutton;
|
---|
| 191 | pos_event_t event;
|
---|
| 192 | gfx_rect_t rect;
|
---|
| 193 | errno_t rc;
|
---|
| 194 |
|
---|
| 195 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 196 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 197 |
|
---|
| 198 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 199 |
|
---|
| 200 | rect.p0.x = 10;
|
---|
| 201 | rect.p0.y = 20;
|
---|
| 202 | rect.p1.x = 30;
|
---|
| 203 | rect.p1.y = 40;
|
---|
| 204 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
| 205 |
|
---|
| 206 | /* Press outside does nothing */
|
---|
| 207 | event.type = POS_PRESS;
|
---|
| 208 | event.hpos = 9;
|
---|
| 209 | event.vpos = 20;
|
---|
| 210 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 211 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 212 |
|
---|
| 213 | /* Press inside depresses button */
|
---|
| 214 | event.type = POS_PRESS;
|
---|
| 215 | event.hpos = 10;
|
---|
| 216 | event.vpos = 20;
|
---|
| 217 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 218 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 219 |
|
---|
| 220 | /* Release outside (or anywhere) relases button */
|
---|
| 221 | event.type = POS_RELEASE;
|
---|
| 222 | event.hpos = 9;
|
---|
| 223 | event.vpos = 20;
|
---|
| 224 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 225 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 226 |
|
---|
| 227 | ui_pbutton_destroy(pbutton);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[4ed00d3] | 230 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
| 231 | {
|
---|
| 232 | (void) arg;
|
---|
| 233 | (void) color;
|
---|
| 234 | return EOK;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 238 | {
|
---|
| 239 | (void) arg;
|
---|
| 240 | (void) rect;
|
---|
| 241 | return EOK;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 245 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 246 | {
|
---|
| 247 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 248 | testgc_bitmap_t *tbm;
|
---|
| 249 |
|
---|
| 250 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 251 | if (tbm == NULL)
|
---|
| 252 | return ENOMEM;
|
---|
| 253 |
|
---|
| 254 | if (alloc == NULL) {
|
---|
| 255 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 256 | sizeof(uint32_t);
|
---|
| 257 | tbm->alloc.off0 = 0;
|
---|
| 258 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
[faca61b8] | 259 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 260 | (params->rect.p1.y - params->rect.p0.y));
|
---|
[4ed00d3] | 261 | tbm->myalloc = true;
|
---|
| 262 | if (tbm->alloc.pixels == NULL) {
|
---|
| 263 | free(tbm);
|
---|
| 264 | return ENOMEM;
|
---|
| 265 | }
|
---|
| 266 | } else {
|
---|
| 267 | tbm->alloc = *alloc;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | tbm->tgc = tgc;
|
---|
| 271 | tgc->bm_created = true;
|
---|
| 272 | tgc->bm_params = *params;
|
---|
| 273 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 274 | *rbm = (void *)tbm;
|
---|
| 275 | return EOK;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 279 | {
|
---|
| 280 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 281 | if (tbm->myalloc)
|
---|
| 282 | free(tbm->alloc.pixels);
|
---|
| 283 | tbm->tgc->bm_destroyed = true;
|
---|
| 284 | free(tbm);
|
---|
| 285 | return EOK;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 289 | gfx_coord2_t *offs)
|
---|
| 290 | {
|
---|
| 291 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 292 | tbm->tgc->bm_rendered = true;
|
---|
| 293 | tbm->tgc->bm_srect = *srect;
|
---|
| 294 | tbm->tgc->bm_offs = *offs;
|
---|
| 295 | return EOK;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 299 | {
|
---|
| 300 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 301 | *alloc = tbm->alloc;
|
---|
| 302 | tbm->tgc->bm_got_alloc = true;
|
---|
| 303 | return EOK;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
[f80690a] | 306 | PCUT_EXPORT(pbutton);
|
---|