2 * Copyright (c) 2023 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>
35 #include <ui/filelist.h>
36 #include <ui/scrollbar.h>
38 #include "../private/filelist.h"
39 #include "../private/list.h"
43 PCUT_TEST_SUITE(file_list
);
48 ui_file_list_t
*activate_req_file_list
;
51 ui_file_list_t
*selected_file_list
;
52 const char *selected_fname
;
55 static void test_file_list_activate_req(ui_file_list_t
*, void *);
56 static void test_file_list_selected(ui_file_list_t
*, void *, const char *);
58 static ui_file_list_cb_t test_cb
= {
59 .activate_req
= test_file_list_activate_req
,
60 .selected
= test_file_list_selected
63 /** Create and destroy file list. */
64 PCUT_TEST(create_destroy
)
68 ui_wnd_params_t params
;
69 ui_file_list_t
*flist
;
72 rc
= ui_create_disp(NULL
, &ui
);
73 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
75 ui_wnd_params_init(¶ms
);
76 params
.caption
= "Test";
78 rc
= ui_window_create(ui
, ¶ms
, &window
);
79 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
81 rc
= ui_file_list_create(window
, true, &flist
);
82 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
84 ui_file_list_destroy(flist
);
85 ui_window_destroy(window
);
89 /** ui_file_list_set_cb() sets callback */
94 ui_wnd_params_t params
;
95 ui_file_list_t
*flist
;
99 rc
= ui_create_disp(NULL
, &ui
);
100 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
102 ui_wnd_params_init(¶ms
);
103 params
.caption
= "Test";
105 rc
= ui_window_create(ui
, ¶ms
, &window
);
106 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
108 rc
= ui_file_list_create(window
, true, &flist
);
109 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
111 ui_file_list_set_cb(flist
, &test_cb
, &resp
);
112 PCUT_ASSERT_EQUALS(&test_cb
, flist
->cb
);
113 PCUT_ASSERT_EQUALS(&resp
, flist
->cb_arg
);
115 ui_file_list_destroy(flist
);
116 ui_window_destroy(window
);
120 /** Test ui_file_list_paint() */
125 ui_wnd_params_t params
;
126 ui_file_list_t
*flist
;
129 rc
= ui_create_disp(NULL
, &ui
);
130 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
132 ui_wnd_params_init(¶ms
);
133 params
.caption
= "Test";
135 rc
= ui_window_create(ui
, ¶ms
, &window
);
136 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
138 rc
= ui_file_list_create(window
, true, &flist
);
139 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
141 rc
= ui_file_list_paint(flist
);
142 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
144 ui_file_list_destroy(flist
);
145 ui_window_destroy(window
);
149 /** ui_file_list_ctl() returns a valid UI control */
154 ui_wnd_params_t params
;
155 ui_file_list_t
*flist
;
156 ui_control_t
*control
;
159 rc
= ui_create_disp(NULL
, &ui
);
160 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
162 ui_wnd_params_init(¶ms
);
163 params
.caption
= "Test";
165 rc
= ui_window_create(ui
, ¶ms
, &window
);
166 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
168 rc
= ui_file_list_create(window
, true, &flist
);
169 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
171 control
= ui_file_list_ctl(flist
);
172 PCUT_ASSERT_NOT_NULL(control
);
174 ui_file_list_destroy(flist
);
175 ui_window_destroy(window
);
179 /** ui_file_list_set_rect() sets internal field */
184 ui_wnd_params_t params
;
185 ui_file_list_t
*flist
;
189 rc
= ui_create_disp(NULL
, &ui
);
190 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
192 ui_wnd_params_init(¶ms
);
193 params
.caption
= "Test";
195 rc
= ui_window_create(ui
, ¶ms
, &window
);
196 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
198 rc
= ui_file_list_create(window
, true, &flist
);
199 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
206 ui_file_list_set_rect(flist
, &rect
);
207 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, flist
->list
->rect
.p0
.x
);
208 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, flist
->list
->rect
.p0
.y
);
209 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, flist
->list
->rect
.p1
.x
);
210 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, flist
->list
->rect
.p1
.y
);
212 ui_file_list_destroy(flist
);
213 ui_window_destroy(window
);
217 /** ui_file_list_is_active() returns file list activity state */
222 ui_wnd_params_t params
;
223 ui_file_list_t
*flist
;
226 rc
= ui_create_disp(NULL
, &ui
);
227 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
229 ui_wnd_params_init(¶ms
);
230 params
.caption
= "Test";
232 rc
= ui_window_create(ui
, ¶ms
, &window
);
233 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
235 rc
= ui_file_list_create(window
, true, &flist
);
236 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
237 PCUT_ASSERT_TRUE(ui_file_list_is_active(flist
));
238 ui_file_list_destroy(flist
);
240 rc
= ui_file_list_create(window
, false, &flist
);
241 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
242 PCUT_ASSERT_FALSE(ui_file_list_is_active(flist
));
243 ui_file_list_destroy(flist
);
244 ui_window_destroy(window
);
248 /** ui_file_list_activate() activates file list */
253 ui_wnd_params_t params
;
254 ui_file_list_t
*flist
;
257 rc
= ui_create_disp(NULL
, &ui
);
258 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
260 ui_wnd_params_init(¶ms
);
261 params
.caption
= "Test";
263 rc
= ui_window_create(ui
, ¶ms
, &window
);
264 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
266 rc
= ui_file_list_create(window
, false, &flist
);
267 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
269 PCUT_ASSERT_FALSE(ui_file_list_is_active(flist
));
270 rc
= ui_file_list_activate(flist
);
271 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
272 PCUT_ASSERT_TRUE(ui_file_list_is_active(flist
));
274 ui_file_list_destroy(flist
);
275 ui_window_destroy(window
);
279 /** ui_file_list_deactivate() deactivates file list */
280 PCUT_TEST(deactivate
)
284 ui_wnd_params_t params
;
285 ui_file_list_t
*flist
;
288 rc
= ui_create_disp(NULL
, &ui
);
289 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
291 ui_wnd_params_init(¶ms
);
292 params
.caption
= "Test";
294 rc
= ui_window_create(ui
, ¶ms
, &window
);
295 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
297 rc
= ui_file_list_create(window
, true, &flist
);
298 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
300 PCUT_ASSERT_TRUE(ui_file_list_is_active(flist
));
301 ui_file_list_deactivate(flist
);
302 PCUT_ASSERT_FALSE(ui_file_list_is_active(flist
));
304 ui_file_list_destroy(flist
);
305 ui_window_destroy(window
);
309 /** ui_file_list_entry_append() appends new entry */
310 PCUT_TEST(entry_append
)
314 ui_wnd_params_t params
;
315 ui_file_list_t
*flist
;
316 ui_file_list_entry_attr_t attr
;
319 rc
= ui_create_disp(NULL
, &ui
);
320 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
322 ui_wnd_params_init(¶ms
);
323 params
.caption
= "Test";
325 rc
= ui_window_create(ui
, ¶ms
, &window
);
326 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
328 rc
= ui_file_list_create(window
, true, &flist
);
329 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
331 ui_file_list_entry_attr_init(&attr
);
335 rc
= ui_file_list_entry_append(flist
, &attr
);
336 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
338 PCUT_ASSERT_INT_EQUALS(1, ui_list_entries_cnt(flist
->list
));
342 rc
= ui_file_list_entry_append(flist
, &attr
);
343 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
345 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(flist
->list
));
347 ui_file_list_destroy(flist
);
348 ui_window_destroy(window
);
352 /** ui_file_list_entry_destroy() destroys entry */
353 PCUT_TEST(entry_destroy
)
357 ui_wnd_params_t params
;
358 ui_file_list_t
*flist
;
359 ui_file_list_entry_t
*entry
;
360 ui_file_list_entry_attr_t attr
;
363 rc
= ui_create_disp(NULL
, &ui
);
364 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
366 ui_wnd_params_init(¶ms
);
367 params
.caption
= "Test";
369 rc
= ui_window_create(ui
, ¶ms
, &window
);
370 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
372 rc
= ui_file_list_create(window
, true, &flist
);
373 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
377 rc
= ui_file_list_entry_append(flist
, &attr
);
378 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
382 rc
= ui_file_list_entry_append(flist
, &attr
);
383 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
385 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(flist
->list
));
387 entry
= ui_file_list_first(flist
);
388 ui_file_list_entry_destroy(entry
);
390 PCUT_ASSERT_INT_EQUALS(1, ui_list_entries_cnt(flist
->list
));
392 entry
= ui_file_list_first(flist
);
393 ui_file_list_entry_destroy(entry
);
395 PCUT_ASSERT_INT_EQUALS(0, ui_list_entries_cnt(flist
->list
));
397 ui_file_list_destroy(flist
);
398 ui_window_destroy(window
);
402 /** ui_file_list_clear_entries() removes all entries from file list */
403 PCUT_TEST(clear_entries
)
407 ui_wnd_params_t params
;
408 ui_file_list_t
*flist
;
409 ui_file_list_entry_attr_t attr
;
412 rc
= ui_create_disp(NULL
, &ui
);
413 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
415 ui_wnd_params_init(¶ms
);
416 params
.caption
= "Test";
418 rc
= ui_window_create(ui
, ¶ms
, &window
);
419 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
421 rc
= ui_file_list_create(window
, true, &flist
);
422 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
424 ui_file_list_entry_attr_init(&attr
);
427 rc
= ui_file_list_entry_append(flist
, &attr
);
428 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
432 rc
= ui_file_list_entry_append(flist
, &attr
);
433 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
435 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(flist
->list
));
437 ui_file_list_clear_entries(flist
);
438 PCUT_ASSERT_INT_EQUALS(0, ui_list_entries_cnt(flist
->list
));
440 ui_file_list_destroy(flist
);
441 ui_window_destroy(window
);
445 /** ui_file_list_read_dir() reads the contents of a directory */
450 ui_wnd_params_t params
;
451 ui_file_list_t
*flist
;
452 ui_file_list_entry_t
*entry
;
460 /* Create name for temporary directory */
462 PCUT_ASSERT_NOT_NULL(p
);
464 /* Create temporary directory */
465 rc
= vfs_link_path(p
, KIND_DIRECTORY
, NULL
);
466 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
468 rv
= asprintf(&fname
, "%s/%s", p
, "a");
469 PCUT_ASSERT_TRUE(rv
>= 0);
471 f
= fopen(fname
, "wb");
472 PCUT_ASSERT_NOT_NULL(f
);
474 rv
= fprintf(f
, "X");
475 PCUT_ASSERT_TRUE(rv
>= 0);
478 PCUT_ASSERT_INT_EQUALS(0, rv
);
480 rc
= ui_create_disp(NULL
, &ui
);
481 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
483 ui_wnd_params_init(¶ms
);
484 params
.caption
= "Test";
486 rc
= ui_window_create(ui
, ¶ms
, &window
);
487 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
489 rc
= ui_file_list_create(window
, true, &flist
);
490 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
492 rc
= ui_file_list_read_dir(flist
, p
);
493 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
495 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(flist
->list
));
497 entry
= ui_file_list_first(flist
);
498 PCUT_ASSERT_NOT_NULL(entry
);
499 PCUT_ASSERT_STR_EQUALS("..", entry
->name
);
501 entry
= ui_file_list_next(entry
);
502 PCUT_ASSERT_NOT_NULL(entry
);
503 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
504 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
506 ui_file_list_destroy(flist
);
509 PCUT_ASSERT_INT_EQUALS(0, rv
);
512 PCUT_ASSERT_INT_EQUALS(0, rv
);
515 ui_window_destroy(window
);
519 /** When moving to parent directory from a subdir, we seek to the
522 PCUT_TEST(read_dir_up
)
526 ui_wnd_params_t params
;
527 ui_file_list_t
*flist
;
536 /* Create name for temporary directory */
538 PCUT_ASSERT_NOT_NULL(p
);
540 /* Create temporary directory */
541 rc
= vfs_link_path(p
, KIND_DIRECTORY
, NULL
);
542 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
544 /* Create some subdirectories */
546 rv
= asprintf(&subdir_a
, "%s/%s", p
, "a");
547 PCUT_ASSERT_TRUE(rv
>= 0);
548 rc
= vfs_link_path(subdir_a
, KIND_DIRECTORY
, NULL
);
549 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
551 rv
= asprintf(&subdir_b
, "%s/%s", p
, "b");
552 PCUT_ASSERT_TRUE(rv
>= 0);
553 rc
= vfs_link_path(subdir_b
, KIND_DIRECTORY
, NULL
);
554 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
556 rv
= asprintf(&subdir_c
, "%s/%s", p
, "c");
557 PCUT_ASSERT_TRUE(rv
>= 0);
558 rc
= vfs_link_path(subdir_c
, KIND_DIRECTORY
, NULL
);
559 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
561 rc
= ui_create_disp(NULL
, &ui
);
562 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
564 ui_wnd_params_init(¶ms
);
565 params
.caption
= "Test";
567 rc
= ui_window_create(ui
, ¶ms
, &window
);
568 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
570 rc
= ui_file_list_create(window
, true, &flist
);
571 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
573 /* Start in subdirectory "b" */
574 rc
= ui_file_list_read_dir(flist
, subdir_b
);
575 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
577 /* Now go up (into p) */
579 rc
= ui_file_list_read_dir(flist
, "..");
580 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
582 PCUT_ASSERT_NOT_NULL(ui_file_list_get_cursor(flist
));
583 PCUT_ASSERT_STR_EQUALS("b", ui_file_list_get_cursor(flist
)->name
);
585 ui_file_list_destroy(flist
);
587 rv
= remove(subdir_a
);
588 PCUT_ASSERT_INT_EQUALS(0, rv
);
590 rv
= remove(subdir_b
);
591 PCUT_ASSERT_INT_EQUALS(0, rv
);
593 rv
= remove(subdir_c
);
594 PCUT_ASSERT_INT_EQUALS(0, rv
);
597 PCUT_ASSERT_INT_EQUALS(0, rv
);
603 ui_window_destroy(window
);
607 /** ui_file_list_sort() sorts file list entries */
612 ui_wnd_params_t params
;
613 ui_file_list_t
*flist
;
614 ui_file_list_entry_t
*entry
;
615 ui_file_list_entry_attr_t attr
;
618 rc
= ui_create_disp(NULL
, &ui
);
619 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
621 ui_wnd_params_init(¶ms
);
622 params
.caption
= "Test";
624 rc
= ui_window_create(ui
, ¶ms
, &window
);
625 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
627 rc
= ui_file_list_create(window
, true, &flist
);
628 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
630 ui_file_list_entry_attr_init(&attr
);
634 rc
= ui_file_list_entry_append(flist
, &attr
);
635 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
639 rc
= ui_file_list_entry_append(flist
, &attr
);
640 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
644 rc
= ui_file_list_entry_append(flist
, &attr
);
645 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
647 rc
= ui_file_list_sort(flist
);
648 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
650 entry
= ui_file_list_first(flist
);
651 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
652 PCUT_ASSERT_INT_EQUALS(2, entry
->size
);
654 entry
= ui_file_list_next(entry
);
655 PCUT_ASSERT_STR_EQUALS("b", entry
->name
);
656 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
658 entry
= ui_file_list_next(entry
);
659 PCUT_ASSERT_STR_EQUALS("c", entry
->name
);
660 PCUT_ASSERT_INT_EQUALS(3, entry
->size
);
662 ui_file_list_destroy(flist
);
663 ui_window_destroy(window
);
667 /** ui_file_list_list_compare compares two file list entries */
668 PCUT_TEST(list_compare
)
672 ui_wnd_params_t params
;
673 ui_file_list_t
*flist
;
674 ui_file_list_entry_t
*a
, *b
;
675 ui_file_list_entry_attr_t attr
;
679 rc
= ui_create_disp(NULL
, &ui
);
680 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
682 ui_wnd_params_init(¶ms
);
683 params
.caption
= "Test";
685 rc
= ui_window_create(ui
, ¶ms
, &window
);
686 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
688 rc
= ui_file_list_create(window
, true, &flist
);
689 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
691 ui_file_list_entry_attr_init(&attr
);
695 rc
= ui_file_list_entry_append(flist
, &attr
);
696 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
700 rc
= ui_file_list_entry_append(flist
, &attr
);
701 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
703 a
= ui_file_list_first(flist
);
704 PCUT_ASSERT_NOT_NULL(a
);
705 b
= ui_file_list_next(a
);
706 PCUT_ASSERT_NOT_NULL(b
);
709 rel
= ui_file_list_list_compare(a
->entry
, b
->entry
);
710 PCUT_ASSERT_TRUE(rel
< 0);
713 rel
= ui_file_list_list_compare(b
->entry
, a
->entry
);
714 PCUT_ASSERT_TRUE(rel
> 0);
717 rel
= ui_file_list_list_compare(a
->entry
, a
->entry
);
718 PCUT_ASSERT_INT_EQUALS(0, rel
);
720 ui_file_list_destroy(flist
);
721 ui_window_destroy(window
);
725 /** ui_file_list_entry_attr_init() initializes file list attribute structure */
726 PCUT_TEST(entry_attr_init
)
728 ui_file_list_entry_attr_t attr
;
730 ui_file_list_entry_attr_init(&attr
);
731 PCUT_ASSERT_NULL(attr
.name
);
732 PCUT_ASSERT_INT_EQUALS(0, attr
.size
);
733 PCUT_ASSERT_EQUALS(false, attr
.isdir
);
734 PCUT_ASSERT_INT_EQUALS(0, attr
.svc
);
737 /** ui_file_list_first() returns valid entry or @c NULL as appropriate */
742 ui_wnd_params_t params
;
743 ui_file_list_t
*flist
;
744 ui_file_list_entry_t
*entry
;
745 ui_file_list_entry_attr_t attr
;
748 rc
= ui_create_disp(NULL
, &ui
);
749 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
751 ui_wnd_params_init(¶ms
);
752 params
.caption
= "Test";
754 rc
= ui_window_create(ui
, ¶ms
, &window
);
755 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
757 rc
= ui_file_list_create(window
, true, &flist
);
758 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
760 ui_file_list_entry_attr_init(&attr
);
762 entry
= ui_file_list_first(flist
);
763 PCUT_ASSERT_NULL(entry
);
768 rc
= ui_file_list_entry_append(flist
, &attr
);
769 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
771 /* Now try getting it */
772 entry
= ui_file_list_first(flist
);
773 PCUT_ASSERT_NOT_NULL(entry
);
774 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
775 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
777 /* Add another entry */
780 rc
= ui_file_list_entry_append(flist
, &attr
);
781 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
783 /* We should still get the first entry */
784 entry
= ui_file_list_first(flist
);
785 PCUT_ASSERT_NOT_NULL(entry
);
786 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
787 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
789 ui_file_list_destroy(flist
);
790 ui_window_destroy(window
);
794 /** ui_file_list_last() returns valid entry or @c NULL as appropriate */
799 ui_wnd_params_t params
;
800 ui_file_list_t
*flist
;
801 ui_file_list_entry_t
*entry
;
802 ui_file_list_entry_attr_t attr
;
805 rc
= ui_create_disp(NULL
, &ui
);
806 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
808 ui_wnd_params_init(¶ms
);
809 params
.caption
= "Test";
811 rc
= ui_window_create(ui
, ¶ms
, &window
);
812 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
814 rc
= ui_file_list_create(window
, true, &flist
);
815 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
817 ui_file_list_entry_attr_init(&attr
);
819 entry
= ui_file_list_last(flist
);
820 PCUT_ASSERT_NULL(entry
);
825 rc
= ui_file_list_entry_append(flist
, &attr
);
826 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
828 /* Now try getting it */
829 entry
= ui_file_list_last(flist
);
830 PCUT_ASSERT_NOT_NULL(entry
);
831 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
832 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
834 /* Add another entry */
837 rc
= ui_file_list_entry_append(flist
, &attr
);
838 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
840 /* We should get new entry now */
841 entry
= ui_file_list_last(flist
);
842 PCUT_ASSERT_NOT_NULL(entry
);
845 PCUT_ASSERT_STR_EQUALS("b", entry
->name
);
846 PCUT_ASSERT_INT_EQUALS(2, entry
->size
);
848 ui_file_list_destroy(flist
);
849 ui_window_destroy(window
);
853 /** ui_file_list_next() returns the next entry or @c NULL as appropriate */
858 ui_wnd_params_t params
;
859 ui_file_list_t
*flist
;
860 ui_file_list_entry_t
*entry
;
861 ui_file_list_entry_attr_t attr
;
864 rc
= ui_create_disp(NULL
, &ui
);
865 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
867 ui_wnd_params_init(¶ms
);
868 params
.caption
= "Test";
870 rc
= ui_window_create(ui
, ¶ms
, &window
);
871 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
873 rc
= ui_file_list_create(window
, true, &flist
);
874 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
876 ui_file_list_entry_attr_init(&attr
);
881 rc
= ui_file_list_entry_append(flist
, &attr
);
882 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
884 /* Now try getting its successor */
885 entry
= ui_file_list_first(flist
);
886 PCUT_ASSERT_NOT_NULL(entry
);
888 entry
= ui_file_list_next(entry
);
889 PCUT_ASSERT_NULL(entry
);
891 /* Add another entry */
894 rc
= ui_file_list_entry_append(flist
, &attr
);
895 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
897 /* Try getting the successor of first entry again */
898 entry
= ui_file_list_first(flist
);
899 PCUT_ASSERT_NOT_NULL(entry
);
901 entry
= ui_file_list_next(entry
);
902 PCUT_ASSERT_NOT_NULL(entry
);
903 PCUT_ASSERT_STR_EQUALS("b", entry
->name
);
904 PCUT_ASSERT_INT_EQUALS(2, entry
->size
);
906 ui_file_list_destroy(flist
);
907 ui_window_destroy(window
);
911 /** ui_file_list_prev() returns the previous entry or @c NULL as appropriate */
916 ui_wnd_params_t params
;
917 ui_file_list_t
*flist
;
918 ui_file_list_entry_t
*entry
;
919 ui_file_list_entry_attr_t attr
;
922 rc
= ui_create_disp(NULL
, &ui
);
923 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
925 ui_wnd_params_init(¶ms
);
926 params
.caption
= "Test";
928 rc
= ui_window_create(ui
, ¶ms
, &window
);
929 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
931 rc
= ui_file_list_create(window
, true, &flist
);
932 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
934 ui_file_list_entry_attr_init(&attr
);
939 rc
= ui_file_list_entry_append(flist
, &attr
);
940 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
942 /* Now try getting its predecessor */
943 entry
= ui_file_list_last(flist
);
944 PCUT_ASSERT_NOT_NULL(entry
);
946 entry
= ui_file_list_prev(entry
);
947 PCUT_ASSERT_NULL(entry
);
949 /* Add another entry */
952 rc
= ui_file_list_entry_append(flist
, &attr
);
953 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
955 /* Try getting the predecessor of the new entry */
956 entry
= ui_file_list_last(flist
);
957 PCUT_ASSERT_NOT_NULL(entry
);
959 entry
= ui_file_list_prev(entry
);
960 PCUT_ASSERT_NOT_NULL(entry
);
961 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
962 PCUT_ASSERT_INT_EQUALS(1, entry
->size
);
964 ui_file_list_destroy(flist
);
965 ui_window_destroy(window
);
969 /** ui_file_list_open_dir() opens a directory entry */
974 ui_wnd_params_t params
;
975 ui_file_list_t
*flist
;
976 ui_file_list_entry_t
*entry
;
983 rc
= ui_create_disp(NULL
, &ui
);
984 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
986 ui_wnd_params_init(¶ms
);
987 params
.caption
= "Test";
989 rc
= ui_window_create(ui
, ¶ms
, &window
);
990 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
992 /* Create name for temporary directory */
994 PCUT_ASSERT_NOT_NULL(p
);
996 /* Create temporary directory */
997 rc
= vfs_link_path(p
, KIND_DIRECTORY
, NULL
);
998 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1000 rv
= asprintf(&sdname
, "%s/%s", p
, "a");
1001 PCUT_ASSERT_TRUE(rv
>= 0);
1003 /* Create sub-directory */
1004 rc
= vfs_link_path(sdname
, KIND_DIRECTORY
, NULL
);
1005 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1007 rc
= ui_file_list_create(window
, true, &flist
);
1008 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1010 rc
= ui_file_list_read_dir(flist
, p
);
1011 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1012 PCUT_ASSERT_STR_EQUALS(p
, flist
->dir
);
1014 PCUT_ASSERT_INT_EQUALS(2, ui_list_entries_cnt(flist
->list
));
1016 entry
= ui_file_list_first(flist
);
1017 PCUT_ASSERT_NOT_NULL(entry
);
1018 PCUT_ASSERT_STR_EQUALS("..", entry
->name
);
1020 entry
= ui_file_list_next(entry
);
1021 PCUT_ASSERT_NOT_NULL(entry
);
1022 PCUT_ASSERT_STR_EQUALS("a", entry
->name
);
1023 PCUT_ASSERT_TRUE(entry
->isdir
);
1025 rc
= ui_file_list_open_dir(flist
, entry
);
1026 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1028 PCUT_ASSERT_STR_EQUALS(sdname
, flist
->dir
);
1030 ui_file_list_destroy(flist
);
1031 ui_window_destroy(window
);
1034 rv
= remove(sdname
);
1035 PCUT_ASSERT_INT_EQUALS(0, rv
);
1038 PCUT_ASSERT_INT_EQUALS(0, rv
);
1043 /** ui_file_list_open_file() runs selected callback */
1044 PCUT_TEST(open_file
)
1047 ui_window_t
*window
;
1048 ui_wnd_params_t params
;
1049 ui_file_list_t
*flist
;
1050 ui_file_list_entry_attr_t attr
;
1054 rc
= ui_create_disp(NULL
, &ui
);
1055 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1057 ui_wnd_params_init(¶ms
);
1058 params
.caption
= "Test";
1060 rc
= ui_window_create(ui
, ¶ms
, &window
);
1061 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1063 rc
= ui_file_list_create(window
, true, &flist
);
1064 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1066 ui_file_list_set_cb(flist
, &test_cb
, &resp
);
1068 attr
.name
= "hello.txt";
1070 rc
= ui_file_list_entry_append(flist
, &attr
);
1071 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1073 resp
.selected
= false;
1074 resp
.selected_file_list
= NULL
;
1075 resp
.selected_fname
= NULL
;
1077 ui_file_list_open_file(flist
, ui_file_list_first(flist
));
1078 PCUT_ASSERT_TRUE(resp
.selected
);
1079 PCUT_ASSERT_EQUALS(flist
, resp
.selected_file_list
);
1080 PCUT_ASSERT_STR_EQUALS("hello.txt", resp
.selected_fname
);
1082 ui_file_list_destroy(flist
);
1083 ui_window_destroy(window
);
1087 /** ui_file_list_activate_req() sends activation request */
1088 PCUT_TEST(activate_req
)
1091 ui_window_t
*window
;
1092 ui_wnd_params_t params
;
1093 ui_file_list_t
*flist
;
1097 rc
= ui_create_disp(NULL
, &ui
);
1098 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1100 ui_wnd_params_init(¶ms
);
1101 params
.caption
= "Test";
1103 rc
= ui_window_create(ui
, ¶ms
, &window
);
1104 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1106 rc
= ui_file_list_create(window
, true, &flist
);
1107 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1109 ui_file_list_set_cb(flist
, &test_cb
, &resp
);
1111 resp
.activate_req
= false;
1112 resp
.activate_req_file_list
= NULL
;
1114 ui_file_list_activate_req(flist
);
1115 PCUT_ASSERT_TRUE(resp
.activate_req
);
1116 PCUT_ASSERT_EQUALS(flist
, resp
.activate_req_file_list
);
1118 ui_file_list_destroy(flist
);
1119 ui_window_destroy(window
);
1123 /** ui_file_list_selected() runs selected callback */
1127 ui_window_t
*window
;
1128 ui_wnd_params_t params
;
1129 ui_file_list_t
*flist
;
1133 rc
= ui_create_disp(NULL
, &ui
);
1134 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1136 ui_wnd_params_init(¶ms
);
1137 params
.caption
= "Test";
1139 rc
= ui_window_create(ui
, ¶ms
, &window
);
1140 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1142 rc
= ui_file_list_create(window
, true, &flist
);
1143 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1145 ui_file_list_set_cb(flist
, &test_cb
, &resp
);
1147 resp
.selected
= false;
1148 resp
.selected_file_list
= NULL
;
1149 resp
.selected_fname
= NULL
;
1151 ui_file_list_selected(flist
, "hello.txt");
1152 PCUT_ASSERT_TRUE(resp
.selected
);
1153 PCUT_ASSERT_EQUALS(flist
, resp
.selected_file_list
);
1154 PCUT_ASSERT_STR_EQUALS("hello.txt", resp
.selected_fname
);
1156 ui_file_list_destroy(flist
);
1157 ui_window_destroy(window
);
1161 /** ui_file_list_get_cursor() returns the current cursor position */
1162 PCUT_TEST(get_cursor
)
1165 ui_window_t
*window
;
1166 ui_wnd_params_t params
;
1167 ui_file_list_t
*flist
;
1168 ui_file_list_entry_attr_t attr
;
1169 ui_file_list_entry_t
*entry
;
1170 ui_file_list_entry_t
*cursor
;
1173 rc
= ui_create_disp(NULL
, &ui
);
1174 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1176 ui_wnd_params_init(¶ms
);
1177 params
.caption
= "Test";
1179 rc
= ui_window_create(ui
, ¶ms
, &window
);
1180 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1182 rc
= ui_file_list_create(window
, true, &flist
);
1183 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1185 ui_file_list_entry_attr_init(&attr
);
1190 rc
= ui_file_list_entry_append(flist
, &attr
);
1191 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1193 entry
= ui_file_list_first(flist
);
1194 PCUT_ASSERT_NOT_NULL(entry
);
1196 /* Cursor should be at the only entry */
1197 cursor
= ui_file_list_get_cursor(flist
);
1198 PCUT_ASSERT_EQUALS(entry
, cursor
);
1200 ui_file_list_destroy(flist
);
1201 ui_window_destroy(window
);
1205 static void test_file_list_activate_req(ui_file_list_t
*flist
, void *arg
)
1207 test_resp_t
*resp
= (test_resp_t
*)arg
;
1209 resp
->activate_req
= true;
1210 resp
->activate_req_file_list
= flist
;
1213 static void test_file_list_selected(ui_file_list_t
*flist
, void *arg
,
1216 test_resp_t
*resp
= (test_resp_t
*)arg
;
1218 resp
->selected
= true;
1219 resp
->selected_file_list
= flist
;
1220 resp
->selected_fname
= fname
;
1223 PCUT_EXPORT(file_list
);