[f80690a] | 1 | /*
|
---|
[f0ccb2ab] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[f80690a] | 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>
|
---|
[b71c0fc] | 34 | #include <ui/control.h>
|
---|
[f80690a] | 35 | #include <ui/pbutton.h>
|
---|
[3583ffb] | 36 | #include <ui/resource.h>
|
---|
[4ed00d3] | 37 | #include "../private/pbutton.h"
|
---|
[f80690a] | 38 |
|
---|
| 39 | PCUT_INIT;
|
---|
| 40 |
|
---|
| 41 | PCUT_TEST_SUITE(pbutton);
|
---|
| 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 |
|
---|
[8ef48ece] | 64 | static void test_pbutton_clicked(ui_pbutton_t *, void *);
|
---|
[d4ea1f6] | 65 | static void test_pbutton_down(ui_pbutton_t *, void *);
|
---|
| 66 | static void test_pbutton_up(ui_pbutton_t *, void *);
|
---|
[8ef48ece] | 67 |
|
---|
| 68 | static ui_pbutton_cb_t test_pbutton_cb = {
|
---|
[d4ea1f6] | 69 | .clicked = test_pbutton_clicked,
|
---|
| 70 | .down = test_pbutton_down,
|
---|
| 71 | .up = test_pbutton_up
|
---|
[8ef48ece] | 72 | };
|
---|
| 73 |
|
---|
| 74 | static ui_pbutton_cb_t dummy_pbutton_cb = {
|
---|
| 75 | };
|
---|
| 76 |
|
---|
[3583ffb] | 77 | typedef struct {
|
---|
| 78 | bool bm_created;
|
---|
| 79 | bool bm_destroyed;
|
---|
| 80 | gfx_bitmap_params_t bm_params;
|
---|
| 81 | void *bm_pixels;
|
---|
| 82 | gfx_rect_t bm_srect;
|
---|
| 83 | gfx_coord2_t bm_offs;
|
---|
| 84 | bool bm_rendered;
|
---|
| 85 | bool bm_got_alloc;
|
---|
| 86 | } test_gc_t;
|
---|
| 87 |
|
---|
| 88 | typedef struct {
|
---|
| 89 | test_gc_t *tgc;
|
---|
| 90 | gfx_bitmap_alloc_t alloc;
|
---|
| 91 | bool myalloc;
|
---|
| 92 | } testgc_bitmap_t;
|
---|
| 93 |
|
---|
[8ef48ece] | 94 | typedef struct {
|
---|
| 95 | bool clicked;
|
---|
[d4ea1f6] | 96 | bool down;
|
---|
| 97 | bool up;
|
---|
[8ef48ece] | 98 | } test_cb_resp_t;
|
---|
| 99 |
|
---|
[4ed00d3] | 100 | /** Create and destroy button */
|
---|
[f80690a] | 101 | PCUT_TEST(create_destroy)
|
---|
[4ed00d3] | 102 | {
|
---|
| 103 | ui_pbutton_t *pbutton = NULL;
|
---|
| 104 | errno_t rc;
|
---|
| 105 |
|
---|
| 106 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 107 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 108 | PCUT_ASSERT_NOT_NULL(pbutton);
|
---|
| 109 |
|
---|
| 110 | ui_pbutton_destroy(pbutton);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** ui_pbutton_destroy() can take NULL argument (no-op) */
|
---|
| 114 | PCUT_TEST(destroy_null)
|
---|
| 115 | {
|
---|
| 116 | ui_pbutton_destroy(NULL);
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[b71c0fc] | 119 | /** ui_pbutton_ctl() returns control that has a working virtual destructor */
|
---|
| 120 | PCUT_TEST(ctl)
|
---|
| 121 | {
|
---|
| 122 | ui_pbutton_t *pbutton;
|
---|
| 123 | ui_control_t *control;
|
---|
| 124 | errno_t rc;
|
---|
| 125 |
|
---|
| 126 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 127 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 128 |
|
---|
| 129 | control = ui_pbutton_ctl(pbutton);
|
---|
| 130 | PCUT_ASSERT_NOT_NULL(control);
|
---|
| 131 |
|
---|
| 132 | ui_control_destroy(control);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[4ed00d3] | 135 | /** Set button rectangle sets internal field */
|
---|
| 136 | PCUT_TEST(set_rect)
|
---|
[f80690a] | 137 | {
|
---|
| 138 | ui_pbutton_t *pbutton;
|
---|
[4ed00d3] | 139 | gfx_rect_t rect;
|
---|
[f80690a] | 140 | errno_t rc;
|
---|
| 141 |
|
---|
[47728678] | 142 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
[f80690a] | 143 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 144 |
|
---|
[4ed00d3] | 145 | rect.p0.x = 1;
|
---|
| 146 | rect.p0.y = 2;
|
---|
| 147 | rect.p1.x = 3;
|
---|
| 148 | rect.p1.y = 4;
|
---|
| 149 |
|
---|
| 150 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
| 151 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, pbutton->rect.p0.x);
|
---|
| 152 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, pbutton->rect.p0.y);
|
---|
| 153 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, pbutton->rect.p1.x);
|
---|
| 154 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, pbutton->rect.p1.y);
|
---|
| 155 |
|
---|
[f80690a] | 156 | ui_pbutton_destroy(pbutton);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[4ed00d3] | 159 | /** Set default flag sets internal field */
|
---|
| 160 | PCUT_TEST(set_default)
|
---|
| 161 | {
|
---|
| 162 | ui_pbutton_t *pbutton;
|
---|
| 163 | errno_t rc;
|
---|
| 164 |
|
---|
| 165 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 166 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 167 |
|
---|
| 168 | ui_pbutton_set_default(pbutton, true);
|
---|
| 169 | PCUT_ASSERT_TRUE(pbutton->isdefault);
|
---|
| 170 |
|
---|
| 171 | ui_pbutton_set_default(pbutton, false);
|
---|
| 172 | PCUT_ASSERT_FALSE(pbutton->isdefault);
|
---|
| 173 |
|
---|
| 174 | ui_pbutton_destroy(pbutton);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /** Paint button */
|
---|
| 178 | PCUT_TEST(paint)
|
---|
| 179 | {
|
---|
| 180 | errno_t rc;
|
---|
[3583ffb] | 181 | gfx_context_t *gc = NULL;
|
---|
| 182 | test_gc_t tgc;
|
---|
| 183 | ui_resource_t *resource = NULL;
|
---|
[4ed00d3] | 184 | ui_pbutton_t *pbutton;
|
---|
| 185 |
|
---|
[3583ffb] | 186 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 187 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
[4ed00d3] | 188 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 189 |
|
---|
[9c7dc8e] | 190 | rc = ui_resource_create(gc, false, &resource);
|
---|
[3583ffb] | 191 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 192 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 193 |
|
---|
| 194 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[4ed00d3] | 195 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 196 |
|
---|
| 197 | rc = ui_pbutton_paint(pbutton);
|
---|
| 198 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 199 |
|
---|
| 200 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 201 | ui_resource_destroy(resource);
|
---|
| 202 |
|
---|
| 203 | rc = gfx_context_delete(gc);
|
---|
| 204 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[4ed00d3] | 205 | }
|
---|
| 206 |
|
---|
[8ef48ece] | 207 | /** Test ui_pbutton_clicked() */
|
---|
| 208 | PCUT_TEST(clicked)
|
---|
| 209 | {
|
---|
| 210 | errno_t rc;
|
---|
| 211 | ui_pbutton_t *pbutton;
|
---|
| 212 | test_cb_resp_t resp;
|
---|
| 213 |
|
---|
| 214 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 215 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 216 |
|
---|
| 217 | /* Clicked with no callbacks set */
|
---|
| 218 | ui_pbutton_clicked(pbutton);
|
---|
| 219 |
|
---|
| 220 | /* Clicked with callback not implementing clicked */
|
---|
| 221 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
| 222 | ui_pbutton_clicked(pbutton);
|
---|
| 223 |
|
---|
| 224 | /* Clicked with real callback set */
|
---|
| 225 | resp.clicked = false;
|
---|
| 226 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
| 227 | ui_pbutton_clicked(pbutton);
|
---|
| 228 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
| 229 |
|
---|
| 230 | ui_pbutton_destroy(pbutton);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[d4ea1f6] | 233 | /** Test ui_pbutton_down() */
|
---|
| 234 | PCUT_TEST(down)
|
---|
| 235 | {
|
---|
| 236 | errno_t rc;
|
---|
| 237 | ui_pbutton_t *pbutton;
|
---|
| 238 | test_cb_resp_t resp;
|
---|
| 239 |
|
---|
| 240 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 241 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 242 |
|
---|
| 243 | /* Down with no callbacks set */
|
---|
| 244 | ui_pbutton_clicked(pbutton);
|
---|
| 245 |
|
---|
| 246 | /* Down with callback not implementing down */
|
---|
| 247 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
| 248 | ui_pbutton_down(pbutton);
|
---|
| 249 |
|
---|
| 250 | /* Down with real callback set */
|
---|
| 251 | resp.down = false;
|
---|
| 252 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
| 253 | ui_pbutton_down(pbutton);
|
---|
| 254 | PCUT_ASSERT_TRUE(resp.down);
|
---|
| 255 |
|
---|
| 256 | ui_pbutton_destroy(pbutton);
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /** Test ui_pbutton_up() */
|
---|
| 260 | PCUT_TEST(up)
|
---|
| 261 | {
|
---|
| 262 | errno_t rc;
|
---|
| 263 | ui_pbutton_t *pbutton;
|
---|
| 264 | test_cb_resp_t resp;
|
---|
| 265 |
|
---|
| 266 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
| 267 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 268 |
|
---|
| 269 | /* Up with no callbacks set */
|
---|
| 270 | ui_pbutton_clicked(pbutton);
|
---|
| 271 |
|
---|
| 272 | /* Up with callback not implementing up */
|
---|
| 273 | ui_pbutton_set_cb(pbutton, &dummy_pbutton_cb, NULL);
|
---|
| 274 | ui_pbutton_up(pbutton);
|
---|
| 275 |
|
---|
| 276 | /* Up with real callback set */
|
---|
| 277 | resp.up = false;
|
---|
| 278 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
| 279 | ui_pbutton_up(pbutton);
|
---|
| 280 | PCUT_ASSERT_TRUE(resp.up);
|
---|
| 281 |
|
---|
| 282 | ui_pbutton_destroy(pbutton);
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[8ef48ece] | 285 | /** Press and release button */
|
---|
[4ed00d3] | 286 | PCUT_TEST(press_release)
|
---|
| 287 | {
|
---|
[8ef48ece] | 288 | errno_t rc;
|
---|
[3583ffb] | 289 | gfx_context_t *gc = NULL;
|
---|
| 290 | test_gc_t tgc;
|
---|
| 291 | ui_resource_t *resource = NULL;
|
---|
[4ed00d3] | 292 | ui_pbutton_t *pbutton;
|
---|
[8ef48ece] | 293 | test_cb_resp_t resp;
|
---|
| 294 |
|
---|
[3583ffb] | 295 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 296 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 297 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 298 |
|
---|
[9c7dc8e] | 299 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 300 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 301 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 302 |
|
---|
[3583ffb] | 303 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 304 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 305 |
|
---|
| 306 | resp.clicked = false;
|
---|
[d4ea1f6] | 307 | resp.down = false;
|
---|
| 308 | resp.up = false;
|
---|
[8ef48ece] | 309 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
| 310 |
|
---|
| 311 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 312 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 313 |
|
---|
| 314 | ui_pbutton_press(pbutton);
|
---|
| 315 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 316 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
[d4ea1f6] | 317 | PCUT_ASSERT_TRUE(resp.down);
|
---|
| 318 | PCUT_ASSERT_FALSE(resp.up);
|
---|
[8ef48ece] | 319 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
| 320 |
|
---|
| 321 | ui_pbutton_release(pbutton);
|
---|
| 322 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 323 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
[d4ea1f6] | 324 | PCUT_ASSERT_TRUE(resp.up);
|
---|
[8ef48ece] | 325 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
| 326 |
|
---|
| 327 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 328 | ui_resource_destroy(resource);
|
---|
| 329 |
|
---|
| 330 | rc = gfx_context_delete(gc);
|
---|
| 331 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 332 | }
|
---|
| 333 |
|
---|
| 334 | /** Press, leave and release button */
|
---|
| 335 | PCUT_TEST(press_leave_release)
|
---|
| 336 | {
|
---|
[4ed00d3] | 337 | errno_t rc;
|
---|
[3583ffb] | 338 | gfx_context_t *gc = NULL;
|
---|
| 339 | test_gc_t tgc;
|
---|
| 340 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 341 | ui_pbutton_t *pbutton;
|
---|
| 342 | test_cb_resp_t resp;
|
---|
[4ed00d3] | 343 |
|
---|
[3583ffb] | 344 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 345 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 346 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 347 |
|
---|
[9c7dc8e] | 348 | rc = ui_resource_create(gc, false, &resource);
|
---|
[4ed00d3] | 349 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 350 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 351 |
|
---|
[3583ffb] | 352 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 353 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 354 |
|
---|
| 355 | resp.clicked = false;
|
---|
| 356 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
[4ed00d3] | 357 |
|
---|
| 358 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[8ef48ece] | 359 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
[4ed00d3] | 360 |
|
---|
| 361 | ui_pbutton_press(pbutton);
|
---|
| 362 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
[8ef48ece] | 363 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
| 364 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
| 365 |
|
---|
| 366 | ui_pbutton_leave(pbutton);
|
---|
| 367 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 368 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 369 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
[4ed00d3] | 370 |
|
---|
| 371 | ui_pbutton_release(pbutton);
|
---|
| 372 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[8ef48ece] | 373 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 374 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
[4ed00d3] | 375 |
|
---|
| 376 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 377 | ui_resource_destroy(resource);
|
---|
| 378 |
|
---|
| 379 | rc = gfx_context_delete(gc);
|
---|
| 380 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 381 | }
|
---|
| 382 |
|
---|
| 383 | /** Press, leave, enter and release button */
|
---|
| 384 | PCUT_TEST(press_leave_enter_release)
|
---|
| 385 | {
|
---|
| 386 | errno_t rc;
|
---|
[3583ffb] | 387 | gfx_context_t *gc = NULL;
|
---|
| 388 | test_gc_t tgc;
|
---|
| 389 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 390 | ui_pbutton_t *pbutton;
|
---|
| 391 | test_cb_resp_t resp;
|
---|
| 392 |
|
---|
[3583ffb] | 393 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 394 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
[8ef48ece] | 395 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 396 |
|
---|
[9c7dc8e] | 397 | rc = ui_resource_create(gc, false, &resource);
|
---|
[3583ffb] | 398 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 399 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 400 |
|
---|
| 401 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 402 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 403 |
|
---|
| 404 | resp.clicked = false;
|
---|
| 405 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
| 406 |
|
---|
| 407 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 408 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 409 |
|
---|
| 410 | ui_pbutton_press(pbutton);
|
---|
| 411 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 412 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
| 413 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
| 414 |
|
---|
| 415 | ui_pbutton_leave(pbutton);
|
---|
| 416 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 417 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 418 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
| 419 |
|
---|
| 420 | ui_pbutton_enter(pbutton);
|
---|
| 421 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
| 422 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
| 423 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
| 424 |
|
---|
| 425 | ui_pbutton_release(pbutton);
|
---|
| 426 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 427 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
| 428 | PCUT_ASSERT_TRUE(resp.clicked);
|
---|
| 429 |
|
---|
| 430 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 431 | ui_resource_destroy(resource);
|
---|
| 432 |
|
---|
| 433 | rc = gfx_context_delete(gc);
|
---|
| 434 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[4ed00d3] | 435 | }
|
---|
| 436 |
|
---|
[faca61b8] | 437 | /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
|
---|
| 438 | PCUT_TEST(pos_event_press_release)
|
---|
| 439 | {
|
---|
[8ef48ece] | 440 | errno_t rc;
|
---|
[3583ffb] | 441 | gfx_context_t *gc = NULL;
|
---|
| 442 | test_gc_t tgc;
|
---|
| 443 | ui_resource_t *resource = NULL;
|
---|
[faca61b8] | 444 | ui_pbutton_t *pbutton;
|
---|
[a2f173b] | 445 | ui_evclaim_t claim;
|
---|
[faca61b8] | 446 | pos_event_t event;
|
---|
| 447 | gfx_rect_t rect;
|
---|
| 448 |
|
---|
[3583ffb] | 449 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 450 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 451 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 452 |
|
---|
[9c7dc8e] | 453 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 454 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 455 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 456 |
|
---|
[3583ffb] | 457 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[faca61b8] | 458 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 459 |
|
---|
| 460 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
| 461 |
|
---|
| 462 | rect.p0.x = 10;
|
---|
| 463 | rect.p0.y = 20;
|
---|
| 464 | rect.p1.x = 30;
|
---|
| 465 | rect.p1.y = 40;
|
---|
| 466 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
| 467 |
|
---|
[a2f173b] | 468 | /* Press outside is not claimed and does nothing */
|
---|
[faca61b8] | 469 | event.type = POS_PRESS;
|
---|
| 470 | event.hpos = 9;
|
---|
| 471 | event.vpos = 20;
|
---|
[a2f173b] | 472 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 473 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[a2f173b] | 474 | PCUT_ASSERT_EQUALS(ui_unclaimed, claim);
|
---|
[faca61b8] | 475 |
|
---|
[a2f173b] | 476 | /* Press inside is claimed and depresses button */
|
---|
[faca61b8] | 477 | event.type = POS_PRESS;
|
---|
| 478 | event.hpos = 10;
|
---|
| 479 | event.vpos = 20;
|
---|
[a2f173b] | 480 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 481 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
[a2f173b] | 482 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
[faca61b8] | 483 |
|
---|
[a2f173b] | 484 | /* Release outside (or anywhere) is claimed and relases button */
|
---|
[faca61b8] | 485 | event.type = POS_RELEASE;
|
---|
| 486 | event.hpos = 9;
|
---|
| 487 | event.vpos = 20;
|
---|
[a2f173b] | 488 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 489 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[a2f173b] | 490 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
[faca61b8] | 491 |
|
---|
| 492 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 493 | ui_resource_destroy(resource);
|
---|
| 494 |
|
---|
| 495 | rc = gfx_context_delete(gc);
|
---|
| 496 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 497 | }
|
---|
| 498 |
|
---|
| 499 | /** ui_pos_event() correctly translates POS_UPDATE to enter/leave */
|
---|
| 500 | PCUT_TEST(pos_event_enter_leave)
|
---|
| 501 | {
|
---|
| 502 | errno_t rc;
|
---|
[3583ffb] | 503 | gfx_context_t *gc = NULL;
|
---|
| 504 | test_gc_t tgc;
|
---|
| 505 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 506 | ui_pbutton_t *pbutton;
|
---|
| 507 | pos_event_t event;
|
---|
| 508 | gfx_rect_t rect;
|
---|
| 509 |
|
---|
[3583ffb] | 510 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 511 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 512 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 513 |
|
---|
[9c7dc8e] | 514 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 515 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 516 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 517 |
|
---|
[3583ffb] | 518 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 519 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 520 |
|
---|
| 521 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 522 |
|
---|
| 523 | rect.p0.x = 10;
|
---|
| 524 | rect.p0.y = 20;
|
---|
| 525 | rect.p1.x = 30;
|
---|
| 526 | rect.p1.y = 40;
|
---|
| 527 | ui_pbutton_set_rect(pbutton, &rect);
|
---|
| 528 |
|
---|
| 529 | /* Moving outside does nothing */
|
---|
| 530 | event.type = POS_UPDATE;
|
---|
| 531 | event.hpos = 9;
|
---|
| 532 | event.vpos = 20;
|
---|
| 533 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 534 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 535 |
|
---|
| 536 | /* Moving inside sets inside flag */
|
---|
| 537 | event.type = POS_UPDATE;
|
---|
| 538 | event.hpos = 10;
|
---|
| 539 | event.vpos = 20;
|
---|
| 540 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 541 | PCUT_ASSERT_TRUE(pbutton->inside);
|
---|
| 542 |
|
---|
| 543 | /* Moving outside clears inside flag */
|
---|
| 544 | event.type = POS_UPDATE;
|
---|
| 545 | event.hpos = 9;
|
---|
| 546 | event.vpos = 20;
|
---|
| 547 | ui_pbutton_pos_event(pbutton, &event);
|
---|
| 548 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 549 |
|
---|
| 550 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 551 | ui_resource_destroy(resource);
|
---|
| 552 |
|
---|
| 553 | rc = gfx_context_delete(gc);
|
---|
| 554 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 555 | }
|
---|
| 556 |
|
---|
[7470d97] | 557 | static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
|
---|
| 558 | {
|
---|
| 559 | (void) arg;
|
---|
| 560 | (void) rect;
|
---|
| 561 | return EOK;
|
---|
| 562 | }
|
---|
| 563 |
|
---|
[3583ffb] | 564 | static errno_t testgc_set_color(void *arg, gfx_color_t *color)
|
---|
| 565 | {
|
---|
| 566 | (void) arg;
|
---|
| 567 | (void) color;
|
---|
| 568 | return EOK;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
|
---|
| 572 | {
|
---|
| 573 | (void) arg;
|
---|
| 574 | (void) rect;
|
---|
| 575 | return EOK;
|
---|
| 576 | }
|
---|
| 577 |
|
---|
[f0ccb2ab] | 578 | static errno_t testgc_update(void *arg)
|
---|
| 579 | {
|
---|
| 580 | (void) arg;
|
---|
| 581 | return EOK;
|
---|
| 582 | }
|
---|
| 583 |
|
---|
[3583ffb] | 584 | static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
|
---|
| 585 | gfx_bitmap_alloc_t *alloc, void **rbm)
|
---|
| 586 | {
|
---|
| 587 | test_gc_t *tgc = (test_gc_t *) arg;
|
---|
| 588 | testgc_bitmap_t *tbm;
|
---|
| 589 |
|
---|
| 590 | tbm = calloc(1, sizeof(testgc_bitmap_t));
|
---|
| 591 | if (tbm == NULL)
|
---|
| 592 | return ENOMEM;
|
---|
| 593 |
|
---|
| 594 | if (alloc == NULL) {
|
---|
| 595 | tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 596 | sizeof(uint32_t);
|
---|
| 597 | tbm->alloc.off0 = 0;
|
---|
| 598 | tbm->alloc.pixels = calloc(sizeof(uint32_t),
|
---|
| 599 | (params->rect.p1.x - params->rect.p0.x) *
|
---|
| 600 | (params->rect.p1.y - params->rect.p0.y));
|
---|
| 601 | tbm->myalloc = true;
|
---|
| 602 | if (tbm->alloc.pixels == NULL) {
|
---|
| 603 | free(tbm);
|
---|
| 604 | return ENOMEM;
|
---|
| 605 | }
|
---|
| 606 | } else {
|
---|
| 607 | tbm->alloc = *alloc;
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | tbm->tgc = tgc;
|
---|
| 611 | tgc->bm_created = true;
|
---|
| 612 | tgc->bm_params = *params;
|
---|
| 613 | tgc->bm_pixels = tbm->alloc.pixels;
|
---|
| 614 | *rbm = (void *)tbm;
|
---|
| 615 | return EOK;
|
---|
| 616 | }
|
---|
| 617 |
|
---|
| 618 | static errno_t testgc_bitmap_destroy(void *bm)
|
---|
| 619 | {
|
---|
| 620 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 621 | if (tbm->myalloc)
|
---|
| 622 | free(tbm->alloc.pixels);
|
---|
| 623 | tbm->tgc->bm_destroyed = true;
|
---|
| 624 | free(tbm);
|
---|
| 625 | return EOK;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
|
---|
| 629 | gfx_coord2_t *offs)
|
---|
| 630 | {
|
---|
| 631 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 632 | tbm->tgc->bm_rendered = true;
|
---|
| 633 | tbm->tgc->bm_srect = *srect;
|
---|
| 634 | tbm->tgc->bm_offs = *offs;
|
---|
| 635 | return EOK;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
|
---|
| 639 | {
|
---|
| 640 | testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
|
---|
| 641 | *alloc = tbm->alloc;
|
---|
| 642 | tbm->tgc->bm_got_alloc = true;
|
---|
| 643 | return EOK;
|
---|
[4ed00d3] | 644 | }
|
---|
| 645 |
|
---|
[8ef48ece] | 646 | static void test_pbutton_clicked(ui_pbutton_t *pbutton, void *arg)
|
---|
| 647 | {
|
---|
| 648 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 649 |
|
---|
| 650 | resp->clicked = true;
|
---|
| 651 | }
|
---|
| 652 |
|
---|
[d4ea1f6] | 653 | static void test_pbutton_down(ui_pbutton_t *pbutton, void *arg)
|
---|
| 654 | {
|
---|
| 655 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 656 |
|
---|
| 657 | resp->down = true;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | static void test_pbutton_up(ui_pbutton_t *pbutton, void *arg)
|
---|
| 661 | {
|
---|
| 662 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 663 |
|
---|
| 664 | resp->up = true;
|
---|
| 665 | }
|
---|
| 666 |
|
---|
[f80690a] | 667 | PCUT_EXPORT(pbutton);
|
---|