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/coord.h>
31 #include <pcut/pcut.h>
33 #include <ui/control.h>
34 #include <ui/menubar.h>
35 #include <ui/menudd.h>
37 #include <ui/window.h>
38 #include "../private/menubar.h"
39 #include "../private/wdecor.h"
40 #include "../private/window.h"
44 PCUT_TEST_SUITE(menubar
);
46 /** Create and destroy menu bar */
47 PCUT_TEST(create_destroy
)
50 ui_window_t
*window
= NULL
;
51 ui_wnd_params_t params
;
52 ui_menu_bar_t
*mbar
= NULL
;
55 rc
= ui_create_disp(NULL
, &ui
);
56 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
58 ui_wnd_params_init(¶ms
);
59 params
.caption
= "Hello";
61 rc
= ui_window_create(ui
, ¶ms
, &window
);
62 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
63 PCUT_ASSERT_NOT_NULL(window
);
65 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
66 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
67 PCUT_ASSERT_NOT_NULL(mbar
);
69 ui_menu_bar_destroy(mbar
);
70 ui_window_destroy(window
);
74 /** ui_menu_bar_destroy() can take NULL argument (no-op) */
75 PCUT_TEST(destroy_null
)
77 ui_menu_bar_destroy(NULL
);
80 /** ui_menu_bar_ctl() returns control that has a working virtual destructor */
84 ui_window_t
*window
= NULL
;
85 ui_wnd_params_t params
;
86 ui_menu_bar_t
*mbar
= NULL
;
87 ui_control_t
*control
;
90 rc
= ui_create_disp(NULL
, &ui
);
91 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
93 ui_wnd_params_init(¶ms
);
94 params
.caption
= "Hello";
96 rc
= ui_window_create(ui
, ¶ms
, &window
);
97 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
98 PCUT_ASSERT_NOT_NULL(window
);
100 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
101 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
102 PCUT_ASSERT_NOT_NULL(mbar
);
104 control
= ui_menu_bar_ctl(mbar
);
105 PCUT_ASSERT_NOT_NULL(control
);
107 ui_control_destroy(control
);
108 ui_window_destroy(window
);
112 /** Set menu bar rectangle sets internal field */
117 ui_window_t
*window
= NULL
;
118 ui_wnd_params_t params
;
119 ui_menu_bar_t
*mbar
= NULL
;
122 rc
= ui_create_disp(NULL
, &ui
);
123 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
125 ui_wnd_params_init(¶ms
);
126 params
.caption
= "Hello";
128 rc
= ui_window_create(ui
, ¶ms
, &window
);
129 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
130 PCUT_ASSERT_NOT_NULL(window
);
132 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
133 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
134 PCUT_ASSERT_NOT_NULL(mbar
);
141 ui_menu_bar_set_rect(mbar
, &rect
);
142 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, mbar
->rect
.p0
.x
);
143 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, mbar
->rect
.p0
.y
);
144 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, mbar
->rect
.p1
.x
);
145 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, mbar
->rect
.p1
.y
);
147 ui_menu_bar_destroy(mbar
);
148 ui_window_destroy(window
);
152 /** Paint menu bar */
156 ui_window_t
*window
= NULL
;
157 ui_wnd_params_t params
;
158 ui_menu_bar_t
*mbar
= NULL
;
161 rc
= ui_create_disp(NULL
, &ui
);
162 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
164 ui_wnd_params_init(¶ms
);
165 params
.caption
= "Hello";
167 rc
= ui_window_create(ui
, ¶ms
, &window
);
168 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
169 PCUT_ASSERT_NOT_NULL(window
);
171 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
172 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
173 PCUT_ASSERT_NOT_NULL(mbar
);
175 rc
= ui_menu_bar_paint(mbar
);
176 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
178 ui_menu_bar_destroy(mbar
);
179 ui_window_destroy(window
);
183 /** Deliver menu bar keyboard event */
187 ui_window_t
*window
= NULL
;
188 ui_wnd_params_t params
;
189 ui_menu_bar_t
*mbar
= NULL
;
190 ui_menu_t
*menu
= NULL
;
191 ui_evclaim_t claimed
;
196 rc
= ui_create_disp(NULL
, &ui
);
197 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
199 ui_wnd_params_init(¶ms
);
200 params
.caption
= "Hello";
202 rc
= ui_window_create(ui
, ¶ms
, &window
);
203 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
204 PCUT_ASSERT_NOT_NULL(window
);
206 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
207 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
208 PCUT_ASSERT_NOT_NULL(mbar
);
214 ui_menu_bar_set_rect(mbar
, &rect
);
216 rc
= ui_menu_dd_create(mbar
, "Test", NULL
, &menu
);
217 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
218 PCUT_ASSERT_NOT_NULL(menu
);
220 event
.type
= KEY_PRESS
;
223 claimed
= ui_menu_bar_kbd_event(mbar
, &event
);
224 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
225 PCUT_ASSERT_EQUALS(ui_claimed
, claimed
);
227 ui_menu_bar_destroy(mbar
);
228 ui_window_destroy(window
);
232 /** ui_menu_bar_press_accel() opens the corresponding menu */
233 PCUT_TEST(press_accel
)
236 ui_window_t
*window
= NULL
;
237 ui_wnd_params_t params
;
238 ui_menu_bar_t
*mbar
= NULL
;
239 ui_menu_t
*menu
= NULL
;
240 ui_menu_dd_t
*mdd
= NULL
;
244 rc
= ui_create_disp(NULL
, &ui
);
245 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
247 ui_wnd_params_init(¶ms
);
248 params
.caption
= "Hello";
250 rc
= ui_window_create(ui
, ¶ms
, &window
);
251 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
252 PCUT_ASSERT_NOT_NULL(window
);
254 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
255 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
256 PCUT_ASSERT_NOT_NULL(mbar
);
262 ui_menu_bar_set_rect(mbar
, &rect
);
264 rc
= ui_menu_dd_create(mbar
, "~T~est", &mdd
, &menu
);
265 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
266 PCUT_ASSERT_NOT_NULL(mdd
);
267 PCUT_ASSERT_NOT_NULL(menu
);
269 PCUT_ASSERT_FALSE(ui_menu_dd_is_open(mdd
));
271 ui_menu_bar_press_accel(mbar
, 't', 0);
273 PCUT_ASSERT_TRUE(ui_menu_dd_is_open(mdd
));
275 ui_menu_bar_destroy(mbar
);
276 ui_window_destroy(window
);
280 /** Press event on menu bar entry selects menu */
281 PCUT_TEST(pos_event_select
)
284 ui_window_t
*window
= NULL
;
285 ui_wnd_params_t params
;
286 ui_menu_bar_t
*mbar
= NULL
;
287 ui_menu_t
*menu
= NULL
;
288 ui_menu_dd_t
*mdd
= NULL
;
289 ui_evclaim_t claimed
;
294 rc
= ui_create_disp(NULL
, &ui
);
295 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
297 ui_wnd_params_init(¶ms
);
298 params
.caption
= "Hello";
300 rc
= ui_window_create(ui
, ¶ms
, &window
);
301 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
302 PCUT_ASSERT_NOT_NULL(window
);
304 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
305 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
306 PCUT_ASSERT_NOT_NULL(mbar
);
312 ui_menu_bar_set_rect(mbar
, &rect
);
314 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, &menu
);
315 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
316 PCUT_ASSERT_NOT_NULL(menu
);
318 event
.type
= POS_PRESS
;
321 claimed
= ui_menu_bar_pos_event(mbar
, &event
);
322 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
323 PCUT_ASSERT_EQUALS(ui_claimed
, claimed
);
325 /* Clicking the menu bar entry should select menu drop-down */
326 PCUT_ASSERT_EQUALS(mdd
, mbar
->selected
);
328 ui_menu_bar_destroy(mbar
);
329 ui_window_destroy(window
);
333 /** Calling ui_menu_bar_select() with another menu selects it */
334 PCUT_TEST(select_different
)
337 ui_window_t
*window
= NULL
;
338 ui_wnd_params_t params
;
339 ui_menu_bar_t
*mbar
= NULL
;
340 ui_menu_dd_t
*mdd1
= NULL
;
341 ui_menu_dd_t
*mdd2
= NULL
;
344 rc
= ui_create_disp(NULL
, &ui
);
345 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
347 ui_wnd_params_init(¶ms
);
348 params
.caption
= "Hello";
350 rc
= ui_window_create(ui
, ¶ms
, &window
);
351 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
352 PCUT_ASSERT_NOT_NULL(window
);
354 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
355 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
356 PCUT_ASSERT_NOT_NULL(mbar
);
358 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd1
, NULL
);
359 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
360 PCUT_ASSERT_NOT_NULL(mdd1
);
362 rc
= ui_menu_dd_create(mbar
, "Test 2", &mdd2
, NULL
);
363 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
364 PCUT_ASSERT_NOT_NULL(mdd2
);
366 ui_menu_bar_select(mbar
, mdd1
, true, 0);
367 PCUT_ASSERT_EQUALS(mdd1
, mbar
->selected
);
369 /* Selecting different menu should select it */
370 ui_menu_bar_select(mbar
, mdd2
, true, 0);
371 PCUT_ASSERT_EQUALS(mdd2
, mbar
->selected
);
373 ui_menu_bar_destroy(mbar
);
374 ui_window_destroy(window
);
378 /** ui_menu_bar_select_first/last() selects first/last drop-down */
379 PCUT_TEST(select_first_last
)
382 ui_window_t
*window
= NULL
;
383 ui_wnd_params_t params
;
384 ui_menu_bar_t
*mbar
= NULL
;
385 ui_menu_dd_t
*mdd1
= NULL
;
386 ui_menu_dd_t
*mdd2
= NULL
;
389 rc
= ui_create_disp(NULL
, &ui
);
390 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
392 ui_wnd_params_init(¶ms
);
393 params
.caption
= "Hello";
395 rc
= ui_window_create(ui
, ¶ms
, &window
);
396 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
397 PCUT_ASSERT_NOT_NULL(window
);
399 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
400 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
401 PCUT_ASSERT_NOT_NULL(mbar
);
403 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd1
, NULL
);
404 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
405 PCUT_ASSERT_NOT_NULL(mdd1
);
407 rc
= ui_menu_dd_create(mbar
, "Test 2", &mdd2
, NULL
);
408 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
409 PCUT_ASSERT_NOT_NULL(mdd2
);
411 ui_menu_bar_select_first(mbar
, true, 0);
412 PCUT_ASSERT_EQUALS(mdd1
, mbar
->selected
);
414 ui_menu_bar_select_last(mbar
, true, 0);
415 PCUT_ASSERT_EQUALS(mdd2
, mbar
->selected
);
417 ui_menu_bar_destroy(mbar
);
418 ui_window_destroy(window
);
422 /** ui_menu_bar_select_sysmenu() activates system menu */
423 PCUT_TEST(select_sysmenu
)
426 ui_window_t
*window
= NULL
;
427 ui_wnd_params_t params
;
428 ui_menu_bar_t
*mbar
= NULL
;
429 ui_menu_dd_t
*mdd1
= NULL
;
430 ui_menu_dd_t
*mdd2
= NULL
;
433 rc
= ui_create_disp(NULL
, &ui
);
434 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
436 ui_wnd_params_init(¶ms
);
437 params
.caption
= "Hello";
439 rc
= ui_window_create(ui
, ¶ms
, &window
);
440 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
441 PCUT_ASSERT_NOT_NULL(window
);
443 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
444 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
445 PCUT_ASSERT_NOT_NULL(mbar
);
447 rc
= ui_menu_dd_create(mbar
, "Test 1", &mdd1
, NULL
);
448 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
449 PCUT_ASSERT_NOT_NULL(mdd1
);
451 rc
= ui_menu_dd_create(mbar
, "Test 2", &mdd2
, NULL
);
452 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
453 PCUT_ASSERT_NOT_NULL(mdd2
);
455 ui_menu_bar_select_sysmenu(mbar
, false, 0);
456 PCUT_ASSERT_TRUE(window
->wdecor
->sysmenu_hdl_active
);
458 ui_menu_bar_destroy(mbar
);
459 ui_window_destroy(window
);
463 /** ui_menu_bar_activate() activates/deactivates menu bar */
464 PCUT_TEST(activate_deactivate
)
467 ui_window_t
*window
= NULL
;
468 ui_wnd_params_t params
;
469 ui_menu_bar_t
*mbar
= NULL
;
470 ui_menu_dd_t
*mdd
= NULL
;
473 rc
= ui_create_disp(NULL
, &ui
);
474 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
476 ui_wnd_params_init(¶ms
);
477 params
.caption
= "Hello";
479 rc
= ui_window_create(ui
, ¶ms
, &window
);
480 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
481 PCUT_ASSERT_NOT_NULL(window
);
483 rc
= ui_menu_bar_create(ui
, window
, &mbar
);
484 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
485 PCUT_ASSERT_NOT_NULL(mbar
);
487 rc
= ui_menu_dd_create(mbar
, "Test", &mdd
, NULL
);
488 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
489 PCUT_ASSERT_NOT_NULL(mdd
);
491 ui_menu_bar_activate(mbar
);
492 PCUT_ASSERT_EQUALS(mdd
, mbar
->selected
);
494 ui_menu_bar_deactivate(mbar
);
495 PCUT_ASSERT_NULL(mbar
->selected
);
497 ui_menu_bar_destroy(mbar
);
498 ui_window_destroy(window
);
502 PCUT_EXPORT(menubar
);