| 1 | /*
|
|---|
| 2 | * Copyright (c) 2020 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 <io/kbd_event.h>
|
|---|
| 32 | #include <io/pos_event.h>
|
|---|
| 33 | #include <mem.h>
|
|---|
| 34 | #include <pcut/pcut.h>
|
|---|
| 35 | #include <stdbool.h>
|
|---|
| 36 | #include <ui/resource.h>
|
|---|
| 37 | #include <ui/ui.h>
|
|---|
| 38 | #include <ui/window.h>
|
|---|
| 39 | #include "../private/window.h"
|
|---|
| 40 |
|
|---|
| 41 | PCUT_INIT;
|
|---|
| 42 |
|
|---|
| 43 | PCUT_TEST_SUITE(window);
|
|---|
| 44 |
|
|---|
| 45 | static void test_window_close(ui_window_t *, void *);
|
|---|
| 46 | static void test_window_focus(ui_window_t *, void *);
|
|---|
| 47 | static void test_window_kbd(ui_window_t *, void *, kbd_event_t *);
|
|---|
| 48 | static void test_window_pos(ui_window_t *, void *, pos_event_t *);
|
|---|
| 49 | static void test_window_unfocus(ui_window_t *, void *);
|
|---|
| 50 |
|
|---|
| 51 | static ui_window_cb_t test_window_cb = {
|
|---|
| 52 | .close = test_window_close,
|
|---|
| 53 | .focus = test_window_focus,
|
|---|
| 54 | .kbd = test_window_kbd,
|
|---|
| 55 | .pos = test_window_pos,
|
|---|
| 56 | .unfocus = test_window_unfocus
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | static ui_window_cb_t dummy_window_cb = {
|
|---|
| 60 | };
|
|---|
| 61 |
|
|---|
| 62 | typedef struct {
|
|---|
| 63 | bool close;
|
|---|
| 64 | bool focus;
|
|---|
| 65 | bool kbd;
|
|---|
| 66 | kbd_event_t kbd_event;
|
|---|
| 67 | bool pos;
|
|---|
| 68 | pos_event_t pos_event;
|
|---|
| 69 | bool unfocus;
|
|---|
| 70 | } test_cb_resp_t;
|
|---|
| 71 |
|
|---|
| 72 | /** Create and destroy window */
|
|---|
| 73 | PCUT_TEST(create_destroy)
|
|---|
| 74 | {
|
|---|
| 75 | errno_t rc;
|
|---|
| 76 | ui_t *ui = NULL;
|
|---|
| 77 | ui_wnd_params_t params;
|
|---|
| 78 | ui_window_t *window = NULL;
|
|---|
| 79 |
|
|---|
| 80 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 81 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 82 |
|
|---|
| 83 | ui_wnd_params_init(¶ms);
|
|---|
| 84 | params.caption = "Hello";
|
|---|
| 85 |
|
|---|
| 86 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 87 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 88 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 89 |
|
|---|
| 90 | ui_window_destroy(window);
|
|---|
| 91 | ui_destroy(ui);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /** ui_window_destroy() can take NULL argument (no-op) */
|
|---|
| 95 | PCUT_TEST(destroy_null)
|
|---|
| 96 | {
|
|---|
| 97 | ui_window_destroy(NULL);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /** ui_window_get_res/gc/rect() return valid objects */
|
|---|
| 101 | PCUT_TEST(get_res_gc_rect)
|
|---|
| 102 | {
|
|---|
| 103 | errno_t rc;
|
|---|
| 104 | ui_t *ui = NULL;
|
|---|
| 105 | ui_wnd_params_t params;
|
|---|
| 106 | ui_window_t *window = NULL;
|
|---|
| 107 | ui_resource_t *res;
|
|---|
| 108 | gfx_context_t *gc;
|
|---|
| 109 | gfx_rect_t rect;
|
|---|
| 110 |
|
|---|
| 111 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 113 |
|
|---|
| 114 | ui_wnd_params_init(¶ms);
|
|---|
| 115 | params.caption = "Hello";
|
|---|
| 116 |
|
|---|
| 117 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 118 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 119 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 120 |
|
|---|
| 121 | res = ui_window_get_res(window);
|
|---|
| 122 | PCUT_ASSERT_NOT_NULL(res);
|
|---|
| 123 |
|
|---|
| 124 | gc = ui_window_get_gc(window);
|
|---|
| 125 | PCUT_ASSERT_NOT_NULL(gc);
|
|---|
| 126 |
|
|---|
| 127 | ui_window_get_app_rect(window, &rect);
|
|---|
| 128 |
|
|---|
| 129 | ui_window_destroy(window);
|
|---|
| 130 | ui_destroy(ui);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** ui_window_close() calls close callback set via ui_window_set_cb() */
|
|---|
| 134 | PCUT_TEST(close)
|
|---|
| 135 | {
|
|---|
| 136 | errno_t rc;
|
|---|
| 137 | ui_t *ui = NULL;
|
|---|
| 138 | ui_wnd_params_t params;
|
|---|
| 139 | ui_window_t *window = NULL;
|
|---|
| 140 | test_cb_resp_t resp;
|
|---|
| 141 |
|
|---|
| 142 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 143 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 144 |
|
|---|
| 145 | ui_wnd_params_init(¶ms);
|
|---|
| 146 | params.caption = "Hello";
|
|---|
| 147 |
|
|---|
| 148 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 149 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 150 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 151 |
|
|---|
| 152 | /* Close callback with no callbacks set */
|
|---|
| 153 | ui_window_close(window);
|
|---|
| 154 |
|
|---|
| 155 | /* Close callback with close callback not implemented */
|
|---|
| 156 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
|---|
| 157 | ui_window_close(window);
|
|---|
| 158 |
|
|---|
| 159 | /* Close callback with real callback set */
|
|---|
| 160 | resp.close = false;
|
|---|
| 161 | ui_window_set_cb(window, &test_window_cb, &resp);
|
|---|
| 162 | ui_window_close(window);
|
|---|
| 163 | PCUT_ASSERT_TRUE(resp.close);
|
|---|
| 164 |
|
|---|
| 165 | ui_window_destroy(window);
|
|---|
| 166 | ui_destroy(ui);
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /** ui_window_focus() calls focus callback set via ui_window_set_cb() */
|
|---|
| 170 | PCUT_TEST(focus)
|
|---|
| 171 | {
|
|---|
| 172 | errno_t rc;
|
|---|
| 173 | ui_t *ui = NULL;
|
|---|
| 174 | ui_wnd_params_t params;
|
|---|
| 175 | ui_window_t *window = NULL;
|
|---|
| 176 | test_cb_resp_t resp;
|
|---|
| 177 |
|
|---|
| 178 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 179 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 180 |
|
|---|
| 181 | ui_wnd_params_init(¶ms);
|
|---|
| 182 | params.caption = "Hello";
|
|---|
| 183 |
|
|---|
| 184 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 185 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 186 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 187 |
|
|---|
| 188 | /* Focus callback with no callbacks set */
|
|---|
| 189 | ui_window_focus(window);
|
|---|
| 190 |
|
|---|
| 191 | /* Focus callback with focus callback not implemented */
|
|---|
| 192 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
|---|
| 193 | ui_window_focus(window);
|
|---|
| 194 |
|
|---|
| 195 | /* Focus callback with real callback set */
|
|---|
| 196 | resp.close = false;
|
|---|
| 197 | ui_window_set_cb(window, &test_window_cb, &resp);
|
|---|
| 198 | ui_window_focus(window);
|
|---|
| 199 | PCUT_ASSERT_TRUE(resp.focus);
|
|---|
| 200 |
|
|---|
| 201 | ui_window_destroy(window);
|
|---|
| 202 | ui_destroy(ui);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** ui_window_kbd() calls kbd callback set via ui_window_set_cb() */
|
|---|
| 206 | PCUT_TEST(kbd)
|
|---|
| 207 | {
|
|---|
| 208 | errno_t rc;
|
|---|
| 209 | ui_t *ui = NULL;
|
|---|
| 210 | ui_wnd_params_t params;
|
|---|
| 211 | ui_window_t *window = NULL;
|
|---|
| 212 | kbd_event_t kbd_event;
|
|---|
| 213 | test_cb_resp_t resp;
|
|---|
| 214 |
|
|---|
| 215 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 216 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 217 |
|
|---|
| 218 | ui_wnd_params_init(¶ms);
|
|---|
| 219 | params.caption = "Hello";
|
|---|
| 220 |
|
|---|
| 221 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 222 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 223 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 224 |
|
|---|
| 225 | kbd_event.type = POS_PRESS;
|
|---|
| 226 | kbd_event.key = KC_X;
|
|---|
| 227 | kbd_event.mods = 0;
|
|---|
| 228 | kbd_event.c = 'x';
|
|---|
| 229 |
|
|---|
| 230 | /* Kbd callback with no callbacks set */
|
|---|
| 231 | ui_window_kbd(window, &kbd_event);
|
|---|
| 232 |
|
|---|
| 233 | /* Kbd callback with kbd callback not implemented */
|
|---|
| 234 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
|---|
| 235 | ui_window_kbd(window, &kbd_event);
|
|---|
| 236 |
|
|---|
| 237 | /* Kbd callback with real callback set */
|
|---|
| 238 | resp.kbd = false;
|
|---|
| 239 | ui_window_set_cb(window, &test_window_cb, &resp);
|
|---|
| 240 | ui_window_kbd(window, &kbd_event);
|
|---|
| 241 | PCUT_ASSERT_TRUE(resp.kbd);
|
|---|
| 242 | PCUT_ASSERT_EQUALS(kbd_event.type, resp.kbd_event.type);
|
|---|
| 243 | PCUT_ASSERT_INT_EQUALS(kbd_event.key, resp.kbd_event.key);
|
|---|
| 244 | PCUT_ASSERT_INT_EQUALS(kbd_event.mods, resp.kbd_event.mods);
|
|---|
| 245 | PCUT_ASSERT_INT_EQUALS(kbd_event.c, resp.kbd_event.c);
|
|---|
| 246 |
|
|---|
| 247 | ui_window_destroy(window);
|
|---|
| 248 | ui_destroy(ui);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /** ui_window_pos() calls pos callback set via ui_window_set_cb() */
|
|---|
| 252 | PCUT_TEST(pos)
|
|---|
| 253 | {
|
|---|
| 254 | errno_t rc;
|
|---|
| 255 | ui_t *ui = NULL;
|
|---|
| 256 | ui_wnd_params_t params;
|
|---|
| 257 | ui_window_t *window = NULL;
|
|---|
| 258 | pos_event_t pos_event;
|
|---|
| 259 | test_cb_resp_t resp;
|
|---|
| 260 |
|
|---|
| 261 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 262 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 263 |
|
|---|
| 264 | ui_wnd_params_init(¶ms);
|
|---|
| 265 | params.caption = "Hello";
|
|---|
| 266 |
|
|---|
| 267 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 268 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 269 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 270 |
|
|---|
| 271 | pos_event.pos_id = 1;
|
|---|
| 272 | pos_event.type = POS_PRESS;
|
|---|
| 273 | pos_event.btn_num = 2;
|
|---|
| 274 | pos_event.hpos = 3;
|
|---|
| 275 | pos_event.vpos = 4;
|
|---|
| 276 |
|
|---|
| 277 | /* Pos callback with no callbacks set */
|
|---|
| 278 | ui_window_pos(window, &pos_event);
|
|---|
| 279 |
|
|---|
| 280 | /* Pos callback with pos callback not implemented */
|
|---|
| 281 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
|---|
| 282 | ui_window_pos(window, &pos_event);
|
|---|
| 283 |
|
|---|
| 284 | /* Pos callback with real callback set */
|
|---|
| 285 | resp.pos = false;
|
|---|
| 286 | ui_window_set_cb(window, &test_window_cb, &resp);
|
|---|
| 287 | ui_window_pos(window, &pos_event);
|
|---|
| 288 | PCUT_ASSERT_TRUE(resp.pos);
|
|---|
| 289 | PCUT_ASSERT_INT_EQUALS(pos_event.pos_id, resp.pos_event.pos_id);
|
|---|
| 290 | PCUT_ASSERT_EQUALS(pos_event.type, resp.pos_event.type);
|
|---|
| 291 | PCUT_ASSERT_INT_EQUALS(pos_event.btn_num, resp.pos_event.btn_num);
|
|---|
| 292 | PCUT_ASSERT_INT_EQUALS(pos_event.hpos, resp.pos_event.hpos);
|
|---|
| 293 | PCUT_ASSERT_INT_EQUALS(pos_event.vpos, resp.pos_event.vpos);
|
|---|
| 294 |
|
|---|
| 295 | ui_window_destroy(window);
|
|---|
| 296 | ui_destroy(ui);
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | /** ui_window_unfocus() calls unfocus callback set via ui_window_set_cb() */
|
|---|
| 300 | PCUT_TEST(unfocus)
|
|---|
| 301 | {
|
|---|
| 302 | errno_t rc;
|
|---|
| 303 | ui_t *ui = NULL;
|
|---|
| 304 | ui_wnd_params_t params;
|
|---|
| 305 | ui_window_t *window = NULL;
|
|---|
| 306 | test_cb_resp_t resp;
|
|---|
| 307 |
|
|---|
| 308 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 309 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 310 |
|
|---|
| 311 | ui_wnd_params_init(¶ms);
|
|---|
| 312 | params.caption = "Hello";
|
|---|
| 313 |
|
|---|
| 314 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 315 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 316 | PCUT_ASSERT_NOT_NULL(window);
|
|---|
| 317 |
|
|---|
| 318 | /* Unfocus callback with no callbacks set */
|
|---|
| 319 | ui_window_unfocus(window);
|
|---|
| 320 |
|
|---|
| 321 | /* Unfocus callback with unfocus callback not implemented */
|
|---|
| 322 | ui_window_set_cb(window, &dummy_window_cb, NULL);
|
|---|
| 323 | ui_window_unfocus(window);
|
|---|
| 324 |
|
|---|
| 325 | /* Unfocus callback with real callback set */
|
|---|
| 326 | resp.close = false;
|
|---|
| 327 | ui_window_set_cb(window, &test_window_cb, &resp);
|
|---|
| 328 | ui_window_unfocus(window);
|
|---|
| 329 | PCUT_ASSERT_TRUE(resp.unfocus);
|
|---|
| 330 |
|
|---|
| 331 | ui_window_destroy(window);
|
|---|
| 332 | ui_destroy(ui);
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | static void test_window_close(ui_window_t *window, void *arg)
|
|---|
| 336 | {
|
|---|
| 337 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
|---|
| 338 |
|
|---|
| 339 | resp->close = true;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | static void test_window_focus(ui_window_t *window, void *arg)
|
|---|
| 343 | {
|
|---|
| 344 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
|---|
| 345 |
|
|---|
| 346 | resp->focus = true;
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | static void test_window_kbd(ui_window_t *window, void *arg,
|
|---|
| 350 | kbd_event_t *event)
|
|---|
| 351 | {
|
|---|
| 352 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
|---|
| 353 |
|
|---|
| 354 | resp->kbd = true;
|
|---|
| 355 | resp->kbd_event = *event;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | static void test_window_pos(ui_window_t *window, void *arg,
|
|---|
| 359 | pos_event_t *event)
|
|---|
| 360 | {
|
|---|
| 361 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
|---|
| 362 |
|
|---|
| 363 | resp->pos = true;
|
|---|
| 364 | resp->pos_event = *event;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | static void test_window_unfocus(ui_window_t *window, void *arg)
|
|---|
| 368 | {
|
|---|
| 369 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
|---|
| 370 |
|
|---|
| 371 | resp->unfocus = true;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | PCUT_EXPORT(window);
|
|---|