[6aa85c1] | 1 | /*
|
---|
[accdf882] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[6aa85c1] | 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 <pcut/pcut.h>
|
---|
[f59212cc] | 31 | #include <stdbool.h>
|
---|
[6aa85c1] | 32 | #include "../menu.h"
|
---|
| 33 |
|
---|
| 34 | PCUT_INIT;
|
---|
| 35 |
|
---|
| 36 | PCUT_TEST_SUITE(menu);
|
---|
| 37 |
|
---|
[f59212cc] | 38 | static void test_menu_file_open(void *);
|
---|
[accdf882] | 39 | static void test_menu_file_edit(void *);
|
---|
[f59212cc] | 40 | static void test_menu_file_exit(void *);
|
---|
| 41 |
|
---|
| 42 | static nav_menu_cb_t dummy_cb;
|
---|
| 43 | static nav_menu_cb_t test_cb = {
|
---|
| 44 | .file_open = test_menu_file_open,
|
---|
[accdf882] | 45 | .file_edit = test_menu_file_edit,
|
---|
[f59212cc] | 46 | .file_exit = test_menu_file_exit
|
---|
| 47 | };
|
---|
| 48 |
|
---|
| 49 | /** Test response */
|
---|
| 50 | typedef struct {
|
---|
| 51 | bool file_open;
|
---|
[accdf882] | 52 | bool file_edit;
|
---|
[f59212cc] | 53 | bool file_exit;
|
---|
| 54 | } test_resp_t;
|
---|
| 55 |
|
---|
[6aa85c1] | 56 | /** Create and destroy menu. */
|
---|
| 57 | PCUT_TEST(create_destroy)
|
---|
| 58 | {
|
---|
| 59 | ui_t *ui;
|
---|
| 60 | ui_window_t *window;
|
---|
| 61 | ui_wnd_params_t params;
|
---|
| 62 | nav_menu_t *menu;
|
---|
| 63 | errno_t rc;
|
---|
| 64 |
|
---|
| 65 | rc = ui_create_disp(NULL, &ui);
|
---|
| 66 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 67 |
|
---|
| 68 | ui_wnd_params_init(¶ms);
|
---|
| 69 | params.caption = "Test";
|
---|
| 70 |
|
---|
| 71 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 72 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 73 |
|
---|
| 74 | rc = nav_menu_create(window, &menu);
|
---|
| 75 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 76 |
|
---|
| 77 | nav_menu_destroy(menu);
|
---|
[b36ebb42] | 78 | ui_window_destroy(window);
|
---|
| 79 | ui_destroy(ui);
|
---|
[6aa85c1] | 80 | }
|
---|
| 81 |
|
---|
[f59212cc] | 82 | /** nav_menu_set_cb() */
|
---|
| 83 | PCUT_TEST(set_cb)
|
---|
| 84 | {
|
---|
| 85 | ui_t *ui;
|
---|
| 86 | ui_window_t *window;
|
---|
| 87 | ui_wnd_params_t params;
|
---|
| 88 | nav_menu_t *menu;
|
---|
| 89 | int foo;
|
---|
| 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 = nav_menu_create(window, &menu);
|
---|
| 102 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 103 |
|
---|
| 104 | nav_menu_set_cb(menu, &test_cb, &foo);
|
---|
| 105 | PCUT_ASSERT_EQUALS(&test_cb, menu->cb);
|
---|
| 106 | PCUT_ASSERT_EQUALS(&foo, menu->cb_arg);
|
---|
| 107 |
|
---|
| 108 | nav_menu_destroy(menu);
|
---|
| 109 | ui_window_destroy(window);
|
---|
| 110 | ui_destroy(ui);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /** File / Open callback */
|
---|
| 114 | PCUT_TEST(file_open)
|
---|
| 115 | {
|
---|
| 116 | ui_t *ui;
|
---|
| 117 | ui_window_t *window;
|
---|
| 118 | ui_wnd_params_t params;
|
---|
| 119 | nav_menu_t *menu;
|
---|
| 120 | test_resp_t resp;
|
---|
| 121 | errno_t rc;
|
---|
| 122 |
|
---|
| 123 | rc = ui_create_disp(NULL, &ui);
|
---|
| 124 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 125 |
|
---|
| 126 | ui_wnd_params_init(¶ms);
|
---|
| 127 | params.caption = "Test";
|
---|
| 128 |
|
---|
| 129 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 130 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 131 |
|
---|
| 132 | rc = nav_menu_create(window, &menu);
|
---|
| 133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 134 |
|
---|
| 135 | /* Call back with no callbacks set */
|
---|
| 136 | nav_menu_file_open(NULL, menu);
|
---|
| 137 |
|
---|
| 138 | /* Call back with dummy callbacks set */
|
---|
| 139 | nav_menu_set_cb(menu, &dummy_cb, &resp);
|
---|
| 140 | nav_menu_file_open(NULL, menu);
|
---|
| 141 |
|
---|
| 142 | /* Call back with test callbacks set */
|
---|
| 143 | resp.file_open = false;
|
---|
| 144 | nav_menu_set_cb(menu, &test_cb, &resp);
|
---|
| 145 | nav_menu_file_open(NULL, menu);
|
---|
| 146 | PCUT_ASSERT_TRUE(resp.file_open);
|
---|
| 147 |
|
---|
| 148 | nav_menu_destroy(menu);
|
---|
| 149 | ui_window_destroy(window);
|
---|
| 150 | ui_destroy(ui);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[accdf882] | 153 | /** File / Edit callback */
|
---|
| 154 | PCUT_TEST(file_edit)
|
---|
| 155 | {
|
---|
| 156 | ui_t *ui;
|
---|
| 157 | ui_window_t *window;
|
---|
| 158 | ui_wnd_params_t params;
|
---|
| 159 | nav_menu_t *menu;
|
---|
| 160 | test_resp_t resp;
|
---|
| 161 | errno_t rc;
|
---|
| 162 |
|
---|
| 163 | rc = ui_create_disp(NULL, &ui);
|
---|
| 164 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 165 |
|
---|
| 166 | ui_wnd_params_init(¶ms);
|
---|
| 167 | params.caption = "Test";
|
---|
| 168 |
|
---|
| 169 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 170 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 171 |
|
---|
| 172 | rc = nav_menu_create(window, &menu);
|
---|
| 173 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 174 |
|
---|
| 175 | /* Call back with no callbacks set */
|
---|
| 176 | nav_menu_file_edit(NULL, menu);
|
---|
| 177 |
|
---|
| 178 | /* Call back with dummy callbacks set */
|
---|
| 179 | nav_menu_set_cb(menu, &dummy_cb, &resp);
|
---|
| 180 | nav_menu_file_edit(NULL, menu);
|
---|
| 181 |
|
---|
| 182 | /* Call back with test callbacks set */
|
---|
| 183 | resp.file_edit = false;
|
---|
| 184 | nav_menu_set_cb(menu, &test_cb, &resp);
|
---|
| 185 | nav_menu_file_edit(NULL, menu);
|
---|
| 186 | PCUT_ASSERT_TRUE(resp.file_edit);
|
---|
| 187 |
|
---|
| 188 | nav_menu_destroy(menu);
|
---|
| 189 | ui_window_destroy(window);
|
---|
| 190 | ui_destroy(ui);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | /** File / Exit callback */
|
---|
| 194 | PCUT_TEST(file_exit)
|
---|
| 195 | {
|
---|
| 196 | ui_t *ui;
|
---|
| 197 | ui_window_t *window;
|
---|
| 198 | ui_wnd_params_t params;
|
---|
| 199 | nav_menu_t *menu;
|
---|
| 200 | test_resp_t resp;
|
---|
| 201 | errno_t rc;
|
---|
| 202 |
|
---|
| 203 | rc = ui_create_disp(NULL, &ui);
|
---|
| 204 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 205 |
|
---|
| 206 | ui_wnd_params_init(¶ms);
|
---|
| 207 | params.caption = "Test";
|
---|
| 208 |
|
---|
| 209 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
| 210 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 211 |
|
---|
| 212 | rc = nav_menu_create(window, &menu);
|
---|
| 213 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
| 214 |
|
---|
| 215 | /* Call back with no callbacks set */
|
---|
| 216 | nav_menu_file_exit(NULL, menu);
|
---|
| 217 |
|
---|
| 218 | /* Call back with dummy callbacks set */
|
---|
| 219 | nav_menu_set_cb(menu, &dummy_cb, &resp);
|
---|
| 220 | nav_menu_file_exit(NULL, menu);
|
---|
| 221 |
|
---|
| 222 | /* Call back with test callbacks set */
|
---|
| 223 | resp.file_exit = false;
|
---|
| 224 | nav_menu_set_cb(menu, &test_cb, &resp);
|
---|
| 225 | nav_menu_file_exit(NULL, menu);
|
---|
| 226 | PCUT_ASSERT_TRUE(resp.file_exit);
|
---|
| 227 |
|
---|
| 228 | nav_menu_destroy(menu);
|
---|
| 229 | ui_window_destroy(window);
|
---|
| 230 | ui_destroy(ui);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[f59212cc] | 233 | /** Testing File / Open callback */
|
---|
| 234 | static void test_menu_file_open(void *arg)
|
---|
| 235 | {
|
---|
| 236 | test_resp_t *resp = (test_resp_t *)arg;
|
---|
| 237 |
|
---|
| 238 | resp->file_open = true;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[accdf882] | 241 | /** Testing File / Edit callback */
|
---|
| 242 | static void test_menu_file_edit(void *arg)
|
---|
| 243 | {
|
---|
| 244 | test_resp_t *resp = (test_resp_t *)arg;
|
---|
| 245 |
|
---|
| 246 | resp->file_edit = true;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[f59212cc] | 249 | /** Testing File / Exit callback */
|
---|
| 250 | static void test_menu_file_exit(void *arg)
|
---|
| 251 | {
|
---|
| 252 | test_resp_t *resp = (test_resp_t *)arg;
|
---|
| 253 |
|
---|
| 254 | resp->file_exit = true;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[6aa85c1] | 257 | PCUT_EXPORT(menu);
|
---|