Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / menubar.c
bloba03fa571ae5db87652f560a05e7663777bf31920
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
30 #include <mem.h>
31 #include <pcut/pcut.h>
32 #include <stdbool.h>
33 #include <ui/control.h>
34 #include <ui/menubar.h>
35 #include <ui/menudd.h>
36 #include <ui/ui.h>
37 #include <ui/window.h>
38 #include "../private/menubar.h"
39 #include "../private/wdecor.h"
40 #include "../private/window.h"
42 PCUT_INIT;
44 PCUT_TEST_SUITE(menubar);
46 /** Create and destroy menu bar */
47 PCUT_TEST(create_destroy)
49 ui_t *ui = NULL;
50 ui_window_t *window = NULL;
51 ui_wnd_params_t params;
52 ui_menu_bar_t *mbar = NULL;
53 errno_t rc;
55 rc = ui_create_disp(NULL, &ui);
56 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
58 ui_wnd_params_init(&params);
59 params.caption = "Hello";
61 rc = ui_window_create(ui, &params, &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);
71 ui_destroy(ui);
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 */
81 PCUT_TEST(ctl)
83 ui_t *ui = NULL;
84 ui_window_t *window = NULL;
85 ui_wnd_params_t params;
86 ui_menu_bar_t *mbar = NULL;
87 ui_control_t *control;
88 errno_t rc;
90 rc = ui_create_disp(NULL, &ui);
91 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93 ui_wnd_params_init(&params);
94 params.caption = "Hello";
96 rc = ui_window_create(ui, &params, &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);
109 ui_destroy(ui);
112 /** Set menu bar rectangle sets internal field */
113 PCUT_TEST(set_rect)
115 errno_t rc;
116 ui_t *ui = NULL;
117 ui_window_t *window = NULL;
118 ui_wnd_params_t params;
119 ui_menu_bar_t *mbar = NULL;
120 gfx_rect_t rect;
122 rc = ui_create_disp(NULL, &ui);
123 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
125 ui_wnd_params_init(&params);
126 params.caption = "Hello";
128 rc = ui_window_create(ui, &params, &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);
136 rect.p0.x = 1;
137 rect.p0.y = 2;
138 rect.p1.x = 3;
139 rect.p1.y = 4;
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);
149 ui_destroy(ui);
152 /** Paint menu bar */
153 PCUT_TEST(paint)
155 ui_t *ui = NULL;
156 ui_window_t *window = NULL;
157 ui_wnd_params_t params;
158 ui_menu_bar_t *mbar = NULL;
159 errno_t rc;
161 rc = ui_create_disp(NULL, &ui);
162 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
164 ui_wnd_params_init(&params);
165 params.caption = "Hello";
167 rc = ui_window_create(ui, &params, &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);
180 ui_destroy(ui);
183 /** Deliver menu bar keyboard event */
184 PCUT_TEST(kbd_event)
186 ui_t *ui = NULL;
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;
192 kbd_event_t event;
193 gfx_rect_t rect;
194 errno_t rc;
196 rc = ui_create_disp(NULL, &ui);
197 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
199 ui_wnd_params_init(&params);
200 params.caption = "Hello";
202 rc = ui_window_create(ui, &params, &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);
210 rect.p0.x = 0;
211 rect.p0.y = 0;
212 rect.p1.x = 50;
213 rect.p1.y = 25;
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;
221 event.key = KC_F10;
222 event.mods = 0;
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);
229 ui_destroy(ui);
232 /** ui_menu_bar_press_accel() opens the corresponding menu */
233 PCUT_TEST(press_accel)
235 ui_t *ui = NULL;
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;
241 gfx_rect_t rect;
242 errno_t rc;
244 rc = ui_create_disp(NULL, &ui);
245 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
247 ui_wnd_params_init(&params);
248 params.caption = "Hello";
250 rc = ui_window_create(ui, &params, &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);
258 rect.p0.x = 0;
259 rect.p0.y = 0;
260 rect.p1.x = 50;
261 rect.p1.y = 25;
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);
277 ui_destroy(ui);
280 /** Press event on menu bar entry selects menu */
281 PCUT_TEST(pos_event_select)
283 ui_t *ui = NULL;
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;
290 pos_event_t event;
291 gfx_rect_t rect;
292 errno_t rc;
294 rc = ui_create_disp(NULL, &ui);
295 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
297 ui_wnd_params_init(&params);
298 params.caption = "Hello";
300 rc = ui_window_create(ui, &params, &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);
308 rect.p0.x = 0;
309 rect.p0.y = 0;
310 rect.p1.x = 50;
311 rect.p1.y = 25;
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;
319 event.hpos = 4;
320 event.vpos = 4;
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);
330 ui_destroy(ui);
333 /** Calling ui_menu_bar_select() with another menu selects it */
334 PCUT_TEST(select_different)
336 ui_t *ui = NULL;
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;
342 errno_t rc;
344 rc = ui_create_disp(NULL, &ui);
345 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
347 ui_wnd_params_init(&params);
348 params.caption = "Hello";
350 rc = ui_window_create(ui, &params, &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);
375 ui_destroy(ui);
378 /** ui_menu_bar_select_first/last() selects first/last drop-down */
379 PCUT_TEST(select_first_last)
381 ui_t *ui = NULL;
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;
387 errno_t rc;
389 rc = ui_create_disp(NULL, &ui);
390 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
392 ui_wnd_params_init(&params);
393 params.caption = "Hello";
395 rc = ui_window_create(ui, &params, &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);
419 ui_destroy(ui);
422 /** ui_menu_bar_select_sysmenu() activates system menu */
423 PCUT_TEST(select_sysmenu)
425 ui_t *ui = NULL;
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;
431 errno_t rc;
433 rc = ui_create_disp(NULL, &ui);
434 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
436 ui_wnd_params_init(&params);
437 params.caption = "Hello";
439 rc = ui_window_create(ui, &params, &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);
460 ui_destroy(ui);
463 /** ui_menu_bar_activate() activates/deactivates menu bar */
464 PCUT_TEST(activate_deactivate)
466 ui_t *ui = NULL;
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;
471 errno_t rc;
473 rc = ui_create_disp(NULL, &ui);
474 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
476 ui_wnd_params_init(&params);
477 params.caption = "Hello";
479 rc = ui_window_create(ui, &params, &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);
499 ui_destroy(ui);
502 PCUT_EXPORT(menubar);