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