Array of strings sys_dirs must be NULL-terminated
[helenos.git] / uspace / app / nav / test / panel.c
blobefe1d7fc775d8761026e3325cfc5e9ec93b19fac
1 /*
2 * Copyright (c) 2024 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 <errno.h>
30 #include <io/kbd_event.h>
31 #include <io/pos_event.h>
32 #include <pcut/pcut.h>
33 #include <stdio.h>
34 #include <ui/ui.h>
35 #include <vfs/vfs.h>
36 #include "../panel.h"
38 PCUT_INIT;
40 PCUT_TEST_SUITE(panel);
42 /** Test response */
43 typedef struct {
44 bool activate_req;
45 panel_t *activate_req_panel;
46 } test_resp_t;
48 static void test_panel_activate_req(void *, panel_t *);
50 static panel_cb_t test_cb = {
51 .activate_req = test_panel_activate_req
54 /** Create and destroy panel. */
55 PCUT_TEST(create_destroy)
57 ui_t *ui;
58 ui_window_t *window;
59 ui_wnd_params_t params;
60 panel_t *panel;
61 errno_t rc;
63 rc = ui_create_disp(NULL, &ui);
64 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
66 ui_wnd_params_init(&params);
67 params.caption = "Test";
69 rc = ui_window_create(ui, &params, &window);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
72 rc = panel_create(window, true, &panel);
73 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75 panel_destroy(panel);
76 ui_window_destroy(window);
77 ui_destroy(ui);
80 /** panel_set_cb() sets callback */
81 PCUT_TEST(set_cb)
83 ui_t *ui;
84 ui_window_t *window;
85 ui_wnd_params_t params;
86 panel_t *panel;
87 errno_t rc;
88 test_resp_t resp;
90 rc = ui_create_disp(NULL, &ui);
91 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93 ui_wnd_params_init(&params);
94 params.caption = "Test";
96 rc = ui_window_create(ui, &params, &window);
97 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
99 rc = panel_create(window, true, &panel);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
102 panel_set_cb(panel, &test_cb, &resp);
103 PCUT_ASSERT_EQUALS(&test_cb, panel->cb);
104 PCUT_ASSERT_EQUALS(&resp, panel->cb_arg);
106 panel_destroy(panel);
107 ui_window_destroy(window);
108 ui_destroy(ui);
111 /** Test panel_paint() */
112 PCUT_TEST(paint)
114 ui_t *ui;
115 ui_window_t *window;
116 ui_wnd_params_t params;
117 panel_t *panel;
118 errno_t rc;
120 rc = ui_create_disp(NULL, &ui);
121 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
123 ui_wnd_params_init(&params);
124 params.caption = "Test";
126 rc = ui_window_create(ui, &params, &window);
127 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
129 rc = panel_create(window, true, &panel);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
132 rc = panel_paint(panel);
133 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
135 panel_destroy(panel);
136 ui_window_destroy(window);
137 ui_destroy(ui);
140 /** panel_ctl() returns a valid UI control */
141 PCUT_TEST(ctl)
143 ui_t *ui;
144 ui_window_t *window;
145 ui_wnd_params_t params;
146 panel_t *panel;
147 ui_control_t *control;
148 errno_t rc;
150 rc = ui_create_disp(NULL, &ui);
151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
153 ui_wnd_params_init(&params);
154 params.caption = "Test";
156 rc = ui_window_create(ui, &params, &window);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
159 rc = panel_create(window, true, &panel);
160 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 control = panel_ctl(panel);
163 PCUT_ASSERT_NOT_NULL(control);
165 panel_destroy(panel);
166 ui_window_destroy(window);
167 ui_destroy(ui);
170 /** Test panel_kbd_event() */
171 PCUT_TEST(kbd_event)
173 ui_t *ui;
174 ui_window_t *window;
175 ui_wnd_params_t params;
176 panel_t *panel;
177 ui_evclaim_t claimed;
178 kbd_event_t event;
179 errno_t rc;
181 /* Active panel should claim events */
183 rc = ui_create_disp(NULL, &ui);
184 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
186 ui_wnd_params_init(&params);
187 params.caption = "Test";
189 rc = ui_window_create(ui, &params, &window);
190 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192 rc = panel_create(window, true, &panel);
193 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
195 event.type = KEY_PRESS;
196 event.key = KC_ESCAPE;
197 event.mods = 0;
198 event.c = '\0';
200 claimed = panel_kbd_event(panel, &event);
201 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
203 panel_destroy(panel);
205 /* Inactive panel should not claim events */
207 rc = panel_create(window, false, &panel);
208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
210 event.type = KEY_PRESS;
211 event.key = KC_ESCAPE;
212 event.mods = 0;
213 event.c = '\0';
215 claimed = panel_kbd_event(panel, &event);
216 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
218 panel_destroy(panel);
219 ui_window_destroy(window);
220 ui_destroy(ui);
223 /** Test panel_pos_event() */
224 PCUT_TEST(pos_event)
228 /** panel_set_rect() sets internal field */
229 PCUT_TEST(set_rect)
231 ui_t *ui;
232 ui_window_t *window;
233 ui_wnd_params_t params;
234 panel_t *panel;
235 gfx_rect_t rect;
236 errno_t rc;
238 rc = ui_create_disp(NULL, &ui);
239 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
241 ui_wnd_params_init(&params);
242 params.caption = "Test";
244 rc = ui_window_create(ui, &params, &window);
245 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
247 rc = panel_create(window, true, &panel);
248 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
250 rect.p0.x = 1;
251 rect.p0.y = 2;
252 rect.p1.x = 3;
253 rect.p1.y = 4;
255 panel_set_rect(panel, &rect);
256 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
257 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
258 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
259 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
261 panel_destroy(panel);
262 ui_window_destroy(window);
263 ui_destroy(ui);
266 /** panel_is_active() returns panel activity state */
267 PCUT_TEST(is_active)
269 ui_t *ui;
270 ui_window_t *window;
271 ui_wnd_params_t params;
272 panel_t *panel;
273 errno_t rc;
275 rc = ui_create_disp(NULL, &ui);
276 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
278 ui_wnd_params_init(&params);
279 params.caption = "Test";
281 rc = ui_window_create(ui, &params, &window);
282 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
284 rc = panel_create(window, true, &panel);
285 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
286 PCUT_ASSERT_TRUE(panel_is_active(panel));
287 panel_destroy(panel);
289 rc = panel_create(window, false, &panel);
290 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
291 PCUT_ASSERT_FALSE(panel_is_active(panel));
292 panel_destroy(panel);
293 ui_window_destroy(window);
294 ui_destroy(ui);
297 /** panel_activate() activates panel */
298 PCUT_TEST(activate)
300 ui_t *ui;
301 ui_window_t *window;
302 ui_wnd_params_t params;
303 panel_t *panel;
304 errno_t rc;
306 rc = ui_create_disp(NULL, &ui);
307 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
309 ui_wnd_params_init(&params);
310 params.caption = "Test";
312 rc = ui_window_create(ui, &params, &window);
313 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
315 rc = panel_create(window, false, &panel);
316 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
318 PCUT_ASSERT_FALSE(panel_is_active(panel));
319 rc = panel_activate(panel);
320 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
321 PCUT_ASSERT_TRUE(panel_is_active(panel));
323 panel_destroy(panel);
324 ui_window_destroy(window);
325 ui_destroy(ui);
328 /** panel_deactivate() deactivates panel */
329 PCUT_TEST(deactivate)
331 ui_t *ui;
332 ui_window_t *window;
333 ui_wnd_params_t params;
334 panel_t *panel;
335 errno_t rc;
337 rc = ui_create_disp(NULL, &ui);
338 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
340 ui_wnd_params_init(&params);
341 params.caption = "Test";
343 rc = ui_window_create(ui, &params, &window);
344 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
346 rc = panel_create(window, true, &panel);
347 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
349 PCUT_ASSERT_TRUE(panel_is_active(panel));
350 panel_deactivate(panel);
351 PCUT_ASSERT_FALSE(panel_is_active(panel));
353 panel_destroy(panel);
354 ui_window_destroy(window);
355 ui_destroy(ui);
358 /** panel_activate_req() sends activation request */
359 PCUT_TEST(activate_req)
361 ui_t *ui;
362 ui_window_t *window;
363 ui_wnd_params_t params;
364 panel_t *panel;
365 errno_t rc;
366 test_resp_t resp;
368 rc = ui_create_disp(NULL, &ui);
369 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
371 ui_wnd_params_init(&params);
372 params.caption = "Test";
374 rc = ui_window_create(ui, &params, &window);
375 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
377 rc = panel_create(window, true, &panel);
378 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
380 panel_set_cb(panel, &test_cb, &resp);
382 resp.activate_req = false;
383 resp.activate_req_panel = NULL;
385 panel_activate_req(panel);
386 PCUT_ASSERT_TRUE(resp.activate_req);
387 PCUT_ASSERT_EQUALS(panel, resp.activate_req_panel);
389 panel_destroy(panel);
390 ui_window_destroy(window);
391 ui_destroy(ui);
394 static void test_panel_activate_req(void *arg, panel_t *panel)
396 test_resp_t *resp = (test_resp_t *)arg;
398 resp->activate_req = true;
399 resp->activate_req_panel = panel;
402 PCUT_EXPORT(panel);