| 1 | /*
|
|---|
| 2 | * Copyright (c) 2022 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 <errno.h>
|
|---|
| 30 | #include <io/kbd_event.h>
|
|---|
| 31 | #include <io/pos_event.h>
|
|---|
| 32 | #include <pcut/pcut.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <ui/ui.h>
|
|---|
| 35 | #include <vfs/vfs.h>
|
|---|
| 36 | #include "../panel.h"
|
|---|
| 37 |
|
|---|
| 38 | PCUT_INIT;
|
|---|
| 39 |
|
|---|
| 40 | PCUT_TEST_SUITE(panel);
|
|---|
| 41 |
|
|---|
| 42 | /** Test response */
|
|---|
| 43 | typedef struct {
|
|---|
| 44 | bool activate_req;
|
|---|
| 45 | panel_t *activate_req_panel;
|
|---|
| 46 | } test_resp_t;
|
|---|
| 47 |
|
|---|
| 48 | static void test_panel_activate_req(void *, panel_t *);
|
|---|
| 49 |
|
|---|
| 50 | static panel_cb_t test_cb = {
|
|---|
| 51 | .activate_req = test_panel_activate_req
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | /** Create and destroy panel. */
|
|---|
| 55 | PCUT_TEST(create_destroy)
|
|---|
| 56 | {
|
|---|
| 57 | panel_t *panel;
|
|---|
| 58 | errno_t rc;
|
|---|
| 59 |
|
|---|
| 60 | rc = panel_create(NULL, true, &panel);
|
|---|
| 61 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 62 |
|
|---|
| 63 | panel_destroy(panel);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /** panel_set_cb() sets callback */
|
|---|
| 67 | PCUT_TEST(set_cb)
|
|---|
| 68 | {
|
|---|
| 69 | panel_t *panel;
|
|---|
| 70 | errno_t rc;
|
|---|
| 71 | test_resp_t resp;
|
|---|
| 72 |
|
|---|
| 73 | rc = panel_create(NULL, true, &panel);
|
|---|
| 74 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 75 |
|
|---|
| 76 | panel_set_cb(panel, &test_cb, &resp);
|
|---|
| 77 | PCUT_ASSERT_EQUALS(&test_cb, panel->cb);
|
|---|
| 78 | PCUT_ASSERT_EQUALS(&resp, panel->cb_arg);
|
|---|
| 79 |
|
|---|
| 80 | panel_destroy(panel);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | /** Test panel_paint() */
|
|---|
| 84 | PCUT_TEST(paint)
|
|---|
| 85 | {
|
|---|
| 86 | ui_t *ui;
|
|---|
| 87 | ui_window_t *window;
|
|---|
| 88 | ui_wnd_params_t params;
|
|---|
| 89 | panel_t *panel;
|
|---|
| 90 | errno_t rc;
|
|---|
| 91 |
|
|---|
| 92 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 93 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 94 |
|
|---|
| 95 | ui_wnd_params_init(¶ms);
|
|---|
| 96 | params.caption = "Test";
|
|---|
| 97 |
|
|---|
| 98 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 99 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 100 |
|
|---|
| 101 | rc = panel_create(window, true, &panel);
|
|---|
| 102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 103 |
|
|---|
| 104 | rc = panel_paint(panel);
|
|---|
| 105 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 106 |
|
|---|
| 107 | panel_destroy(panel);
|
|---|
| 108 | ui_window_destroy(window);
|
|---|
| 109 | ui_destroy(ui);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /** panel_ctl() returns a valid UI control */
|
|---|
| 113 | PCUT_TEST(ctl)
|
|---|
| 114 | {
|
|---|
| 115 | panel_t *panel;
|
|---|
| 116 | ui_control_t *control;
|
|---|
| 117 | errno_t rc;
|
|---|
| 118 |
|
|---|
| 119 | rc = panel_create(NULL, true, &panel);
|
|---|
| 120 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 121 |
|
|---|
| 122 | control = panel_ctl(panel);
|
|---|
| 123 | PCUT_ASSERT_NOT_NULL(control);
|
|---|
| 124 |
|
|---|
| 125 | panel_destroy(panel);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Test panel_kbd_event() */
|
|---|
| 129 | PCUT_TEST(kbd_event)
|
|---|
| 130 | {
|
|---|
| 131 | panel_t *panel;
|
|---|
| 132 | ui_evclaim_t claimed;
|
|---|
| 133 | kbd_event_t event;
|
|---|
| 134 | errno_t rc;
|
|---|
| 135 |
|
|---|
| 136 | /* Active panel should claim events */
|
|---|
| 137 |
|
|---|
| 138 | rc = panel_create(NULL, true, &panel);
|
|---|
| 139 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 140 |
|
|---|
| 141 | event.type = KEY_PRESS;
|
|---|
| 142 | event.key = KC_ESCAPE;
|
|---|
| 143 | event.mods = 0;
|
|---|
| 144 | event.c = '\0';
|
|---|
| 145 |
|
|---|
| 146 | claimed = panel_kbd_event(panel, &event);
|
|---|
| 147 | PCUT_ASSERT_EQUALS(ui_claimed, claimed);
|
|---|
| 148 |
|
|---|
| 149 | panel_destroy(panel);
|
|---|
| 150 |
|
|---|
| 151 | /* Inactive panel should not claim events */
|
|---|
| 152 |
|
|---|
| 153 | rc = panel_create(NULL, false, &panel);
|
|---|
| 154 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 155 |
|
|---|
| 156 | event.type = KEY_PRESS;
|
|---|
| 157 | event.key = KC_ESCAPE;
|
|---|
| 158 | event.mods = 0;
|
|---|
| 159 | event.c = '\0';
|
|---|
| 160 |
|
|---|
| 161 | claimed = panel_kbd_event(panel, &event);
|
|---|
| 162 | PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
|
|---|
| 163 |
|
|---|
| 164 | panel_destroy(panel);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** Test panel_pos_event() */
|
|---|
| 168 | PCUT_TEST(pos_event)
|
|---|
| 169 | {
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | /** panel_set_rect() sets internal field */
|
|---|
| 173 | PCUT_TEST(set_rect)
|
|---|
| 174 | {
|
|---|
| 175 | panel_t *panel;
|
|---|
| 176 | gfx_rect_t rect;
|
|---|
| 177 | errno_t rc;
|
|---|
| 178 |
|
|---|
| 179 | rc = panel_create(NULL, true, &panel);
|
|---|
| 180 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 181 |
|
|---|
| 182 | rect.p0.x = 1;
|
|---|
| 183 | rect.p0.y = 2;
|
|---|
| 184 | rect.p1.x = 3;
|
|---|
| 185 | rect.p1.y = 4;
|
|---|
| 186 |
|
|---|
| 187 | panel_set_rect(panel, &rect);
|
|---|
| 188 | PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
|
|---|
| 189 | PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
|
|---|
| 190 | PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
|
|---|
| 191 | PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
|
|---|
| 192 |
|
|---|
| 193 | panel_destroy(panel);
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | /** panel_is_active() returns panel activity state */
|
|---|
| 197 | PCUT_TEST(is_active)
|
|---|
| 198 | {
|
|---|
| 199 | panel_t *panel;
|
|---|
| 200 | errno_t rc;
|
|---|
| 201 |
|
|---|
| 202 | rc = panel_create(NULL, true, &panel);
|
|---|
| 203 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 204 | PCUT_ASSERT_TRUE(panel_is_active(panel));
|
|---|
| 205 | panel_destroy(panel);
|
|---|
| 206 |
|
|---|
| 207 | rc = panel_create(NULL, false, &panel);
|
|---|
| 208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 209 | PCUT_ASSERT_FALSE(panel_is_active(panel));
|
|---|
| 210 | panel_destroy(panel);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | /** panel_activate() activates panel */
|
|---|
| 214 | PCUT_TEST(activate)
|
|---|
| 215 | {
|
|---|
| 216 | ui_t *ui;
|
|---|
| 217 | ui_window_t *window;
|
|---|
| 218 | ui_wnd_params_t params;
|
|---|
| 219 | panel_t *panel;
|
|---|
| 220 | errno_t rc;
|
|---|
| 221 |
|
|---|
| 222 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 223 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 224 |
|
|---|
| 225 | ui_wnd_params_init(¶ms);
|
|---|
| 226 | params.caption = "Test";
|
|---|
| 227 |
|
|---|
| 228 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 229 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 230 |
|
|---|
| 231 | rc = panel_create(window, false, &panel);
|
|---|
| 232 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 233 |
|
|---|
| 234 | PCUT_ASSERT_FALSE(panel_is_active(panel));
|
|---|
| 235 | rc = panel_activate(panel);
|
|---|
| 236 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 237 | PCUT_ASSERT_TRUE(panel_is_active(panel));
|
|---|
| 238 |
|
|---|
| 239 | panel_destroy(panel);
|
|---|
| 240 | ui_window_destroy(window);
|
|---|
| 241 | ui_destroy(ui);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /** panel_deactivate() deactivates panel */
|
|---|
| 245 | PCUT_TEST(deactivate)
|
|---|
| 246 | {
|
|---|
| 247 | ui_t *ui;
|
|---|
| 248 | ui_window_t *window;
|
|---|
| 249 | ui_wnd_params_t params;
|
|---|
| 250 | panel_t *panel;
|
|---|
| 251 | errno_t rc;
|
|---|
| 252 |
|
|---|
| 253 | rc = ui_create_disp(NULL, &ui);
|
|---|
| 254 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 255 |
|
|---|
| 256 | ui_wnd_params_init(¶ms);
|
|---|
| 257 | params.caption = "Test";
|
|---|
| 258 |
|
|---|
| 259 | rc = ui_window_create(ui, ¶ms, &window);
|
|---|
| 260 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 261 |
|
|---|
| 262 | rc = panel_create(window, true, &panel);
|
|---|
| 263 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 264 |
|
|---|
| 265 | PCUT_ASSERT_TRUE(panel_is_active(panel));
|
|---|
| 266 | panel_deactivate(panel);
|
|---|
| 267 | PCUT_ASSERT_FALSE(panel_is_active(panel));
|
|---|
| 268 |
|
|---|
| 269 | panel_destroy(panel);
|
|---|
| 270 | ui_window_destroy(window);
|
|---|
| 271 | ui_destroy(ui);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /** panel_activate_req() sends activation request */
|
|---|
| 275 | PCUT_TEST(activate_req)
|
|---|
| 276 | {
|
|---|
| 277 | panel_t *panel;
|
|---|
| 278 | errno_t rc;
|
|---|
| 279 | test_resp_t resp;
|
|---|
| 280 |
|
|---|
| 281 | rc = panel_create(NULL, true, &panel);
|
|---|
| 282 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
|---|
| 283 |
|
|---|
| 284 | panel_set_cb(panel, &test_cb, &resp);
|
|---|
| 285 |
|
|---|
| 286 | resp.activate_req = false;
|
|---|
| 287 | resp.activate_req_panel = NULL;
|
|---|
| 288 |
|
|---|
| 289 | panel_activate_req(panel);
|
|---|
| 290 | PCUT_ASSERT_TRUE(resp.activate_req);
|
|---|
| 291 | PCUT_ASSERT_EQUALS(panel, resp.activate_req_panel);
|
|---|
| 292 |
|
|---|
| 293 | panel_destroy(panel);
|
|---|
| 294 | }
|
|---|
| 295 |
|
|---|
| 296 | static void test_panel_activate_req(void *arg, panel_t *panel)
|
|---|
| 297 | {
|
|---|
| 298 | test_resp_t *resp = (test_resp_t *)arg;
|
|---|
| 299 |
|
|---|
| 300 | resp->activate_req = true;
|
|---|
| 301 | resp->activate_req_panel = panel;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | PCUT_EXPORT(panel);
|
|---|