2 * Copyright (c) 2023 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <gfx/context.h>
30 #include <gfx/coord.h>
32 #include <pcut/pcut.h>
35 #include <ui/control.h>
36 #include <ui/menudd.h>
37 #include <ui/menubar.h>
39 #include <ui/window.h>
40 #include "../private/menu.h"
41 #include "../private/menubar.h"
45 PCUT_TEST_SUITE(menudd
);
47 /** Create and destroy menu drop-down */
48 PCUT_TEST(create_destroy
)
51 ui_window_t
*window
= NULL
;
52 ui_wnd_params_t params
;
53 ui_menu_bar_t
*mbar
= NULL
;
54 ui_menu_dd_t
*mdd
= NULL
;
55 ui_menu_t
*menu
= NULL
;
58 rc
= ui_create_disp(NULL
, &ui
);
59 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
61 ui_wnd_params_init(¶ms
);
62 params
.caption
= "Hello";
64 rc
= ui_window_create(ui
, ¶ms
, &window
);
65 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
66 PCUT_ASSERT_NOT_NULL(window
);
68 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
69 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
71 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, &menu
);
72 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
73 PCUT_ASSERT_NOT_NULL(mdd
);
74 PCUT_ASSERT_NOT_NULL(menu
);
77 * Normally we don't need to destroy a menu drop-down explicitly,
78 * it will be destroyed along with menu bar, but here we'll test
79 * destroying it explicitly.
81 ui_menu_dd_destroy(mdd
);
82 ui_menu_bar_destroy(mbar
);
83 ui_window_destroy(window
);
87 /** ui_menu_dd_destroy() can take NULL argument (no-op) */
88 PCUT_TEST(destroy_null
)
90 ui_menu_dd_destroy(NULL
);
93 /** ui_menu_dd_first() / ui_menu_dd_next() iterate over menu drop-downs */
97 ui_window_t
*window
= NULL
;
98 ui_wnd_params_t params
;
99 ui_menu_bar_t
*mbar
= NULL
;
100 ui_menu_dd_t
*mdd1
= NULL
;
101 ui_menu_dd_t
*mdd2
= NULL
;
105 rc
= ui_create_disp(NULL
, &ui
);
106 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
108 ui_wnd_params_init(¶ms
);
109 params
.caption
= "Hello";
111 rc
= ui_window_create(ui
, ¶ms
, &window
);
112 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
113 PCUT_ASSERT_NOT_NULL(window
);
115 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
116 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
117 PCUT_ASSERT_NOT_NULL(mbar
);
119 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd1
, NULL
);
120 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
121 PCUT_ASSERT_NOT_NULL(mdd1
);
123 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd2
, NULL
);
124 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
125 PCUT_ASSERT_NOT_NULL(mdd2
);
127 m
= ui_menu_dd_first(mbar
);
128 PCUT_ASSERT_EQUALS(mdd1
, m
);
130 m
= ui_menu_dd_next(m
);
131 PCUT_ASSERT_EQUALS(mdd2
, m
);
133 m
= ui_menu_dd_next(m
);
136 ui_menu_bar_destroy(mbar
);
137 ui_window_destroy(window
);
141 /** ui_menu_dd_last() / ui_menu_dd_prev() iterate over menu drop-downs
147 ui_window_t
*window
= NULL
;
148 ui_wnd_params_t params
;
149 ui_menu_bar_t
*mbar
= NULL
;
150 ui_menu_dd_t
*mdd1
= NULL
;
151 ui_menu_dd_t
*mdd2
= NULL
;
155 rc
= ui_create_disp(NULL
, &ui
);
156 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
158 ui_wnd_params_init(¶ms
);
159 params
.caption
= "Hello";
161 rc
= ui_window_create(ui
, ¶ms
, &window
);
162 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
163 PCUT_ASSERT_NOT_NULL(window
);
165 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
166 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
167 PCUT_ASSERT_NOT_NULL(mbar
);
169 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd1
, NULL
);
170 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
171 PCUT_ASSERT_NOT_NULL(mdd1
);
173 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd2
, NULL
);
174 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
175 PCUT_ASSERT_NOT_NULL(mdd2
);
177 m
= ui_menu_dd_last(mbar
);
178 PCUT_ASSERT_EQUALS(mdd2
, m
);
180 m
= ui_menu_dd_prev(m
);
181 PCUT_ASSERT_EQUALS(mdd1
, m
);
183 m
= ui_menu_dd_prev(m
);
186 ui_menu_bar_destroy(mbar
);
187 ui_window_destroy(window
);
191 /** ui_menu_dd_caption() returns the drop down's caption */
195 ui_window_t
*window
= NULL
;
196 ui_wnd_params_t params
;
197 ui_menu_bar_t
*mbar
= NULL
;
198 ui_menu_dd_t
*mdd
= NULL
;
202 rc
= ui_create_disp(NULL
, &ui
);
203 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
205 ui_wnd_params_init(¶ms
);
206 params
.caption
= "Hello";
208 rc
= ui_window_create(ui
, ¶ms
, &window
);
209 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
210 PCUT_ASSERT_NOT_NULL(window
);
212 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
213 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
214 PCUT_ASSERT_NOT_NULL(mbar
);
216 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, NULL
);
217 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
218 PCUT_ASSERT_NOT_NULL(mdd
);
220 caption
= ui_menu_dd_caption(mdd
);
221 PCUT_ASSERT_NOT_NULL(caption
);
223 PCUT_ASSERT_INT_EQUALS(0, str_cmp(caption
, "Test"));
225 ui_menu_bar_destroy(mbar
);
226 ui_window_destroy(window
);
230 /** ui_menu_dd_get_accel() returns the accelerator character */
234 ui_window_t
*window
= NULL
;
235 ui_wnd_params_t params
;
236 ui_menu_bar_t
*mbar
= NULL
;
237 ui_menu_dd_t
*mdd
= NULL
;
241 rc
= ui_create_disp(NULL
, &ui
);
242 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
244 ui_wnd_params_init(¶ms
);
245 params
.caption
= "Hello";
247 rc
= ui_window_create(ui
, ¶ms
, &window
);
248 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
249 PCUT_ASSERT_NOT_NULL(window
);
251 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
252 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
253 PCUT_ASSERT_NOT_NULL(mbar
);
255 rc
= ui_menu_dd_create(mbar
, "~T~est", &mdd
, NULL
);
256 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
257 PCUT_ASSERT_NOT_NULL(mdd
);
259 accel
= ui_menu_dd_get_accel(mdd
);
260 printf("accel='%c' (%d)\n", (char)accel
, (int)accel
);
261 PCUT_ASSERT_EQUALS((char32_t
)'t', accel
);
263 ui_menu_bar_destroy(mbar
);
264 ui_window_destroy(window
);
268 /** Open and close menu drop-down with ui_menu_dd_open() / ui_menu_dd_close() */
269 PCUT_TEST(open_close
)
272 ui_window_t
*window
= NULL
;
273 ui_wnd_params_t params
;
274 ui_menu_bar_t
*mbar
= NULL
;
275 ui_menu_dd_t
*mdd
= NULL
;
279 rc
= ui_create_disp(NULL
, &ui
);
280 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
282 ui_wnd_params_init(¶ms
);
283 params
.caption
= "Hello";
285 rc
= ui_window_create(ui
, ¶ms
, &window
);
286 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
287 PCUT_ASSERT_NOT_NULL(window
);
289 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
290 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
291 PCUT_ASSERT_NOT_NULL(mbar
);
293 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, NULL
);
294 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
295 PCUT_ASSERT_NOT_NULL(mdd
);
303 rc
= ui_menu_dd_open(mdd
, &prect
, 0);
304 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
306 ui_menu_dd_close(mdd
);
308 ui_menu_bar_destroy(mbar
);
309 ui_window_destroy(window
);
313 /** ui_menu_dd_is_open() correctly returns menu drop-down state */
317 ui_window_t
*window
= NULL
;
318 ui_wnd_params_t params
;
319 ui_menu_bar_t
*mbar
= NULL
;
320 ui_menu_dd_t
*mdd
= NULL
;
325 rc
= ui_create_disp(NULL
, &ui
);
326 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
328 ui_wnd_params_init(¶ms
);
329 params
.caption
= "Hello";
331 rc
= ui_window_create(ui
, ¶ms
, &window
);
332 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
333 PCUT_ASSERT_NOT_NULL(window
);
335 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
336 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
337 PCUT_ASSERT_NOT_NULL(mbar
);
339 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, NULL
);
340 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
341 PCUT_ASSERT_NOT_NULL(mdd
);
348 open
= ui_menu_dd_is_open(mdd
);
349 PCUT_ASSERT_FALSE(open
);
351 rc
= ui_menu_dd_open(mdd
, &prect
, 0);
352 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
354 open
= ui_menu_dd_is_open(mdd
);
355 PCUT_ASSERT_TRUE(open
);
357 ui_menu_dd_close(mdd
);
359 open
= ui_menu_dd_is_open(mdd
);
360 PCUT_ASSERT_FALSE(open
);
362 ui_menu_bar_destroy(mbar
);
363 ui_window_destroy(window
);