Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / menudd.c
blobcc40e99b4f71809c5c570a0d2a7e3519346b8286
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/context.h>
30 #include <gfx/coord.h>
31 #include <mem.h>
32 #include <pcut/pcut.h>
33 #include <stdbool.h>
34 #include <str.h>
35 #include <ui/control.h>
36 #include <ui/menudd.h>
37 #include <ui/menubar.h>
38 #include <ui/ui.h>
39 #include <ui/window.h>
40 #include "../private/menu.h"
41 #include "../private/menubar.h"
43 PCUT_INIT;
45 PCUT_TEST_SUITE(menudd);
47 /** Create and destroy menu drop-down */
48 PCUT_TEST(create_destroy)
50 ui_t *ui = NULL;
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;
56 errno_t rc;
58 rc = ui_create_disp(NULL, &ui);
59 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
61 ui_wnd_params_init(&params);
62 params.caption = "Hello";
64 rc = ui_window_create(ui, &params, &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);
84 ui_destroy(ui);
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 */
94 PCUT_TEST(first_next)
96 ui_t *ui = NULL;
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;
102 ui_menu_dd_t *m;
103 errno_t rc;
105 rc = ui_create_disp(NULL, &ui);
106 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
108 ui_wnd_params_init(&params);
109 params.caption = "Hello";
111 rc = ui_window_create(ui, &params, &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);
134 PCUT_ASSERT_NULL(m);
136 ui_menu_bar_destroy(mbar);
137 ui_window_destroy(window);
138 ui_destroy(ui);
141 /** ui_menu_dd_last() / ui_menu_dd_prev() iterate over menu drop-downs
142 * in reverse.
144 PCUT_TEST(last_prev)
146 ui_t *ui = NULL;
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;
152 ui_menu_dd_t *m;
153 errno_t rc;
155 rc = ui_create_disp(NULL, &ui);
156 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158 ui_wnd_params_init(&params);
159 params.caption = "Hello";
161 rc = ui_window_create(ui, &params, &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);
184 PCUT_ASSERT_NULL(m);
186 ui_menu_bar_destroy(mbar);
187 ui_window_destroy(window);
188 ui_destroy(ui);
191 /** ui_menu_dd_caption() returns the drop down's caption */
192 PCUT_TEST(caption)
194 ui_t *ui = NULL;
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;
199 const char *caption;
200 errno_t rc;
202 rc = ui_create_disp(NULL, &ui);
203 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
205 ui_wnd_params_init(&params);
206 params.caption = "Hello";
208 rc = ui_window_create(ui, &params, &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);
227 ui_destroy(ui);
230 /** ui_menu_dd_get_accel() returns the accelerator character */
231 PCUT_TEST(get_accel)
233 ui_t *ui = NULL;
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;
238 char32_t accel;
239 errno_t rc;
241 rc = ui_create_disp(NULL, &ui);
242 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
244 ui_wnd_params_init(&params);
245 params.caption = "Hello";
247 rc = ui_window_create(ui, &params, &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);
265 ui_destroy(ui);
268 /** Open and close menu drop-down with ui_menu_dd_open() / ui_menu_dd_close() */
269 PCUT_TEST(open_close)
271 ui_t *ui = NULL;
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;
276 gfx_rect_t prect;
277 errno_t rc;
279 rc = ui_create_disp(NULL, &ui);
280 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
282 ui_wnd_params_init(&params);
283 params.caption = "Hello";
285 rc = ui_window_create(ui, &params, &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);
297 prect.p0.x = 0;
298 prect.p0.y = 0;
299 prect.p1.x = 0;
300 prect.p1.y = 0;
302 /* Open and close */
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);
310 ui_destroy(ui);
313 /** ui_menu_dd_is_open() correctly returns menu drop-down state */
314 PCUT_TEST(is_open)
316 ui_t *ui = NULL;
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;
321 gfx_rect_t prect;
322 bool open;
323 errno_t rc;
325 rc = ui_create_disp(NULL, &ui);
326 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
328 ui_wnd_params_init(&params);
329 params.caption = "Hello";
331 rc = ui_window_create(ui, &params, &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);
343 prect.p0.x = 0;
344 prect.p0.y = 0;
345 prect.p1.x = 0;
346 prect.p1.y = 0;
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);
364 ui_destroy(ui);
367 PCUT_EXPORT(menudd);