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