[3c8c580] | 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 <gfx/render.h>
|
---|
| 32 | #include <io/kbd_event.h>
|
---|
| 33 | #include <io/pos_event.h>
|
---|
| 34 | #include <mem.h>
|
---|
| 35 | #include <pcut/pcut.h>
|
---|
| 36 | #include <stdbool.h>
|
---|
| 37 | #include <ui/control.h>
|
---|
| 38 | #include <ui/popup.h>
|
---|
| 39 | #include <ui/resource.h>
|
---|
| 40 | #include <ui/ui.h>
|
---|
| 41 | #include <ui/window.h>
|
---|
| 42 | #include "../private/popup.h"
|
---|
| 43 | #include "../private/window.h"
|
---|
| 44 |
|
---|
| 45 | PCUT_INIT;
|
---|
| 46 |
|
---|
| 47 | PCUT_TEST_SUITE(popup);
|
---|
| 48 |
|
---|
| 49 | static void test_popup_close(ui_popup_t *, void *);
|
---|
| 50 | static void test_popup_kbd(ui_popup_t *, void *, kbd_event_t *);
|
---|
| 51 | static errno_t test_popup_paint(ui_popup_t *, void *);
|
---|
| 52 | static void test_popup_pos(ui_popup_t *, void *, pos_event_t *);
|
---|
| 53 |
|
---|
| 54 | static ui_popup_cb_t test_popup_cb = {
|
---|
| 55 | .close = test_popup_close,
|
---|
| 56 | .kbd = test_popup_kbd,
|
---|
| 57 | .paint = test_popup_paint,
|
---|
| 58 | .pos = test_popup_pos,
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | static ui_popup_cb_t dummy_popup_cb = {
|
---|
| 62 | };
|
---|
| 63 |
|
---|
| 64 | static errno_t test_ctl_paint(void *);
|
---|
| 65 | static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *);
|
---|
| 66 | static void test_ctl_unfocus(void *);
|
---|
| 67 |
|
---|
| 68 | static ui_control_ops_t test_ctl_ops = {
|
---|
| 69 | .paint = test_ctl_paint,
|
---|
| 70 | .pos_event = test_ctl_pos_event,
|
---|
| 71 | .unfocus = test_ctl_unfocus
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 | typedef struct {
|
---|
| 75 | errno_t rc;
|
---|
| 76 | bool close;
|
---|
| 77 | bool focus;
|
---|
| 78 | bool kbd;
|
---|
| 79 | kbd_event_t kbd_event;
|
---|
| 80 | bool paint;
|
---|
| 81 | bool pos;
|
---|
| 82 | pos_event_t pos_event;
|
---|
| 83 | bool unfocus;
|
---|
| 84 | } test_cb_resp_t;
|
---|
| 85 |
|
---|
| 86 | typedef struct {
|
---|
| 87 | errno_t rc;
|
---|
| 88 | ui_evclaim_t claim;
|
---|
| 89 | bool paint;
|
---|
| 90 | bool pos;
|
---|
| 91 | pos_event_t pos_event;
|
---|
| 92 | bool unfocus;
|
---|
| 93 | } test_ctl_resp_t;
|
---|
| 94 |
|
---|
| 95 | /** Create and destroy popup window */
|
---|
| 96 | PCUT_TEST(create_destroy)
|
---|
| 97 | {
|
---|
| 98 | errno_t rc;
|
---|
| 99 | ui_t *ui = NULL;
|
---|
[f0155e4] | 100 | ui_wnd_params_t wparams;
|
---|
| 101 | ui_window_t *window = NULL;
|
---|
[3c8c580] | 102 | ui_popup_params_t params;
|
---|
| 103 | ui_popup_t *popup = NULL;
|
---|
| 104 |
|
---|
| 105 | rc = ui_create_disp(NULL, &ui);
|
---|
| 106 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 107 |
|
---|
[f0155e4] | 108 | ui_wnd_params_init(&wparams);
|
---|
| 109 | wparams.caption = "Hello";
|
---|
| 110 |
|
---|
| 111 | rc = ui_window_create(ui, &wparams, &window);
|
---|
| 112 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 113 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 114 |
|
---|
[3c8c580] | 115 | ui_popup_params_init(¶ms);
|
---|
| 116 |
|
---|
[f0155e4] | 117 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
[3c8c580] | 118 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 119 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
| 120 |
|
---|
| 121 | ui_popup_destroy(popup);
|
---|
[f0155e4] | 122 | ui_window_destroy(window);
|
---|
[3c8c580] | 123 | ui_destroy(ui);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /** ui_popup_destroy() can take NULL argument (no-op) */
|
---|
| 127 | PCUT_TEST(destroy_null)
|
---|
| 128 | {
|
---|
| 129 | ui_popup_destroy(NULL);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** ui_popup_add()/ui_popup_remove() ... */
|
---|
| 133 | PCUT_TEST(add_remove)
|
---|
| 134 | {
|
---|
| 135 | errno_t rc;
|
---|
| 136 | ui_t *ui = NULL;
|
---|
[f0155e4] | 137 | ui_wnd_params_t wparams;
|
---|
| 138 | ui_window_t *window = NULL;
|
---|
[3c8c580] | 139 | ui_popup_params_t params;
|
---|
| 140 | ui_popup_t *popup = NULL;
|
---|
| 141 | ui_control_t *control = NULL;
|
---|
| 142 | test_ctl_resp_t resp;
|
---|
| 143 |
|
---|
| 144 | rc = ui_create_disp(NULL, &ui);
|
---|
| 145 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 146 |
|
---|
[f0155e4] | 147 | ui_wnd_params_init(&wparams);
|
---|
| 148 | wparams.caption = "Hello";
|
---|
| 149 |
|
---|
| 150 | rc = ui_window_create(ui, &wparams, &window);
|
---|
| 151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 152 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 153 |
|
---|
[3c8c580] | 154 | ui_popup_params_init(¶ms);
|
---|
| 155 |
|
---|
[f0155e4] | 156 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
[3c8c580] | 157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 158 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
| 159 |
|
---|
| 160 | rc = ui_control_new(&test_ctl_ops, &resp, &control);
|
---|
| 161 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 162 |
|
---|
| 163 | /* Control not called since it hasn't been added yet */
|
---|
| 164 | resp.rc = ENOMEM;
|
---|
| 165 | resp.paint = false;
|
---|
| 166 | rc = ui_window_def_paint(popup->window);
|
---|
| 167 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 168 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
| 169 |
|
---|
| 170 | ui_popup_add(popup, control);
|
---|
| 171 |
|
---|
| 172 | /* Now paint request should be delivered to control */
|
---|
| 173 | resp.rc = EOK;
|
---|
| 174 | resp.paint = false;
|
---|
| 175 | rc = ui_window_def_paint(popup->window);
|
---|
| 176 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 177 | PCUT_ASSERT_TRUE(resp.paint);
|
---|
| 178 |
|
---|
| 179 | ui_popup_remove(popup, control);
|
---|
| 180 |
|
---|
| 181 | /*
|
---|
| 182 | * After having removed the control the request should no longer
|
---|
| 183 | * be delivered to it.
|
---|
| 184 | */
|
---|
| 185 | resp.rc = ENOMEM;
|
---|
| 186 | resp.paint = false;
|
---|
| 187 | rc = ui_window_def_paint(popup->window);
|
---|
| 188 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 189 | PCUT_ASSERT_FALSE(resp.paint);
|
---|
| 190 |
|
---|
| 191 | ui_popup_destroy(popup);
|
---|
[f0155e4] | 192 | ui_window_destroy(window);
|
---|
[3c8c580] | 193 | ui_destroy(ui);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /** ui_popup_get_res/gc() return valid objects */
|
---|
| 197 | PCUT_TEST(get_res_gc)
|
---|
| 198 | {
|
---|
| 199 | errno_t rc;
|
---|
| 200 | ui_t *ui = NULL;
|
---|
[f0155e4] | 201 | ui_wnd_params_t wparams;
|
---|
[3c8c580] | 202 | ui_window_t *window = NULL;
|
---|
[f0155e4] | 203 | ui_popup_params_t params;
|
---|
| 204 | ui_popup_t *popup = NULL;
|
---|
[3c8c580] | 205 | ui_resource_t *res;
|
---|
| 206 | gfx_context_t *gc;
|
---|
| 207 |
|
---|
| 208 | rc = ui_create_disp(NULL, &ui);
|
---|
| 209 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 210 |
|
---|
[f0155e4] | 211 | ui_wnd_params_init(&wparams);
|
---|
| 212 | wparams.caption = "Hello";
|
---|
[3c8c580] | 213 |
|
---|
[f0155e4] | 214 | rc = ui_window_create(ui, &wparams, &window);
|
---|
[3c8c580] | 215 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 216 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 217 |
|
---|
[f0155e4] | 218 | ui_popup_params_init(¶ms);
|
---|
| 219 |
|
---|
| 220 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
| 221 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 222 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
| 223 |
|
---|
| 224 | res = ui_popup_get_res(popup);
|
---|
[3c8c580] | 225 | PCUT_ASSERT_NOT_NULL(res);
|
---|
| 226 |
|
---|
[f0155e4] | 227 | gc = ui_popup_get_gc(popup);
|
---|
[3c8c580] | 228 | PCUT_ASSERT_NOT_NULL(gc);
|
---|
| 229 |
|
---|
[f0155e4] | 230 | ui_popup_destroy(popup);
|
---|
[3c8c580] | 231 | ui_window_destroy(window);
|
---|
| 232 | ui_destroy(ui);
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /** Test position event callback set via ui_popup_set_cb() */
|
---|
| 236 | PCUT_TEST(send_pos)
|
---|
| 237 | {
|
---|
| 238 | errno_t rc;
|
---|
| 239 | ui_t *ui = NULL;
|
---|
[f0155e4] | 240 | ui_wnd_params_t wparams;
|
---|
| 241 | ui_window_t *window = NULL;
|
---|
[3c8c580] | 242 | ui_popup_params_t params;
|
---|
| 243 | ui_popup_t *popup = NULL;
|
---|
| 244 | pos_event_t pos_event;
|
---|
| 245 | test_cb_resp_t resp;
|
---|
| 246 |
|
---|
| 247 | rc = ui_create_disp(NULL, &ui);
|
---|
| 248 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 249 |
|
---|
[f0155e4] | 250 | ui_wnd_params_init(&wparams);
|
---|
| 251 | wparams.caption = "Hello";
|
---|
| 252 |
|
---|
| 253 | rc = ui_window_create(ui, &wparams, &window);
|
---|
| 254 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 255 | PCUT_ASSERT_NOT_NULL(window);
|
---|
| 256 |
|
---|
[3c8c580] | 257 | ui_popup_params_init(¶ms);
|
---|
| 258 |
|
---|
[f0155e4] | 259 | rc = ui_popup_create(ui, window, ¶ms, &popup);
|
---|
[3c8c580] | 260 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 261 | PCUT_ASSERT_NOT_NULL(popup);
|
---|
| 262 |
|
---|
| 263 | pos_event.pos_id = 1;
|
---|
| 264 | pos_event.type = POS_PRESS;
|
---|
| 265 | pos_event.btn_num = 2;
|
---|
| 266 | pos_event.hpos = 3;
|
---|
| 267 | pos_event.vpos = 4;
|
---|
| 268 |
|
---|
| 269 | /* Pos callback with no callbacks set */
|
---|
| 270 | ui_window_send_pos(popup->window, &pos_event);
|
---|
| 271 |
|
---|
| 272 | /* Pos callback with pos callback not implemented */
|
---|
| 273 | ui_popup_set_cb(popup, &dummy_popup_cb, NULL);
|
---|
| 274 | ui_window_send_pos(popup->window, &pos_event);
|
---|
| 275 |
|
---|
| 276 | /* Pos callback with real callback set */
|
---|
| 277 | resp.pos = false;
|
---|
| 278 | ui_popup_set_cb(popup, &test_popup_cb, &resp);
|
---|
| 279 | ui_window_send_pos(popup->window, &pos_event);
|
---|
| 280 | PCUT_ASSERT_TRUE(resp.pos);
|
---|
| 281 | PCUT_ASSERT_INT_EQUALS(pos_event.pos_id, resp.pos_event.pos_id);
|
---|
| 282 | PCUT_ASSERT_EQUALS(pos_event.type, resp.pos_event.type);
|
---|
| 283 | PCUT_ASSERT_INT_EQUALS(pos_event.btn_num, resp.pos_event.btn_num);
|
---|
| 284 | PCUT_ASSERT_INT_EQUALS(pos_event.hpos, resp.pos_event.hpos);
|
---|
| 285 | PCUT_ASSERT_INT_EQUALS(pos_event.vpos, resp.pos_event.vpos);
|
---|
| 286 |
|
---|
| 287 | ui_popup_destroy(popup);
|
---|
[f0155e4] | 288 | ui_window_destroy(window);
|
---|
[3c8c580] | 289 | ui_destroy(ui);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | static void test_popup_close(ui_popup_t *popup, void *arg)
|
---|
| 293 | {
|
---|
| 294 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 295 |
|
---|
| 296 | resp->close = true;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | static void test_popup_kbd(ui_popup_t *popup, void *arg,
|
---|
| 300 | kbd_event_t *event)
|
---|
| 301 | {
|
---|
| 302 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 303 |
|
---|
| 304 | resp->kbd = true;
|
---|
| 305 | resp->kbd_event = *event;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | static errno_t test_popup_paint(ui_popup_t *popup, void *arg)
|
---|
| 309 | {
|
---|
| 310 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 311 |
|
---|
| 312 | resp->paint = true;
|
---|
| 313 | return resp->rc;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | static void test_popup_pos(ui_popup_t *popup, void *arg,
|
---|
| 317 | pos_event_t *event)
|
---|
| 318 | {
|
---|
| 319 | test_cb_resp_t *resp = (test_cb_resp_t *) arg;
|
---|
| 320 |
|
---|
| 321 | resp->pos = true;
|
---|
| 322 | resp->pos_event = *event;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | static errno_t test_ctl_paint(void *arg)
|
---|
| 326 | {
|
---|
| 327 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
| 328 |
|
---|
| 329 | resp->paint = true;
|
---|
| 330 | return resp->rc;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event)
|
---|
| 334 | {
|
---|
| 335 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
| 336 |
|
---|
| 337 | resp->pos = true;
|
---|
| 338 | resp->pos_event = *event;
|
---|
| 339 |
|
---|
| 340 | return resp->claim;
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | static void test_ctl_unfocus(void *arg)
|
---|
| 344 | {
|
---|
| 345 | test_ctl_resp_t *resp = (test_ctl_resp_t *) arg;
|
---|
| 346 |
|
---|
| 347 | resp->unfocus = true;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | PCUT_EXPORT(popup);
|
---|