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