[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 |
|
---|
[3583ffb] | 43 | static errno_t testgc_set_color(void *, gfx_color_t *);
|
---|
| 44 | static errno_t testgc_fill_rect(void *, gfx_rect_t *);
|
---|
[f0ccb2ab] | 45 | static errno_t testgc_update(void *);
|
---|
[3583ffb] | 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,
|
---|
[f0ccb2ab] | 55 | .update = testgc_update,
|
---|
[3583ffb] | 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 |
|
---|
[8ef48ece] | 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 |
|
---|
[3583ffb] | 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 |
|
---|
[8ef48ece] | 88 | typedef struct {
|
---|
| 89 | bool clicked;
|
---|
| 90 | } test_cb_resp_t;
|
---|
| 91 |
|
---|
[4ed00d3] | 92 | /** Create and destroy button */
|
---|
[f80690a] | 93 | PCUT_TEST(create_destroy)
|
---|
[4ed00d3] | 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 |
|
---|
[b71c0fc] | 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 |
|
---|
[4ed00d3] | 127 | /** Set button rectangle sets internal field */
|
---|
| 128 | PCUT_TEST(set_rect)
|
---|
[f80690a] | 129 | {
|
---|
| 130 | ui_pbutton_t *pbutton;
|
---|
[4ed00d3] | 131 | gfx_rect_t rect;
|
---|
[f80690a] | 132 | errno_t rc;
|
---|
| 133 |
|
---|
[47728678] | 134 | rc = ui_pbutton_create(NULL, "Hello", &pbutton);
|
---|
[f80690a] | 135 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 136 |
|
---|
[4ed00d3] | 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 |
|
---|
[f80690a] | 148 | ui_pbutton_destroy(pbutton);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[4ed00d3] | 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;
|
---|
[3583ffb] | 173 | gfx_context_t *gc = NULL;
|
---|
| 174 | test_gc_t tgc;
|
---|
| 175 | ui_resource_t *resource = NULL;
|
---|
[4ed00d3] | 176 | ui_pbutton_t *pbutton;
|
---|
| 177 |
|
---|
[3583ffb] | 178 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 179 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
[4ed00d3] | 180 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 181 |
|
---|
[9c7dc8e] | 182 | rc = ui_resource_create(gc, false, &resource);
|
---|
[3583ffb] | 183 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 184 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 185 |
|
---|
| 186 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[4ed00d3] | 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);
|
---|
[3583ffb] | 193 | ui_resource_destroy(resource);
|
---|
| 194 |
|
---|
| 195 | rc = gfx_context_delete(gc);
|
---|
| 196 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[4ed00d3] | 197 | }
|
---|
| 198 |
|
---|
[8ef48ece] | 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 */
|
---|
[4ed00d3] | 226 | PCUT_TEST(press_release)
|
---|
| 227 | {
|
---|
[8ef48ece] | 228 | errno_t rc;
|
---|
[3583ffb] | 229 | gfx_context_t *gc = NULL;
|
---|
| 230 | test_gc_t tgc;
|
---|
| 231 | ui_resource_t *resource = NULL;
|
---|
[4ed00d3] | 232 | ui_pbutton_t *pbutton;
|
---|
[8ef48ece] | 233 | test_cb_resp_t resp;
|
---|
| 234 |
|
---|
[3583ffb] | 235 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 236 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 237 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 238 |
|
---|
[9c7dc8e] | 239 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 240 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 241 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 242 |
|
---|
[3583ffb] | 243 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 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);
|
---|
[3583ffb] | 263 | ui_resource_destroy(resource);
|
---|
| 264 |
|
---|
| 265 | rc = gfx_context_delete(gc);
|
---|
| 266 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 267 | }
|
---|
| 268 |
|
---|
| 269 | /** Press, leave and release button */
|
---|
| 270 | PCUT_TEST(press_leave_release)
|
---|
| 271 | {
|
---|
[4ed00d3] | 272 | errno_t rc;
|
---|
[3583ffb] | 273 | gfx_context_t *gc = NULL;
|
---|
| 274 | test_gc_t tgc;
|
---|
| 275 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 276 | ui_pbutton_t *pbutton;
|
---|
| 277 | test_cb_resp_t resp;
|
---|
[4ed00d3] | 278 |
|
---|
[3583ffb] | 279 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 280 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 281 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 282 |
|
---|
[9c7dc8e] | 283 | rc = ui_resource_create(gc, false, &resource);
|
---|
[4ed00d3] | 284 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 285 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 286 |
|
---|
[3583ffb] | 287 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 288 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 289 |
|
---|
| 290 | resp.clicked = false;
|
---|
| 291 | ui_pbutton_set_cb(pbutton, &test_pbutton_cb, &resp);
|
---|
[4ed00d3] | 292 |
|
---|
| 293 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[8ef48ece] | 294 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
[4ed00d3] | 295 |
|
---|
| 296 | ui_pbutton_press(pbutton);
|
---|
| 297 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
[8ef48ece] | 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);
|
---|
[4ed00d3] | 305 |
|
---|
| 306 | ui_pbutton_release(pbutton);
|
---|
| 307 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[8ef48ece] | 308 | PCUT_ASSERT_FALSE(pbutton->inside);
|
---|
| 309 | PCUT_ASSERT_FALSE(resp.clicked);
|
---|
[4ed00d3] | 310 |
|
---|
| 311 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 312 | ui_resource_destroy(resource);
|
---|
| 313 |
|
---|
| 314 | rc = gfx_context_delete(gc);
|
---|
| 315 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 316 | }
|
---|
| 317 |
|
---|
| 318 | /** Press, leave, enter and release button */
|
---|
| 319 | PCUT_TEST(press_leave_enter_release)
|
---|
| 320 | {
|
---|
| 321 | errno_t rc;
|
---|
[3583ffb] | 322 | gfx_context_t *gc = NULL;
|
---|
| 323 | test_gc_t tgc;
|
---|
| 324 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 325 | ui_pbutton_t *pbutton;
|
---|
| 326 | test_cb_resp_t resp;
|
---|
| 327 |
|
---|
[3583ffb] | 328 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 329 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
[8ef48ece] | 330 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 331 |
|
---|
[9c7dc8e] | 332 | rc = ui_resource_create(gc, false, &resource);
|
---|
[3583ffb] | 333 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 334 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
| 335 |
|
---|
| 336 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 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);
|
---|
[3583ffb] | 366 | ui_resource_destroy(resource);
|
---|
| 367 |
|
---|
| 368 | rc = gfx_context_delete(gc);
|
---|
| 369 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[4ed00d3] | 370 | }
|
---|
| 371 |
|
---|
[faca61b8] | 372 | /** ui_pos_event() correctly translates POS_PRESS/POS_RELEASE */
|
---|
| 373 | PCUT_TEST(pos_event_press_release)
|
---|
| 374 | {
|
---|
[8ef48ece] | 375 | errno_t rc;
|
---|
[3583ffb] | 376 | gfx_context_t *gc = NULL;
|
---|
| 377 | test_gc_t tgc;
|
---|
| 378 | ui_resource_t *resource = NULL;
|
---|
[faca61b8] | 379 | ui_pbutton_t *pbutton;
|
---|
[a2f173b] | 380 | ui_evclaim_t claim;
|
---|
[faca61b8] | 381 | pos_event_t event;
|
---|
| 382 | gfx_rect_t rect;
|
---|
| 383 |
|
---|
[3583ffb] | 384 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 385 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 386 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 387 |
|
---|
[9c7dc8e] | 388 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 389 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 390 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 391 |
|
---|
[3583ffb] | 392 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[faca61b8] | 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 |
|
---|
[a2f173b] | 403 | /* Press outside is not claimed and does nothing */
|
---|
[faca61b8] | 404 | event.type = POS_PRESS;
|
---|
| 405 | event.hpos = 9;
|
---|
| 406 | event.vpos = 20;
|
---|
[a2f173b] | 407 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 408 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[a2f173b] | 409 | PCUT_ASSERT_EQUALS(ui_unclaimed, claim);
|
---|
[faca61b8] | 410 |
|
---|
[a2f173b] | 411 | /* Press inside is claimed and depresses button */
|
---|
[faca61b8] | 412 | event.type = POS_PRESS;
|
---|
| 413 | event.hpos = 10;
|
---|
| 414 | event.vpos = 20;
|
---|
[a2f173b] | 415 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 416 | PCUT_ASSERT_TRUE(pbutton->held);
|
---|
[a2f173b] | 417 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
[faca61b8] | 418 |
|
---|
[a2f173b] | 419 | /* Release outside (or anywhere) is claimed and relases button */
|
---|
[faca61b8] | 420 | event.type = POS_RELEASE;
|
---|
| 421 | event.hpos = 9;
|
---|
| 422 | event.vpos = 20;
|
---|
[a2f173b] | 423 | claim = ui_pbutton_pos_event(pbutton, &event);
|
---|
[faca61b8] | 424 | PCUT_ASSERT_FALSE(pbutton->held);
|
---|
[a2f173b] | 425 | PCUT_ASSERT_EQUALS(ui_claimed, claim);
|
---|
[faca61b8] | 426 |
|
---|
| 427 | ui_pbutton_destroy(pbutton);
|
---|
[3583ffb] | 428 | ui_resource_destroy(resource);
|
---|
| 429 |
|
---|
| 430 | rc = gfx_context_delete(gc);
|
---|
| 431 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[8ef48ece] | 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;
|
---|
[3583ffb] | 438 | gfx_context_t *gc = NULL;
|
---|
| 439 | test_gc_t tgc;
|
---|
| 440 | ui_resource_t *resource = NULL;
|
---|
[8ef48ece] | 441 | ui_pbutton_t *pbutton;
|
---|
| 442 | pos_event_t event;
|
---|
| 443 | gfx_rect_t rect;
|
---|
| 444 |
|
---|
[3583ffb] | 445 | memset(&tgc, 0, sizeof(tgc));
|
---|
| 446 | rc = gfx_context_new(&ops, &tgc, &gc);
|
---|
| 447 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 448 |
|
---|
[9c7dc8e] | 449 | rc = ui_resource_create(gc, false, &resource);
|
---|
[8ef48ece] | 450 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
[3583ffb] | 451 | PCUT_ASSERT_NOT_NULL(resource);
|
---|
[8ef48ece] | 452 |
|
---|
[3583ffb] | 453 | rc = ui_pbutton_create(resource, "Hello", &pbutton);
|
---|
[8ef48ece] | 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);
|
---|
[3583ffb] | 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 |
|
---|
[f0ccb2ab] | 506 | static errno_t testgc_update(void *arg)
|
---|
| 507 | {
|
---|
| 508 | (void) arg;
|
---|
| 509 | return EOK;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[3583ffb] | 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;
|
---|
[4ed00d3] | 572 | }
|
---|
| 573 |
|
---|
[8ef48ece] | 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 |
|
---|
[f80690a] | 581 | PCUT_EXPORT(pbutton);
|
---|