2 * Copyright (c) 2024 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.
30 #include <io/kbd_event.h>
31 #include <io/pos_event.h>
32 #include <pcut/pcut.h>
40 PCUT_TEST_SUITE(panel
);
45 panel_t
*activate_req_panel
;
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
)
59 ui_wnd_params_t params
;
63 rc
= ui_create_disp(NULL
, &ui
);
64 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
66 ui_wnd_params_init(¶ms
);
67 params
.caption
= "Test";
69 rc
= ui_window_create(ui
, ¶ms
, &window
);
70 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
72 rc
= panel_create(window
, true, &panel
);
73 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
76 ui_window_destroy(window
);
80 /** panel_set_cb() sets callback */
85 ui_wnd_params_t params
;
90 rc
= ui_create_disp(NULL
, &ui
);
91 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
93 ui_wnd_params_init(¶ms
);
94 params
.caption
= "Test";
96 rc
= ui_window_create(ui
, ¶ms
, &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
);
111 /** Test panel_paint() */
116 ui_wnd_params_t params
;
120 rc
= ui_create_disp(NULL
, &ui
);
121 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
123 ui_wnd_params_init(¶ms
);
124 params
.caption
= "Test";
126 rc
= ui_window_create(ui
, ¶ms
, &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
);
140 /** panel_ctl() returns a valid UI control */
145 ui_wnd_params_t params
;
147 ui_control_t
*control
;
150 rc
= ui_create_disp(NULL
, &ui
);
151 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
153 ui_wnd_params_init(¶ms
);
154 params
.caption
= "Test";
156 rc
= ui_window_create(ui
, ¶ms
, &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
);
170 /** Test panel_kbd_event() */
175 ui_wnd_params_t params
;
177 ui_evclaim_t claimed
;
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(¶ms
);
187 params
.caption
= "Test";
189 rc
= ui_window_create(ui
, ¶ms
, &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
;
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
;
215 claimed
= panel_kbd_event(panel
, &event
);
216 PCUT_ASSERT_EQUALS(ui_unclaimed
, claimed
);
218 panel_destroy(panel
);
219 ui_window_destroy(window
);
223 /** Test panel_pos_event() */
228 /** panel_set_rect() sets internal field */
233 ui_wnd_params_t params
;
238 rc
= ui_create_disp(NULL
, &ui
);
239 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
241 ui_wnd_params_init(¶ms
);
242 params
.caption
= "Test";
244 rc
= ui_window_create(ui
, ¶ms
, &window
);
245 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
247 rc
= panel_create(window
, true, &panel
);
248 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
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
);
266 /** panel_is_active() returns panel activity state */
271 ui_wnd_params_t params
;
275 rc
= ui_create_disp(NULL
, &ui
);
276 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
278 ui_wnd_params_init(¶ms
);
279 params
.caption
= "Test";
281 rc
= ui_window_create(ui
, ¶ms
, &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
);
297 /** panel_activate() activates panel */
302 ui_wnd_params_t params
;
306 rc
= ui_create_disp(NULL
, &ui
);
307 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
309 ui_wnd_params_init(¶ms
);
310 params
.caption
= "Test";
312 rc
= ui_window_create(ui
, ¶ms
, &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
);
328 /** panel_deactivate() deactivates panel */
329 PCUT_TEST(deactivate
)
333 ui_wnd_params_t params
;
337 rc
= ui_create_disp(NULL
, &ui
);
338 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
340 ui_wnd_params_init(¶ms
);
341 params
.caption
= "Test";
343 rc
= ui_window_create(ui
, ¶ms
, &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
);
358 /** panel_activate_req() sends activation request */
359 PCUT_TEST(activate_req
)
363 ui_wnd_params_t params
;
368 rc
= ui_create_disp(NULL
, &ui
);
369 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
371 ui_wnd_params_init(¶ms
);
372 params
.caption
= "Test";
374 rc
= ui_window_create(ui
, ¶ms
, &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
);
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
;