Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / filelist.c
blob98e713d7e9ee5e86d8a7403958dbda0175798384
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <errno.h>
30 #include <io/kbd_event.h>
31 #include <io/pos_event.h>
32 #include <pcut/pcut.h>
33 #include <stdio.h>
34 #include <ui/ui.h>
35 #include <ui/filelist.h>
36 #include <ui/scrollbar.h>
37 #include <vfs/vfs.h>
38 #include "../private/filelist.h"
39 #include "../private/list.h"
41 PCUT_INIT;
43 PCUT_TEST_SUITE(file_list);
45 /** Test response */
46 typedef struct {
47 bool activate_req;
48 ui_file_list_t *activate_req_file_list;
50 bool selected;
51 ui_file_list_t *selected_file_list;
52 const char *selected_fname;
53 } test_resp_t;
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)
66 ui_t *ui;
67 ui_window_t *window;
68 ui_wnd_params_t params;
69 ui_file_list_t *flist;
70 errno_t rc;
72 rc = ui_create_disp(NULL, &ui);
73 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75 ui_wnd_params_init(&params);
76 params.caption = "Test";
78 rc = ui_window_create(ui, &params, &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);
86 ui_destroy(ui);
89 /** ui_file_list_set_cb() sets callback */
90 PCUT_TEST(set_cb)
92 ui_t *ui;
93 ui_window_t *window;
94 ui_wnd_params_t params;
95 ui_file_list_t *flist;
96 errno_t rc;
97 test_resp_t resp;
99 rc = ui_create_disp(NULL, &ui);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
102 ui_wnd_params_init(&params);
103 params.caption = "Test";
105 rc = ui_window_create(ui, &params, &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);
117 ui_destroy(ui);
120 /** Test ui_file_list_paint() */
121 PCUT_TEST(paint)
123 ui_t *ui;
124 ui_window_t *window;
125 ui_wnd_params_t params;
126 ui_file_list_t *flist;
127 errno_t rc;
129 rc = ui_create_disp(NULL, &ui);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
132 ui_wnd_params_init(&params);
133 params.caption = "Test";
135 rc = ui_window_create(ui, &params, &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);
146 ui_destroy(ui);
149 /** ui_file_list_ctl() returns a valid UI control */
150 PCUT_TEST(ctl)
152 ui_t *ui;
153 ui_window_t *window;
154 ui_wnd_params_t params;
155 ui_file_list_t *flist;
156 ui_control_t *control;
157 errno_t rc;
159 rc = ui_create_disp(NULL, &ui);
160 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
162 ui_wnd_params_init(&params);
163 params.caption = "Test";
165 rc = ui_window_create(ui, &params, &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);
176 ui_destroy(ui);
179 /** ui_file_list_set_rect() sets internal field */
180 PCUT_TEST(set_rect)
182 ui_t *ui;
183 ui_window_t *window;
184 ui_wnd_params_t params;
185 ui_file_list_t *flist;
186 gfx_rect_t rect;
187 errno_t rc;
189 rc = ui_create_disp(NULL, &ui);
190 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
192 ui_wnd_params_init(&params);
193 params.caption = "Test";
195 rc = ui_window_create(ui, &params, &window);
196 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
198 rc = ui_file_list_create(window, true, &flist);
199 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
201 rect.p0.x = 1;
202 rect.p0.y = 2;
203 rect.p1.x = 3;
204 rect.p1.y = 4;
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);
214 ui_destroy(ui);
217 /** ui_file_list_is_active() returns file list activity state */
218 PCUT_TEST(is_active)
220 ui_t *ui;
221 ui_window_t *window;
222 ui_wnd_params_t params;
223 ui_file_list_t *flist;
224 errno_t rc;
226 rc = ui_create_disp(NULL, &ui);
227 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
229 ui_wnd_params_init(&params);
230 params.caption = "Test";
232 rc = ui_window_create(ui, &params, &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);
245 ui_destroy(ui);
248 /** ui_file_list_activate() activates file list */
249 PCUT_TEST(activate)
251 ui_t *ui;
252 ui_window_t *window;
253 ui_wnd_params_t params;
254 ui_file_list_t *flist;
255 errno_t rc;
257 rc = ui_create_disp(NULL, &ui);
258 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
260 ui_wnd_params_init(&params);
261 params.caption = "Test";
263 rc = ui_window_create(ui, &params, &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);
276 ui_destroy(ui);
279 /** ui_file_list_deactivate() deactivates file list */
280 PCUT_TEST(deactivate)
282 ui_t *ui;
283 ui_window_t *window;
284 ui_wnd_params_t params;
285 ui_file_list_t *flist;
286 errno_t rc;
288 rc = ui_create_disp(NULL, &ui);
289 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
291 ui_wnd_params_init(&params);
292 params.caption = "Test";
294 rc = ui_window_create(ui, &params, &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);
306 ui_destroy(ui);
309 /** ui_file_list_entry_append() appends new entry */
310 PCUT_TEST(entry_append)
312 ui_t *ui;
313 ui_window_t *window;
314 ui_wnd_params_t params;
315 ui_file_list_t *flist;
316 ui_file_list_entry_attr_t attr;
317 errno_t rc;
319 rc = ui_create_disp(NULL, &ui);
320 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
322 ui_wnd_params_init(&params);
323 params.caption = "Test";
325 rc = ui_window_create(ui, &params, &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);
333 attr.name = "a";
334 attr.size = 1;
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));
340 attr.name = "b";
341 attr.size = 2;
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);
349 ui_destroy(ui);
352 /** ui_file_list_entry_destroy() destroys entry */
353 PCUT_TEST(entry_destroy)
355 ui_t *ui;
356 ui_window_t *window;
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;
361 errno_t rc;
363 rc = ui_create_disp(NULL, &ui);
364 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
366 ui_wnd_params_init(&params);
367 params.caption = "Test";
369 rc = ui_window_create(ui, &params, &window);
370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
372 rc = ui_file_list_create(window, true, &flist);
373 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
375 attr.name = "a";
376 attr.size = 1;
377 rc = ui_file_list_entry_append(flist, &attr);
378 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
380 attr.name = "b";
381 attr.size = 2;
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);
399 ui_destroy(ui);
402 /** ui_file_list_clear_entries() removes all entries from file list */
403 PCUT_TEST(clear_entries)
405 ui_t *ui;
406 ui_window_t *window;
407 ui_wnd_params_t params;
408 ui_file_list_t *flist;
409 ui_file_list_entry_attr_t attr;
410 errno_t rc;
412 rc = ui_create_disp(NULL, &ui);
413 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
415 ui_wnd_params_init(&params);
416 params.caption = "Test";
418 rc = ui_window_create(ui, &params, &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);
425 attr.name = "a";
426 attr.size = 1;
427 rc = ui_file_list_entry_append(flist, &attr);
428 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
430 attr.name = "a";
431 attr.size = 2;
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);
442 ui_destroy(ui);
445 /** ui_file_list_read_dir() reads the contents of a directory */
446 PCUT_TEST(read_dir)
448 ui_t *ui;
449 ui_window_t *window;
450 ui_wnd_params_t params;
451 ui_file_list_t *flist;
452 ui_file_list_entry_t *entry;
453 char buf[L_tmpnam];
454 char *fname;
455 char *p;
456 errno_t rc;
457 FILE *f;
458 int rv;
460 /* Create name for temporary directory */
461 p = tmpnam(buf);
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);
477 rv = fclose(f);
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(&params);
484 params.caption = "Test";
486 rc = ui_window_create(ui, &params, &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);
508 rv = remove(fname);
509 PCUT_ASSERT_INT_EQUALS(0, rv);
511 rv = remove(p);
512 PCUT_ASSERT_INT_EQUALS(0, rv);
514 free(fname);
515 ui_window_destroy(window);
516 ui_destroy(ui);
519 /** When moving to parent directory from a subdir, we seek to the
520 * coresponding entry
522 PCUT_TEST(read_dir_up)
524 ui_t *ui;
525 ui_window_t *window;
526 ui_wnd_params_t params;
527 ui_file_list_t *flist;
528 char buf[L_tmpnam];
529 char *subdir_a;
530 char *subdir_b;
531 char *subdir_c;
532 char *p;
533 errno_t rc;
534 int rv;
536 /* Create name for temporary directory */
537 p = tmpnam(buf);
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(&params);
565 params.caption = "Test";
567 rc = ui_window_create(ui, &params, &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);
596 rv = remove(p);
597 PCUT_ASSERT_INT_EQUALS(0, rv);
599 free(subdir_a);
600 free(subdir_b);
601 free(subdir_c);
603 ui_window_destroy(window);
604 ui_destroy(ui);
607 /** ui_file_list_sort() sorts file list entries */
608 PCUT_TEST(sort)
610 ui_t *ui;
611 ui_window_t *window;
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;
616 errno_t rc;
618 rc = ui_create_disp(NULL, &ui);
619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
621 ui_wnd_params_init(&params);
622 params.caption = "Test";
624 rc = ui_window_create(ui, &params, &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);
632 attr.name = "b";
633 attr.size = 1;
634 rc = ui_file_list_entry_append(flist, &attr);
635 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
637 attr.name = "c";
638 attr.size = 3;
639 rc = ui_file_list_entry_append(flist, &attr);
640 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
642 attr.name = "a";
643 attr.size = 2;
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);
664 ui_destroy(ui);
667 /** ui_file_list_list_compare compares two file list entries */
668 PCUT_TEST(list_compare)
670 ui_t *ui;
671 ui_window_t *window;
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;
676 int rel;
677 errno_t rc;
679 rc = ui_create_disp(NULL, &ui);
680 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
682 ui_wnd_params_init(&params);
683 params.caption = "Test";
685 rc = ui_window_create(ui, &params, &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);
693 attr.name = "a";
694 attr.size = 2;
695 rc = ui_file_list_entry_append(flist, &attr);
696 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
698 attr.name = "b";
699 attr.size = 1;
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);
708 /* a < b */
709 rel = ui_file_list_list_compare(a->entry, b->entry);
710 PCUT_ASSERT_TRUE(rel < 0);
712 /* b > a */
713 rel = ui_file_list_list_compare(b->entry, a->entry);
714 PCUT_ASSERT_TRUE(rel > 0);
716 /* a == a */
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);
722 ui_destroy(ui);
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 */
738 PCUT_TEST(first)
740 ui_t *ui;
741 ui_window_t *window;
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;
746 errno_t rc;
748 rc = ui_create_disp(NULL, &ui);
749 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
751 ui_wnd_params_init(&params);
752 params.caption = "Test";
754 rc = ui_window_create(ui, &params, &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);
765 /* Add one entry */
766 attr.name = "a";
767 attr.size = 1;
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 */
778 attr.name = "b";
779 attr.size = 2;
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);
791 ui_destroy(ui);
794 /** ui_file_list_last() returns valid entry or @c NULL as appropriate */
795 PCUT_TEST(last)
797 ui_t *ui;
798 ui_window_t *window;
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;
803 errno_t rc;
805 rc = ui_create_disp(NULL, &ui);
806 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
808 ui_wnd_params_init(&params);
809 params.caption = "Test";
811 rc = ui_window_create(ui, &params, &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);
822 /* Add one entry */
823 attr.name = "a";
824 attr.size = 1;
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 */
835 attr.name = "b";
836 attr.size = 2;
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);
843 attr.name = "b";
844 attr.size = 2;
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);
850 ui_destroy(ui);
853 /** ui_file_list_next() returns the next entry or @c NULL as appropriate */
854 PCUT_TEST(next)
856 ui_t *ui;
857 ui_window_t *window;
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;
862 errno_t rc;
864 rc = ui_create_disp(NULL, &ui);
865 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
867 ui_wnd_params_init(&params);
868 params.caption = "Test";
870 rc = ui_window_create(ui, &params, &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);
878 /* Add one entry */
879 attr.name = "a";
880 attr.size = 1;
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 */
892 attr.name = "b";
893 attr.size = 2;
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);
908 ui_destroy(ui);
911 /** ui_file_list_prev() returns the previous entry or @c NULL as appropriate */
912 PCUT_TEST(prev)
914 ui_t *ui;
915 ui_window_t *window;
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;
920 errno_t rc;
922 rc = ui_create_disp(NULL, &ui);
923 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
925 ui_wnd_params_init(&params);
926 params.caption = "Test";
928 rc = ui_window_create(ui, &params, &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);
936 /* Add one entry */
937 attr.name = "a";
938 attr.size = 1;
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 */
950 attr.name = "b";
951 attr.size = 2;
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);
966 ui_destroy(ui);
969 /** ui_file_list_open_dir() opens a directory entry */
970 PCUT_TEST(open_dir)
972 ui_t *ui;
973 ui_window_t *window;
974 ui_wnd_params_t params;
975 ui_file_list_t *flist;
976 ui_file_list_entry_t *entry;
977 char buf[L_tmpnam];
978 char *sdname;
979 char *p;
980 errno_t rc;
981 int rv;
983 rc = ui_create_disp(NULL, &ui);
984 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
986 ui_wnd_params_init(&params);
987 params.caption = "Test";
989 rc = ui_window_create(ui, &params, &window);
990 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
992 /* Create name for temporary directory */
993 p = tmpnam(buf);
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);
1032 ui_destroy(ui);
1034 rv = remove(sdname);
1035 PCUT_ASSERT_INT_EQUALS(0, rv);
1037 rv = remove(p);
1038 PCUT_ASSERT_INT_EQUALS(0, rv);
1040 free(sdname);
1043 /** ui_file_list_open_file() runs selected callback */
1044 PCUT_TEST(open_file)
1046 ui_t *ui;
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;
1051 errno_t rc;
1052 test_resp_t resp;
1054 rc = ui_create_disp(NULL, &ui);
1055 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1057 ui_wnd_params_init(&params);
1058 params.caption = "Test";
1060 rc = ui_window_create(ui, &params, &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";
1069 attr.size = 1;
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);
1084 ui_destroy(ui);
1087 /** ui_file_list_activate_req() sends activation request */
1088 PCUT_TEST(activate_req)
1090 ui_t *ui;
1091 ui_window_t *window;
1092 ui_wnd_params_t params;
1093 ui_file_list_t *flist;
1094 errno_t rc;
1095 test_resp_t resp;
1097 rc = ui_create_disp(NULL, &ui);
1098 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1100 ui_wnd_params_init(&params);
1101 params.caption = "Test";
1103 rc = ui_window_create(ui, &params, &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);
1120 ui_destroy(ui);
1123 /** ui_file_list_selected() runs selected callback */
1124 PCUT_TEST(selected)
1126 ui_t *ui;
1127 ui_window_t *window;
1128 ui_wnd_params_t params;
1129 ui_file_list_t *flist;
1130 errno_t rc;
1131 test_resp_t resp;
1133 rc = ui_create_disp(NULL, &ui);
1134 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1136 ui_wnd_params_init(&params);
1137 params.caption = "Test";
1139 rc = ui_window_create(ui, &params, &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);
1158 ui_destroy(ui);
1161 /** ui_file_list_get_cursor() returns the current cursor position */
1162 PCUT_TEST(get_cursor)
1164 ui_t *ui;
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;
1171 errno_t rc;
1173 rc = ui_create_disp(NULL, &ui);
1174 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1176 ui_wnd_params_init(&params);
1177 params.caption = "Test";
1179 rc = ui_window_create(ui, &params, &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);
1187 /* Append entry */
1188 attr.name = "a";
1189 attr.size = 1;
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);
1202 ui_destroy(ui);
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,
1214 const char *fname)
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);