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

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

Cursor movement (up, down, to top, to bottom)

  • Property mode set to 100644
File size: 18.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 <vfs/vfs.h>
35#include "../panel.h"
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(panel);
40
41/** Create and destroy panel. */
42PCUT_TEST(create_destroy)
43{
44 panel_t *panel;
45 errno_t rc;
46
47 rc = panel_create(NULL, &panel);
48 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
49
50 panel_destroy(panel);
51}
52
53/** Test panel_entry_paint() */
54PCUT_TEST(entry_paint)
55{
56 ui_t *ui;
57 ui_window_t *window;
58 ui_wnd_params_t params;
59 panel_t *panel;
60 errno_t rc;
61
62 rc = ui_create_disp(NULL, &ui);
63 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
64
65 ui_wnd_params_init(&params);
66 params.caption = "Test";
67
68 rc = ui_window_create(ui, &params, &window);
69 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
70
71 rc = panel_create(window, &panel);
72 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
73
74 rc = panel_entry_append(panel, "a", 1);
75 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
76
77 rc = panel_entry_paint(panel_first(panel), 0);
78 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
79
80 panel_destroy(panel);
81 ui_window_destroy(window);
82 ui_destroy(ui);
83}
84
85/** Test panel_paint() */
86PCUT_TEST(paint)
87{
88 ui_t *ui;
89 ui_window_t *window;
90 ui_wnd_params_t params;
91 panel_t *panel;
92 errno_t rc;
93
94 rc = ui_create_disp(NULL, &ui);
95 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
96
97 ui_wnd_params_init(&params);
98 params.caption = "Test";
99
100 rc = ui_window_create(ui, &params, &window);
101 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
102
103 rc = panel_create(window, &panel);
104 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
105
106 rc = panel_paint(panel);
107 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
108
109 panel_destroy(panel);
110 ui_window_destroy(window);
111 ui_destroy(ui);
112}
113
114/** panel_ctl() returns a valid UI control */
115PCUT_TEST(ctl)
116{
117 panel_t *panel;
118 ui_control_t *control;
119 errno_t rc;
120
121 rc = panel_create(NULL, &panel);
122 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
123
124 control = panel_ctl(panel);
125 PCUT_ASSERT_NOT_NULL(control);
126
127 panel_destroy(panel);
128}
129
130/** Test panel_kbd_event() */
131PCUT_TEST(kbd_event)
132{
133 panel_t *panel;
134 ui_control_t *control;
135 ui_evclaim_t claimed;
136 kbd_event_t event;
137 errno_t rc;
138
139 rc = panel_create(NULL, &panel);
140 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
141
142 control = panel_ctl(panel);
143 PCUT_ASSERT_NOT_NULL(control);
144
145 event.type = KEY_PRESS;
146 event.key = KC_ENTER;
147 event.mods = 0;
148 event.c = '\0';
149
150 claimed = panel_kbd_event(panel, &event);
151 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
152
153 panel_destroy(panel);
154}
155
156/** Test panel_pos_event() */
157PCUT_TEST(pos_event)
158{
159 panel_t *panel;
160 ui_control_t *control;
161 ui_evclaim_t claimed;
162 pos_event_t event;
163 errno_t rc;
164
165 rc = panel_create(NULL, &panel);
166 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
167
168 control = panel_ctl(panel);
169 PCUT_ASSERT_NOT_NULL(control);
170
171 event.pos_id = 0;
172 event.type = POS_PRESS;
173 event.btn_num = 1;
174 event.hpos = 0;
175 event.vpos = 0;
176
177 claimed = panel_pos_event(panel, &event);
178 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
179
180 panel_destroy(panel);
181}
182
183/** panel_set_rect() sets internal field */
184PCUT_TEST(set_rect)
185{
186 panel_t *panel;
187 ui_control_t *control;
188 gfx_rect_t rect;
189 errno_t rc;
190
191 rc = panel_create(NULL, &panel);
192 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
193
194 control = panel_ctl(panel);
195 PCUT_ASSERT_NOT_NULL(control);
196
197 rect.p0.x = 1;
198 rect.p0.y = 2;
199 rect.p1.x = 3;
200 rect.p1.y = 4;
201
202 panel_set_rect(panel, &rect);
203 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
204 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
205 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
206 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
207
208 panel_destroy(panel);
209}
210
211/** panel_entry_append() appends new entry */
212PCUT_TEST(entry_append)
213{
214 panel_t *panel;
215 errno_t rc;
216
217 rc = panel_create(NULL, &panel);
218 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
219
220 rc = panel_entry_append(panel, "a", 1);
221 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
222
223 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
224
225 rc = panel_entry_append(panel, "b", 2);
226 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
227
228 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
229
230 panel_destroy(panel);
231}
232
233/** panel_entry_delete() deletes entry */
234PCUT_TEST(entry_delete)
235{
236 panel_t *panel;
237 panel_entry_t *entry;
238 errno_t rc;
239
240 rc = panel_create(NULL, &panel);
241 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
242
243 rc = panel_entry_append(panel, "a", 1);
244 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
245
246 rc = panel_entry_append(panel, "b", 2);
247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
248
249 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
250
251 entry = panel_first(panel);
252 panel_entry_delete(entry);
253
254 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
255
256 entry = panel_first(panel);
257 panel_entry_delete(entry);
258
259 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
260
261 panel_destroy(panel);
262}
263
264/** panel_clear_entries() removes all entries from panel */
265PCUT_TEST(clear_entries)
266{
267 panel_t *panel;
268 errno_t rc;
269
270 rc = panel_create(NULL, &panel);
271 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
272
273 rc = panel_entry_append(panel, "a", 1);
274 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
275
276 rc = panel_entry_append(panel, "b", 2);
277 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
278
279 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
280
281 panel_clear_entries(panel);
282 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
283
284 panel_destroy(panel);
285}
286
287/** panel_read_dir() reads the contents of a directory */
288PCUT_TEST(read_dir)
289{
290 panel_t *panel;
291 panel_entry_t *entry;
292 char buf[L_tmpnam];
293 char *fname;
294 char *p;
295 errno_t rc;
296 FILE *f;
297 int rv;
298
299 /* Create name for temporary directory */
300 p = tmpnam(buf);
301 PCUT_ASSERT_NOT_NULL(p);
302
303 /* Create temporary directory */
304 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
305 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
306
307 rv = asprintf(&fname, "%s/%s", p, "a");
308 PCUT_ASSERT_TRUE(rv >= 0);
309
310 f = fopen(fname, "wb");
311 PCUT_ASSERT_NOT_NULL(f);
312
313 rv = fprintf(f, "X");
314 PCUT_ASSERT_TRUE(rv >= 0);
315
316 rv = fclose(f);
317 PCUT_ASSERT_INT_EQUALS(0, rv);
318
319 rc = panel_create(NULL, &panel);
320 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
321
322 rc = panel_read_dir(panel, p);
323 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
324
325 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
326
327 entry = panel_first(panel);
328 PCUT_ASSERT_NOT_NULL(entry);
329 PCUT_ASSERT_STR_EQUALS("a", entry->name);
330 // PCUT_ASSERT_INT_EQUALS(1, entry->size);
331
332 panel_destroy(panel);
333
334 rv = remove(fname);
335 PCUT_ASSERT_INT_EQUALS(0, rv);
336
337 rv = remove(p);
338 PCUT_ASSERT_INT_EQUALS(0, rv);
339 free(fname);
340}
341
342/** panel_first() returns valid entry or @c NULL as appropriate */
343PCUT_TEST(first)
344{
345 panel_t *panel;
346 panel_entry_t *entry;
347 errno_t rc;
348
349 rc = panel_create(NULL, &panel);
350 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
351
352 entry = panel_first(panel);
353 PCUT_ASSERT_NULL(entry);
354
355 /* Add one entry */
356 rc = panel_entry_append(panel, "a", 1);
357 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
358
359 /* Now try getting it */
360 entry = panel_first(panel);
361 PCUT_ASSERT_NOT_NULL(entry);
362 PCUT_ASSERT_STR_EQUALS("a", entry->name);
363 PCUT_ASSERT_INT_EQUALS(1, entry->size);
364
365 /* Add another entry */
366 rc = panel_entry_append(panel, "b", 2);
367 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
368
369 /* We should still get the first entry */
370 entry = panel_first(panel);
371 PCUT_ASSERT_NOT_NULL(entry);
372 PCUT_ASSERT_STR_EQUALS("a", entry->name);
373 PCUT_ASSERT_INT_EQUALS(1, entry->size);
374
375 panel_destroy(panel);
376}
377
378/** panel_last() returns valid entry or @c NULL as appropriate */
379PCUT_TEST(last)
380{
381 panel_t *panel;
382 panel_entry_t *entry;
383 errno_t rc;
384
385 rc = panel_create(NULL, &panel);
386 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
387
388 entry = panel_last(panel);
389 PCUT_ASSERT_NULL(entry);
390
391 /* Add one entry */
392 rc = panel_entry_append(panel, "a", 1);
393 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
394
395 /* Now try getting it */
396 entry = panel_last(panel);
397 PCUT_ASSERT_NOT_NULL(entry);
398 PCUT_ASSERT_STR_EQUALS("a", entry->name);
399 PCUT_ASSERT_INT_EQUALS(1, entry->size);
400
401 /* Add another entry */
402 rc = panel_entry_append(panel, "b", 2);
403 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
404
405 /* We should get new entry now */
406 entry = panel_last(panel);
407 PCUT_ASSERT_NOT_NULL(entry);
408 PCUT_ASSERT_STR_EQUALS("b", entry->name);
409 PCUT_ASSERT_INT_EQUALS(2, entry->size);
410
411 panel_destroy(panel);
412}
413
414/** panel_next() returns the next entry or @c NULL as appropriate */
415PCUT_TEST(next)
416{
417 panel_t *panel;
418 panel_entry_t *entry;
419 errno_t rc;
420
421 rc = panel_create(NULL, &panel);
422 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
423
424 /* Add one entry */
425 rc = panel_entry_append(panel, "a", 1);
426 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
427
428 /* Now try getting its successor */
429 entry = panel_first(panel);
430 PCUT_ASSERT_NOT_NULL(entry);
431
432 entry = panel_next(entry);
433 PCUT_ASSERT_NULL(entry);
434
435 /* Add another entry */
436 rc = panel_entry_append(panel, "b", 2);
437 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
438
439 /* Try getting the successor of first entry again */
440 entry = panel_first(panel);
441 PCUT_ASSERT_NOT_NULL(entry);
442
443 entry = panel_next(entry);
444 PCUT_ASSERT_NOT_NULL(entry);
445 PCUT_ASSERT_STR_EQUALS("b", entry->name);
446 PCUT_ASSERT_INT_EQUALS(2, entry->size);
447
448 panel_destroy(panel);
449}
450
451/** panel_prev() returns the previous entry or @c NULL as appropriate */
452PCUT_TEST(prev)
453{
454 panel_t *panel;
455 panel_entry_t *entry;
456 errno_t rc;
457
458 rc = panel_create(NULL, &panel);
459 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
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 its predecessor */
466 entry = panel_last(panel);
467 PCUT_ASSERT_NOT_NULL(entry);
468
469 entry = panel_prev(entry);
470 PCUT_ASSERT_NULL(entry);
471
472 /* Add another entry */
473 rc = panel_entry_append(panel, "b", 2);
474 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
475
476 /* Try getting the predecessor of the new entry */
477 entry = panel_last(panel);
478 PCUT_ASSERT_NOT_NULL(entry);
479
480 entry = panel_prev(entry);
481 PCUT_ASSERT_NOT_NULL(entry);
482 PCUT_ASSERT_STR_EQUALS("a", entry->name);
483 PCUT_ASSERT_INT_EQUALS(1, entry->size);
484
485 panel_destroy(panel);
486}
487
488/** panel_cursor_move() ... */
489PCUT_TEST(cursor_move)
490{
491}
492
493/** panel_cursor_up() moves cursor one entry up */
494PCUT_TEST(cursor_up)
495{
496 ui_t *ui;
497 ui_window_t *window;
498 ui_wnd_params_t params;
499 panel_t *panel;
500 gfx_rect_t rect;
501 errno_t rc;
502
503 rc = ui_create_disp(NULL, &ui);
504 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
505
506 ui_wnd_params_init(&params);
507 params.caption = "Test";
508
509 rc = ui_window_create(ui, &params, &window);
510 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
511
512 rc = panel_create(window, &panel);
513 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
514
515 rect.p0.x = 0;
516 rect.p0.y = 0;
517 rect.p1.x = 10;
518 rect.p1.y = 4; // XXX Assuming this makes page size 2
519 panel_set_rect(panel, &rect);
520
521 /* Add tree entries (more than page size, which is 2) */
522 rc = panel_entry_append(panel, "a", 1);
523 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
524
525 rc = panel_entry_append(panel, "b", 2);
526 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
527
528 rc = panel_entry_append(panel, "c", 3);
529 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
530
531 /* Cursor to the last entry and page start to the next-to-last entry */
532 panel->cursor = panel_last(panel);
533 panel->cursor_idx = 2;
534 panel->page = panel_prev(panel->cursor);
535 panel->page_idx = 1;
536
537 /* Move cursor one entry up */
538 panel_cursor_up(panel);
539
540 /* Cursor and page start should now both be at the second entry */
541 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
542 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
543 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
544 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
545 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
546
547 /* Move cursor one entry up. This should scroll up. */
548 panel_cursor_up(panel);
549
550 /* Cursor and page start should now both be at the first entry */
551 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
552 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
553 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
554 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
555 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
556
557 /* Moving further up should do nothing (we are at the top). */
558 panel_cursor_up(panel);
559
560 /* Cursor and page start should still be at the first entry */
561 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
562 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
563 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
564 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
565 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
566
567 panel_destroy(panel);
568 ui_window_destroy(window);
569 ui_destroy(ui);
570}
571
572/** panel_cursor_down() moves cursor one entry down */
573PCUT_TEST(cursor_down)
574{
575 ui_t *ui;
576 ui_window_t *window;
577 ui_wnd_params_t params;
578 panel_t *panel;
579 gfx_rect_t rect;
580 errno_t rc;
581
582 rc = ui_create_disp(NULL, &ui);
583 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
584
585 ui_wnd_params_init(&params);
586 params.caption = "Test";
587
588 rc = ui_window_create(ui, &params, &window);
589 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
590
591 rc = panel_create(window, &panel);
592 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
593
594 rect.p0.x = 0;
595 rect.p0.y = 0;
596 rect.p1.x = 10;
597 rect.p1.y = 4; // XXX Assuming this makes page size 2
598 panel_set_rect(panel, &rect);
599
600 /* Add tree entries (more than page size, which is 2) */
601 rc = panel_entry_append(panel, "a", 1);
602 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
603
604 rc = panel_entry_append(panel, "b", 2);
605 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
606
607 rc = panel_entry_append(panel, "c", 3);
608 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
609
610 /* Cursor and page start to the first entry */
611 panel->cursor = panel_first(panel);
612 panel->cursor_idx = 0;
613 panel->page = panel->cursor;
614 panel->page_idx = 0;
615
616 /* Move cursor one entry down */
617 panel_cursor_down(panel);
618
619 /* Cursor should now be at the second entry, page stays the same */
620 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
621 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
622 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
623 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page);
624 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
625
626 /* Move cursor one entry down. This should scroll down. */
627 panel_cursor_down(panel);
628
629 /* Cursor should now be at the third and page at the second entry. */
630 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
631 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
632 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
633 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
634 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
635 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
636
637 /* Moving further down should do nothing (we are at the bottom). */
638 panel_cursor_down(panel);
639
640 /* Cursor should still be at the third and page at the second entry. */
641 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
642 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
643 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
644 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
645 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
646 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
647
648 panel_destroy(panel);
649 ui_window_destroy(window);
650 ui_destroy(ui);
651}
652
653/** panel_cursor_top() moves cursor to the first entry */
654PCUT_TEST(cursor_top)
655{
656 ui_t *ui;
657 ui_window_t *window;
658 ui_wnd_params_t params;
659 panel_t *panel;
660 gfx_rect_t rect;
661 errno_t rc;
662
663 rc = ui_create_disp(NULL, &ui);
664 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
665
666 ui_wnd_params_init(&params);
667 params.caption = "Test";
668
669 rc = ui_window_create(ui, &params, &window);
670 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
671
672 rc = panel_create(window, &panel);
673 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
674
675 rect.p0.x = 0;
676 rect.p0.y = 0;
677 rect.p1.x = 10;
678 rect.p1.y = 4; // XXX Assuming this makes page size 2
679 panel_set_rect(panel, &rect);
680
681 /* Add tree entries (more than page size, which is 2) */
682 rc = panel_entry_append(panel, "a", 1);
683 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
684
685 rc = panel_entry_append(panel, "b", 2);
686 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
687
688 rc = panel_entry_append(panel, "c", 3);
689 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
690
691 /* Cursor to the last entry and page start to the next-to-last entry */
692 panel->cursor = panel_last(panel);
693 panel->cursor_idx = 2;
694 panel->page = panel_prev(panel->cursor);
695 panel->page_idx = 1;
696
697 /* Move cursor to the top. This should scroll up. */
698 panel_cursor_top(panel);
699
700 /* Cursor and page start should now both be at the first entry */
701 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
702 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
703 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
704 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
705 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
706
707 panel_destroy(panel);
708 ui_window_destroy(window);
709 ui_destroy(ui);
710}
711
712/** panel_cursor_bottom() moves cursor to the last entry */
713PCUT_TEST(cursor_bottom)
714{
715 ui_t *ui;
716 ui_window_t *window;
717 ui_wnd_params_t params;
718 panel_t *panel;
719 gfx_rect_t rect;
720 errno_t rc;
721
722 rc = ui_create_disp(NULL, &ui);
723 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
724
725 ui_wnd_params_init(&params);
726 params.caption = "Test";
727
728 rc = ui_window_create(ui, &params, &window);
729 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
730
731 rc = panel_create(window, &panel);
732 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
733
734 rect.p0.x = 0;
735 rect.p0.y = 0;
736 rect.p1.x = 10;
737 rect.p1.y = 4; // XXX Assuming this makes page size 2
738 panel_set_rect(panel, &rect);
739
740 /* Add tree entries (more than page size, which is 2) */
741 rc = panel_entry_append(panel, "a", 1);
742 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
743
744 rc = panel_entry_append(panel, "b", 2);
745 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
746
747 rc = panel_entry_append(panel, "c", 3);
748 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
749
750 /* Cursor and page start to the first entry */
751 panel->cursor = panel_first(panel);
752 panel->cursor_idx = 0;
753 panel->page = panel->cursor;
754 panel->page_idx = 0;
755
756 /* Move cursor to the bottom. This should scroll down. */
757 panel_cursor_bottom(panel);
758
759 /* Cursor should now be at the third and page at the second entry. */
760 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
761 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
762 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
763 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
764 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
765 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
766
767 panel_destroy(panel);
768 ui_window_destroy(window);
769 ui_destroy(ui);
770}
771
772PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.