source: mainline/uspace/app/nav/test/panel.c@ 4fcc2de

serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4fcc2de was 4fcc2de, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

Opening directories

  • Property mode set to 100644
File size: 29.4 KB
Line 
1/*
2 * Copyright (c) 2021 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <errno.h>
30#include <io/kbd_event.h>
31#include <io/pos_event.h>
32#include <pcut/pcut.h>
33#include <stdio.h>
34#include <ui/ui.h>
35#include <vfs/vfs.h>
36#include "../panel.h"
37
38PCUT_INIT;
39
40PCUT_TEST_SUITE(panel);
41
42/** Create and destroy panel. */
43PCUT_TEST(create_destroy)
44{
45 panel_t *panel;
46 errno_t rc;
47
48 rc = panel_create(NULL, true, &panel);
49 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
50
51 panel_destroy(panel);
52}
53
54/** Test panel_entry_paint() */
55PCUT_TEST(entry_paint)
56{
57 ui_t *ui;
58 ui_window_t *window;
59 ui_wnd_params_t params;
60 panel_t *panel;
61 errno_t rc;
62
63 rc = ui_create_disp(NULL, &ui);
64 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
65
66 ui_wnd_params_init(&params);
67 params.caption = "Test";
68
69 rc = ui_window_create(ui, &params, &window);
70 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
71
72 rc = panel_create(window, true, &panel);
73 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
74
75 rc = panel_entry_append(panel, "a", 1, false);
76 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
77
78 rc = panel_entry_paint(panel_first(panel), 0);
79 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
80
81 panel_destroy(panel);
82 ui_window_destroy(window);
83 ui_destroy(ui);
84}
85
86/** Test panel_paint() */
87PCUT_TEST(paint)
88{
89 ui_t *ui;
90 ui_window_t *window;
91 ui_wnd_params_t params;
92 panel_t *panel;
93 errno_t rc;
94
95 rc = ui_create_disp(NULL, &ui);
96 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
97
98 ui_wnd_params_init(&params);
99 params.caption = "Test";
100
101 rc = ui_window_create(ui, &params, &window);
102 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
103
104 rc = panel_create(window, true, &panel);
105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
106
107 rc = panel_paint(panel);
108 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
109
110 panel_destroy(panel);
111 ui_window_destroy(window);
112 ui_destroy(ui);
113}
114
115/** panel_ctl() returns a valid UI control */
116PCUT_TEST(ctl)
117{
118 panel_t *panel;
119 ui_control_t *control;
120 errno_t rc;
121
122 rc = panel_create(NULL, true, &panel);
123 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
124
125 control = panel_ctl(panel);
126 PCUT_ASSERT_NOT_NULL(control);
127
128 panel_destroy(panel);
129}
130
131/** Test panel_kbd_event() */
132PCUT_TEST(kbd_event)
133{
134 panel_t *panel;
135 ui_evclaim_t claimed;
136 kbd_event_t event;
137 errno_t rc;
138
139 /* Active panel should claim events */
140
141 rc = panel_create(NULL, true, &panel);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 event.type = KEY_PRESS;
145 event.key = KC_ESCAPE;
146 event.mods = 0;
147 event.c = '\0';
148
149 claimed = panel_kbd_event(panel, &event);
150 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
151
152 panel_destroy(panel);
153
154 /* Inactive panel should not claim events */
155
156 rc = panel_create(NULL, false, &panel);
157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158
159 event.type = KEY_PRESS;
160 event.key = KC_ESCAPE;
161 event.mods = 0;
162 event.c = '\0';
163
164 claimed = panel_kbd_event(panel, &event);
165 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
166
167 panel_destroy(panel);
168}
169
170/** Test panel_pos_event() */
171PCUT_TEST(pos_event)
172{
173 panel_t *panel;
174 ui_evclaim_t claimed;
175 pos_event_t event;
176 errno_t rc;
177
178 rc = panel_create(NULL, true, &panel);
179 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
180
181 event.pos_id = 0;
182 event.type = POS_PRESS;
183 event.btn_num = 1;
184 event.hpos = 0;
185 event.vpos = 0;
186
187 claimed = panel_pos_event(panel, &event);
188 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
189
190 panel_destroy(panel);
191}
192
193/** panel_set_rect() sets internal field */
194PCUT_TEST(set_rect)
195{
196 panel_t *panel;
197 gfx_rect_t rect;
198 errno_t rc;
199
200 rc = panel_create(NULL, true, &panel);
201 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
202
203 rect.p0.x = 1;
204 rect.p0.y = 2;
205 rect.p1.x = 3;
206 rect.p1.y = 4;
207
208 panel_set_rect(panel, &rect);
209 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
210 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
211 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
212 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
213
214 panel_destroy(panel);
215}
216
217/** panel_page_size() returns correct size */
218PCUT_TEST(page_size)
219{
220 panel_t *panel;
221 gfx_rect_t rect;
222 errno_t rc;
223
224 rc = panel_create(NULL, true, &panel);
225 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
226
227 rect.p0.x = 10;
228 rect.p0.y = 20;
229 rect.p1.x = 30;
230 rect.p1.y = 40;
231
232 panel_set_rect(panel, &rect);
233
234 /* NOTE If page size changes, we have problems elsewhere in the tests */
235 PCUT_ASSERT_INT_EQUALS(18, panel_page_size(panel));
236
237 panel_destroy(panel);
238}
239
240/** panel_is_active() returns panel activity state */
241PCUT_TEST(is_active)
242{
243 panel_t *panel;
244 errno_t rc;
245
246 rc = panel_create(NULL, true, &panel);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
248 PCUT_ASSERT_TRUE(panel_is_active(panel));
249 panel_destroy(panel);
250
251 rc = panel_create(NULL, false, &panel);
252 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
253 PCUT_ASSERT_FALSE(panel_is_active(panel));
254 panel_destroy(panel);
255}
256
257/** panel_activate() activates panel */
258PCUT_TEST(activate)
259{
260 ui_t *ui;
261 ui_window_t *window;
262 ui_wnd_params_t params;
263 panel_t *panel;
264 errno_t rc;
265
266 rc = ui_create_disp(NULL, &ui);
267 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
268
269 ui_wnd_params_init(&params);
270 params.caption = "Test";
271
272 rc = ui_window_create(ui, &params, &window);
273 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
274
275 rc = panel_create(window, false, &panel);
276 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
277
278 PCUT_ASSERT_FALSE(panel_is_active(panel));
279 rc = panel_activate(panel);
280 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
281 PCUT_ASSERT_TRUE(panel_is_active(panel));
282
283 panel_destroy(panel);
284 ui_window_destroy(window);
285 ui_destroy(ui);
286}
287
288/** panel_deactivate() deactivates panel */
289PCUT_TEST(deactivate)
290{
291 ui_t *ui;
292 ui_window_t *window;
293 ui_wnd_params_t params;
294 panel_t *panel;
295 errno_t rc;
296
297 rc = ui_create_disp(NULL, &ui);
298 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
299
300 ui_wnd_params_init(&params);
301 params.caption = "Test";
302
303 rc = ui_window_create(ui, &params, &window);
304 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
305
306 rc = panel_create(window, true, &panel);
307 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
308
309 PCUT_ASSERT_TRUE(panel_is_active(panel));
310 panel_deactivate(panel);
311 PCUT_ASSERT_FALSE(panel_is_active(panel));
312
313 panel_destroy(panel);
314 ui_window_destroy(window);
315 ui_destroy(ui);
316}
317
318/** panel_entry_append() appends new entry */
319PCUT_TEST(entry_append)
320{
321 panel_t *panel;
322 errno_t rc;
323
324 rc = panel_create(NULL, true, &panel);
325 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
326
327 rc = panel_entry_append(panel, "a", 1, false);
328 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
329
330 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
331
332 rc = panel_entry_append(panel, "b", 2, false);
333 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
334
335 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
336
337 panel_destroy(panel);
338}
339
340/** panel_entry_delete() deletes entry */
341PCUT_TEST(entry_delete)
342{
343 panel_t *panel;
344 panel_entry_t *entry;
345 errno_t rc;
346
347 rc = panel_create(NULL, true, &panel);
348 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
349
350 rc = panel_entry_append(panel, "a", 1, false);
351 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
352
353 rc = panel_entry_append(panel, "b", 2, false);
354 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
355
356 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
357
358 entry = panel_first(panel);
359 panel_entry_delete(entry);
360
361 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
362
363 entry = panel_first(panel);
364 panel_entry_delete(entry);
365
366 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
367
368 panel_destroy(panel);
369}
370
371/** panel_clear_entries() removes all entries from panel */
372PCUT_TEST(clear_entries)
373{
374 panel_t *panel;
375 errno_t rc;
376
377 rc = panel_create(NULL, true, &panel);
378 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
379
380 rc = panel_entry_append(panel, "a", 1, false);
381 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
382
383 rc = panel_entry_append(panel, "b", 2, false);
384 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
385
386 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
387
388 panel_clear_entries(panel);
389 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
390
391 panel_destroy(panel);
392}
393
394/** panel_read_dir() reads the contents of a directory */
395PCUT_TEST(read_dir)
396{
397 panel_t *panel;
398 panel_entry_t *entry;
399 char buf[L_tmpnam];
400 char *fname;
401 char *p;
402 errno_t rc;
403 FILE *f;
404 int rv;
405
406 /* Create name for temporary directory */
407 p = tmpnam(buf);
408 PCUT_ASSERT_NOT_NULL(p);
409
410 /* Create temporary directory */
411 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
412 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
413
414 rv = asprintf(&fname, "%s/%s", p, "a");
415 PCUT_ASSERT_TRUE(rv >= 0);
416
417 f = fopen(fname, "wb");
418 PCUT_ASSERT_NOT_NULL(f);
419
420 rv = fprintf(f, "X");
421 PCUT_ASSERT_TRUE(rv >= 0);
422
423 rv = fclose(f);
424 PCUT_ASSERT_INT_EQUALS(0, rv);
425
426 rc = panel_create(NULL, true, &panel);
427 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
428
429 rc = panel_read_dir(panel, p);
430 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
431
432 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
433
434 entry = panel_first(panel);
435 PCUT_ASSERT_NOT_NULL(entry);
436 PCUT_ASSERT_STR_EQUALS("..", entry->name);
437
438 entry = panel_next(entry);
439 PCUT_ASSERT_NOT_NULL(entry);
440 PCUT_ASSERT_STR_EQUALS("a", entry->name);
441 PCUT_ASSERT_INT_EQUALS(1, entry->size);
442
443 panel_destroy(panel);
444
445 rv = remove(fname);
446 PCUT_ASSERT_INT_EQUALS(0, rv);
447
448 rv = remove(p);
449 PCUT_ASSERT_INT_EQUALS(0, rv);
450 free(fname);
451}
452
453/** panel_sort() sorts panel entries */
454PCUT_TEST(sort)
455{
456 panel_t *panel;
457 panel_entry_t *entry;
458 errno_t rc;
459
460 rc = panel_create(NULL, true, &panel);
461 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
462
463 rc = panel_entry_append(panel, "b", 1, false);
464 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
465
466 rc = panel_entry_append(panel, "c", 3, false);
467 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
468
469 rc = panel_entry_append(panel, "a", 2, false);
470 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
471
472 rc = panel_sort(panel);
473 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
474
475 entry = panel_first(panel);
476 PCUT_ASSERT_STR_EQUALS("a", entry->name);
477 PCUT_ASSERT_INT_EQUALS(2, entry->size);
478
479 entry = panel_next(entry);
480 PCUT_ASSERT_STR_EQUALS("b", entry->name);
481 PCUT_ASSERT_INT_EQUALS(1, entry->size);
482
483 entry = panel_next(entry);
484 PCUT_ASSERT_STR_EQUALS("c", entry->name);
485 PCUT_ASSERT_INT_EQUALS(3, entry->size);
486
487 panel_destroy(panel);
488}
489
490/** panel_entry_ptr_cmp compares two indirectly referenced entries */
491PCUT_TEST(entry_ptr_cmp)
492{
493 panel_t *panel;
494 panel_entry_t *a, *b;
495 int rel;
496 errno_t rc;
497
498 rc = panel_create(NULL, true, &panel);
499 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
500
501 rc = panel_entry_append(panel, "a", 2, false);
502 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
503
504 rc = panel_entry_append(panel, "b", 1, false);
505 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
506
507 a = panel_first(panel);
508 PCUT_ASSERT_NOT_NULL(a);
509 b = panel_next(a);
510 PCUT_ASSERT_NOT_NULL(b);
511
512 /* a < b */
513 rel = panel_entry_ptr_cmp(&a, &b);
514 PCUT_ASSERT_TRUE(rel < 0);
515
516 /* b > a */
517 rel = panel_entry_ptr_cmp(&b, &a);
518 PCUT_ASSERT_TRUE(rel > 0);
519
520 /* a == a */
521 rel = panel_entry_ptr_cmp(&a, &a);
522 PCUT_ASSERT_INT_EQUALS(0, rel);
523
524 panel_destroy(panel);
525}
526
527/** panel_first() returns valid entry or @c NULL as appropriate */
528PCUT_TEST(first)
529{
530 panel_t *panel;
531 panel_entry_t *entry;
532 errno_t rc;
533
534 rc = panel_create(NULL, true, &panel);
535 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
536
537 entry = panel_first(panel);
538 PCUT_ASSERT_NULL(entry);
539
540 /* Add one entry */
541 rc = panel_entry_append(panel, "a", 1, false);
542 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
543
544 /* Now try getting it */
545 entry = panel_first(panel);
546 PCUT_ASSERT_NOT_NULL(entry);
547 PCUT_ASSERT_STR_EQUALS("a", entry->name);
548 PCUT_ASSERT_INT_EQUALS(1, entry->size);
549
550 /* Add another entry */
551 rc = panel_entry_append(panel, "b", 2, false);
552 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
553
554 /* We should still get the first entry */
555 entry = panel_first(panel);
556 PCUT_ASSERT_NOT_NULL(entry);
557 PCUT_ASSERT_STR_EQUALS("a", entry->name);
558 PCUT_ASSERT_INT_EQUALS(1, entry->size);
559
560 panel_destroy(panel);
561}
562
563/** panel_last() returns valid entry or @c NULL as appropriate */
564PCUT_TEST(last)
565{
566 panel_t *panel;
567 panel_entry_t *entry;
568 errno_t rc;
569
570 rc = panel_create(NULL, true, &panel);
571 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
572
573 entry = panel_last(panel);
574 PCUT_ASSERT_NULL(entry);
575
576 /* Add one entry */
577 rc = panel_entry_append(panel, "a", 1, false);
578 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
579
580 /* Now try getting it */
581 entry = panel_last(panel);
582 PCUT_ASSERT_NOT_NULL(entry);
583 PCUT_ASSERT_STR_EQUALS("a", entry->name);
584 PCUT_ASSERT_INT_EQUALS(1, entry->size);
585
586 /* Add another entry */
587 rc = panel_entry_append(panel, "b", 2, false);
588 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
589
590 /* We should get new entry now */
591 entry = panel_last(panel);
592 PCUT_ASSERT_NOT_NULL(entry);
593 PCUT_ASSERT_STR_EQUALS("b", entry->name);
594 PCUT_ASSERT_INT_EQUALS(2, entry->size);
595
596 panel_destroy(panel);
597}
598
599/** panel_next() returns the next entry or @c NULL as appropriate */
600PCUT_TEST(next)
601{
602 panel_t *panel;
603 panel_entry_t *entry;
604 errno_t rc;
605
606 rc = panel_create(NULL, true, &panel);
607 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
608
609 /* Add one entry */
610 rc = panel_entry_append(panel, "a", 1, false);
611 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
612
613 /* Now try getting its successor */
614 entry = panel_first(panel);
615 PCUT_ASSERT_NOT_NULL(entry);
616
617 entry = panel_next(entry);
618 PCUT_ASSERT_NULL(entry);
619
620 /* Add another entry */
621 rc = panel_entry_append(panel, "b", 2, false);
622 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
623
624 /* Try getting the successor of first entry again */
625 entry = panel_first(panel);
626 PCUT_ASSERT_NOT_NULL(entry);
627
628 entry = panel_next(entry);
629 PCUT_ASSERT_NOT_NULL(entry);
630 PCUT_ASSERT_STR_EQUALS("b", entry->name);
631 PCUT_ASSERT_INT_EQUALS(2, entry->size);
632
633 panel_destroy(panel);
634}
635
636/** panel_prev() returns the previous entry or @c NULL as appropriate */
637PCUT_TEST(prev)
638{
639 panel_t *panel;
640 panel_entry_t *entry;
641 errno_t rc;
642
643 rc = panel_create(NULL, true, &panel);
644 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
645
646 /* Add one entry */
647 rc = panel_entry_append(panel, "a", 1, false);
648 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
649
650 /* Now try getting its predecessor */
651 entry = panel_last(panel);
652 PCUT_ASSERT_NOT_NULL(entry);
653
654 entry = panel_prev(entry);
655 PCUT_ASSERT_NULL(entry);
656
657 /* Add another entry */
658 rc = panel_entry_append(panel, "b", 2, false);
659 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
660
661 /* Try getting the predecessor of the new entry */
662 entry = panel_last(panel);
663 PCUT_ASSERT_NOT_NULL(entry);
664
665 entry = panel_prev(entry);
666 PCUT_ASSERT_NOT_NULL(entry);
667 PCUT_ASSERT_STR_EQUALS("a", entry->name);
668 PCUT_ASSERT_INT_EQUALS(1, entry->size);
669
670 panel_destroy(panel);
671}
672
673/** panel_cursor_move() ... */
674PCUT_TEST(cursor_move)
675{
676}
677
678/** panel_cursor_up() moves cursor one entry up */
679PCUT_TEST(cursor_up)
680{
681 ui_t *ui;
682 ui_window_t *window;
683 ui_wnd_params_t params;
684 panel_t *panel;
685 gfx_rect_t rect;
686 errno_t rc;
687
688 rc = ui_create_disp(NULL, &ui);
689 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
690
691 ui_wnd_params_init(&params);
692 params.caption = "Test";
693
694 rc = ui_window_create(ui, &params, &window);
695 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
696
697 rc = panel_create(window, true, &panel);
698 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
699
700 rect.p0.x = 0;
701 rect.p0.y = 0;
702 rect.p1.x = 10;
703 rect.p1.y = 4; // XXX Assuming this makes page size 2
704 panel_set_rect(panel, &rect);
705
706 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
707
708 /* Add tree entries (more than page size, which is 2) */
709 rc = panel_entry_append(panel, "a", 1, false);
710 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
711
712 rc = panel_entry_append(panel, "b", 2, false);
713 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
714
715 rc = panel_entry_append(panel, "c", 3, false);
716 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
717
718 /* Cursor to the last entry and page start to the next-to-last entry */
719 panel->cursor = panel_last(panel);
720 panel->cursor_idx = 2;
721 panel->page = panel_prev(panel->cursor);
722 panel->page_idx = 1;
723
724 /* Move cursor one entry up */
725 panel_cursor_up(panel);
726
727 /* Cursor and page start should now both be at the second entry */
728 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
729 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
730 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
731 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
732 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
733
734 /* Move cursor one entry up. This should scroll up. */
735 panel_cursor_up(panel);
736
737 /* Cursor and page start should now both be at the first entry */
738 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
739 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
740 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
741 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
742 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
743
744 /* Moving further up should do nothing (we are at the top). */
745 panel_cursor_up(panel);
746
747 /* Cursor and page start should still be at the first entry */
748 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
749 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
750 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
751 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
752 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
753
754 panel_destroy(panel);
755 ui_window_destroy(window);
756 ui_destroy(ui);
757}
758
759/** panel_cursor_down() moves cursor one entry down */
760PCUT_TEST(cursor_down)
761{
762 ui_t *ui;
763 ui_window_t *window;
764 ui_wnd_params_t params;
765 panel_t *panel;
766 gfx_rect_t rect;
767 errno_t rc;
768
769 rc = ui_create_disp(NULL, &ui);
770 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
771
772 ui_wnd_params_init(&params);
773 params.caption = "Test";
774
775 rc = ui_window_create(ui, &params, &window);
776 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
777
778 rc = panel_create(window, true, &panel);
779 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
780
781 rect.p0.x = 0;
782 rect.p0.y = 0;
783 rect.p1.x = 10;
784 rect.p1.y = 4; // XXX Assuming this makes page size 2
785 panel_set_rect(panel, &rect);
786
787 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
788
789 /* Add tree entries (more than page size, which is 2) */
790 rc = panel_entry_append(panel, "a", 1, false);
791 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
792
793 rc = panel_entry_append(panel, "b", 2, false);
794 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
795
796 rc = panel_entry_append(panel, "c", 3, false);
797 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
798
799 /* Cursor and page start to the first entry */
800 panel->cursor = panel_first(panel);
801 panel->cursor_idx = 0;
802 panel->page = panel->cursor;
803 panel->page_idx = 0;
804
805 /* Move cursor one entry down */
806 panel_cursor_down(panel);
807
808 /* Cursor should now be at the second entry, page stays the same */
809 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
810 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
811 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
812 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page);
813 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
814
815 /* Move cursor one entry down. This should scroll down. */
816 panel_cursor_down(panel);
817
818 /* Cursor should now be at the third and page at the second entry. */
819 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
820 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
821 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
822 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
823 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
824 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
825
826 /* Moving further down should do nothing (we are at the bottom). */
827 panel_cursor_down(panel);
828
829 /* Cursor should still be at the third and page at the second entry. */
830 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
831 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
832 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
833 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
834 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
835 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
836
837 panel_destroy(panel);
838 ui_window_destroy(window);
839 ui_destroy(ui);
840}
841
842/** panel_cursor_top() moves cursor to the first entry */
843PCUT_TEST(cursor_top)
844{
845 ui_t *ui;
846 ui_window_t *window;
847 ui_wnd_params_t params;
848 panel_t *panel;
849 gfx_rect_t rect;
850 errno_t rc;
851
852 rc = ui_create_disp(NULL, &ui);
853 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
854
855 ui_wnd_params_init(&params);
856 params.caption = "Test";
857
858 rc = ui_window_create(ui, &params, &window);
859 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
860
861 rc = panel_create(window, true, &panel);
862 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
863
864 rect.p0.x = 0;
865 rect.p0.y = 0;
866 rect.p1.x = 10;
867 rect.p1.y = 4; // XXX Assuming this makes page size 2
868 panel_set_rect(panel, &rect);
869
870 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
871
872 /* Add tree entries (more than page size, which is 2) */
873 rc = panel_entry_append(panel, "a", 1, false);
874 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
875
876 rc = panel_entry_append(panel, "b", 2, false);
877 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
878
879 rc = panel_entry_append(panel, "c", 3, false);
880 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
881
882 /* Cursor to the last entry and page start to the next-to-last entry */
883 panel->cursor = panel_last(panel);
884 panel->cursor_idx = 2;
885 panel->page = panel_prev(panel->cursor);
886 panel->page_idx = 1;
887
888 /* Move cursor to the top. This should scroll up. */
889 panel_cursor_top(panel);
890
891 /* Cursor and page start should now both be at the first entry */
892 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
893 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
894 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
895 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
896 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
897
898 panel_destroy(panel);
899 ui_window_destroy(window);
900 ui_destroy(ui);
901}
902
903/** panel_cursor_bottom() moves cursor to the last entry */
904PCUT_TEST(cursor_bottom)
905{
906 ui_t *ui;
907 ui_window_t *window;
908 ui_wnd_params_t params;
909 panel_t *panel;
910 gfx_rect_t rect;
911 errno_t rc;
912
913 rc = ui_create_disp(NULL, &ui);
914 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
915
916 ui_wnd_params_init(&params);
917 params.caption = "Test";
918
919 rc = ui_window_create(ui, &params, &window);
920 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
921
922 rc = panel_create(window, true, &panel);
923 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
924
925 rect.p0.x = 0;
926 rect.p0.y = 0;
927 rect.p1.x = 10;
928 rect.p1.y = 4; // XXX Assuming this makes page size 2
929 panel_set_rect(panel, &rect);
930
931 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
932
933 /* Add tree entries (more than page size, which is 2) */
934 rc = panel_entry_append(panel, "a", 1, false);
935 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
936
937 rc = panel_entry_append(panel, "b", 2, false);
938 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
939
940 rc = panel_entry_append(panel, "c", 3, false);
941 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
942
943 /* Cursor and page start to the first entry */
944 panel->cursor = panel_first(panel);
945 panel->cursor_idx = 0;
946 panel->page = panel->cursor;
947 panel->page_idx = 0;
948
949 /* Move cursor to the bottom. This should scroll down. */
950 panel_cursor_bottom(panel);
951
952 /* Cursor should now be at the third and page at the second entry. */
953 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
954 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
955 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
956 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
957 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
958 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
959
960 panel_destroy(panel);
961 ui_window_destroy(window);
962 ui_destroy(ui);
963}
964
965/** panel_page_up() moves one page up */
966PCUT_TEST(page_up)
967{
968 ui_t *ui;
969 ui_window_t *window;
970 ui_wnd_params_t params;
971 panel_t *panel;
972 gfx_rect_t rect;
973 errno_t rc;
974
975 rc = ui_create_disp(NULL, &ui);
976 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
977
978 ui_wnd_params_init(&params);
979 params.caption = "Test";
980
981 rc = ui_window_create(ui, &params, &window);
982 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
983
984 rc = panel_create(window, true, &panel);
985 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
986
987 rect.p0.x = 0;
988 rect.p0.y = 0;
989 rect.p1.x = 10;
990 rect.p1.y = 4; // XXX Assuming this makes page size 2
991 panel_set_rect(panel, &rect);
992
993 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
994
995 /* Add five entries (2 full pages, one partial) */
996 rc = panel_entry_append(panel, "a", 1, false);
997 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
998
999 rc = panel_entry_append(panel, "b", 2, false);
1000 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1001
1002 rc = panel_entry_append(panel, "c", 3, false);
1003 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1004
1005 rc = panel_entry_append(panel, "d", 4, false);
1006 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1007
1008 rc = panel_entry_append(panel, "e", 5, false);
1009 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1010
1011 /* Cursor to the last entry and page start to the next-to-last entry */
1012 panel->cursor = panel_last(panel);
1013 panel->cursor_idx = 4;
1014 panel->page = panel_prev(panel->cursor);
1015 panel->page_idx = 3;
1016
1017 /* Move one page up */
1018 panel_page_up(panel);
1019
1020 /* Page should now start at second entry and cursor at third */
1021 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1022 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1023 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1024 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
1025 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
1026 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1027
1028 /* Move one page up again. */
1029 panel_page_up(panel);
1030
1031 /* Cursor and page start should now both be at the first entry */
1032 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1033 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1034 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1035 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1036 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1037
1038 /* Moving further up should do nothing (we are at the top). */
1039 panel_page_up(panel);
1040
1041 /* Cursor and page start should still be at the first entry */
1042 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1043 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1044 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1045 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1046 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1047
1048 panel_destroy(panel);
1049 ui_window_destroy(window);
1050 ui_destroy(ui);
1051}
1052
1053/** panel_page_up() moves one page down */
1054PCUT_TEST(page_down)
1055{
1056 ui_t *ui;
1057 ui_window_t *window;
1058 ui_wnd_params_t params;
1059 panel_t *panel;
1060 gfx_rect_t rect;
1061 errno_t rc;
1062
1063 rc = ui_create_disp(NULL, &ui);
1064 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1065
1066 ui_wnd_params_init(&params);
1067 params.caption = "Test";
1068
1069 rc = ui_window_create(ui, &params, &window);
1070 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1071
1072 rc = panel_create(window, true, &panel);
1073 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1074
1075 rect.p0.x = 0;
1076 rect.p0.y = 0;
1077 rect.p1.x = 10;
1078 rect.p1.y = 4; // XXX Assuming this makes page size 2
1079 panel_set_rect(panel, &rect);
1080
1081 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1082
1083 /* Add five entries (2 full pages, one partial) */
1084 rc = panel_entry_append(panel, "a", 1, false);
1085 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1086
1087 rc = panel_entry_append(panel, "b", 2, false);
1088 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1089
1090 rc = panel_entry_append(panel, "c", 3, false);
1091 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1092
1093 rc = panel_entry_append(panel, "d", 4, false);
1094 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1095
1096 rc = panel_entry_append(panel, "e", 5, false);
1097 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1098
1099 /* Cursor and page to the first entry */
1100 panel->cursor = panel_first(panel);
1101 panel->cursor_idx = 0;
1102 panel->page = panel->cursor;
1103 panel->page_idx = 0;
1104
1105 /* Move one page down */
1106 panel_page_down(panel);
1107
1108 /* Page and cursor should point to the third entry */
1109 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1110 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1111 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1112 PCUT_ASSERT_STR_EQUALS("c", panel->page->name);
1113 PCUT_ASSERT_INT_EQUALS(3, panel->page->size);
1114 PCUT_ASSERT_INT_EQUALS(2, panel->page_idx);
1115
1116 /* Move one page down again. */
1117 panel_page_down(panel);
1118
1119 /* Cursor should point to last and page to next-to-last entry */
1120 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1121 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1122 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1123 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1124 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1125 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1126
1127 /* Moving further down should do nothing (we are at the bottom). */
1128 panel_page_down(panel);
1129
1130 /* Cursor should still point to last and page to next-to-last entry */
1131 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1132 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1133 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1134 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1135 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1136 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1137
1138 panel_destroy(panel);
1139 ui_window_destroy(window);
1140 ui_destroy(ui);
1141}
1142
1143/** panel_open() opens a directory entry */
1144PCUT_TEST(open)
1145{
1146 ui_t *ui;
1147 ui_window_t *window;
1148 ui_wnd_params_t params;
1149 panel_t *panel;
1150 panel_entry_t *entry;
1151 char buf[L_tmpnam];
1152 char *sdname;
1153 char *p;
1154 errno_t rc;
1155 int rv;
1156
1157 rc = ui_create_disp(NULL, &ui);
1158 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1159
1160 ui_wnd_params_init(&params);
1161 params.caption = "Test";
1162
1163 rc = ui_window_create(ui, &params, &window);
1164 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1165
1166 /* Create name for temporary directory */
1167 p = tmpnam(buf);
1168 PCUT_ASSERT_NOT_NULL(p);
1169
1170 /* Create temporary directory */
1171 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
1172 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1173
1174 rv = asprintf(&sdname, "%s/%s", p, "a");
1175 PCUT_ASSERT_TRUE(rv >= 0);
1176
1177 /* Create sub-directory */
1178 rc = vfs_link_path(sdname, KIND_DIRECTORY, NULL);
1179 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1180
1181 rc = panel_create(window, true, &panel);
1182 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1183
1184 rc = panel_read_dir(panel, p);
1185 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1186 PCUT_ASSERT_STR_EQUALS(p, panel->dir);
1187
1188 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
1189
1190 entry = panel_first(panel);
1191 PCUT_ASSERT_NOT_NULL(entry);
1192 PCUT_ASSERT_STR_EQUALS("..", entry->name);
1193
1194 entry = panel_next(entry);
1195 PCUT_ASSERT_NOT_NULL(entry);
1196 PCUT_ASSERT_STR_EQUALS("a", entry->name);
1197 PCUT_ASSERT_TRUE(entry->isdir);
1198
1199 rc = panel_open(panel, entry);
1200 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1201
1202 PCUT_ASSERT_STR_EQUALS(sdname, panel->dir);
1203
1204 panel_destroy(panel);
1205 ui_window_destroy(window);
1206 ui_destroy(ui);
1207
1208 rv = remove(sdname);
1209 PCUT_ASSERT_INT_EQUALS(0, rv);
1210
1211 rv = remove(p);
1212 PCUT_ASSERT_INT_EQUALS(0, rv);
1213
1214 free(sdname);
1215}
1216
1217PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.