Handle Enter/Escape keys in message dialog.
[helenos.git] / uspace / lib / ui / test / list.c
blob9292ce6023f117531c2d3d9f282661c1af5b160d
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 <str.h>
35 #include <ui/ui.h>
36 #include <ui/list.h>
37 #include <ui/scrollbar.h>
38 #include <vfs/vfs.h>
39 #include "../private/list.h"
41 PCUT_INIT;
43 PCUT_TEST_SUITE(list);
45 /** Test response */
46 typedef struct {
47 bool activate_req;
48 ui_list_t *activate_req_list;
50 bool selected;
51 ui_list_entry_t *selected_entry;
52 } test_resp_t;
54 static void test_list_activate_req(ui_list_t *, void *);
55 static void test_list_selected(ui_list_entry_t *, void *);
56 static int test_list_compare(ui_list_entry_t *, ui_list_entry_t *);
58 static ui_list_cb_t test_cb = {
59 .activate_req = test_list_activate_req,
60 .selected = test_list_selected,
61 .compare = test_list_compare
64 /** Create and destroy file list. */
65 PCUT_TEST(create_destroy)
67 ui_t *ui;
68 ui_window_t *window;
69 ui_wnd_params_t params;
70 ui_list_t *list;
71 errno_t rc;
73 rc = ui_create_disp(NULL, &ui);
74 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
76 ui_wnd_params_init(&params);
77 params.caption = "Test";
79 rc = ui_window_create(ui, &params, &window);
80 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
82 rc = ui_list_create(window, true, &list);
83 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
85 ui_list_destroy(list);
86 ui_window_destroy(window);
87 ui_destroy(ui);
90 /** ui_list_set_cb() sets callback */
91 PCUT_TEST(set_cb)
93 ui_t *ui;
94 ui_window_t *window;
95 ui_wnd_params_t params;
96 ui_list_t *list;
97 errno_t rc;
98 test_resp_t resp;
100 rc = ui_create_disp(NULL, &ui);
101 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
103 ui_wnd_params_init(&params);
104 params.caption = "Test";
106 rc = ui_window_create(ui, &params, &window);
107 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
109 rc = ui_list_create(window, true, &list);
110 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
112 ui_list_set_cb(list, &test_cb, &resp);
113 PCUT_ASSERT_EQUALS(&test_cb, list->cb);
114 PCUT_ASSERT_EQUALS(&resp, list->cb_arg);
116 ui_list_destroy(list);
117 ui_window_destroy(window);
118 ui_destroy(ui);
121 /** ui_list_get_cb_arg() returns the callback argument */
122 PCUT_TEST(get_cb_arg)
124 ui_t *ui;
125 ui_window_t *window;
126 ui_wnd_params_t params;
127 ui_list_t *list;
128 errno_t rc;
129 test_resp_t resp;
130 void *arg;
132 rc = ui_create_disp(NULL, &ui);
133 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
135 ui_wnd_params_init(&params);
136 params.caption = "Test";
138 rc = ui_window_create(ui, &params, &window);
139 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
141 rc = ui_list_create(window, true, &list);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
144 ui_list_set_cb(list, &test_cb, &resp);
145 arg = ui_list_get_cb_arg(list);
146 PCUT_ASSERT_EQUALS((void *)&resp, arg);
148 ui_list_destroy(list);
149 ui_window_destroy(window);
150 ui_destroy(ui);
153 /** ui_list_entry_height() gives the correct height */
154 PCUT_TEST(entry_height)
156 ui_t *ui;
157 ui_window_t *window;
158 ui_wnd_params_t params;
159 ui_list_t *list;
160 errno_t rc;
161 gfx_coord_t height;
163 rc = ui_create_disp(NULL, &ui);
164 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
166 ui_wnd_params_init(&params);
167 params.caption = "Test";
169 rc = ui_window_create(ui, &params, &window);
170 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
172 rc = ui_list_create(window, true, &list);
173 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
175 /* Font height is 13, padding: 2 (top) + 2 (bottom) */
176 height = ui_list_entry_height(list);
177 PCUT_ASSERT_INT_EQUALS(17, height);
179 ui_list_destroy(list);
180 ui_window_destroy(window);
181 ui_destroy(ui);
184 /** Test ui_list_entry_paint() */
185 PCUT_TEST(entry_paint)
187 ui_t *ui;
188 ui_window_t *window;
189 ui_wnd_params_t params;
190 ui_list_t *list;
191 ui_list_entry_attr_t attr;
192 errno_t rc;
194 rc = ui_create_disp(NULL, &ui);
195 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
197 ui_wnd_params_init(&params);
198 params.caption = "Test";
200 rc = ui_window_create(ui, &params, &window);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
203 rc = ui_list_create(window, true, &list);
204 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
206 ui_list_entry_attr_init(&attr);
207 attr.caption = "a";
208 attr.arg = (void *)1;
210 rc = ui_list_entry_append(list, &attr, NULL);
211 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
213 rc = ui_list_entry_paint(ui_list_first(list), 0);
214 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
216 ui_list_destroy(list);
217 ui_window_destroy(window);
218 ui_destroy(ui);
221 /** Test ui_list_paint() */
222 PCUT_TEST(paint)
224 ui_t *ui;
225 ui_window_t *window;
226 ui_wnd_params_t params;
227 ui_list_t *list;
228 errno_t rc;
230 rc = ui_create_disp(NULL, &ui);
231 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
233 ui_wnd_params_init(&params);
234 params.caption = "Test";
236 rc = ui_window_create(ui, &params, &window);
237 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
239 rc = ui_list_create(window, true, &list);
240 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242 rc = ui_list_paint(list);
243 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
245 ui_list_destroy(list);
246 ui_window_destroy(window);
247 ui_destroy(ui);
250 /** ui_list_ctl() returns a valid UI control */
251 PCUT_TEST(ctl)
253 ui_t *ui;
254 ui_window_t *window;
255 ui_wnd_params_t params;
256 ui_list_t *list;
257 ui_control_t *control;
258 errno_t rc;
260 rc = ui_create_disp(NULL, &ui);
261 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
263 ui_wnd_params_init(&params);
264 params.caption = "Test";
266 rc = ui_window_create(ui, &params, &window);
267 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
269 rc = ui_list_create(window, true, &list);
270 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
272 control = ui_list_ctl(list);
273 PCUT_ASSERT_NOT_NULL(control);
275 ui_list_destroy(list);
276 ui_window_destroy(window);
277 ui_destroy(ui);
280 /** Test ui_list_kbd_event() */
281 PCUT_TEST(kbd_event)
283 ui_t *ui;
284 ui_window_t *window;
285 ui_wnd_params_t params;
286 ui_list_t *list;
287 ui_evclaim_t claimed;
288 kbd_event_t event;
289 errno_t rc;
291 /* Active list should claim events */
293 rc = ui_create_disp(NULL, &ui);
294 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
296 ui_wnd_params_init(&params);
297 params.caption = "Test";
299 rc = ui_window_create(ui, &params, &window);
300 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
302 rc = ui_list_create(window, true, &list);
303 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
305 event.type = KEY_PRESS;
306 event.key = KC_ESCAPE;
307 event.mods = 0;
308 event.c = '\0';
310 claimed = ui_list_kbd_event(list, &event);
311 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
313 ui_list_destroy(list);
315 /* Inactive list should not claim events */
317 rc = ui_create_disp(NULL, &ui);
318 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
320 ui_wnd_params_init(&params);
321 params.caption = "Test";
323 rc = ui_window_create(ui, &params, &window);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
326 rc = ui_list_create(window, false, &list);
327 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
329 event.type = KEY_PRESS;
330 event.key = KC_ESCAPE;
331 event.mods = 0;
332 event.c = '\0';
334 claimed = ui_list_kbd_event(list, &event);
335 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
337 ui_list_destroy(list);
338 ui_window_destroy(window);
339 ui_destroy(ui);
342 /** Test ui_list_pos_event() */
343 PCUT_TEST(pos_event)
345 ui_t *ui;
346 ui_window_t *window;
347 ui_wnd_params_t params;
348 ui_list_t *list;
349 ui_evclaim_t claimed;
350 pos_event_t event;
351 gfx_rect_t rect;
352 ui_list_entry_attr_t attr;
353 errno_t rc;
355 rc = ui_create_disp(NULL, &ui);
356 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
358 ui_wnd_params_init(&params);
359 params.caption = "Test";
361 rc = ui_window_create(ui, &params, &window);
362 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
364 rc = ui_list_create(window, true, &list);
365 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
367 rect.p0.x = 10;
368 rect.p0.y = 20;
369 rect.p1.x = 50;
370 rect.p1.y = 220;
372 ui_list_set_rect(list, &rect);
374 ui_list_entry_attr_init(&attr);
375 attr.caption = "a";
376 attr.arg = (void *)1;
377 rc = ui_list_entry_append(list, &attr, NULL);
378 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
380 attr.caption = "b";
381 attr.arg = (void *)2;
382 rc = ui_list_entry_append(list, &attr, NULL);
383 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
385 attr.caption = "c";
386 attr.arg = (void *)3;
387 rc = ui_list_entry_append(list, &attr, NULL);
388 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
390 list->cursor = ui_list_first(list);
391 list->cursor_idx = 0;
392 list->page = ui_list_first(list);
393 list->page_idx = 0;
395 event.pos_id = 0;
396 event.type = POS_PRESS;
397 event.btn_num = 1;
399 /* Clicking on the middle entry should select it */
400 event.hpos = 20;
401 event.vpos = 40;
403 claimed = ui_list_pos_event(list, &event);
404 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
406 PCUT_ASSERT_NOT_NULL(list->cursor);
407 PCUT_ASSERT_STR_EQUALS("b", list->cursor->caption);
408 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->cursor->arg);
410 /* Clicking on the top edge should do a page-up */
411 event.hpos = 20;
412 event.vpos = 20;
413 claimed = ui_list_pos_event(list, &event);
414 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
416 PCUT_ASSERT_NOT_NULL(list->cursor);
417 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
418 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
420 ui_list_destroy(list);
421 ui_window_destroy(window);
422 ui_destroy(ui);
425 /** ui_list_set_rect() sets internal field */
426 PCUT_TEST(set_rect)
428 ui_t *ui;
429 ui_window_t *window;
430 ui_wnd_params_t params;
431 ui_list_t *list;
432 gfx_rect_t rect;
433 errno_t rc;
435 rc = ui_create_disp(NULL, &ui);
436 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
438 ui_wnd_params_init(&params);
439 params.caption = "Test";
441 rc = ui_window_create(ui, &params, &window);
442 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
444 rc = ui_list_create(window, true, &list);
445 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
447 rect.p0.x = 1;
448 rect.p0.y = 2;
449 rect.p1.x = 3;
450 rect.p1.y = 4;
452 ui_list_set_rect(list, &rect);
453 PCUT_ASSERT_INT_EQUALS(rect.p0.x, list->rect.p0.x);
454 PCUT_ASSERT_INT_EQUALS(rect.p0.y, list->rect.p0.y);
455 PCUT_ASSERT_INT_EQUALS(rect.p1.x, list->rect.p1.x);
456 PCUT_ASSERT_INT_EQUALS(rect.p1.y, list->rect.p1.y);
458 ui_list_destroy(list);
459 ui_window_destroy(window);
460 ui_destroy(ui);
463 /** ui_list_page_size() returns correct size */
464 PCUT_TEST(page_size)
466 ui_t *ui;
467 ui_window_t *window;
468 ui_wnd_params_t params;
469 ui_list_t *list;
470 gfx_rect_t rect;
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 = "Test";
479 rc = ui_window_create(ui, &params, &window);
480 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
482 rc = ui_list_create(window, true, &list);
483 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
485 rect.p0.x = 10;
486 rect.p0.y = 20;
487 rect.p1.x = 50;
488 rect.p1.y = 220;
490 ui_list_set_rect(list, &rect);
492 /* NOTE If page size changes, we have problems elsewhere in the tests */
493 PCUT_ASSERT_INT_EQUALS(11, ui_list_page_size(list));
495 ui_list_destroy(list);
496 ui_window_destroy(window);
497 ui_destroy(ui);
500 /** ui_list_inside_rect() gives correct interior rectangle */
501 PCUT_TEST(inside_rect)
503 ui_t *ui;
504 ui_window_t *window;
505 ui_wnd_params_t params;
506 ui_list_t *list;
507 gfx_rect_t rect;
508 gfx_rect_t irect;
509 errno_t rc;
511 rc = ui_create_disp(NULL, &ui);
512 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
514 ui_wnd_params_init(&params);
515 params.caption = "Test";
517 rc = ui_window_create(ui, &params, &window);
518 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
520 rc = ui_list_create(window, true, &list);
521 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
523 rect.p0.x = 10;
524 rect.p0.y = 20;
525 rect.p1.x = 50;
526 rect.p1.y = 220;
528 ui_list_set_rect(list, &rect);
530 ui_list_inside_rect(list, &irect);
531 PCUT_ASSERT_INT_EQUALS(10 + 2, irect.p0.x);
532 PCUT_ASSERT_INT_EQUALS(20 + 2, irect.p0.y);
533 PCUT_ASSERT_INT_EQUALS(50 - 2 - 23, irect.p1.x);
534 PCUT_ASSERT_INT_EQUALS(220 - 2, irect.p1.y);
536 ui_list_destroy(list);
537 ui_window_destroy(window);
538 ui_destroy(ui);
541 /** ui_list_scrollbar_rect() gives correct scrollbar rectangle */
542 PCUT_TEST(scrollbar_rect)
544 ui_t *ui;
545 ui_window_t *window;
546 ui_wnd_params_t params;
547 ui_list_t *list;
548 gfx_rect_t rect;
549 gfx_rect_t srect;
550 errno_t rc;
552 rc = ui_create_disp(NULL, &ui);
553 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
555 ui_wnd_params_init(&params);
556 params.caption = "Test";
558 rc = ui_window_create(ui, &params, &window);
559 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
561 rc = ui_list_create(window, true, &list);
562 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
564 rect.p0.x = 10;
565 rect.p0.y = 20;
566 rect.p1.x = 50;
567 rect.p1.y = 220;
569 ui_list_set_rect(list, &rect);
571 ui_list_scrollbar_rect(list, &srect);
572 PCUT_ASSERT_INT_EQUALS(50 - 2 - 23, srect.p0.x);
573 PCUT_ASSERT_INT_EQUALS(20 + 2, srect.p0.y);
574 PCUT_ASSERT_INT_EQUALS(50 - 2, srect.p1.x);
575 PCUT_ASSERT_INT_EQUALS(220 - 2, srect.p1.y);
577 ui_list_destroy(list);
578 ui_window_destroy(window);
579 ui_destroy(ui);
582 /** ui_list_scrollbar_update() updates scrollbar position */
583 PCUT_TEST(scrollbar_update)
585 ui_t *ui;
586 ui_window_t *window;
587 ui_wnd_params_t params;
588 ui_list_t *list;
589 gfx_rect_t rect;
590 ui_list_entry_attr_t attr;
591 ui_list_entry_t *entry;
592 gfx_coord_t pos;
593 gfx_coord_t move_len;
594 errno_t rc;
596 rc = ui_create_disp(NULL, &ui);
597 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
599 ui_wnd_params_init(&params);
600 params.caption = "Test";
602 rc = ui_window_create(ui, &params, &window);
603 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
605 rc = ui_list_create(window, true, &list);
606 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
608 rect.p0.x = 0;
609 rect.p0.y = 0;
610 rect.p1.x = 50;
611 rect.p1.y = 38;
613 ui_list_set_rect(list, &rect);
615 ui_list_entry_attr_init(&attr);
616 attr.caption = "a";
617 attr.arg = (void *)1;
618 rc = ui_list_entry_append(list, &attr, NULL);
619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
621 attr.caption = "b";
622 attr.arg = (void *)2;
623 rc = ui_list_entry_append(list, &attr, NULL);
624 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
626 attr.caption = "c";
627 attr.arg = (void *)3;
628 rc = ui_list_entry_append(list, &attr, NULL);
629 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
631 entry = ui_list_next(ui_list_first(list));
633 list->cursor = entry;
634 list->cursor_idx = 1;
635 list->page = entry;
636 list->page_idx = 1;
638 ui_list_scrollbar_update(list);
640 /* Now scrollbar thumb should be all the way down */
641 move_len = ui_scrollbar_move_length(list->scrollbar);
642 pos = ui_scrollbar_get_pos(list->scrollbar);
643 PCUT_ASSERT_INT_EQUALS(move_len, pos);
645 ui_list_destroy(list);
646 ui_window_destroy(window);
647 ui_destroy(ui);
650 /** ui_list_is_active() returns list activity state */
651 PCUT_TEST(is_active)
653 ui_t *ui;
654 ui_window_t *window;
655 ui_wnd_params_t params;
656 ui_list_t *list;
657 errno_t rc;
659 rc = ui_create_disp(NULL, &ui);
660 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
662 ui_wnd_params_init(&params);
663 params.caption = "Test";
665 rc = ui_window_create(ui, &params, &window);
666 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
668 rc = ui_list_create(window, true, &list);
669 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
670 PCUT_ASSERT_TRUE(ui_list_is_active(list));
671 ui_list_destroy(list);
673 rc = ui_list_create(window, false, &list);
674 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
675 PCUT_ASSERT_FALSE(ui_list_is_active(list));
676 ui_list_destroy(list);
677 ui_window_destroy(window);
678 ui_destroy(ui);
681 /** ui_list_activate() activates list */
682 PCUT_TEST(activate)
684 ui_t *ui;
685 ui_window_t *window;
686 ui_wnd_params_t params;
687 ui_list_t *list;
688 errno_t rc;
690 rc = ui_create_disp(NULL, &ui);
691 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
693 ui_wnd_params_init(&params);
694 params.caption = "Test";
696 rc = ui_window_create(ui, &params, &window);
697 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
699 rc = ui_list_create(window, false, &list);
700 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
702 PCUT_ASSERT_FALSE(ui_list_is_active(list));
703 rc = ui_list_activate(list);
704 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
705 PCUT_ASSERT_TRUE(ui_list_is_active(list));
707 ui_list_destroy(list);
708 ui_window_destroy(window);
709 ui_destroy(ui);
712 /** ui_list_deactivate() deactivates list */
713 PCUT_TEST(deactivate)
715 ui_t *ui;
716 ui_window_t *window;
717 ui_wnd_params_t params;
718 ui_list_t *list;
719 errno_t rc;
721 rc = ui_create_disp(NULL, &ui);
722 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
724 ui_wnd_params_init(&params);
725 params.caption = "Test";
727 rc = ui_window_create(ui, &params, &window);
728 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
730 rc = ui_list_create(window, true, &list);
731 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
733 PCUT_ASSERT_TRUE(ui_list_is_active(list));
734 ui_list_deactivate(list);
735 PCUT_ASSERT_FALSE(ui_list_is_active(list));
737 ui_list_destroy(list);
738 ui_window_destroy(window);
739 ui_destroy(ui);
742 /** ui_list_get_cursor() returns the current cursor position */
743 PCUT_TEST(get_cursor)
745 ui_t *ui;
746 ui_window_t *window;
747 ui_wnd_params_t params;
748 ui_list_t *list;
749 ui_list_entry_attr_t attr;
750 ui_list_entry_t *entry;
751 ui_list_entry_t *cursor;
752 errno_t rc;
754 rc = ui_create_disp(NULL, &ui);
755 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
757 ui_wnd_params_init(&params);
758 params.caption = "Test";
760 rc = ui_window_create(ui, &params, &window);
761 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
763 rc = ui_list_create(window, true, &list);
764 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
766 ui_list_entry_attr_init(&attr);
768 /* Append entry and get pointer to it */
769 attr.caption = "a";
770 attr.arg = (void *)1;
771 entry = NULL;
772 rc = ui_list_entry_append(list, &attr, &entry);
773 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
774 PCUT_ASSERT_NOT_NULL(entry);
776 /* Cursor should be at the only entry */
777 cursor = ui_list_get_cursor(list);
778 PCUT_ASSERT_EQUALS(entry, cursor);
780 ui_list_destroy(list);
781 ui_window_destroy(window);
782 ui_destroy(ui);
785 /** ui_list_set_cursor() sets list cursor position */
786 PCUT_TEST(set_cursor)
788 ui_t *ui;
789 ui_window_t *window;
790 ui_wnd_params_t params;
791 ui_list_t *list;
792 ui_list_entry_attr_t attr;
793 ui_list_entry_t *e1;
794 ui_list_entry_t *e2;
795 errno_t rc;
797 rc = ui_create_disp(NULL, &ui);
798 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
800 ui_wnd_params_init(&params);
801 params.caption = "Test";
803 rc = ui_window_create(ui, &params, &window);
804 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
806 rc = ui_list_create(window, true, &list);
807 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
809 ui_list_entry_attr_init(&attr);
811 /* Append entry and get pointer to it */
812 attr.caption = "a";
813 attr.arg = (void *)1;
814 rc = ui_list_entry_append(list, &attr, &e1);
815 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
816 PCUT_ASSERT_NOT_NULL(e1);
818 /* Append entry and get pointer to it */
819 attr.caption = "b";
820 attr.arg = (void *)2;
821 rc = ui_list_entry_append(list, &attr, &e2);
822 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
823 PCUT_ASSERT_NOT_NULL(e2);
825 /* Append entry */
826 attr.caption = "c";
827 attr.arg = (void *)3;
828 rc = ui_list_entry_append(list, &attr, NULL);
829 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
831 /* Cursor should be at the first entry */
832 PCUT_ASSERT_EQUALS(e1, list->cursor);
833 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
835 /* Set cursor to the second entry */
836 ui_list_set_cursor(list, e2);
837 PCUT_ASSERT_EQUALS(e2, list->cursor);
838 PCUT_ASSERT_INT_EQUALS(1, list->cursor_idx);
840 ui_list_destroy(list);
841 ui_window_destroy(window);
842 ui_destroy(ui);
845 /** ui_list_entry_attr_init() initializes entry attribute structure */
846 PCUT_TEST(entry_attr_init)
848 ui_list_entry_attr_t attr;
850 ui_list_entry_attr_init(&attr);
851 PCUT_ASSERT_NULL(attr.caption);
852 PCUT_ASSERT_NULL(attr.arg);
853 PCUT_ASSERT_NULL(attr.color);
854 PCUT_ASSERT_NULL(attr.bgcolor);
857 /** ui_list_entry_append() appends new entry */
858 PCUT_TEST(entry_append)
860 ui_t *ui;
861 ui_window_t *window;
862 ui_wnd_params_t params;
863 ui_list_t *list;
864 ui_list_entry_attr_t attr;
865 ui_list_entry_t *entry;
866 errno_t rc;
868 rc = ui_create_disp(NULL, &ui);
869 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
871 ui_wnd_params_init(&params);
872 params.caption = "Test";
874 rc = ui_window_create(ui, &params, &window);
875 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
877 rc = ui_list_create(window, true, &list);
878 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
880 ui_list_entry_attr_init(&attr);
882 /* Append entry without retrieving pointer to it */
883 attr.caption = "a";
884 attr.arg = (void *)1;
885 rc = ui_list_entry_append(list, &attr, NULL);
886 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
888 PCUT_ASSERT_INT_EQUALS(1, list_count(&list->entries));
890 /* Append entry and get pointer to it */
891 attr.caption = "b";
892 attr.arg = (void *)2;
893 entry = NULL;
894 rc = ui_list_entry_append(list, &attr, &entry);
895 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
896 PCUT_ASSERT_NOT_NULL(entry);
897 PCUT_ASSERT_EQUALS(attr.arg, entry->arg);
899 PCUT_ASSERT_INT_EQUALS(2, list_count(&list->entries));
901 ui_list_destroy(list);
902 ui_window_destroy(window);
903 ui_destroy(ui);
906 /** ui_list_entry_move_up() moves entry up */
907 PCUT_TEST(entry_move_up)
909 ui_t *ui;
910 ui_window_t *window;
911 ui_wnd_params_t params;
912 ui_list_t *list;
913 ui_list_entry_attr_t attr;
914 ui_list_entry_t *e1, *e2, *e3;
915 ui_list_entry_t *e;
916 errno_t rc;
918 rc = ui_create_disp(NULL, &ui);
919 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
921 ui_wnd_params_init(&params);
922 params.caption = "Test";
924 rc = ui_window_create(ui, &params, &window);
925 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
927 rc = ui_list_create(window, true, &list);
928 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
930 ui_list_entry_attr_init(&attr);
932 /* Create entries */
934 attr.caption = "a";
935 attr.arg = (void *)1;
936 rc = ui_list_entry_append(list, &attr, &e1);
937 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
939 attr.caption = "b";
940 attr.arg = (void *)2;
941 rc = ui_list_entry_append(list, &attr, &e2);
942 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
944 attr.caption = "c";
945 attr.arg = (void *)3;
946 rc = ui_list_entry_append(list, &attr, &e3);
947 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
949 e = ui_list_first(list);
950 PCUT_ASSERT_EQUALS(e1, e);
952 /* Moving first entry up should have no effect */
953 ui_list_entry_move_up(e1);
955 e = ui_list_first(list);
956 PCUT_ASSERT_EQUALS(e1, e);
958 e = ui_list_next(e);
959 PCUT_ASSERT_EQUALS(e2, e);
961 e = ui_list_next(e);
962 PCUT_ASSERT_EQUALS(e3, e);
964 e = ui_list_next(e);
965 PCUT_ASSERT_NULL(e);
967 /* Move second entry up */
968 ui_list_entry_move_up(e2);
970 e = ui_list_first(list);
971 PCUT_ASSERT_EQUALS(e2, e);
973 e = ui_list_next(e);
974 PCUT_ASSERT_EQUALS(e1, e);
976 e = ui_list_next(e);
977 PCUT_ASSERT_EQUALS(e3, e);
979 e = ui_list_next(e);
980 PCUT_ASSERT_NULL(e);
982 ui_list_destroy(list);
983 ui_window_destroy(window);
984 ui_destroy(ui);
987 /** ui_list_entry_move_down() moves entry down */
988 PCUT_TEST(entry_move_down)
990 ui_t *ui;
991 ui_window_t *window;
992 ui_wnd_params_t params;
993 ui_list_t *list;
994 ui_list_entry_attr_t attr;
995 ui_list_entry_t *e1, *e2, *e3;
996 ui_list_entry_t *e;
997 errno_t rc;
999 rc = ui_create_disp(NULL, &ui);
1000 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1002 ui_wnd_params_init(&params);
1003 params.caption = "Test";
1005 rc = ui_window_create(ui, &params, &window);
1006 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1008 rc = ui_list_create(window, true, &list);
1009 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1011 ui_list_entry_attr_init(&attr);
1013 /* Create entries */
1015 attr.caption = "a";
1016 attr.arg = (void *)1;
1017 rc = ui_list_entry_append(list, &attr, &e1);
1018 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1020 attr.caption = "b";
1021 attr.arg = (void *)2;
1022 rc = ui_list_entry_append(list, &attr, &e2);
1023 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1025 attr.caption = "c";
1026 attr.arg = (void *)3;
1027 rc = ui_list_entry_append(list, &attr, &e3);
1028 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1030 e = ui_list_first(list);
1031 PCUT_ASSERT_EQUALS(e1, e);
1033 /* Moving last entry down should have no effect */
1034 ui_list_entry_move_down(e3);
1036 e = ui_list_first(list);
1037 PCUT_ASSERT_EQUALS(e1, e);
1039 e = ui_list_next(e);
1040 PCUT_ASSERT_EQUALS(e2, e);
1042 e = ui_list_next(e);
1043 PCUT_ASSERT_EQUALS(e3, e);
1045 e = ui_list_next(e);
1046 PCUT_ASSERT_NULL(e);
1048 /* Move second-to-last entry down */
1049 ui_list_entry_move_down(e2);
1051 e = ui_list_first(list);
1052 PCUT_ASSERT_EQUALS(e1, e);
1054 e = ui_list_next(e);
1055 PCUT_ASSERT_EQUALS(e3, e);
1057 e = ui_list_next(e);
1058 PCUT_ASSERT_EQUALS(e2, e);
1060 e = ui_list_next(e);
1061 PCUT_ASSERT_NULL(e);
1063 ui_list_destroy(list);
1064 ui_window_destroy(window);
1065 ui_destroy(ui);
1068 /** ui_list_entry_delete() deletes entry */
1069 PCUT_TEST(entry_delete)
1071 ui_t *ui;
1072 ui_window_t *window;
1073 ui_wnd_params_t params;
1074 ui_list_t *list;
1075 ui_list_entry_t *entry;
1076 ui_list_entry_attr_t attr;
1077 errno_t rc;
1079 rc = ui_create_disp(NULL, &ui);
1080 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1082 ui_wnd_params_init(&params);
1083 params.caption = "Test";
1085 rc = ui_window_create(ui, &params, &window);
1086 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1088 rc = ui_list_create(window, true, &list);
1089 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1091 attr.caption = "a";
1092 attr.arg = (void *)1;
1093 rc = ui_list_entry_append(list, &attr, NULL);
1094 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1096 attr.caption = "b";
1097 attr.arg = (void *)2;
1098 rc = ui_list_entry_append(list, &attr, NULL);
1099 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1101 PCUT_ASSERT_INT_EQUALS(2, list_count(&list->entries));
1103 entry = ui_list_first(list);
1104 ui_list_entry_delete(entry);
1106 PCUT_ASSERT_INT_EQUALS(1, list_count(&list->entries));
1108 entry = ui_list_first(list);
1109 ui_list_entry_delete(entry);
1111 PCUT_ASSERT_INT_EQUALS(0, list_count(&list->entries));
1113 ui_list_destroy(list);
1114 ui_window_destroy(window);
1115 ui_destroy(ui);
1118 /** ui_list_entry_get_arg() gets entry argument */
1119 PCUT_TEST(entry_get_arg)
1121 ui_t *ui;
1122 ui_window_t *window;
1123 ui_wnd_params_t params;
1124 ui_list_t *list;
1125 ui_list_entry_attr_t attr;
1126 ui_list_entry_t *entry;
1127 void *arg;
1128 errno_t rc;
1130 rc = ui_create_disp(NULL, &ui);
1131 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1133 ui_wnd_params_init(&params);
1134 params.caption = "Test";
1136 rc = ui_window_create(ui, &params, &window);
1137 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1139 rc = ui_list_create(window, true, &list);
1140 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1142 ui_list_entry_attr_init(&attr);
1144 /* Append entry and get pointer to it */
1145 attr.caption = "a";
1146 attr.arg = (void *)1;
1147 entry = NULL;
1148 rc = ui_list_entry_append(list, &attr, &entry);
1149 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1150 PCUT_ASSERT_NOT_NULL(entry);
1152 arg = ui_list_entry_get_arg(entry);
1153 PCUT_ASSERT_EQUALS(attr.arg, arg);
1155 ui_list_destroy(list);
1156 ui_window_destroy(window);
1157 ui_destroy(ui);
1160 /** ui_list_entry_get_list() returns the containing list */
1161 PCUT_TEST(entry_get_list)
1163 ui_t *ui;
1164 ui_window_t *window;
1165 ui_wnd_params_t params;
1166 ui_list_t *list;
1167 ui_list_t *elist;
1168 ui_list_entry_attr_t attr;
1169 ui_list_entry_t *entry;
1170 errno_t rc;
1172 rc = ui_create_disp(NULL, &ui);
1173 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1175 ui_wnd_params_init(&params);
1176 params.caption = "Test";
1178 rc = ui_window_create(ui, &params, &window);
1179 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1181 rc = ui_list_create(window, true, &list);
1182 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1184 ui_list_entry_attr_init(&attr);
1186 /* Append entry and get pointer to it */
1187 attr.caption = "a";
1188 attr.arg = (void *)1;
1189 entry = NULL;
1190 rc = ui_list_entry_append(list, &attr, &entry);
1191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1192 PCUT_ASSERT_NOT_NULL(entry);
1194 /* Get the containing list */
1195 elist = ui_list_entry_get_list(entry);
1196 PCUT_ASSERT_EQUALS(list, elist);
1198 ui_list_destroy(list);
1199 ui_window_destroy(window);
1200 ui_destroy(ui);
1203 /** ui_list_entry_set_caption() sets entry captino */
1204 PCUT_TEST(entry_set_caption)
1206 ui_t *ui;
1207 ui_window_t *window;
1208 ui_wnd_params_t params;
1209 ui_list_t *list;
1210 ui_list_entry_attr_t attr;
1211 ui_list_entry_t *entry;
1212 errno_t rc;
1214 rc = ui_create_disp(NULL, &ui);
1215 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1217 ui_wnd_params_init(&params);
1218 params.caption = "Test";
1220 rc = ui_window_create(ui, &params, &window);
1221 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1223 rc = ui_list_create(window, true, &list);
1224 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1226 ui_list_entry_attr_init(&attr);
1228 /* Append entry and get pointer to it */
1229 attr.caption = "a";
1230 attr.arg = (void *)1;
1231 entry = NULL;
1232 rc = ui_list_entry_append(list, &attr, &entry);
1233 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1234 PCUT_ASSERT_NOT_NULL(entry);
1236 /* Change caption */
1237 rc = ui_list_entry_set_caption(entry, "b");
1238 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1239 PCUT_ASSERT_STR_EQUALS("b", entry->caption);
1241 ui_list_destroy(list);
1242 ui_window_destroy(window);
1243 ui_destroy(ui);
1246 /** ui_list_entries_cnt() returns the number of entries */
1247 PCUT_TEST(entries_cnt)
1249 ui_t *ui;
1250 ui_window_t *window;
1251 ui_wnd_params_t params;
1252 ui_list_t *list;
1253 ui_list_entry_attr_t attr;
1254 errno_t rc;
1256 rc = ui_create_disp(NULL, &ui);
1257 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1259 ui_wnd_params_init(&params);
1260 params.caption = "Test";
1262 rc = ui_window_create(ui, &params, &window);
1263 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1265 rc = ui_list_create(window, true, &list);
1266 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1268 PCUT_ASSERT_INT_EQUALS(0, ui_list_entries_cnt(list));
1270 ui_list_entry_attr_init(&attr);
1272 /* Append entry */
1273 attr.caption = "a";
1274 attr.arg = (void *)1;
1275 rc = ui_list_entry_append(list, &attr, NULL);
1276 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1278 PCUT_ASSERT_INT_EQUALS(1, ui_list_entries_cnt(list));
1280 /* Append another entry */
1281 attr.caption = "b";
1282 attr.arg = (void *)2;
1283 rc = ui_list_entry_append(list, &attr, NULL);
1284 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1286 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(list));
1288 ui_list_destroy(list);
1289 ui_window_destroy(window);
1290 ui_destroy(ui);
1293 /** ui_list_sort() sorts UI list entries */
1294 PCUT_TEST(sort)
1296 ui_t *ui;
1297 ui_window_t *window;
1298 ui_wnd_params_t params;
1299 ui_list_t *list;
1300 ui_list_entry_t *entry;
1301 ui_list_entry_attr_t attr;
1302 test_resp_t resp;
1303 errno_t rc;
1305 rc = ui_create_disp(NULL, &ui);
1306 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1308 ui_wnd_params_init(&params);
1309 params.caption = "Test";
1311 rc = ui_window_create(ui, &params, &window);
1312 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1314 rc = ui_list_create(window, true, &list);
1315 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1317 ui_list_set_cb(list, &test_cb, &resp);
1319 ui_list_entry_attr_init(&attr);
1321 attr.caption = "b";
1322 attr.arg = (void *)1;
1323 rc = ui_list_entry_append(list, &attr, NULL);
1324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1326 attr.caption = "c";
1327 attr.arg = (void *)3;
1328 rc = ui_list_entry_append(list, &attr, NULL);
1329 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1331 attr.caption = "a";
1332 attr.arg = (void *)2;
1333 rc = ui_list_entry_append(list, &attr, NULL);
1334 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1336 rc = ui_list_sort(list);
1337 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1339 entry = ui_list_first(list);
1340 PCUT_ASSERT_STR_EQUALS("a", entry->caption);
1341 PCUT_ASSERT_EQUALS((void *)2, entry->arg);
1343 entry = ui_list_next(entry);
1344 PCUT_ASSERT_STR_EQUALS("b", entry->caption);
1345 PCUT_ASSERT_EQUALS((void *)1, entry->arg);
1347 entry = ui_list_next(entry);
1348 PCUT_ASSERT_STR_EQUALS("c", entry->caption);
1349 PCUT_ASSERT_EQUALS((void *)3, entry->arg);
1351 ui_list_destroy(list);
1352 ui_window_destroy(window);
1353 ui_destroy(ui);
1356 /** ui_list_cursor_center()...XXX */
1357 PCUT_TEST(cursor_center)
1359 ui_t *ui;
1360 ui_window_t *window;
1361 ui_wnd_params_t params;
1362 ui_list_t *list;
1363 ui_list_entry_t *a, *b, *c, *d, *e;
1364 ui_list_entry_attr_t attr;
1365 gfx_rect_t rect;
1366 test_resp_t resp;
1367 errno_t rc;
1369 rc = ui_create_disp(NULL, &ui);
1370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1372 ui_wnd_params_init(&params);
1373 params.caption = "Test";
1375 rc = ui_window_create(ui, &params, &window);
1376 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1378 rc = ui_list_create(window, true, &list);
1379 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1381 ui_list_set_cb(list, &test_cb, &resp);
1383 rect.p0.x = 10;
1384 rect.p0.y = 20;
1385 rect.p1.x = 50;
1386 rect.p1.y = 80;
1388 ui_list_set_rect(list, &rect);
1390 PCUT_ASSERT_INT_EQUALS(3, ui_list_page_size(list));
1392 ui_list_entry_attr_init(&attr);
1394 attr.caption = "a";
1395 attr.arg = (void *)1;
1396 rc = ui_list_entry_append(list, &attr, &a);
1397 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1399 attr.caption = "b";
1400 attr.arg = (void *)2;
1401 rc = ui_list_entry_append(list, &attr, &b);
1402 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1404 /* We only have two entries, but three fit onto the page */
1405 ui_list_cursor_center(list, b);
1406 PCUT_ASSERT_EQUALS(b, list->cursor);
1407 /* Page should start at the beginning */
1408 PCUT_ASSERT_EQUALS(a, list->page);
1409 PCUT_ASSERT_EQUALS(0, list->page_idx);
1411 /* Add more entries */
1413 attr.caption = "c";
1414 attr.arg = (void *)3;
1415 rc = ui_list_entry_append(list, &attr, &c);
1416 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1418 attr.caption = "d";
1419 attr.arg = (void *)4;
1420 rc = ui_list_entry_append(list, &attr, &d);
1421 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1423 attr.caption = "e";
1424 attr.arg = (void *)5;
1425 rc = ui_list_entry_append(list, &attr, &e);
1426 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1428 ui_list_cursor_center(list, c);
1429 PCUT_ASSERT_EQUALS(c, list->cursor);
1431 * We have enough entries, c should be in the middle of the three
1432 * entries on the page, i.e., page should start on 'b'.
1434 PCUT_ASSERT_EQUALS(b, list->page);
1435 PCUT_ASSERT_EQUALS(1, list->page_idx);
1437 ui_list_destroy(list);
1438 ui_window_destroy(window);
1439 ui_destroy(ui);
1442 /** ui_list_clear_entries() removes all entries from list */
1443 PCUT_TEST(clear_entries)
1445 ui_t *ui;
1446 ui_window_t *window;
1447 ui_wnd_params_t params;
1448 ui_list_t *list;
1449 ui_list_entry_attr_t attr;
1450 errno_t rc;
1452 rc = ui_create_disp(NULL, &ui);
1453 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1455 ui_wnd_params_init(&params);
1456 params.caption = "Test";
1458 rc = ui_window_create(ui, &params, &window);
1459 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1461 rc = ui_list_create(window, true, &list);
1462 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1464 ui_list_entry_attr_init(&attr);
1465 attr.caption = "a";
1466 attr.arg = (void *)1;
1467 rc = ui_list_entry_append(list, &attr, NULL);
1468 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1470 attr.caption = "a";
1471 attr.arg = (void *)2;
1472 rc = ui_list_entry_append(list, &attr, NULL);
1473 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1475 PCUT_ASSERT_INT_EQUALS(2, list_count(&list->entries));
1477 ui_list_clear_entries(list);
1478 PCUT_ASSERT_INT_EQUALS(0, list_count(&list->entries));
1480 ui_list_destroy(list);
1481 ui_window_destroy(window);
1482 ui_destroy(ui);
1485 /** ui_list_first() returns valid entry or @c NULL as appropriate */
1486 PCUT_TEST(first)
1488 ui_t *ui;
1489 ui_window_t *window;
1490 ui_wnd_params_t params;
1491 ui_list_t *list;
1492 ui_list_entry_t *entry;
1493 ui_list_entry_attr_t attr;
1494 errno_t rc;
1496 rc = ui_create_disp(NULL, &ui);
1497 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1499 ui_wnd_params_init(&params);
1500 params.caption = "Test";
1502 rc = ui_window_create(ui, &params, &window);
1503 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1505 rc = ui_list_create(window, true, &list);
1506 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1508 ui_list_entry_attr_init(&attr);
1510 entry = ui_list_first(list);
1511 PCUT_ASSERT_NULL(entry);
1513 /* Add one entry */
1514 attr.caption = "a";
1515 attr.arg = (void *)1;
1516 rc = ui_list_entry_append(list, &attr, NULL);
1517 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1519 /* Now try getting it */
1520 entry = ui_list_first(list);
1521 PCUT_ASSERT_NOT_NULL(entry);
1522 PCUT_ASSERT_STR_EQUALS("a", entry->caption);
1523 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)entry->arg);
1525 /* Add another entry */
1526 attr.caption = "b";
1527 attr.arg = (void *)2;
1528 rc = ui_list_entry_append(list, &attr, NULL);
1529 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1531 /* We should still get the first entry */
1532 entry = ui_list_first(list);
1533 PCUT_ASSERT_NOT_NULL(entry);
1534 PCUT_ASSERT_STR_EQUALS("a", entry->caption);
1535 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)entry->arg);
1537 ui_list_destroy(list);
1538 ui_window_destroy(window);
1539 ui_destroy(ui);
1542 /** ui_list_last() returns valid entry or @c NULL as appropriate */
1543 PCUT_TEST(last)
1545 ui_t *ui;
1546 ui_window_t *window;
1547 ui_wnd_params_t params;
1548 ui_list_t *list;
1549 ui_list_entry_t *entry;
1550 ui_list_entry_attr_t attr;
1551 errno_t rc;
1553 rc = ui_create_disp(NULL, &ui);
1554 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1556 ui_wnd_params_init(&params);
1557 params.caption = "Test";
1559 rc = ui_window_create(ui, &params, &window);
1560 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1562 rc = ui_list_create(window, true, &list);
1563 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1565 ui_list_entry_attr_init(&attr);
1567 entry = ui_list_last(list);
1568 PCUT_ASSERT_NULL(entry);
1570 /* Add one entry */
1571 attr.caption = "a";
1572 attr.arg = (void *)1;
1573 rc = ui_list_entry_append(list, &attr, NULL);
1574 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1576 /* Now try getting it */
1577 entry = ui_list_last(list);
1578 PCUT_ASSERT_NOT_NULL(entry);
1579 PCUT_ASSERT_STR_EQUALS("a", entry->caption);
1580 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)entry->arg);
1582 /* Add another entry */
1583 attr.caption = "b";
1584 attr.arg = (void *)2;
1585 rc = ui_list_entry_append(list, &attr, NULL);
1586 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1588 /* We should get new entry now */
1589 entry = ui_list_last(list);
1590 PCUT_ASSERT_NOT_NULL(entry);
1591 attr.caption = "b";
1592 attr.arg = (void *)2;
1593 PCUT_ASSERT_STR_EQUALS("b", entry->caption);
1594 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)entry->arg);
1596 ui_list_destroy(list);
1597 ui_window_destroy(window);
1598 ui_destroy(ui);
1601 /** ui_list_next() returns the next entry or @c NULL as appropriate */
1602 PCUT_TEST(next)
1604 ui_t *ui;
1605 ui_window_t *window;
1606 ui_wnd_params_t params;
1607 ui_list_t *list;
1608 ui_list_entry_t *entry;
1609 ui_list_entry_attr_t attr;
1610 errno_t rc;
1612 rc = ui_create_disp(NULL, &ui);
1613 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1615 ui_wnd_params_init(&params);
1616 params.caption = "Test";
1618 rc = ui_window_create(ui, &params, &window);
1619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1621 rc = ui_list_create(window, true, &list);
1622 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1624 ui_list_entry_attr_init(&attr);
1626 /* Add one entry */
1627 attr.caption = "a";
1628 attr.arg = (void *)1;
1629 rc = ui_list_entry_append(list, &attr, NULL);
1630 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1632 /* Now try getting its successor */
1633 entry = ui_list_first(list);
1634 PCUT_ASSERT_NOT_NULL(entry);
1636 entry = ui_list_next(entry);
1637 PCUT_ASSERT_NULL(entry);
1639 /* Add another entry */
1640 attr.caption = "b";
1641 attr.arg = (void *)2;
1642 rc = ui_list_entry_append(list, &attr, NULL);
1643 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1645 /* Try getting the successor of first entry again */
1646 entry = ui_list_first(list);
1647 PCUT_ASSERT_NOT_NULL(entry);
1649 entry = ui_list_next(entry);
1650 PCUT_ASSERT_NOT_NULL(entry);
1651 PCUT_ASSERT_STR_EQUALS("b", entry->caption);
1652 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)entry->arg);
1654 ui_list_destroy(list);
1655 ui_window_destroy(window);
1656 ui_destroy(ui);
1659 /** ui_list_prev() returns the previous entry or @c NULL as appropriate */
1660 PCUT_TEST(prev)
1662 ui_t *ui;
1663 ui_window_t *window;
1664 ui_wnd_params_t params;
1665 ui_list_t *list;
1666 ui_list_entry_t *entry;
1667 ui_list_entry_attr_t attr;
1668 errno_t rc;
1670 rc = ui_create_disp(NULL, &ui);
1671 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1673 ui_wnd_params_init(&params);
1674 params.caption = "Test";
1676 rc = ui_window_create(ui, &params, &window);
1677 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1679 rc = ui_list_create(window, true, &list);
1680 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1682 ui_list_entry_attr_init(&attr);
1684 /* Add one entry */
1685 attr.caption = "a";
1686 attr.arg = (void *)1;
1687 rc = ui_list_entry_append(list, &attr, NULL);
1688 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1690 /* Now try getting its predecessor */
1691 entry = ui_list_last(list);
1692 PCUT_ASSERT_NOT_NULL(entry);
1694 entry = ui_list_prev(entry);
1695 PCUT_ASSERT_NULL(entry);
1697 /* Add another entry */
1698 attr.caption = "b";
1699 attr.arg = (void *)2;
1700 rc = ui_list_entry_append(list, &attr, NULL);
1701 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1703 /* Try getting the predecessor of the new entry */
1704 entry = ui_list_last(list);
1705 PCUT_ASSERT_NOT_NULL(entry);
1707 entry = ui_list_prev(entry);
1708 PCUT_ASSERT_NOT_NULL(entry);
1709 PCUT_ASSERT_STR_EQUALS("a", entry->caption);
1710 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)entry->arg);
1712 ui_list_destroy(list);
1713 ui_window_destroy(window);
1714 ui_destroy(ui);
1717 /** ui_list_page_nth_entry() .. */
1718 PCUT_TEST(page_nth_entry)
1720 ui_t *ui;
1721 ui_window_t *window;
1722 ui_wnd_params_t params;
1723 ui_list_t *list;
1724 ui_list_entry_t *entry;
1725 ui_list_entry_attr_t attr;
1726 gfx_rect_t rect;
1727 size_t idx;
1728 errno_t rc;
1730 rc = ui_create_disp(NULL, &ui);
1731 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1733 ui_wnd_params_init(&params);
1734 params.caption = "Test";
1736 rc = ui_window_create(ui, &params, &window);
1737 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1739 rc = ui_list_create(window, true, &list);
1740 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1742 ui_list_entry_attr_init(&attr);
1744 /* Add some entries */
1745 attr.caption = "a";
1746 attr.arg = (void *)1;
1747 rc = ui_list_entry_append(list, &attr, NULL);
1748 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1750 attr.caption = "b";
1751 attr.arg = (void *)2;
1752 rc = ui_list_entry_append(list, &attr, NULL);
1753 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1755 attr.caption = "c";
1756 attr.arg = (void *)3;
1757 rc = ui_list_entry_append(list, &attr, NULL);
1758 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1760 list->page = ui_list_next(ui_list_first(list));
1761 list->page_idx = 1;
1763 rect.p0.x = 0;
1764 rect.p0.y = 0;
1765 rect.p1.x = 100;
1766 rect.p1.y = 100;
1767 ui_list_set_rect(list, &rect);
1769 entry = ui_list_page_nth_entry(list, 0, &idx);
1770 PCUT_ASSERT_NOT_NULL(entry);
1771 PCUT_ASSERT_STR_EQUALS("b", entry->caption);
1772 PCUT_ASSERT_INT_EQUALS(1, idx);
1774 entry = ui_list_page_nth_entry(list, 1, &idx);
1775 PCUT_ASSERT_NOT_NULL(entry);
1776 PCUT_ASSERT_STR_EQUALS("c", entry->caption);
1777 PCUT_ASSERT_INT_EQUALS(2, idx);
1779 entry = ui_list_page_nth_entry(list, 2, &idx);
1780 PCUT_ASSERT_NULL(entry);
1782 ui_list_destroy(list);
1783 ui_window_destroy(window);
1784 ui_destroy(ui);
1787 /** ui_list_cursor_move() moves cursor and scrolls */
1788 PCUT_TEST(cursor_move)
1790 ui_t *ui;
1791 ui_window_t *window;
1792 ui_wnd_params_t params;
1793 ui_list_t *list;
1794 ui_list_entry_attr_t attr;
1795 gfx_rect_t rect;
1796 errno_t rc;
1797 rc = ui_create_disp(NULL, &ui);
1798 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1800 ui_wnd_params_init(&params);
1801 params.caption = "Test";
1803 rc = ui_window_create(ui, &params, &window);
1804 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1806 rc = ui_list_create(window, true, &list);
1807 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1809 rect.p0.x = 0;
1810 rect.p0.y = 0;
1811 rect.p1.x = 10;
1812 rect.p1.y = 38; /* Assuming this makes page size 2 */
1813 ui_list_set_rect(list, &rect);
1815 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
1817 /* Add tree entries (more than page size, which is 2) */
1819 ui_list_entry_attr_init(&attr);
1821 attr.caption = "a";
1822 attr.arg = (void *)1;
1823 rc = ui_list_entry_append(list, &attr, NULL);
1824 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1826 attr.caption = "b";
1827 attr.arg = (void *)2;
1828 rc = ui_list_entry_append(list, &attr, NULL);
1829 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1831 attr.caption = "c";
1832 attr.arg = (void *)3;
1833 rc = ui_list_entry_append(list, &attr, NULL);
1834 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1836 /* Cursor to the last entry and page start to the next-to-last entry */
1837 list->cursor = ui_list_last(list);
1838 list->cursor_idx = 2;
1839 list->page = ui_list_prev(list->cursor);
1840 list->page_idx = 1;
1842 /* Move cursor one entry up */
1843 ui_list_cursor_move(list, ui_list_prev(list->cursor),
1844 list->cursor_idx - 1);
1846 /* Cursor and page start should now both be at the second entry */
1847 PCUT_ASSERT_STR_EQUALS("b", list->cursor->caption);
1848 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->cursor->arg);
1849 PCUT_ASSERT_INT_EQUALS(1, list->cursor_idx);
1850 PCUT_ASSERT_EQUALS(list->cursor, list->page);
1851 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
1853 /* Move cursor to the first entry. This should scroll up. */
1854 ui_list_cursor_move(list, ui_list_first(list), 0);
1856 /* Cursor and page start should now both be at the first entry */
1857 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
1858 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
1859 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
1860 PCUT_ASSERT_EQUALS(list->cursor, list->page);
1861 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
1863 /* Move cursor to the last entry. */
1864 ui_list_cursor_move(list, ui_list_last(list), 2);
1866 /* Cursor should be on the last entry and page on the next to last */
1867 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
1868 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
1869 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
1870 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
1871 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
1872 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
1874 ui_list_destroy(list);
1875 ui_window_destroy(window);
1876 ui_destroy(ui);
1879 /** ui_list_cursor_up() moves cursor one entry up */
1880 PCUT_TEST(cursor_up)
1882 ui_t *ui;
1883 ui_window_t *window;
1884 ui_wnd_params_t params;
1885 ui_list_t *list;
1886 ui_list_entry_attr_t attr;
1887 gfx_rect_t rect;
1888 errno_t rc;
1889 rc = ui_create_disp(NULL, &ui);
1890 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1892 ui_wnd_params_init(&params);
1893 params.caption = "Test";
1895 rc = ui_window_create(ui, &params, &window);
1896 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1898 rc = ui_list_create(window, true, &list);
1899 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1901 rect.p0.x = 0;
1902 rect.p0.y = 0;
1903 rect.p1.x = 10;
1904 rect.p1.y = 38; /* Assuming this makes page size 2 */
1905 ui_list_set_rect(list, &rect);
1907 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
1909 /* Add tree entries (more than page size, which is 2) */
1911 ui_list_entry_attr_init(&attr);
1913 attr.caption = "a";
1914 attr.arg = (void *)1;
1915 rc = ui_list_entry_append(list, &attr, NULL);
1916 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1918 attr.caption = "b";
1919 attr.arg = (void *)2;
1920 rc = ui_list_entry_append(list, &attr, NULL);
1921 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1923 attr.caption = "c";
1924 attr.arg = (void *)3;
1925 rc = ui_list_entry_append(list, &attr, NULL);
1926 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1928 /* Cursor to the last entry and page start to the next-to-last entry */
1929 list->cursor = ui_list_last(list);
1930 list->cursor_idx = 2;
1931 list->page = ui_list_prev(list->cursor);
1932 list->page_idx = 1;
1934 /* Move cursor one entry up */
1935 ui_list_cursor_up(list);
1937 /* Cursor and page start should now both be at the second entry */
1938 PCUT_ASSERT_STR_EQUALS("b", list->cursor->caption);
1939 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->cursor->arg);
1940 PCUT_ASSERT_INT_EQUALS(1, list->cursor_idx);
1941 PCUT_ASSERT_EQUALS(list->cursor, list->page);
1942 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
1944 /* Move cursor one entry up. This should scroll up. */
1945 ui_list_cursor_up(list);
1947 /* Cursor and page start should now both be at the first entry */
1948 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
1949 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
1950 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
1951 PCUT_ASSERT_EQUALS(list->cursor, list->page);
1952 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
1954 /* Moving further up should do nothing (we are at the top). */
1955 ui_list_cursor_up(list);
1957 /* Cursor and page start should still be at the first entry */
1958 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
1959 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
1960 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
1961 PCUT_ASSERT_EQUALS(list->cursor, list->page);
1962 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
1964 ui_list_destroy(list);
1965 ui_window_destroy(window);
1966 ui_destroy(ui);
1969 /** ui_list_cursor_down() moves cursor one entry down */
1970 PCUT_TEST(cursor_down)
1972 ui_t *ui;
1973 ui_window_t *window;
1974 ui_wnd_params_t params;
1975 ui_list_t *list;
1976 ui_list_entry_attr_t attr;
1977 gfx_rect_t rect;
1978 errno_t rc;
1980 rc = ui_create_disp(NULL, &ui);
1981 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1983 ui_wnd_params_init(&params);
1984 params.caption = "Test";
1986 rc = ui_window_create(ui, &params, &window);
1987 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1989 rc = ui_list_create(window, true, &list);
1990 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1992 rect.p0.x = 0;
1993 rect.p0.y = 0;
1994 rect.p1.x = 10;
1995 rect.p1.y = 38; /* Assuming this makes page size 2 */
1996 ui_list_set_rect(list, &rect);
1998 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2000 /* Add tree entries (more than page size, which is 2) */
2002 ui_list_entry_attr_init(&attr);
2004 attr.caption = "a";
2005 attr.arg = (void *)1;
2006 rc = ui_list_entry_append(list, &attr, NULL);
2007 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2009 attr.caption = "b";
2010 attr.arg = (void *)2;
2011 rc = ui_list_entry_append(list, &attr, NULL);
2012 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2014 attr.caption = "c";
2015 attr.arg = (void *)3;
2016 rc = ui_list_entry_append(list, &attr, NULL);
2017 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2019 /* Cursor and page start to the first entry */
2020 list->cursor = ui_list_first(list);
2021 list->cursor_idx = 0;
2022 list->page = list->cursor;
2023 list->page_idx = 0;
2025 /* Move cursor one entry down */
2026 ui_list_cursor_down(list);
2028 /* Cursor should now be at the second entry, page stays the same */
2029 PCUT_ASSERT_STR_EQUALS("b", list->cursor->caption);
2030 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->cursor->arg);
2031 PCUT_ASSERT_INT_EQUALS(1, list->cursor_idx);
2032 PCUT_ASSERT_EQUALS(ui_list_first(list), list->page);
2033 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2035 /* Move cursor one entry down. This should scroll down. */
2036 ui_list_cursor_down(list);
2038 /* Cursor should now be at the third and page at the second entry. */
2039 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2040 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2041 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2042 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2043 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2044 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2046 /* Moving further down should do nothing (we are at the bottom). */
2047 ui_list_cursor_down(list);
2049 /* Cursor should still be at the third and page at the second entry. */
2050 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2051 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2052 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2053 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2054 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2055 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2057 ui_list_destroy(list);
2058 ui_window_destroy(window);
2059 ui_destroy(ui);
2062 /** ui_list_cursor_top() moves cursor to the first entry */
2063 PCUT_TEST(cursor_top)
2065 ui_t *ui;
2066 ui_window_t *window;
2067 ui_wnd_params_t params;
2068 ui_list_t *list;
2069 ui_list_entry_attr_t attr;
2070 gfx_rect_t rect;
2071 errno_t rc;
2073 rc = ui_create_disp(NULL, &ui);
2074 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2076 ui_wnd_params_init(&params);
2077 params.caption = "Test";
2079 rc = ui_window_create(ui, &params, &window);
2080 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2082 rc = ui_list_create(window, true, &list);
2083 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2085 rect.p0.x = 0;
2086 rect.p0.y = 0;
2087 rect.p1.x = 10;
2088 rect.p1.y = 38; /* Assuming this makes page size 2 */
2089 ui_list_set_rect(list, &rect);
2091 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2093 /* Add tree entries (more than page size, which is 2) */
2095 ui_list_entry_attr_init(&attr);
2097 attr.caption = "a";
2098 attr.arg = (void *)1;
2099 rc = ui_list_entry_append(list, &attr, NULL);
2100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2102 attr.caption = "b";
2103 attr.arg = (void *)2;
2104 rc = ui_list_entry_append(list, &attr, NULL);
2105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2107 attr.caption = "c";
2108 attr.arg = (void *)3;
2109 rc = ui_list_entry_append(list, &attr, NULL);
2110 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2112 /* Cursor to the last entry and page start to the next-to-last entry */
2113 list->cursor = ui_list_last(list);
2114 list->cursor_idx = 2;
2115 list->page = ui_list_prev(list->cursor);
2116 list->page_idx = 1;
2118 /* Move cursor to the top. This should scroll up. */
2119 ui_list_cursor_top(list);
2121 /* Cursor and page start should now both be at the first entry */
2122 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2123 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2124 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2125 PCUT_ASSERT_EQUALS(list->cursor, list->page);
2126 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2128 ui_list_destroy(list);
2129 ui_window_destroy(window);
2130 ui_destroy(ui);
2133 /** ui_list_cursor_bottom() moves cursor to the last entry */
2134 PCUT_TEST(cursor_bottom)
2136 ui_t *ui;
2137 ui_window_t *window;
2138 ui_wnd_params_t params;
2139 ui_list_t *list;
2140 ui_list_entry_attr_t attr;
2141 gfx_rect_t rect;
2142 errno_t rc;
2144 rc = ui_create_disp(NULL, &ui);
2145 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2147 ui_wnd_params_init(&params);
2148 params.caption = "Test";
2150 rc = ui_window_create(ui, &params, &window);
2151 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2153 rc = ui_list_create(window, true, &list);
2154 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2156 rect.p0.x = 0;
2157 rect.p0.y = 0;
2158 rect.p1.x = 10;
2159 rect.p1.y = 38; /* Assuming this makes page size 2 */
2160 ui_list_set_rect(list, &rect);
2162 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2164 /* Add tree entries (more than page size, which is 2) */
2166 ui_list_entry_attr_init(&attr);
2168 attr.caption = "a";
2169 attr.arg = (void *)1;
2170 rc = ui_list_entry_append(list, &attr, NULL);
2171 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2173 attr.caption = "b";
2174 attr.arg = (void *)2;
2175 rc = ui_list_entry_append(list, &attr, NULL);
2176 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2178 attr.caption = "c";
2179 attr.arg = (void *)3;
2180 rc = ui_list_entry_append(list, &attr, NULL);
2181 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2183 /* Cursor and page start to the first entry */
2184 list->cursor = ui_list_first(list);
2185 list->cursor_idx = 0;
2186 list->page = list->cursor;
2187 list->page_idx = 0;
2189 /* Move cursor to the bottom. This should scroll down. */
2190 ui_list_cursor_bottom(list);
2192 /* Cursor should now be at the third and page at the second entry. */
2193 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2194 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2195 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2196 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2197 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2198 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2200 ui_list_destroy(list);
2201 ui_window_destroy(window);
2202 ui_destroy(ui);
2205 /** ui_list_page_up() moves one page up */
2206 PCUT_TEST(page_up)
2208 ui_t *ui;
2209 ui_window_t *window;
2210 ui_wnd_params_t params;
2211 ui_list_t *list;
2212 ui_list_entry_attr_t attr;
2213 gfx_rect_t rect;
2214 errno_t rc;
2216 rc = ui_create_disp(NULL, &ui);
2217 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2219 ui_wnd_params_init(&params);
2220 params.caption = "Test";
2222 rc = ui_window_create(ui, &params, &window);
2223 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2225 rc = ui_list_create(window, true, &list);
2226 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2228 rect.p0.x = 0;
2229 rect.p0.y = 0;
2230 rect.p1.x = 10;
2231 rect.p1.y = 38; /* Assuming this makes page size 2 */
2232 ui_list_set_rect(list, &rect);
2234 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2236 /* Add five entries (2 full pages, one partial) */
2238 ui_list_entry_attr_init(&attr);
2240 attr.caption = "a";
2241 attr.arg = (void *)1;
2242 rc = ui_list_entry_append(list, &attr, NULL);
2243 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2245 attr.caption = "b";
2246 attr.arg = (void *)2;
2247 rc = ui_list_entry_append(list, &attr, NULL);
2248 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2250 attr.caption = "c";
2251 attr.arg = (void *)3;
2252 rc = ui_list_entry_append(list, &attr, NULL);
2253 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2255 attr.caption = "d";
2256 attr.arg = (void *)4;
2257 rc = ui_list_entry_append(list, &attr, NULL);
2258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2260 attr.caption = "e";
2261 attr.arg = (void *)5;
2262 rc = ui_list_entry_append(list, &attr, NULL);
2263 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2265 /* Cursor to the last entry and page start to the next-to-last entry */
2266 list->cursor = ui_list_last(list);
2267 list->cursor_idx = 4;
2268 list->page = ui_list_prev(list->cursor);
2269 list->page_idx = 3;
2271 /* Move one page up */
2272 ui_list_page_up(list);
2274 /* Page should now start at second entry and cursor at third */
2275 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2276 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2277 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2278 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2279 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2280 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2282 /* Move one page up again. */
2283 ui_list_page_up(list);
2285 /* Cursor and page start should now both be at the first entry */
2286 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2287 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2288 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2289 PCUT_ASSERT_EQUALS(list->cursor, list->page);
2290 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2292 /* Moving further up should do nothing (we are at the top). */
2293 ui_list_page_up(list);
2295 /* Cursor and page start should still be at the first entry */
2296 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2297 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2298 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2299 PCUT_ASSERT_EQUALS(list->cursor, list->page);
2300 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2302 ui_list_destroy(list);
2303 ui_window_destroy(window);
2304 ui_destroy(ui);
2307 /** ui_list_page_up() moves one page down */
2308 PCUT_TEST(page_down)
2310 ui_t *ui;
2311 ui_window_t *window;
2312 ui_wnd_params_t params;
2313 ui_list_t *list;
2314 ui_list_entry_attr_t attr;
2315 gfx_rect_t rect;
2316 errno_t rc;
2318 rc = ui_create_disp(NULL, &ui);
2319 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2321 ui_wnd_params_init(&params);
2322 params.caption = "Test";
2324 rc = ui_window_create(ui, &params, &window);
2325 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2327 rc = ui_list_create(window, true, &list);
2328 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2330 rect.p0.x = 0;
2331 rect.p0.y = 0;
2332 rect.p1.x = 10;
2333 rect.p1.y = 38; /* Assuming this makes page size 2 */
2334 ui_list_set_rect(list, &rect);
2336 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2338 /* Add five entries (2 full pages, one partial) */
2340 ui_list_entry_attr_init(&attr);
2342 attr.caption = "a";
2343 attr.arg = (void *)1;
2344 rc = ui_list_entry_append(list, &attr, NULL);
2345 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2347 attr.caption = "b";
2348 attr.arg = (void *)2;
2349 rc = ui_list_entry_append(list, &attr, NULL);
2350 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2352 attr.caption = "c";
2353 attr.arg = (void *)3;
2354 rc = ui_list_entry_append(list, &attr, NULL);
2355 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2357 attr.caption = "d";
2358 attr.arg = (void *)4;
2359 rc = ui_list_entry_append(list, &attr, NULL);
2360 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2362 attr.caption = "e";
2363 attr.arg = (void *)5;
2364 rc = ui_list_entry_append(list, &attr, NULL);
2365 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2367 /* Cursor and page to the first entry */
2368 list->cursor = ui_list_first(list);
2369 list->cursor_idx = 0;
2370 list->page = list->cursor;
2371 list->page_idx = 0;
2373 /* Move one page down */
2374 ui_list_page_down(list);
2376 /* Page and cursor should point to the third entry */
2377 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2378 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2379 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2380 PCUT_ASSERT_STR_EQUALS("c", list->page->caption);
2381 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->page->arg);
2382 PCUT_ASSERT_INT_EQUALS(2, list->page_idx);
2384 /* Move one page down again. */
2385 ui_list_page_down(list);
2387 /* Cursor should point to last and page to next-to-last entry */
2388 PCUT_ASSERT_STR_EQUALS("e", list->cursor->caption);
2389 PCUT_ASSERT_INT_EQUALS(5, (intptr_t)list->cursor->arg);
2390 PCUT_ASSERT_INT_EQUALS(4, list->cursor_idx);
2391 PCUT_ASSERT_STR_EQUALS("d", list->page->caption);
2392 PCUT_ASSERT_INT_EQUALS(4, (intptr_t)list->page->arg);
2393 PCUT_ASSERT_INT_EQUALS(3, list->page_idx);
2395 /* Moving further down should do nothing (we are at the bottom). */
2396 ui_list_page_down(list);
2398 /* Cursor should still point to last and page to next-to-last entry */
2399 PCUT_ASSERT_STR_EQUALS("e", list->cursor->caption);
2400 PCUT_ASSERT_INT_EQUALS(5, (intptr_t)list->cursor->arg);
2401 PCUT_ASSERT_INT_EQUALS(4, list->cursor_idx);
2402 PCUT_ASSERT_STR_EQUALS("d", list->page->caption);
2403 PCUT_ASSERT_INT_EQUALS(4, (intptr_t)list->page->arg);
2404 PCUT_ASSERT_INT_EQUALS(3, list->page_idx);
2406 ui_list_destroy(list);
2407 ui_window_destroy(window);
2408 ui_destroy(ui);
2411 /** ui_list_scroll_up() scrolls up by one row */
2412 PCUT_TEST(scroll_up)
2414 ui_t *ui;
2415 ui_window_t *window;
2416 ui_wnd_params_t params;
2417 ui_list_t *list;
2418 ui_list_entry_attr_t attr;
2419 gfx_rect_t rect;
2420 errno_t rc;
2422 rc = ui_create_disp(NULL, &ui);
2423 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2425 ui_wnd_params_init(&params);
2426 params.caption = "Test";
2428 rc = ui_window_create(ui, &params, &window);
2429 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2431 rc = ui_list_create(window, true, &list);
2432 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2434 rect.p0.x = 0;
2435 rect.p0.y = 0;
2436 rect.p1.x = 10;
2437 rect.p1.y = 38; /* Assuming this makes page size 2 */
2438 ui_list_set_rect(list, &rect);
2440 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2442 /* Add tree entries (more than page size, which is 2) */
2444 ui_list_entry_attr_init(&attr);
2446 attr.caption = "a";
2447 attr.arg = (void *)1;
2448 rc = ui_list_entry_append(list, &attr, NULL);
2449 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2451 attr.caption = "b";
2452 attr.arg = (void *)2;
2453 rc = ui_list_entry_append(list, &attr, NULL);
2454 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2456 attr.caption = "c";
2457 attr.arg = (void *)3;
2458 rc = ui_list_entry_append(list, &attr, NULL);
2459 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2461 /* Cursor to the last entry, page to the second */
2462 list->cursor = ui_list_last(list);
2463 list->cursor_idx = 2;
2464 list->page = ui_list_prev(list->cursor);
2465 list->page_idx = 1;
2467 /* Scroll one entry up */
2468 ui_list_scroll_up(list);
2470 /* Page should start on the first entry, cursor unchanged */
2471 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2472 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2473 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2474 PCUT_ASSERT_STR_EQUALS("a", list->page->caption);
2475 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->page->arg);
2476 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2478 /* Try scrolling one more entry up */
2479 ui_list_scroll_up(list);
2481 /* We were at the beginning, so nothing should have changed */
2482 PCUT_ASSERT_STR_EQUALS("c", list->cursor->caption);
2483 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->cursor->arg);
2484 PCUT_ASSERT_INT_EQUALS(2, list->cursor_idx);
2485 PCUT_ASSERT_STR_EQUALS("a", list->page->caption);
2486 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->page->arg);
2487 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2489 ui_list_destroy(list);
2490 ui_window_destroy(window);
2491 ui_destroy(ui);
2494 /** ui_list_scroll_down() scrolls down by one row */
2495 PCUT_TEST(scroll_down)
2497 ui_t *ui;
2498 ui_window_t *window;
2499 ui_wnd_params_t params;
2500 ui_list_t *list;
2501 ui_list_entry_attr_t attr;
2502 gfx_rect_t rect;
2503 errno_t rc;
2505 rc = ui_create_disp(NULL, &ui);
2506 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2508 ui_wnd_params_init(&params);
2509 params.caption = "Test";
2511 rc = ui_window_create(ui, &params, &window);
2512 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2514 rc = ui_list_create(window, true, &list);
2515 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2517 rect.p0.x = 0;
2518 rect.p0.y = 0;
2519 rect.p1.x = 10;
2520 rect.p1.y = 38; /* Assuming this makes page size 2 */
2521 ui_list_set_rect(list, &rect);
2523 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2525 /* Add tree entries (more than page size, which is 2) */
2527 ui_list_entry_attr_init(&attr);
2529 attr.caption = "a";
2530 attr.arg = (void *)1;
2531 rc = ui_list_entry_append(list, &attr, NULL);
2532 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2534 attr.caption = "b";
2535 attr.arg = (void *)2;
2536 rc = ui_list_entry_append(list, &attr, NULL);
2537 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2539 attr.caption = "c";
2540 attr.arg = (void *)3;
2541 rc = ui_list_entry_append(list, &attr, NULL);
2542 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2544 /* Cursor and page start to the first entry */
2545 list->cursor = ui_list_first(list);
2546 list->cursor_idx = 0;
2547 list->page = list->cursor;
2548 list->page_idx = 0;
2550 /* Scroll one entry down */
2551 ui_list_scroll_down(list);
2553 /* Page should start on the second entry, cursor unchanged */
2554 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2555 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2556 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2557 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2558 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2559 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2561 /* Try scrolling one more entry down */
2562 ui_list_scroll_down(list);
2564 /* We were at the end, so nothing should have changed */
2565 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2566 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2567 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2568 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2569 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2570 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2572 ui_list_destroy(list);
2573 ui_window_destroy(window);
2574 ui_destroy(ui);
2577 /** ui_list_scroll_page_up() scrolls up by one page */
2578 PCUT_TEST(scroll_page_up)
2580 ui_t *ui;
2581 ui_window_t *window;
2582 ui_wnd_params_t params;
2583 ui_list_t *list;
2584 ui_list_entry_attr_t attr;
2585 gfx_rect_t rect;
2586 errno_t rc;
2588 rc = ui_create_disp(NULL, &ui);
2589 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2591 ui_wnd_params_init(&params);
2592 params.caption = "Test";
2594 rc = ui_window_create(ui, &params, &window);
2595 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2597 rc = ui_list_create(window, true, &list);
2598 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2600 rect.p0.x = 0;
2601 rect.p0.y = 0;
2602 rect.p1.x = 10;
2603 rect.p1.y = 38; /* Assuming this makes page size 2 */
2604 ui_list_set_rect(list, &rect);
2606 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2608 /* Add five entries (more than twice the page size, which is 2) */
2610 ui_list_entry_attr_init(&attr);
2612 attr.caption = "a";
2613 attr.arg = (void *)1;
2614 rc = ui_list_entry_append(list, &attr, NULL);
2615 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2617 attr.caption = "b";
2618 attr.arg = (void *)2;
2619 rc = ui_list_entry_append(list, &attr, NULL);
2620 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2622 attr.caption = "c";
2623 attr.arg = (void *)3;
2624 rc = ui_list_entry_append(list, &attr, NULL);
2625 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2627 attr.caption = "d";
2628 attr.arg = (void *)4;
2629 rc = ui_list_entry_append(list, &attr, NULL);
2630 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2632 attr.caption = "e";
2633 attr.arg = (void *)5;
2634 rc = ui_list_entry_append(list, &attr, NULL);
2635 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2637 /* Cursor to the last entry, page to the second last */
2638 list->cursor = ui_list_last(list);
2639 list->cursor_idx = 4;
2640 list->page = ui_list_prev(list->cursor);
2641 list->page_idx = 3;
2643 /* Scroll one page up */
2644 ui_list_scroll_page_up(list);
2646 /* Page should start on 'b', cursor unchanged */
2647 PCUT_ASSERT_STR_EQUALS("e", list->cursor->caption);
2648 PCUT_ASSERT_INT_EQUALS(5, (intptr_t)list->cursor->arg);
2649 PCUT_ASSERT_INT_EQUALS(4, list->cursor_idx);
2650 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2651 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2652 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2654 /* Page up again */
2655 ui_list_scroll_page_up(list);
2657 /* Page should now be at the beginning, cursor unchanged */
2658 PCUT_ASSERT_STR_EQUALS("e", list->cursor->caption);
2659 PCUT_ASSERT_INT_EQUALS(5, (intptr_t)list->cursor->arg);
2660 PCUT_ASSERT_INT_EQUALS(4, list->cursor_idx);
2661 PCUT_ASSERT_STR_EQUALS("a", list->page->caption);
2662 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->page->arg);
2663 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2665 /* Page up again */
2666 ui_list_scroll_page_up(list);
2668 /* We were at the beginning, nothing should have changed */
2669 PCUT_ASSERT_STR_EQUALS("e", list->cursor->caption);
2670 PCUT_ASSERT_INT_EQUALS(5, (intptr_t)list->cursor->arg);
2671 PCUT_ASSERT_INT_EQUALS(4, list->cursor_idx);
2672 PCUT_ASSERT_STR_EQUALS("a", list->page->caption);
2673 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->page->arg);
2674 PCUT_ASSERT_INT_EQUALS(0, list->page_idx);
2676 ui_list_destroy(list);
2677 ui_window_destroy(window);
2678 ui_destroy(ui);
2681 /** ui_list_scroll_page_up() scrolls down by one page */
2682 PCUT_TEST(scroll_page_down)
2684 ui_t *ui;
2685 ui_window_t *window;
2686 ui_wnd_params_t params;
2687 ui_list_t *list;
2688 ui_list_entry_attr_t attr;
2689 gfx_rect_t rect;
2690 errno_t rc;
2692 rc = ui_create_disp(NULL, &ui);
2693 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2695 ui_wnd_params_init(&params);
2696 params.caption = "Test";
2698 rc = ui_window_create(ui, &params, &window);
2699 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2701 rc = ui_list_create(window, true, &list);
2702 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2704 rect.p0.x = 0;
2705 rect.p0.y = 0;
2706 rect.p1.x = 10;
2707 rect.p1.y = 38; /* Assuming this makes page size 2 */
2708 ui_list_set_rect(list, &rect);
2710 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2712 /* Add five entries (more than twice the page size, which is 2) */
2714 ui_list_entry_attr_init(&attr);
2716 attr.caption = "a";
2717 attr.arg = (void *)1;
2718 rc = ui_list_entry_append(list, &attr, NULL);
2719 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2721 attr.caption = "b";
2722 attr.arg = (void *)2;
2723 rc = ui_list_entry_append(list, &attr, NULL);
2724 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2726 attr.caption = "c";
2727 attr.arg = (void *)3;
2728 rc = ui_list_entry_append(list, &attr, NULL);
2729 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2731 attr.caption = "d";
2732 attr.arg = (void *)4;
2733 rc = ui_list_entry_append(list, &attr, NULL);
2734 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2736 attr.caption = "e";
2737 attr.arg = (void *)5;
2738 rc = ui_list_entry_append(list, &attr, NULL);
2739 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2741 /* Cursor and page to the first entry */
2742 list->cursor = ui_list_first(list);
2743 list->cursor_idx = 0;
2744 list->page = ui_list_first(list);
2745 list->page_idx = 0;
2747 /* Scroll one page down */
2748 ui_list_scroll_page_down(list);
2750 /* Page should start on 'c', cursor unchanged */
2751 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2752 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2753 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2754 PCUT_ASSERT_STR_EQUALS("c", list->page->caption);
2755 PCUT_ASSERT_INT_EQUALS(3, (intptr_t)list->page->arg);
2756 PCUT_ASSERT_INT_EQUALS(2, list->page_idx);
2758 /* Page down again */
2759 ui_list_scroll_page_down(list);
2761 /* Page should now start at 'd', cursor unchanged */
2762 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2763 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2764 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2765 PCUT_ASSERT_STR_EQUALS("d", list->page->caption);
2766 PCUT_ASSERT_INT_EQUALS(4, (intptr_t)list->page->arg);
2767 PCUT_ASSERT_INT_EQUALS(3, list->page_idx);
2769 /* Page down again */
2770 ui_list_scroll_page_down(list);
2772 /* We were at the end, nothing should have changed */
2773 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2774 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2775 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2776 PCUT_ASSERT_STR_EQUALS("d", list->page->caption);
2777 PCUT_ASSERT_INT_EQUALS(4, (intptr_t)list->page->arg);
2778 PCUT_ASSERT_INT_EQUALS(3, list->page_idx);
2780 ui_list_destroy(list);
2781 ui_window_destroy(window);
2782 ui_destroy(ui);
2785 /** ui_list_scroll_pos() scrolls to a particular entry */
2786 PCUT_TEST(scroll_pos)
2788 ui_t *ui;
2789 ui_window_t *window;
2790 ui_wnd_params_t params;
2791 ui_list_t *list;
2792 ui_list_entry_attr_t attr;
2793 gfx_rect_t rect;
2794 errno_t rc;
2796 rc = ui_create_disp(NULL, &ui);
2797 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2799 ui_wnd_params_init(&params);
2800 params.caption = "Test";
2802 rc = ui_window_create(ui, &params, &window);
2803 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2805 rc = ui_list_create(window, true, &list);
2806 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2808 rect.p0.x = 0;
2809 rect.p0.y = 0;
2810 rect.p1.x = 10;
2811 rect.p1.y = 38; /* Assuming this makes page size 2 */
2812 ui_list_set_rect(list, &rect);
2814 PCUT_ASSERT_INT_EQUALS(2, ui_list_page_size(list));
2816 /* Add five entries (more than twice the page size, which is 2) */
2818 ui_list_entry_attr_init(&attr);
2820 attr.caption = "a";
2821 attr.arg = (void *)1;
2822 rc = ui_list_entry_append(list, &attr, NULL);
2823 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2825 attr.caption = "b";
2826 attr.arg = (void *)2;
2827 rc = ui_list_entry_append(list, &attr, NULL);
2828 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2830 attr.caption = "c";
2831 attr.arg = (void *)3;
2832 rc = ui_list_entry_append(list, &attr, NULL);
2833 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2835 attr.caption = "d";
2836 attr.arg = (void *)4;
2837 rc = ui_list_entry_append(list, &attr, NULL);
2838 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2840 attr.caption = "e";
2841 attr.arg = (void *)5;
2842 rc = ui_list_entry_append(list, &attr, NULL);
2843 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2845 /* Cursor and page to the first entry */
2846 list->cursor = ui_list_first(list);
2847 list->cursor_idx = 0;
2848 list->page = ui_list_first(list);
2849 list->page_idx = 0;
2851 /* Scroll to entry 1 (one down) */
2852 ui_list_scroll_pos(list, 1);
2854 /* Page should start on 'b', cursor unchanged */
2855 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2856 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2857 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2858 PCUT_ASSERT_STR_EQUALS("b", list->page->caption);
2859 PCUT_ASSERT_INT_EQUALS(2, (intptr_t)list->page->arg);
2860 PCUT_ASSERT_INT_EQUALS(1, list->page_idx);
2862 /* Scroll to entry 3 (i.e. the end) */
2863 ui_list_scroll_pos(list, 3);
2865 /* Page should now start at 'd', cursor unchanged */
2866 PCUT_ASSERT_STR_EQUALS("a", list->cursor->caption);
2867 PCUT_ASSERT_INT_EQUALS(1, (intptr_t)list->cursor->arg);
2868 PCUT_ASSERT_INT_EQUALS(0, list->cursor_idx);
2869 PCUT_ASSERT_STR_EQUALS("d", list->page->caption);
2870 PCUT_ASSERT_INT_EQUALS(4, (intptr_t)list->page->arg);
2871 PCUT_ASSERT_INT_EQUALS(3, list->page_idx);
2873 ui_list_destroy(list);
2874 ui_window_destroy(window);
2875 ui_destroy(ui);
2878 /** ui_list_activate_req() sends activation request */
2879 PCUT_TEST(activate_req)
2881 ui_t *ui;
2882 ui_window_t *window;
2883 ui_wnd_params_t params;
2884 ui_list_t *list;
2885 errno_t rc;
2886 test_resp_t resp;
2888 rc = ui_create_disp(NULL, &ui);
2889 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2891 ui_wnd_params_init(&params);
2892 params.caption = "Test";
2894 rc = ui_window_create(ui, &params, &window);
2895 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2897 rc = ui_list_create(window, true, &list);
2898 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2900 ui_list_set_cb(list, &test_cb, &resp);
2902 resp.activate_req = false;
2903 resp.activate_req_list = NULL;
2905 ui_list_activate_req(list);
2906 PCUT_ASSERT_TRUE(resp.activate_req);
2907 PCUT_ASSERT_EQUALS(list, resp.activate_req_list);
2909 ui_list_destroy(list);
2910 ui_window_destroy(window);
2911 ui_destroy(ui);
2914 /** ui_list_selected() runs selected callback */
2915 PCUT_TEST(selected)
2917 ui_t *ui;
2918 ui_window_t *window;
2919 ui_wnd_params_t params;
2920 ui_list_t *list;
2921 ui_list_entry_attr_t attr;
2922 ui_list_entry_t *entry;
2923 errno_t rc;
2924 test_resp_t resp;
2926 rc = ui_create_disp(NULL, &ui);
2927 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2929 ui_wnd_params_init(&params);
2930 params.caption = "Test";
2932 rc = ui_window_create(ui, &params, &window);
2933 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2935 rc = ui_list_create(window, true, &list);
2936 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2938 ui_list_set_cb(list, &test_cb, &resp);
2940 ui_list_entry_attr_init(&attr);
2941 attr.caption = "Hello";
2942 attr.arg = &resp;
2944 rc = ui_list_entry_append(list, &attr, &entry);
2945 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2947 resp.selected = false;
2948 resp.selected_entry = NULL;
2950 ui_list_selected(entry);
2951 PCUT_ASSERT_TRUE(resp.selected);
2952 PCUT_ASSERT_EQUALS(entry, resp.selected_entry);
2954 ui_list_destroy(list);
2955 ui_window_destroy(window);
2956 ui_destroy(ui);
2959 /** ui_list_entry_ptr_cmp compares two indirectly referenced entries */
2960 PCUT_TEST(entry_ptr_cmp)
2962 ui_t *ui;
2963 ui_window_t *window;
2964 ui_wnd_params_t params;
2965 ui_list_t *list;
2966 ui_list_entry_t *a, *b;
2967 ui_list_entry_attr_t attr;
2968 test_resp_t resp;
2969 int rel;
2970 errno_t rc;
2972 rc = ui_create_disp(NULL, &ui);
2973 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2975 ui_wnd_params_init(&params);
2976 params.caption = "Test";
2978 rc = ui_window_create(ui, &params, &window);
2979 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2981 rc = ui_list_create(window, true, &list);
2982 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2984 ui_list_set_cb(list, &test_cb, &resp);
2986 ui_list_entry_attr_init(&attr);
2988 attr.caption = "a";
2989 attr.arg = (void *)2;
2990 rc = ui_list_entry_append(list, &attr, NULL);
2991 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2993 attr.caption = "b";
2994 attr.arg = (void *)1;
2995 rc = ui_list_entry_append(list, &attr, NULL);
2996 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
2998 a = ui_list_first(list);
2999 PCUT_ASSERT_NOT_NULL(a);
3000 b = ui_list_next(a);
3001 PCUT_ASSERT_NOT_NULL(b);
3003 /* a < b */
3004 rel = ui_list_entry_ptr_cmp(&a, &b);
3005 PCUT_ASSERT_TRUE(rel < 0);
3007 /* b > a */
3008 rel = ui_list_entry_ptr_cmp(&b, &a);
3009 PCUT_ASSERT_TRUE(rel > 0);
3011 /* a == a */
3012 rel = ui_list_entry_ptr_cmp(&a, &a);
3013 PCUT_ASSERT_INT_EQUALS(0, rel);
3015 ui_list_destroy(list);
3016 ui_window_destroy(window);
3017 ui_destroy(ui);
3020 /** ui_list_entry_get_idx() returns entry index */
3021 PCUT_TEST(entry_get_idx)
3023 ui_t *ui;
3024 ui_window_t *window;
3025 ui_wnd_params_t params;
3026 ui_list_t *list;
3027 ui_list_entry_t *a, *b;
3028 ui_list_entry_attr_t attr;
3029 test_resp_t resp;
3030 errno_t rc;
3032 rc = ui_create_disp(NULL, &ui);
3033 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
3035 ui_wnd_params_init(&params);
3036 params.caption = "Test";
3038 rc = ui_window_create(ui, &params, &window);
3039 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
3041 rc = ui_list_create(window, true, &list);
3042 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
3044 ui_list_set_cb(list, &test_cb, &resp);
3046 ui_list_entry_attr_init(&attr);
3048 attr.caption = "a";
3049 attr.arg = (void *)2;
3050 rc = ui_list_entry_append(list, &attr, &a);
3051 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
3053 attr.caption = "b";
3054 attr.arg = (void *)1;
3055 rc = ui_list_entry_append(list, &attr, &b);
3056 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
3058 PCUT_ASSERT_INT_EQUALS(0, ui_list_entry_get_idx(a));
3059 PCUT_ASSERT_INT_EQUALS(1, ui_list_entry_get_idx(b));
3061 ui_list_destroy(list);
3062 ui_window_destroy(window);
3063 ui_destroy(ui);
3066 static void test_list_activate_req(ui_list_t *list, void *arg)
3068 test_resp_t *resp = (test_resp_t *)arg;
3070 resp->activate_req = true;
3071 resp->activate_req_list = list;
3074 static void test_list_selected(ui_list_entry_t *entry, void *arg)
3076 test_resp_t *resp = (test_resp_t *)arg;
3078 resp->selected = true;
3079 resp->selected_entry = entry;
3082 static int test_list_compare(ui_list_entry_t *a, ui_list_entry_t *b)
3084 return str_cmp(a->caption, b->caption);
3087 PCUT_EXPORT(list);