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

Last change on this file since fe5c7a1 was fe5c7a1, checked in by Jiri Svoboda <jiri@…>, 4 years ago

Panel activation

Active panel can be switched using the Tab key. Mouse activation is
not implemented.

  • Property mode set to 100644
File size: 25.8 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);
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_ENTER;
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_ENTER;
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 panel_activate(panel);
280 PCUT_ASSERT_TRUE(panel_is_active(panel));
281
282 panel_destroy(panel);
283 ui_window_destroy(window);
284 ui_destroy(ui);
285}
286
287/** panel_deactivate() deactivates panel */
288PCUT_TEST(deactivate)
289{
290 ui_t *ui;
291 ui_window_t *window;
292 ui_wnd_params_t params;
293 panel_t *panel;
294 errno_t rc;
295
296 rc = ui_create_disp(NULL, &ui);
297 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
298
299 ui_wnd_params_init(&params);
300 params.caption = "Test";
301
302 rc = ui_window_create(ui, &params, &window);
303 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
304
305 rc = panel_create(window, true, &panel);
306 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
307
308 PCUT_ASSERT_TRUE(panel_is_active(panel));
309 panel_deactivate(panel);
310 PCUT_ASSERT_FALSE(panel_is_active(panel));
311
312 panel_destroy(panel);
313 ui_window_destroy(window);
314 ui_destroy(ui);
315}
316
317/** panel_entry_append() appends new entry */
318PCUT_TEST(entry_append)
319{
320 panel_t *panel;
321 errno_t rc;
322
323 rc = panel_create(NULL, true, &panel);
324 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
325
326 rc = panel_entry_append(panel, "a", 1);
327 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
328
329 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
330
331 rc = panel_entry_append(panel, "b", 2);
332 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
333
334 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
335
336 panel_destroy(panel);
337}
338
339/** panel_entry_delete() deletes entry */
340PCUT_TEST(entry_delete)
341{
342 panel_t *panel;
343 panel_entry_t *entry;
344 errno_t rc;
345
346 rc = panel_create(NULL, true, &panel);
347 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
348
349 rc = panel_entry_append(panel, "a", 1);
350 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
351
352 rc = panel_entry_append(panel, "b", 2);
353 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
354
355 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
356
357 entry = panel_first(panel);
358 panel_entry_delete(entry);
359
360 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
361
362 entry = panel_first(panel);
363 panel_entry_delete(entry);
364
365 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
366
367 panel_destroy(panel);
368}
369
370/** panel_clear_entries() removes all entries from panel */
371PCUT_TEST(clear_entries)
372{
373 panel_t *panel;
374 errno_t rc;
375
376 rc = panel_create(NULL, true, &panel);
377 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
378
379 rc = panel_entry_append(panel, "a", 1);
380 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
381
382 rc = panel_entry_append(panel, "b", 2);
383 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
384
385 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
386
387 panel_clear_entries(panel);
388 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
389
390 panel_destroy(panel);
391}
392
393/** panel_read_dir() reads the contents of a directory */
394PCUT_TEST(read_dir)
395{
396 panel_t *panel;
397 panel_entry_t *entry;
398 char buf[L_tmpnam];
399 char *fname;
400 char *p;
401 errno_t rc;
402 FILE *f;
403 int rv;
404
405 /* Create name for temporary directory */
406 p = tmpnam(buf);
407 PCUT_ASSERT_NOT_NULL(p);
408
409 /* Create temporary directory */
410 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
411 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
412
413 rv = asprintf(&fname, "%s/%s", p, "a");
414 PCUT_ASSERT_TRUE(rv >= 0);
415
416 f = fopen(fname, "wb");
417 PCUT_ASSERT_NOT_NULL(f);
418
419 rv = fprintf(f, "X");
420 PCUT_ASSERT_TRUE(rv >= 0);
421
422 rv = fclose(f);
423 PCUT_ASSERT_INT_EQUALS(0, rv);
424
425 rc = panel_create(NULL, true, &panel);
426 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
427
428 rc = panel_read_dir(panel, p);
429 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
430
431 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
432
433 entry = panel_first(panel);
434 PCUT_ASSERT_NOT_NULL(entry);
435 PCUT_ASSERT_STR_EQUALS("a", entry->name);
436 // PCUT_ASSERT_INT_EQUALS(1, entry->size);
437
438 panel_destroy(panel);
439
440 rv = remove(fname);
441 PCUT_ASSERT_INT_EQUALS(0, rv);
442
443 rv = remove(p);
444 PCUT_ASSERT_INT_EQUALS(0, rv);
445 free(fname);
446}
447
448/** panel_first() returns valid entry or @c NULL as appropriate */
449PCUT_TEST(first)
450{
451 panel_t *panel;
452 panel_entry_t *entry;
453 errno_t rc;
454
455 rc = panel_create(NULL, true, &panel);
456 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
457
458 entry = panel_first(panel);
459 PCUT_ASSERT_NULL(entry);
460
461 /* Add one entry */
462 rc = panel_entry_append(panel, "a", 1);
463 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
464
465 /* Now try getting it */
466 entry = panel_first(panel);
467 PCUT_ASSERT_NOT_NULL(entry);
468 PCUT_ASSERT_STR_EQUALS("a", entry->name);
469 PCUT_ASSERT_INT_EQUALS(1, entry->size);
470
471 /* Add another entry */
472 rc = panel_entry_append(panel, "b", 2);
473 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
474
475 /* We should still get the first entry */
476 entry = panel_first(panel);
477 PCUT_ASSERT_NOT_NULL(entry);
478 PCUT_ASSERT_STR_EQUALS("a", entry->name);
479 PCUT_ASSERT_INT_EQUALS(1, entry->size);
480
481 panel_destroy(panel);
482}
483
484/** panel_last() returns valid entry or @c NULL as appropriate */
485PCUT_TEST(last)
486{
487 panel_t *panel;
488 panel_entry_t *entry;
489 errno_t rc;
490
491 rc = panel_create(NULL, true, &panel);
492 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
493
494 entry = panel_last(panel);
495 PCUT_ASSERT_NULL(entry);
496
497 /* Add one entry */
498 rc = panel_entry_append(panel, "a", 1);
499 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
500
501 /* Now try getting it */
502 entry = panel_last(panel);
503 PCUT_ASSERT_NOT_NULL(entry);
504 PCUT_ASSERT_STR_EQUALS("a", entry->name);
505 PCUT_ASSERT_INT_EQUALS(1, entry->size);
506
507 /* Add another entry */
508 rc = panel_entry_append(panel, "b", 2);
509 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
510
511 /* We should get new entry now */
512 entry = panel_last(panel);
513 PCUT_ASSERT_NOT_NULL(entry);
514 PCUT_ASSERT_STR_EQUALS("b", entry->name);
515 PCUT_ASSERT_INT_EQUALS(2, entry->size);
516
517 panel_destroy(panel);
518}
519
520/** panel_next() returns the next entry or @c NULL as appropriate */
521PCUT_TEST(next)
522{
523 panel_t *panel;
524 panel_entry_t *entry;
525 errno_t rc;
526
527 rc = panel_create(NULL, true, &panel);
528 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
529
530 /* Add one entry */
531 rc = panel_entry_append(panel, "a", 1);
532 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
533
534 /* Now try getting its successor */
535 entry = panel_first(panel);
536 PCUT_ASSERT_NOT_NULL(entry);
537
538 entry = panel_next(entry);
539 PCUT_ASSERT_NULL(entry);
540
541 /* Add another entry */
542 rc = panel_entry_append(panel, "b", 2);
543 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
544
545 /* Try getting the successor of first entry again */
546 entry = panel_first(panel);
547 PCUT_ASSERT_NOT_NULL(entry);
548
549 entry = panel_next(entry);
550 PCUT_ASSERT_NOT_NULL(entry);
551 PCUT_ASSERT_STR_EQUALS("b", entry->name);
552 PCUT_ASSERT_INT_EQUALS(2, entry->size);
553
554 panel_destroy(panel);
555}
556
557/** panel_prev() returns the previous entry or @c NULL as appropriate */
558PCUT_TEST(prev)
559{
560 panel_t *panel;
561 panel_entry_t *entry;
562 errno_t rc;
563
564 rc = panel_create(NULL, true, &panel);
565 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
566
567 /* Add one entry */
568 rc = panel_entry_append(panel, "a", 1);
569 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
570
571 /* Now try getting its predecessor */
572 entry = panel_last(panel);
573 PCUT_ASSERT_NOT_NULL(entry);
574
575 entry = panel_prev(entry);
576 PCUT_ASSERT_NULL(entry);
577
578 /* Add another entry */
579 rc = panel_entry_append(panel, "b", 2);
580 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
581
582 /* Try getting the predecessor of the new entry */
583 entry = panel_last(panel);
584 PCUT_ASSERT_NOT_NULL(entry);
585
586 entry = panel_prev(entry);
587 PCUT_ASSERT_NOT_NULL(entry);
588 PCUT_ASSERT_STR_EQUALS("a", entry->name);
589 PCUT_ASSERT_INT_EQUALS(1, entry->size);
590
591 panel_destroy(panel);
592}
593
594/** panel_cursor_move() ... */
595PCUT_TEST(cursor_move)
596{
597}
598
599/** panel_cursor_up() moves cursor one entry up */
600PCUT_TEST(cursor_up)
601{
602 ui_t *ui;
603 ui_window_t *window;
604 ui_wnd_params_t params;
605 panel_t *panel;
606 gfx_rect_t rect;
607 errno_t rc;
608
609 rc = ui_create_disp(NULL, &ui);
610 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
611
612 ui_wnd_params_init(&params);
613 params.caption = "Test";
614
615 rc = ui_window_create(ui, &params, &window);
616 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
617
618 rc = panel_create(window, true, &panel);
619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
620
621 rect.p0.x = 0;
622 rect.p0.y = 0;
623 rect.p1.x = 10;
624 rect.p1.y = 4; // XXX Assuming this makes page size 2
625 panel_set_rect(panel, &rect);
626
627 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
628
629 /* Add tree entries (more than page size, which is 2) */
630 rc = panel_entry_append(panel, "a", 1);
631 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
632
633 rc = panel_entry_append(panel, "b", 2);
634 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
635
636 rc = panel_entry_append(panel, "c", 3);
637 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
638
639 /* Cursor to the last entry and page start to the next-to-last entry */
640 panel->cursor = panel_last(panel);
641 panel->cursor_idx = 2;
642 panel->page = panel_prev(panel->cursor);
643 panel->page_idx = 1;
644
645 /* Move cursor one entry up */
646 panel_cursor_up(panel);
647
648 /* Cursor and page start should now both be at the second entry */
649 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
650 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
651 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
652 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
653 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
654
655 /* Move cursor one entry up. This should scroll up. */
656 panel_cursor_up(panel);
657
658 /* Cursor and page start should now both be at the first entry */
659 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
660 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
661 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
662 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
663 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
664
665 /* Moving further up should do nothing (we are at the top). */
666 panel_cursor_up(panel);
667
668 /* Cursor and page start should still be at the first entry */
669 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
670 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
671 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
672 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
673 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
674
675 panel_destroy(panel);
676 ui_window_destroy(window);
677 ui_destroy(ui);
678}
679
680/** panel_cursor_down() moves cursor one entry down */
681PCUT_TEST(cursor_down)
682{
683 ui_t *ui;
684 ui_window_t *window;
685 ui_wnd_params_t params;
686 panel_t *panel;
687 gfx_rect_t rect;
688 errno_t rc;
689
690 rc = ui_create_disp(NULL, &ui);
691 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
692
693 ui_wnd_params_init(&params);
694 params.caption = "Test";
695
696 rc = ui_window_create(ui, &params, &window);
697 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
698
699 rc = panel_create(window, true, &panel);
700 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
701
702 rect.p0.x = 0;
703 rect.p0.y = 0;
704 rect.p1.x = 10;
705 rect.p1.y = 4; // XXX Assuming this makes page size 2
706 panel_set_rect(panel, &rect);
707
708 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
709
710 /* Add tree entries (more than page size, which is 2) */
711 rc = panel_entry_append(panel, "a", 1);
712 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
713
714 rc = panel_entry_append(panel, "b", 2);
715 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
716
717 rc = panel_entry_append(panel, "c", 3);
718 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
719
720 /* Cursor and page start to the first entry */
721 panel->cursor = panel_first(panel);
722 panel->cursor_idx = 0;
723 panel->page = panel->cursor;
724 panel->page_idx = 0;
725
726 /* Move cursor one entry down */
727 panel_cursor_down(panel);
728
729 /* Cursor should now be at the second entry, page stays the same */
730 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
731 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
732 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
733 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page);
734 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
735
736 /* Move cursor one entry down. This should scroll down. */
737 panel_cursor_down(panel);
738
739 /* Cursor should now be at the third and page at the second entry. */
740 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
741 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
742 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
743 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
744 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
745 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
746
747 /* Moving further down should do nothing (we are at the bottom). */
748 panel_cursor_down(panel);
749
750 /* Cursor should still be at the third and page at the second entry. */
751 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
752 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
753 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
754 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
755 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
756 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
757
758 panel_destroy(panel);
759 ui_window_destroy(window);
760 ui_destroy(ui);
761}
762
763/** panel_cursor_top() moves cursor to the first entry */
764PCUT_TEST(cursor_top)
765{
766 ui_t *ui;
767 ui_window_t *window;
768 ui_wnd_params_t params;
769 panel_t *panel;
770 gfx_rect_t rect;
771 errno_t rc;
772
773 rc = ui_create_disp(NULL, &ui);
774 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
775
776 ui_wnd_params_init(&params);
777 params.caption = "Test";
778
779 rc = ui_window_create(ui, &params, &window);
780 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
781
782 rc = panel_create(window, true, &panel);
783 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
784
785 rect.p0.x = 0;
786 rect.p0.y = 0;
787 rect.p1.x = 10;
788 rect.p1.y = 4; // XXX Assuming this makes page size 2
789 panel_set_rect(panel, &rect);
790
791 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
792
793 /* Add tree entries (more than page size, which is 2) */
794 rc = panel_entry_append(panel, "a", 1);
795 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
796
797 rc = panel_entry_append(panel, "b", 2);
798 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
799
800 rc = panel_entry_append(panel, "c", 3);
801 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
802
803 /* Cursor to the last entry and page start to the next-to-last entry */
804 panel->cursor = panel_last(panel);
805 panel->cursor_idx = 2;
806 panel->page = panel_prev(panel->cursor);
807 panel->page_idx = 1;
808
809 /* Move cursor to the top. This should scroll up. */
810 panel_cursor_top(panel);
811
812 /* Cursor and page start should now both be at the first entry */
813 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
814 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
815 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
816 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
817 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
818
819 panel_destroy(panel);
820 ui_window_destroy(window);
821 ui_destroy(ui);
822}
823
824/** panel_cursor_bottom() moves cursor to the last entry */
825PCUT_TEST(cursor_bottom)
826{
827 ui_t *ui;
828 ui_window_t *window;
829 ui_wnd_params_t params;
830 panel_t *panel;
831 gfx_rect_t rect;
832 errno_t rc;
833
834 rc = ui_create_disp(NULL, &ui);
835 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
836
837 ui_wnd_params_init(&params);
838 params.caption = "Test";
839
840 rc = ui_window_create(ui, &params, &window);
841 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
842
843 rc = panel_create(window, true, &panel);
844 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
845
846 rect.p0.x = 0;
847 rect.p0.y = 0;
848 rect.p1.x = 10;
849 rect.p1.y = 4; // XXX Assuming this makes page size 2
850 panel_set_rect(panel, &rect);
851
852 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
853
854 /* Add tree entries (more than page size, which is 2) */
855 rc = panel_entry_append(panel, "a", 1);
856 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
857
858 rc = panel_entry_append(panel, "b", 2);
859 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
860
861 rc = panel_entry_append(panel, "c", 3);
862 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
863
864 /* Cursor and page start to the first entry */
865 panel->cursor = panel_first(panel);
866 panel->cursor_idx = 0;
867 panel->page = panel->cursor;
868 panel->page_idx = 0;
869
870 /* Move cursor to the bottom. This should scroll down. */
871 panel_cursor_bottom(panel);
872
873 /* Cursor should now be at the third and page at the second entry. */
874 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
875 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
876 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
877 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
878 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
879 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
880
881 panel_destroy(panel);
882 ui_window_destroy(window);
883 ui_destroy(ui);
884}
885
886/** panel_page_up() moves one page up */
887PCUT_TEST(page_up)
888{
889 ui_t *ui;
890 ui_window_t *window;
891 ui_wnd_params_t params;
892 panel_t *panel;
893 gfx_rect_t rect;
894 errno_t rc;
895
896 rc = ui_create_disp(NULL, &ui);
897 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
898
899 ui_wnd_params_init(&params);
900 params.caption = "Test";
901
902 rc = ui_window_create(ui, &params, &window);
903 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
904
905 rc = panel_create(window, true, &panel);
906 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
907
908 rect.p0.x = 0;
909 rect.p0.y = 0;
910 rect.p1.x = 10;
911 rect.p1.y = 4; // XXX Assuming this makes page size 2
912 panel_set_rect(panel, &rect);
913
914 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
915
916 /* Add five entries (2 full pages, one partial) */
917 rc = panel_entry_append(panel, "a", 1);
918 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
919
920 rc = panel_entry_append(panel, "b", 2);
921 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
922
923 rc = panel_entry_append(panel, "c", 3);
924 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
925
926 rc = panel_entry_append(panel, "d", 4);
927 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
928
929 rc = panel_entry_append(panel, "e", 5);
930 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
931
932 /* Cursor to the last entry and page start to the next-to-last entry */
933 panel->cursor = panel_last(panel);
934 panel->cursor_idx = 4;
935 panel->page = panel_prev(panel->cursor);
936 panel->page_idx = 3;
937
938 /* Move one page up */
939 panel_page_up(panel);
940
941 /* Page should now start at second entry and cursor at third */
942 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
943 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
944 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
945 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
946 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
947 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
948
949 /* Move one page up again. */
950 panel_page_up(panel);
951
952 /* Cursor and page start should now both be at the first entry */
953 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
954 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
955 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
956 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
957 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
958
959 /* Moving further up should do nothing (we are at the top). */
960 panel_page_up(panel);
961
962 /* Cursor and page start should still be at the first entry */
963 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
964 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
965 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
966 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
967 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
968
969 panel_destroy(panel);
970 ui_window_destroy(window);
971 ui_destroy(ui);
972}
973
974/** panel_page_up() moves one page down */
975PCUT_TEST(page_down)
976{
977 ui_t *ui;
978 ui_window_t *window;
979 ui_wnd_params_t params;
980 panel_t *panel;
981 gfx_rect_t rect;
982 errno_t rc;
983
984 rc = ui_create_disp(NULL, &ui);
985 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
986
987 ui_wnd_params_init(&params);
988 params.caption = "Test";
989
990 rc = ui_window_create(ui, &params, &window);
991 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
992
993 rc = panel_create(window, true, &panel);
994 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
995
996 rect.p0.x = 0;
997 rect.p0.y = 0;
998 rect.p1.x = 10;
999 rect.p1.y = 4; // XXX Assuming this makes page size 2
1000 panel_set_rect(panel, &rect);
1001
1002 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1003
1004 /* Add five entries (2 full pages, one partial) */
1005 rc = panel_entry_append(panel, "a", 1);
1006 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1007
1008 rc = panel_entry_append(panel, "b", 2);
1009 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1010
1011 rc = panel_entry_append(panel, "c", 3);
1012 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1013
1014 rc = panel_entry_append(panel, "d", 4);
1015 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1016
1017 rc = panel_entry_append(panel, "e", 5);
1018 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1019
1020 /* Cursor and page to the first entry */
1021 panel->cursor = panel_first(panel);
1022 panel->cursor_idx = 0;
1023 panel->page = panel->cursor;
1024 panel->page_idx = 0;
1025
1026 /* Move one page down */
1027 panel_page_down(panel);
1028
1029 /* Page and cursor should point to the third entry */
1030 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1031 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1032 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1033 PCUT_ASSERT_STR_EQUALS("c", panel->page->name);
1034 PCUT_ASSERT_INT_EQUALS(3, panel->page->size);
1035 PCUT_ASSERT_INT_EQUALS(2, panel->page_idx);
1036
1037 /* Move one page down again. */
1038 panel_page_down(panel);
1039
1040 /* Cursor should point to last and page to next-to-last entry */
1041 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1042 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1043 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1044 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1045 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1046 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1047
1048 /* Moving further down should do nothing (we are at the bottom). */
1049 panel_page_down(panel);
1050
1051 /* Cursor should still point to last and page to next-to-last entry */
1052 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1053 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1054 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1055 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1056 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1057 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1058
1059 panel_destroy(panel);
1060 ui_window_destroy(window);
1061 ui_destroy(ui);
1062}
1063
1064PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.