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