1 | /*
|
---|
2 | * Copyright (c) 2024 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | */
|
---|
28 |
|
---|
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"
|
---|
37 |
|
---|
38 | PCUT_INIT;
|
---|
39 |
|
---|
40 | PCUT_TEST_SUITE(panel);
|
---|
41 |
|
---|
42 | /** Test response */
|
---|
43 | typedef struct {
|
---|
44 | bool activate_req;
|
---|
45 | panel_t *activate_req_panel;
|
---|
46 | } test_resp_t;
|
---|
47 |
|
---|
48 | static void test_panel_activate_req(void *, panel_t *);
|
---|
49 |
|
---|
50 | static panel_cb_t test_cb = {
|
---|
51 | .activate_req = test_panel_activate_req
|
---|
52 | };
|
---|
53 |
|
---|
54 | /** Create and destroy panel. */
|
---|
55 | PCUT_TEST(create_destroy)
|
---|
56 | {
|
---|
57 | ui_t *ui;
|
---|
58 | ui_window_t *window;
|
---|
59 | ui_wnd_params_t params;
|
---|
60 | panel_t *panel;
|
---|
61 | errno_t rc;
|
---|
62 |
|
---|
63 | rc = ui_create_disp(NULL, &ui);
|
---|
64 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
65 |
|
---|
66 | ui_wnd_params_init(¶ms);
|
---|
67 | params.caption = "Test";
|
---|
68 |
|
---|
69 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
70 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
71 |
|
---|
72 | rc = panel_create(window, true, &panel);
|
---|
73 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
74 |
|
---|
75 | panel_destroy(panel);
|
---|
76 | ui_window_destroy(window);
|
---|
77 | ui_destroy(ui);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** panel_set_cb() sets callback */
|
---|
81 | PCUT_TEST(set_cb)
|
---|
82 | {
|
---|
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;
|
---|
89 |
|
---|
90 | rc = ui_create_disp(NULL, &ui);
|
---|
91 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
92 |
|
---|
93 | ui_wnd_params_init(¶ms);
|
---|
94 | params.caption = "Test";
|
---|
95 |
|
---|
96 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
97 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
98 |
|
---|
99 | rc = panel_create(window, true, &panel);
|
---|
100 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
101 |
|
---|
102 | panel_set_cb(panel, &test_cb, &resp);
|
---|
103 | PCUT_ASSERT_EQUALS(&test_cb, panel->cb);
|
---|
104 | PCUT_ASSERT_EQUALS(&resp, panel->cb_arg);
|
---|
105 |
|
---|
106 | panel_destroy(panel);
|
---|
107 | ui_window_destroy(window);
|
---|
108 | ui_destroy(ui);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Test panel_paint() */
|
---|
112 | PCUT_TEST(paint)
|
---|
113 | {
|
---|
114 | ui_t *ui;
|
---|
115 | ui_window_t *window;
|
---|
116 | ui_wnd_params_t params;
|
---|
117 | panel_t *panel;
|
---|
118 | errno_t rc;
|
---|
119 |
|
---|
120 | rc = ui_create_disp(NULL, &ui);
|
---|
121 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
122 |
|
---|
123 | ui_wnd_params_init(¶ms);
|
---|
124 | params.caption = "Test";
|
---|
125 |
|
---|
126 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
127 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
128 |
|
---|
129 | rc = panel_create(window, true, &panel);
|
---|
130 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
131 |
|
---|
132 | rc = panel_paint(panel);
|
---|
133 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
134 |
|
---|
135 | panel_destroy(panel);
|
---|
136 | ui_window_destroy(window);
|
---|
137 | ui_destroy(ui);
|
---|
138 | }
|
---|
139 |
|
---|
140 | /** panel_ctl() returns a valid UI control */
|
---|
141 | PCUT_TEST(ctl)
|
---|
142 | {
|
---|
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;
|
---|
149 |
|
---|
150 | rc = ui_create_disp(NULL, &ui);
|
---|
151 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
152 |
|
---|
153 | ui_wnd_params_init(¶ms);
|
---|
154 | params.caption = "Test";
|
---|
155 |
|
---|
156 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
157 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
158 |
|
---|
159 | rc = panel_create(window, true, &panel);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 |
|
---|
162 | control = panel_ctl(panel);
|
---|
163 | PCUT_ASSERT_NOT_NULL(control);
|
---|
164 |
|
---|
165 | panel_destroy(panel);
|
---|
166 | ui_window_destroy(window);
|
---|
167 | ui_destroy(ui);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /** Test panel_kbd_event() */
|
---|
171 | PCUT_TEST(kbd_event)
|
---|
172 | {
|
---|
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;
|
---|
180 |
|
---|
181 | /* Active panel should claim events */
|
---|
182 |
|
---|
183 | rc = ui_create_disp(NULL, &ui);
|
---|
184 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
185 |
|
---|
186 | ui_wnd_params_init(¶ms);
|
---|
187 | params.caption = "Test";
|
---|
188 |
|
---|
189 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
190 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
191 |
|
---|
192 | rc = panel_create(window, true, &panel);
|
---|
193 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
194 |
|
---|
195 | event.type = KEY_PRESS;
|
---|
196 | event.key = KC_ESCAPE;
|
---|
197 | event.mods = 0;
|
---|
198 | event.c = '\0';
|
---|
199 |
|
---|
200 | claimed = panel_kbd_event(panel, &event);
|
---|
201 | PCUT_ASSERT_EQUALS(ui_claimed, claimed);
|
---|
202 |
|
---|
203 | panel_destroy(panel);
|
---|
204 |
|
---|
205 | /* Inactive panel should not claim events */
|
---|
206 |
|
---|
207 | rc = panel_create(window, false, &panel);
|
---|
208 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
209 |
|
---|
210 | event.type = KEY_PRESS;
|
---|
211 | event.key = KC_ESCAPE;
|
---|
212 | event.mods = 0;
|
---|
213 | event.c = '\0';
|
---|
214 |
|
---|
215 | claimed = panel_kbd_event(panel, &event);
|
---|
216 | PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
|
---|
217 |
|
---|
218 | panel_destroy(panel);
|
---|
219 | ui_window_destroy(window);
|
---|
220 | ui_destroy(ui);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Test panel_pos_event() */
|
---|
224 | PCUT_TEST(pos_event)
|
---|
225 | {
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** panel_set_rect() sets internal field */
|
---|
229 | PCUT_TEST(set_rect)
|
---|
230 | {
|
---|
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;
|
---|
237 |
|
---|
238 | rc = ui_create_disp(NULL, &ui);
|
---|
239 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
240 |
|
---|
241 | ui_wnd_params_init(¶ms);
|
---|
242 | params.caption = "Test";
|
---|
243 |
|
---|
244 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
245 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
246 |
|
---|
247 | rc = panel_create(window, true, &panel);
|
---|
248 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
249 |
|
---|
250 | rect.p0.x = 1;
|
---|
251 | rect.p0.y = 2;
|
---|
252 | rect.p1.x = 3;
|
---|
253 | rect.p1.y = 4;
|
---|
254 |
|
---|
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);
|
---|
260 |
|
---|
261 | panel_destroy(panel);
|
---|
262 | ui_window_destroy(window);
|
---|
263 | ui_destroy(ui);
|
---|
264 | }
|
---|
265 |
|
---|
266 | /** panel_is_active() returns panel activity state */
|
---|
267 | PCUT_TEST(is_active)
|
---|
268 | {
|
---|
269 | ui_t *ui;
|
---|
270 | ui_window_t *window;
|
---|
271 | ui_wnd_params_t params;
|
---|
272 | panel_t *panel;
|
---|
273 | errno_t rc;
|
---|
274 |
|
---|
275 | rc = ui_create_disp(NULL, &ui);
|
---|
276 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
277 |
|
---|
278 | ui_wnd_params_init(¶ms);
|
---|
279 | params.caption = "Test";
|
---|
280 |
|
---|
281 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
282 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
283 |
|
---|
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);
|
---|
288 |
|
---|
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);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /** panel_activate() activates panel */
|
---|
298 | PCUT_TEST(activate)
|
---|
299 | {
|
---|
300 | ui_t *ui;
|
---|
301 | ui_window_t *window;
|
---|
302 | ui_wnd_params_t params;
|
---|
303 | panel_t *panel;
|
---|
304 | errno_t rc;
|
---|
305 |
|
---|
306 | rc = ui_create_disp(NULL, &ui);
|
---|
307 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
308 |
|
---|
309 | ui_wnd_params_init(¶ms);
|
---|
310 | params.caption = "Test";
|
---|
311 |
|
---|
312 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
313 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
314 |
|
---|
315 | rc = panel_create(window, false, &panel);
|
---|
316 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
317 |
|
---|
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));
|
---|
322 |
|
---|
323 | panel_destroy(panel);
|
---|
324 | ui_window_destroy(window);
|
---|
325 | ui_destroy(ui);
|
---|
326 | }
|
---|
327 |
|
---|
328 | /** panel_deactivate() deactivates panel */
|
---|
329 | PCUT_TEST(deactivate)
|
---|
330 | {
|
---|
331 | ui_t *ui;
|
---|
332 | ui_window_t *window;
|
---|
333 | ui_wnd_params_t params;
|
---|
334 | panel_t *panel;
|
---|
335 | errno_t rc;
|
---|
336 |
|
---|
337 | rc = ui_create_disp(NULL, &ui);
|
---|
338 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
339 |
|
---|
340 | ui_wnd_params_init(¶ms);
|
---|
341 | params.caption = "Test";
|
---|
342 |
|
---|
343 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
344 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
345 |
|
---|
346 | rc = panel_create(window, true, &panel);
|
---|
347 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
348 |
|
---|
349 | PCUT_ASSERT_TRUE(panel_is_active(panel));
|
---|
350 | panel_deactivate(panel);
|
---|
351 | PCUT_ASSERT_FALSE(panel_is_active(panel));
|
---|
352 |
|
---|
353 | panel_destroy(panel);
|
---|
354 | ui_window_destroy(window);
|
---|
355 | ui_destroy(ui);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /** panel_activate_req() sends activation request */
|
---|
359 | PCUT_TEST(activate_req)
|
---|
360 | {
|
---|
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;
|
---|
367 |
|
---|
368 | rc = ui_create_disp(NULL, &ui);
|
---|
369 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
370 |
|
---|
371 | ui_wnd_params_init(¶ms);
|
---|
372 | params.caption = "Test";
|
---|
373 |
|
---|
374 | rc = ui_window_create(ui, ¶ms, &window);
|
---|
375 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
376 |
|
---|
377 | rc = panel_create(window, true, &panel);
|
---|
378 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
379 |
|
---|
380 | panel_set_cb(panel, &test_cb, &resp);
|
---|
381 |
|
---|
382 | resp.activate_req = false;
|
---|
383 | resp.activate_req_panel = NULL;
|
---|
384 |
|
---|
385 | panel_activate_req(panel);
|
---|
386 | PCUT_ASSERT_TRUE(resp.activate_req);
|
---|
387 | PCUT_ASSERT_EQUALS(panel, resp.activate_req_panel);
|
---|
388 |
|
---|
389 | panel_destroy(panel);
|
---|
390 | ui_window_destroy(window);
|
---|
391 | ui_destroy(ui);
|
---|
392 | }
|
---|
393 |
|
---|
394 | static void test_panel_activate_req(void *arg, panel_t *panel)
|
---|
395 | {
|
---|
396 | test_resp_t *resp = (test_resp_t *)arg;
|
---|
397 |
|
---|
398 | resp->activate_req = true;
|
---|
399 | resp->activate_req_panel = panel;
|
---|
400 | }
|
---|
401 |
|
---|
402 | PCUT_EXPORT(panel);
|
---|