| 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/menu.h>
|
|---|
| 36 | #include <ui/menubar.h>
|
|---|
| 37 | #include <ui/menuentry.h>
|
|---|
| 38 | #include <ui/resource.h>
|
|---|
| 39 | #include <ui/ui.h>
|
|---|
| 40 | #include "../private/dummygc.h"
|
|---|
| 41 | #include "../private/menuentry.h"
|
|---|
| 42 |
|
|---|
| 43 | PCUT_INIT;
|
|---|
| 44 |
|
|---|
| 45 | PCUT_TEST_SUITE(menuentry);
|
|---|
| 46 |
|
|---|
| 47 | typedef struct {
|
|---|
| 48 | bool activated;
|
|---|
| 49 | } test_resp_t;
|
|---|
| 50 |
|
|---|
| 51 | static void test_entry_cb(ui_menu_entry_t *, void *);
|
|---|
| 52 |
|
|---|
| 53 | /** Create and destroy menu entry */
|
|---|
| 54 | PCUT_TEST(create_destroy)
|
|---|
| 55 | {
|
|---|
| 56 | dummy_gc_t *dgc;
|
|---|
| 57 | gfx_context_t *gc;
|
|---|
| 58 | ui_resource_t *resource = NULL;
|
|---|
| 59 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 60 | ui_menu_t *menu = NULL;
|
|---|
| 61 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 62 | errno_t rc;
|
|---|
| 63 |
|
|---|
| 64 | rc = dummygc_create(&dgc);
|
|---|
| 65 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 66 |
|
|---|
| 67 | gc = dummygc_get_ctx(dgc);
|
|---|
| 68 |
|
|---|
| 69 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 70 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 71 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 72 |
|
|---|
| 73 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 74 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 75 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 76 |
|
|---|
| 77 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 78 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 79 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 80 |
|
|---|
| 81 | rc = ui_menu_entry_create(menu, "Foo", "F1", &mentry);
|
|---|
| 82 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 83 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 84 |
|
|---|
| 85 | /* Just for sake of test. Menu entry is destroyed along with menu */
|
|---|
| 86 | ui_menu_entry_destroy(mentry);
|
|---|
| 87 |
|
|---|
| 88 | ui_menu_bar_destroy(mbar);
|
|---|
| 89 | dummygc_destroy(dgc);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /** Create and destroy separator menu entry */
|
|---|
| 93 | PCUT_TEST(create_sep_destroy)
|
|---|
| 94 | {
|
|---|
| 95 | dummy_gc_t *dgc;
|
|---|
| 96 | gfx_context_t *gc;
|
|---|
| 97 | ui_resource_t *resource = NULL;
|
|---|
| 98 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 99 | ui_menu_t *menu = NULL;
|
|---|
| 100 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 101 | errno_t rc;
|
|---|
| 102 |
|
|---|
| 103 | rc = dummygc_create(&dgc);
|
|---|
| 104 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 105 |
|
|---|
| 106 | gc = dummygc_get_ctx(dgc);
|
|---|
| 107 |
|
|---|
| 108 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 109 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 110 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 111 |
|
|---|
| 112 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 113 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 114 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 115 |
|
|---|
| 116 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 117 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 118 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 119 |
|
|---|
| 120 | rc = ui_menu_entry_sep_create(menu, &mentry);
|
|---|
| 121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 122 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 123 |
|
|---|
| 124 | /* Just for sake of test. Menu entry is destroyed along with menu */
|
|---|
| 125 | ui_menu_entry_destroy(mentry);
|
|---|
| 126 |
|
|---|
| 127 | ui_menu_bar_destroy(mbar);
|
|---|
| 128 | dummygc_destroy(dgc);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /** ui_menu_bar_destroy() can take NULL argument (no-op) */
|
|---|
| 132 | PCUT_TEST(destroy_null)
|
|---|
| 133 | {
|
|---|
| 134 | ui_menu_bar_destroy(NULL);
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | /** ui_menu_entry_set_cb() .. */
|
|---|
| 138 | PCUT_TEST(set_cb)
|
|---|
| 139 | {
|
|---|
| 140 | dummy_gc_t *dgc;
|
|---|
| 141 | gfx_context_t *gc;
|
|---|
| 142 | ui_resource_t *resource = NULL;
|
|---|
| 143 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 144 | ui_menu_t *menu = NULL;
|
|---|
| 145 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 146 | test_resp_t resp;
|
|---|
| 147 | errno_t rc;
|
|---|
| 148 |
|
|---|
| 149 | rc = dummygc_create(&dgc);
|
|---|
| 150 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 151 |
|
|---|
| 152 | gc = dummygc_get_ctx(dgc);
|
|---|
| 153 |
|
|---|
| 154 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 155 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 156 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 157 |
|
|---|
| 158 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 159 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 160 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 161 |
|
|---|
| 162 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 163 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 164 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 165 |
|
|---|
| 166 | rc = ui_menu_entry_create(menu, "Foo", "F1", &mentry);
|
|---|
| 167 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 168 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 169 |
|
|---|
| 170 | ui_menu_entry_set_cb(mentry, test_entry_cb, &resp);
|
|---|
| 171 |
|
|---|
| 172 | resp.activated = false;
|
|---|
| 173 | ui_menu_entry_cb(mentry);
|
|---|
| 174 | PCUT_ASSERT_TRUE(resp.activated);
|
|---|
| 175 |
|
|---|
| 176 | ui_menu_bar_destroy(mbar);
|
|---|
| 177 | ui_resource_destroy(resource);
|
|---|
| 178 | dummygc_destroy(dgc);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | /** ui_menu_entry_first() / ui_menu_entry_next() iterate over entries */
|
|---|
| 182 | PCUT_TEST(first_next)
|
|---|
| 183 | {
|
|---|
| 184 | dummy_gc_t *dgc;
|
|---|
| 185 | gfx_context_t *gc;
|
|---|
| 186 | ui_resource_t *resource = NULL;
|
|---|
| 187 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 188 | ui_menu_t *menu = NULL;
|
|---|
| 189 | ui_menu_entry_t *entry1 = NULL;
|
|---|
| 190 | ui_menu_entry_t *entry2 = NULL;
|
|---|
| 191 | ui_menu_entry_t *e;
|
|---|
| 192 | errno_t rc;
|
|---|
| 193 |
|
|---|
| 194 | rc = dummygc_create(&dgc);
|
|---|
| 195 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 196 |
|
|---|
| 197 | gc = dummygc_get_ctx(dgc);
|
|---|
| 198 |
|
|---|
| 199 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 200 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 201 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 202 |
|
|---|
| 203 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 204 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 205 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 206 |
|
|---|
| 207 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 209 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 210 |
|
|---|
| 211 | rc = ui_menu_entry_create(menu, "Foo", "F1", &entry1);
|
|---|
| 212 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 213 | PCUT_ASSERT_NOT_NULL(entry1);
|
|---|
| 214 |
|
|---|
| 215 | rc = ui_menu_entry_create(menu, "Bar", "F2", &entry2);
|
|---|
| 216 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 217 | PCUT_ASSERT_NOT_NULL(entry2);
|
|---|
| 218 |
|
|---|
| 219 | e = ui_menu_entry_first(menu);
|
|---|
| 220 | PCUT_ASSERT_EQUALS(entry1, e);
|
|---|
| 221 |
|
|---|
| 222 | e = ui_menu_entry_next(e);
|
|---|
| 223 | PCUT_ASSERT_EQUALS(entry2, e);
|
|---|
| 224 |
|
|---|
| 225 | e = ui_menu_entry_next(e);
|
|---|
| 226 | PCUT_ASSERT_NULL(e);
|
|---|
| 227 |
|
|---|
| 228 | ui_menu_bar_destroy(mbar);
|
|---|
| 229 | ui_resource_destroy(resource);
|
|---|
| 230 | dummygc_destroy(dgc);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /** ui_menu_entry_widths() / ui_menu_entry_height() */
|
|---|
| 234 | PCUT_TEST(widths_height)
|
|---|
| 235 | {
|
|---|
| 236 | dummy_gc_t *dgc;
|
|---|
| 237 | gfx_context_t *gc;
|
|---|
| 238 | ui_resource_t *resource = NULL;
|
|---|
| 239 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 240 | ui_menu_t *menu = NULL;
|
|---|
| 241 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 242 | gfx_coord_t caption_w;
|
|---|
| 243 | gfx_coord_t shortcut_w;
|
|---|
| 244 | gfx_coord_t width;
|
|---|
| 245 | gfx_coord_t height;
|
|---|
| 246 | errno_t rc;
|
|---|
| 247 |
|
|---|
| 248 | rc = dummygc_create(&dgc);
|
|---|
| 249 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 250 |
|
|---|
| 251 | gc = dummygc_get_ctx(dgc);
|
|---|
| 252 |
|
|---|
| 253 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 254 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 255 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 256 |
|
|---|
| 257 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 258 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 259 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 260 |
|
|---|
| 261 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 262 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 263 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 264 |
|
|---|
| 265 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 266 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 267 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 268 |
|
|---|
| 269 | ui_menu_entry_column_widths(mentry, &caption_w, &shortcut_w);
|
|---|
| 270 | PCUT_ASSERT_INT_EQUALS(11, caption_w);
|
|---|
| 271 | PCUT_ASSERT_INT_EQUALS(10, shortcut_w);
|
|---|
| 272 |
|
|---|
| 273 | width = ui_menu_entry_calc_width(menu, caption_w, shortcut_w);
|
|---|
| 274 | PCUT_ASSERT_INT_EQUALS(4 + 11 + 8 + 10 + 4, width);
|
|---|
| 275 |
|
|---|
| 276 | height = ui_menu_entry_height(mentry);
|
|---|
| 277 | PCUT_ASSERT_INT_EQUALS(13 + 8, height);
|
|---|
| 278 |
|
|---|
| 279 | ui_menu_bar_destroy(mbar);
|
|---|
| 280 | ui_resource_destroy(resource);
|
|---|
| 281 | dummygc_destroy(dgc);
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | /** Paint menu entry */
|
|---|
| 285 | PCUT_TEST(paint)
|
|---|
| 286 | {
|
|---|
| 287 | dummy_gc_t *dgc;
|
|---|
| 288 | gfx_context_t *gc;
|
|---|
| 289 | ui_resource_t *resource = NULL;
|
|---|
| 290 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 291 | ui_menu_t *menu = NULL;
|
|---|
| 292 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 293 | gfx_coord2_t pos;
|
|---|
| 294 | errno_t rc;
|
|---|
| 295 |
|
|---|
| 296 | rc = dummygc_create(&dgc);
|
|---|
| 297 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 298 |
|
|---|
| 299 | gc = dummygc_get_ctx(dgc);
|
|---|
| 300 |
|
|---|
| 301 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 302 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 303 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 304 |
|
|---|
| 305 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 306 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 307 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 308 |
|
|---|
| 309 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 310 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 311 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 312 |
|
|---|
| 313 | rc = ui_menu_entry_create(menu, "Foo", "F1", &mentry);
|
|---|
| 314 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 315 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 316 |
|
|---|
| 317 | pos.x = 0;
|
|---|
| 318 | pos.y = 0;
|
|---|
| 319 | rc = ui_menu_entry_paint(mentry, &pos);
|
|---|
| 320 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 321 |
|
|---|
| 322 | ui_menu_bar_destroy(mbar);
|
|---|
| 323 | ui_resource_destroy(resource);
|
|---|
| 324 | dummygc_destroy(dgc);
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | /** Press and release activates menu entry */
|
|---|
| 328 | PCUT_TEST(press_release)
|
|---|
| 329 | {
|
|---|
| 330 | dummy_gc_t *dgc;
|
|---|
| 331 | gfx_context_t *gc;
|
|---|
| 332 | ui_resource_t *resource = NULL;
|
|---|
| 333 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 334 | ui_menu_t *menu = NULL;
|
|---|
| 335 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 336 | gfx_coord2_t pos;
|
|---|
| 337 | test_resp_t resp;
|
|---|
| 338 | errno_t rc;
|
|---|
| 339 |
|
|---|
| 340 | rc = dummygc_create(&dgc);
|
|---|
| 341 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 342 |
|
|---|
| 343 | gc = dummygc_get_ctx(dgc);
|
|---|
| 344 |
|
|---|
| 345 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 346 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 347 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 348 |
|
|---|
| 349 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 350 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 351 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 352 |
|
|---|
| 353 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 354 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 355 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 356 |
|
|---|
| 357 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 358 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 359 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 360 |
|
|---|
| 361 | ui_menu_entry_set_cb(mentry, test_entry_cb, &resp);
|
|---|
| 362 | resp.activated = false;
|
|---|
| 363 |
|
|---|
| 364 | pos.x = 0;
|
|---|
| 365 | pos.y = 0;
|
|---|
| 366 | ui_menu_entry_press(mentry, &pos);
|
|---|
| 367 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 368 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 369 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 370 |
|
|---|
| 371 | ui_menu_entry_release(mentry);
|
|---|
| 372 | PCUT_ASSERT_FALSE(mentry->held);
|
|---|
| 373 | PCUT_ASSERT_TRUE(resp.activated);
|
|---|
| 374 |
|
|---|
| 375 | ui_menu_bar_destroy(mbar);
|
|---|
| 376 | ui_resource_destroy(resource);
|
|---|
| 377 | dummygc_destroy(dgc);
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | /** Press, leave and release does not activate entry */
|
|---|
| 381 | PCUT_TEST(press_leave_release)
|
|---|
| 382 | {
|
|---|
| 383 | dummy_gc_t *dgc;
|
|---|
| 384 | gfx_context_t *gc;
|
|---|
| 385 | ui_resource_t *resource = NULL;
|
|---|
| 386 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 387 | ui_menu_t *menu = NULL;
|
|---|
| 388 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 389 | gfx_coord2_t pos;
|
|---|
| 390 | test_resp_t resp;
|
|---|
| 391 | errno_t rc;
|
|---|
| 392 |
|
|---|
| 393 | rc = dummygc_create(&dgc);
|
|---|
| 394 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 395 |
|
|---|
| 396 | gc = dummygc_get_ctx(dgc);
|
|---|
| 397 |
|
|---|
| 398 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 399 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 400 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 401 |
|
|---|
| 402 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 403 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 404 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 405 |
|
|---|
| 406 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 407 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 408 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 409 |
|
|---|
| 410 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 411 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 412 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 413 |
|
|---|
| 414 | ui_menu_entry_set_cb(mentry, test_entry_cb, &resp);
|
|---|
| 415 | resp.activated = false;
|
|---|
| 416 |
|
|---|
| 417 | pos.x = 0;
|
|---|
| 418 | pos.y = 0;
|
|---|
| 419 | ui_menu_entry_press(mentry, &pos);
|
|---|
| 420 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 421 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 422 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 423 |
|
|---|
| 424 | ui_menu_entry_leave(mentry, &pos);
|
|---|
| 425 | PCUT_ASSERT_FALSE(mentry->inside);
|
|---|
| 426 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 427 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 428 |
|
|---|
| 429 | ui_menu_entry_release(mentry);
|
|---|
| 430 | PCUT_ASSERT_FALSE(mentry->held);
|
|---|
| 431 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 432 |
|
|---|
| 433 | ui_menu_bar_destroy(mbar);
|
|---|
| 434 | ui_resource_destroy(resource);
|
|---|
| 435 | dummygc_destroy(dgc);
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /** Press, leave, enter and release activates menu entry */
|
|---|
| 439 | PCUT_TEST(press_leave_enter_release)
|
|---|
| 440 | {
|
|---|
| 441 | dummy_gc_t *dgc;
|
|---|
| 442 | gfx_context_t *gc;
|
|---|
| 443 | ui_resource_t *resource = NULL;
|
|---|
| 444 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 445 | ui_menu_t *menu = NULL;
|
|---|
| 446 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 447 | gfx_coord2_t pos;
|
|---|
| 448 | test_resp_t resp;
|
|---|
| 449 | errno_t rc;
|
|---|
| 450 |
|
|---|
| 451 | rc = dummygc_create(&dgc);
|
|---|
| 452 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 453 |
|
|---|
| 454 | gc = dummygc_get_ctx(dgc);
|
|---|
| 455 |
|
|---|
| 456 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 457 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 458 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 459 |
|
|---|
| 460 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 461 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 462 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 463 |
|
|---|
| 464 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 465 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 466 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 467 |
|
|---|
| 468 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 469 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 470 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 471 |
|
|---|
| 472 | ui_menu_entry_set_cb(mentry, test_entry_cb, &resp);
|
|---|
| 473 | resp.activated = false;
|
|---|
| 474 |
|
|---|
| 475 | pos.x = 0;
|
|---|
| 476 | pos.y = 0;
|
|---|
| 477 | ui_menu_entry_press(mentry, &pos);
|
|---|
| 478 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 479 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 480 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 481 |
|
|---|
| 482 | ui_menu_entry_leave(mentry, &pos);
|
|---|
| 483 | PCUT_ASSERT_FALSE(mentry->inside);
|
|---|
| 484 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 485 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 486 |
|
|---|
| 487 | ui_menu_entry_enter(mentry, &pos);
|
|---|
| 488 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 489 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 490 | PCUT_ASSERT_FALSE(resp.activated);
|
|---|
| 491 |
|
|---|
| 492 | ui_menu_entry_release(mentry);
|
|---|
| 493 | PCUT_ASSERT_FALSE(mentry->held);
|
|---|
| 494 | PCUT_ASSERT_TRUE(resp.activated);
|
|---|
| 495 |
|
|---|
| 496 | ui_menu_bar_destroy(mbar);
|
|---|
| 497 | ui_resource_destroy(resource);
|
|---|
| 498 | dummygc_destroy(dgc);
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | /** Press event inside menu entry */
|
|---|
| 502 | PCUT_TEST(pos_press_inside)
|
|---|
| 503 | {
|
|---|
| 504 | dummy_gc_t *dgc;
|
|---|
| 505 | gfx_context_t *gc;
|
|---|
| 506 | ui_resource_t *resource = NULL;
|
|---|
| 507 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 508 | ui_menu_t *menu = NULL;
|
|---|
| 509 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 510 | gfx_coord2_t pos;
|
|---|
| 511 | pos_event_t event;
|
|---|
| 512 | errno_t rc;
|
|---|
| 513 |
|
|---|
| 514 | rc = dummygc_create(&dgc);
|
|---|
| 515 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 516 |
|
|---|
| 517 | gc = dummygc_get_ctx(dgc);
|
|---|
| 518 |
|
|---|
| 519 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 520 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 521 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 522 |
|
|---|
| 523 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 524 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 525 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 526 |
|
|---|
| 527 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 528 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 529 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 530 |
|
|---|
| 531 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 532 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 533 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 534 |
|
|---|
| 535 | pos.x = 0;
|
|---|
| 536 | pos.y = 0;
|
|---|
| 537 |
|
|---|
| 538 | event.type = POS_PRESS;
|
|---|
| 539 | event.hpos = 4;
|
|---|
| 540 | event.vpos = 4;
|
|---|
| 541 |
|
|---|
| 542 | ui_menu_entry_pos_event(mentry, &pos, &event);
|
|---|
| 543 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 544 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 545 |
|
|---|
| 546 | ui_menu_bar_destroy(mbar);
|
|---|
| 547 | ui_resource_destroy(resource);
|
|---|
| 548 | dummygc_destroy(dgc);
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /** Press event outside menu entry */
|
|---|
| 552 | PCUT_TEST(pos_press_outside)
|
|---|
| 553 | {
|
|---|
| 554 | dummy_gc_t *dgc;
|
|---|
| 555 | gfx_context_t *gc;
|
|---|
| 556 | ui_resource_t *resource = NULL;
|
|---|
| 557 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 558 | ui_menu_t *menu = NULL;
|
|---|
| 559 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 560 | gfx_coord2_t pos;
|
|---|
| 561 | pos_event_t event;
|
|---|
| 562 | errno_t rc;
|
|---|
| 563 |
|
|---|
| 564 | rc = dummygc_create(&dgc);
|
|---|
| 565 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 566 |
|
|---|
| 567 | gc = dummygc_get_ctx(dgc);
|
|---|
| 568 |
|
|---|
| 569 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 570 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 571 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 572 |
|
|---|
| 573 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 574 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 575 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 576 |
|
|---|
| 577 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 578 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 579 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 580 |
|
|---|
| 581 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 582 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 583 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 584 |
|
|---|
| 585 | pos.x = 0;
|
|---|
| 586 | pos.y = 0;
|
|---|
| 587 |
|
|---|
| 588 | event.type = POS_PRESS;
|
|---|
| 589 | event.hpos = 40;
|
|---|
| 590 | event.vpos = 20;
|
|---|
| 591 |
|
|---|
| 592 | ui_menu_entry_pos_event(mentry, &pos, &event);
|
|---|
| 593 | PCUT_ASSERT_FALSE(mentry->inside);
|
|---|
| 594 | PCUT_ASSERT_FALSE(mentry->held);
|
|---|
| 595 |
|
|---|
| 596 | ui_menu_bar_destroy(mbar);
|
|---|
| 597 | ui_resource_destroy(resource);
|
|---|
| 598 | dummygc_destroy(dgc);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /** Position event moving out of menu entry */
|
|---|
| 602 | PCUT_TEST(pos_move_out)
|
|---|
| 603 | {
|
|---|
| 604 | dummy_gc_t *dgc;
|
|---|
| 605 | gfx_context_t *gc;
|
|---|
| 606 | ui_resource_t *resource = NULL;
|
|---|
| 607 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 608 | ui_menu_t *menu = NULL;
|
|---|
| 609 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 610 | gfx_coord2_t pos;
|
|---|
| 611 | pos_event_t event;
|
|---|
| 612 | errno_t rc;
|
|---|
| 613 |
|
|---|
| 614 | rc = dummygc_create(&dgc);
|
|---|
| 615 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 616 |
|
|---|
| 617 | gc = dummygc_get_ctx(dgc);
|
|---|
| 618 |
|
|---|
| 619 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 620 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 621 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 622 |
|
|---|
| 623 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 624 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 625 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 626 |
|
|---|
| 627 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 628 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 629 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 630 |
|
|---|
| 631 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 632 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 633 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 634 |
|
|---|
| 635 | pos.x = 0;
|
|---|
| 636 | pos.y = 0;
|
|---|
| 637 | ui_menu_entry_press(mentry, &pos);
|
|---|
| 638 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 639 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 640 |
|
|---|
| 641 | event.type = POS_UPDATE;
|
|---|
| 642 | event.hpos = 40;
|
|---|
| 643 | event.vpos = 20;
|
|---|
| 644 |
|
|---|
| 645 | ui_menu_entry_pos_event(mentry, &pos, &event);
|
|---|
| 646 | PCUT_ASSERT_FALSE(mentry->inside);
|
|---|
| 647 | PCUT_ASSERT_TRUE(mentry->held);
|
|---|
| 648 |
|
|---|
| 649 | ui_menu_bar_destroy(mbar);
|
|---|
| 650 | ui_resource_destroy(resource);
|
|---|
| 651 | dummygc_destroy(dgc);
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | /** Position event moving inside menu entry */
|
|---|
| 655 | PCUT_TEST(pos_move_in)
|
|---|
| 656 | {
|
|---|
| 657 | dummy_gc_t *dgc;
|
|---|
| 658 | gfx_context_t *gc;
|
|---|
| 659 | ui_resource_t *resource = NULL;
|
|---|
| 660 | ui_menu_bar_t *mbar = NULL;
|
|---|
| 661 | ui_menu_t *menu = NULL;
|
|---|
| 662 | ui_menu_entry_t *mentry = NULL;
|
|---|
| 663 | gfx_coord2_t pos;
|
|---|
| 664 | pos_event_t event;
|
|---|
| 665 | errno_t rc;
|
|---|
| 666 |
|
|---|
| 667 | rc = dummygc_create(&dgc);
|
|---|
| 668 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 669 |
|
|---|
| 670 | gc = dummygc_get_ctx(dgc);
|
|---|
| 671 |
|
|---|
| 672 | rc = ui_resource_create(gc, false, &resource);
|
|---|
| 673 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 674 | PCUT_ASSERT_NOT_NULL(resource);
|
|---|
| 675 |
|
|---|
| 676 | rc = ui_menu_bar_create(resource, &mbar);
|
|---|
| 677 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 678 | PCUT_ASSERT_NOT_NULL(mbar);
|
|---|
| 679 |
|
|---|
| 680 | rc = ui_menu_create(mbar, "Test", &menu);
|
|---|
| 681 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 682 | PCUT_ASSERT_NOT_NULL(menu);
|
|---|
| 683 |
|
|---|
| 684 | rc = ui_menu_entry_create(menu, "X", "Y", &mentry);
|
|---|
| 685 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 686 | PCUT_ASSERT_NOT_NULL(mentry);
|
|---|
| 687 |
|
|---|
| 688 | event.type = POS_UPDATE;
|
|---|
| 689 | event.hpos = 4;
|
|---|
| 690 | event.vpos = 4;
|
|---|
| 691 |
|
|---|
| 692 | pos.x = 0;
|
|---|
| 693 | pos.y = 0;
|
|---|
| 694 |
|
|---|
| 695 | ui_menu_entry_pos_event(mentry, &pos, &event);
|
|---|
| 696 | PCUT_ASSERT_TRUE(mentry->inside);
|
|---|
| 697 | PCUT_ASSERT_FALSE(mentry->held);
|
|---|
| 698 |
|
|---|
| 699 | ui_menu_bar_destroy(mbar);
|
|---|
| 700 | ui_resource_destroy(resource);
|
|---|
| 701 | dummygc_destroy(dgc);
|
|---|
| 702 | }
|
|---|
| 703 |
|
|---|
| 704 | static void test_entry_cb(ui_menu_entry_t *mentry, void *arg)
|
|---|
| 705 | {
|
|---|
| 706 | test_resp_t *resp = (test_resp_t *) arg;
|
|---|
| 707 |
|
|---|
| 708 | resp->activated = true;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | PCUT_EXPORT(menuentry);
|
|---|