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

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

Move type definitions to separate directory

  • Property mode set to 100644
File size: 24.1 KB
RevLine 
[6aa85c1]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>
[be1d74c1]30#include <io/kbd_event.h>
31#include <io/pos_event.h>
[6aa85c1]32#include <pcut/pcut.h>
[0e80e40]33#include <stdio.h>
[2fb49522]34#include <ui/ui.h>
[0e80e40]35#include <vfs/vfs.h>
[6aa85c1]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
[b36ebb42]48 rc = panel_create(NULL, &panel);
[6aa85c1]49 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
50
51 panel_destroy(panel);
52}
53
[be1d74c1]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, &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
[b36ebb42]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, &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, &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
[be1d74c1]131/** Test panel_kbd_event() */
132PCUT_TEST(kbd_event)
133{
134 panel_t *panel;
135 ui_control_t *control;
136 ui_evclaim_t claimed;
137 kbd_event_t event;
138 errno_t rc;
139
140 rc = panel_create(NULL, &panel);
141 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
142
143 control = panel_ctl(panel);
144 PCUT_ASSERT_NOT_NULL(control);
145
146 event.type = KEY_PRESS;
147 event.key = KC_ENTER;
148 event.mods = 0;
149 event.c = '\0';
150
151 claimed = panel_kbd_event(panel, &event);
152 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
153
154 panel_destroy(panel);
155}
156
[b36ebb42]157/** Test panel_pos_event() */
158PCUT_TEST(pos_event)
159{
160 panel_t *panel;
161 ui_control_t *control;
162 ui_evclaim_t claimed;
163 pos_event_t event;
164 errno_t rc;
165
166 rc = panel_create(NULL, &panel);
167 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
168
169 control = panel_ctl(panel);
170 PCUT_ASSERT_NOT_NULL(control);
171
[be1d74c1]172 event.pos_id = 0;
173 event.type = POS_PRESS;
174 event.btn_num = 1;
175 event.hpos = 0;
176 event.vpos = 0;
177
[b36ebb42]178 claimed = panel_pos_event(panel, &event);
179 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
180
181 panel_destroy(panel);
182}
183
184/** panel_set_rect() sets internal field */
185PCUT_TEST(set_rect)
186{
187 panel_t *panel;
188 ui_control_t *control;
189 gfx_rect_t rect;
190 errno_t rc;
191
192 rc = panel_create(NULL, &panel);
193 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
194
195 control = panel_ctl(panel);
196 PCUT_ASSERT_NOT_NULL(control);
197
198 rect.p0.x = 1;
199 rect.p0.y = 2;
200 rect.p1.x = 3;
201 rect.p1.y = 4;
202
203 panel_set_rect(panel, &rect);
204 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
205 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
206 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
207 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
208
209 panel_destroy(panel);
210}
211
[8c72f533]212/** panel_page_size() returns correct size */
213PCUT_TEST(page_size)
214{
215 panel_t *panel;
216 ui_control_t *control;
217 gfx_rect_t rect;
218 errno_t rc;
219
220 rc = panel_create(NULL, &panel);
221 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
222
223 control = panel_ctl(panel);
224 PCUT_ASSERT_NOT_NULL(control);
225
226 rect.p0.x = 10;
227 rect.p0.y = 20;
228 rect.p1.x = 30;
229 rect.p1.y = 40;
230
231 panel_set_rect(panel, &rect);
232
233 /* NOTE If page size changes, we have problems elsewhere in the tests */
234 PCUT_ASSERT_INT_EQUALS(18, panel_page_size(panel));
235
236 panel_destroy(panel);
237}
238
[61784ed]239/** panel_entry_append() appends new entry */
240PCUT_TEST(entry_append)
241{
242 panel_t *panel;
243 errno_t rc;
244
245 rc = panel_create(NULL, &panel);
246 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
247
248 rc = panel_entry_append(panel, "a", 1);
249 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
250
251 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
252
253 rc = panel_entry_append(panel, "b", 2);
254 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
255
256 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
257
258 panel_destroy(panel);
259}
260
261/** panel_entry_delete() deletes entry */
262PCUT_TEST(entry_delete)
263{
264 panel_t *panel;
265 panel_entry_t *entry;
266 errno_t rc;
267
268 rc = panel_create(NULL, &panel);
269 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
270
271 rc = panel_entry_append(panel, "a", 1);
272 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
273
274 rc = panel_entry_append(panel, "b", 2);
275 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
276
277 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
278
279 entry = panel_first(panel);
280 panel_entry_delete(entry);
281
282 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
283
284 entry = panel_first(panel);
285 panel_entry_delete(entry);
286
287 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
288
289 panel_destroy(panel);
290}
291
[0e80e40]292/** panel_clear_entries() removes all entries from panel */
293PCUT_TEST(clear_entries)
294{
295 panel_t *panel;
296 errno_t rc;
297
298 rc = panel_create(NULL, &panel);
299 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
300
301 rc = panel_entry_append(panel, "a", 1);
302 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
303
304 rc = panel_entry_append(panel, "b", 2);
305 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
306
307 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
308
309 panel_clear_entries(panel);
310 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
311
312 panel_destroy(panel);
313}
314
315/** panel_read_dir() reads the contents of a directory */
316PCUT_TEST(read_dir)
317{
318 panel_t *panel;
319 panel_entry_t *entry;
320 char buf[L_tmpnam];
321 char *fname;
322 char *p;
323 errno_t rc;
324 FILE *f;
325 int rv;
326
327 /* Create name for temporary directory */
328 p = tmpnam(buf);
329 PCUT_ASSERT_NOT_NULL(p);
330
331 /* Create temporary directory */
332 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
333 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
334
335 rv = asprintf(&fname, "%s/%s", p, "a");
336 PCUT_ASSERT_TRUE(rv >= 0);
337
338 f = fopen(fname, "wb");
339 PCUT_ASSERT_NOT_NULL(f);
340
341 rv = fprintf(f, "X");
342 PCUT_ASSERT_TRUE(rv >= 0);
343
344 rv = fclose(f);
345 PCUT_ASSERT_INT_EQUALS(0, rv);
346
347 rc = panel_create(NULL, &panel);
348 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
349
350 rc = panel_read_dir(panel, p);
351 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
352
353 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
354
355 entry = panel_first(panel);
356 PCUT_ASSERT_NOT_NULL(entry);
357 PCUT_ASSERT_STR_EQUALS("a", entry->name);
358 // PCUT_ASSERT_INT_EQUALS(1, entry->size);
359
360 panel_destroy(panel);
361
362 rv = remove(fname);
363 PCUT_ASSERT_INT_EQUALS(0, rv);
364
365 rv = remove(p);
366 PCUT_ASSERT_INT_EQUALS(0, rv);
367 free(fname);
368}
369
[61784ed]370/** panel_first() returns valid entry or @c NULL as appropriate */
371PCUT_TEST(first)
372{
373 panel_t *panel;
374 panel_entry_t *entry;
375 errno_t rc;
376
377 rc = panel_create(NULL, &panel);
378 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
379
380 entry = panel_first(panel);
381 PCUT_ASSERT_NULL(entry);
382
383 /* Add one entry */
384 rc = panel_entry_append(panel, "a", 1);
385 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
386
387 /* Now try getting it */
388 entry = panel_first(panel);
389 PCUT_ASSERT_NOT_NULL(entry);
390 PCUT_ASSERT_STR_EQUALS("a", entry->name);
391 PCUT_ASSERT_INT_EQUALS(1, entry->size);
392
393 /* Add another entry */
394 rc = panel_entry_append(panel, "b", 2);
395 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
396
397 /* We should still get the first entry */
398 entry = panel_first(panel);
399 PCUT_ASSERT_NOT_NULL(entry);
400 PCUT_ASSERT_STR_EQUALS("a", entry->name);
401 PCUT_ASSERT_INT_EQUALS(1, entry->size);
402
403 panel_destroy(panel);
404}
405
[be1d74c1]406/** panel_last() returns valid entry or @c NULL as appropriate */
407PCUT_TEST(last)
408{
409 panel_t *panel;
410 panel_entry_t *entry;
411 errno_t rc;
412
413 rc = panel_create(NULL, &panel);
414 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
415
416 entry = panel_last(panel);
417 PCUT_ASSERT_NULL(entry);
418
419 /* Add one entry */
420 rc = panel_entry_append(panel, "a", 1);
421 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
422
423 /* Now try getting it */
424 entry = panel_last(panel);
425 PCUT_ASSERT_NOT_NULL(entry);
426 PCUT_ASSERT_STR_EQUALS("a", entry->name);
427 PCUT_ASSERT_INT_EQUALS(1, entry->size);
428
429 /* Add another entry */
430 rc = panel_entry_append(panel, "b", 2);
431 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
432
433 /* We should get new entry now */
434 entry = panel_last(panel);
435 PCUT_ASSERT_NOT_NULL(entry);
436 PCUT_ASSERT_STR_EQUALS("b", entry->name);
437 PCUT_ASSERT_INT_EQUALS(2, entry->size);
438
439 panel_destroy(panel);
440}
441
[61784ed]442/** panel_next() returns the next entry or @c NULL as appropriate */
443PCUT_TEST(next)
444{
445 panel_t *panel;
446 panel_entry_t *entry;
447 errno_t rc;
448
449 rc = panel_create(NULL, &panel);
450 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
451
452 /* Add one entry */
453 rc = panel_entry_append(panel, "a", 1);
454 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
455
456 /* Now try getting its successor */
457 entry = panel_first(panel);
458 PCUT_ASSERT_NOT_NULL(entry);
459
460 entry = panel_next(entry);
461 PCUT_ASSERT_NULL(entry);
462
463 /* Add another entry */
464 rc = panel_entry_append(panel, "b", 2);
465 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
466
467 /* Try getting the successor of first entry again */
468 entry = panel_first(panel);
469 PCUT_ASSERT_NOT_NULL(entry);
470
471 entry = panel_next(entry);
472 PCUT_ASSERT_NOT_NULL(entry);
473 PCUT_ASSERT_STR_EQUALS("b", entry->name);
474 PCUT_ASSERT_INT_EQUALS(2, entry->size);
475
476 panel_destroy(panel);
477}
478
[be1d74c1]479/** panel_prev() returns the previous entry or @c NULL as appropriate */
480PCUT_TEST(prev)
481{
482 panel_t *panel;
483 panel_entry_t *entry;
484 errno_t rc;
485
486 rc = panel_create(NULL, &panel);
487 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
488
489 /* Add one entry */
490 rc = panel_entry_append(panel, "a", 1);
491 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
492
493 /* Now try getting its predecessor */
494 entry = panel_last(panel);
495 PCUT_ASSERT_NOT_NULL(entry);
496
497 entry = panel_prev(entry);
498 PCUT_ASSERT_NULL(entry);
499
500 /* Add another entry */
501 rc = panel_entry_append(panel, "b", 2);
502 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
503
504 /* Try getting the predecessor of the new entry */
505 entry = panel_last(panel);
506 PCUT_ASSERT_NOT_NULL(entry);
507
508 entry = panel_prev(entry);
509 PCUT_ASSERT_NOT_NULL(entry);
510 PCUT_ASSERT_STR_EQUALS("a", entry->name);
511 PCUT_ASSERT_INT_EQUALS(1, entry->size);
512
513 panel_destroy(panel);
514}
515
516/** panel_cursor_move() ... */
517PCUT_TEST(cursor_move)
518{
519}
520
521/** panel_cursor_up() moves cursor one entry up */
522PCUT_TEST(cursor_up)
523{
524 ui_t *ui;
525 ui_window_t *window;
526 ui_wnd_params_t params;
527 panel_t *panel;
528 gfx_rect_t rect;
529 errno_t rc;
530
531 rc = ui_create_disp(NULL, &ui);
532 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
533
534 ui_wnd_params_init(&params);
535 params.caption = "Test";
536
537 rc = ui_window_create(ui, &params, &window);
538 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
539
540 rc = panel_create(window, &panel);
541 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
542
543 rect.p0.x = 0;
544 rect.p0.y = 0;
545 rect.p1.x = 10;
546 rect.p1.y = 4; // XXX Assuming this makes page size 2
547 panel_set_rect(panel, &rect);
548
[8c72f533]549 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
550
[be1d74c1]551 /* Add tree entries (more than page size, which is 2) */
552 rc = panel_entry_append(panel, "a", 1);
553 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
554
555 rc = panel_entry_append(panel, "b", 2);
556 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
557
558 rc = panel_entry_append(panel, "c", 3);
559 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
560
561 /* Cursor to the last entry and page start to the next-to-last entry */
562 panel->cursor = panel_last(panel);
563 panel->cursor_idx = 2;
564 panel->page = panel_prev(panel->cursor);
565 panel->page_idx = 1;
566
567 /* Move cursor one entry up */
568 panel_cursor_up(panel);
569
570 /* Cursor and page start should now both be at the second entry */
571 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
572 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
573 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
574 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
575 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
576
577 /* Move cursor one entry up. This should scroll up. */
578 panel_cursor_up(panel);
579
580 /* Cursor and page start should now both be at the first entry */
581 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
582 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
583 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
584 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
585 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
586
587 /* Moving further up should do nothing (we are at the top). */
588 panel_cursor_up(panel);
589
590 /* Cursor and page start should still be at the first entry */
591 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
592 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
593 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
594 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
595 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
596
597 panel_destroy(panel);
598 ui_window_destroy(window);
599 ui_destroy(ui);
600}
601
602/** panel_cursor_down() moves cursor one entry down */
603PCUT_TEST(cursor_down)
604{
605 ui_t *ui;
606 ui_window_t *window;
607 ui_wnd_params_t params;
608 panel_t *panel;
609 gfx_rect_t rect;
610 errno_t rc;
611
612 rc = ui_create_disp(NULL, &ui);
613 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
614
615 ui_wnd_params_init(&params);
616 params.caption = "Test";
617
618 rc = ui_window_create(ui, &params, &window);
619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
620
621 rc = panel_create(window, &panel);
622 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
623
624 rect.p0.x = 0;
625 rect.p0.y = 0;
626 rect.p1.x = 10;
627 rect.p1.y = 4; // XXX Assuming this makes page size 2
628 panel_set_rect(panel, &rect);
629
[8c72f533]630 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
631
[be1d74c1]632 /* Add tree entries (more than page size, which is 2) */
633 rc = panel_entry_append(panel, "a", 1);
634 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
635
636 rc = panel_entry_append(panel, "b", 2);
637 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
638
639 rc = panel_entry_append(panel, "c", 3);
640 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
641
642 /* Cursor and page start to the first entry */
643 panel->cursor = panel_first(panel);
644 panel->cursor_idx = 0;
645 panel->page = panel->cursor;
646 panel->page_idx = 0;
647
648 /* Move cursor one entry down */
649 panel_cursor_down(panel);
650
651 /* Cursor should now be at the second entry, page stays the same */
652 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
653 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
654 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
655 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page);
656 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
657
658 /* Move cursor one entry down. This should scroll down. */
659 panel_cursor_down(panel);
660
661 /* Cursor should now be at the third and page at the second entry. */
662 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
663 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
664 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
665 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
666 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
667 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
668
669 /* Moving further down should do nothing (we are at the bottom). */
670 panel_cursor_down(panel);
671
672 /* Cursor should still be at the third and page at the second entry. */
673 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
674 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
675 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
676 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
677 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
678 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
679
680 panel_destroy(panel);
681 ui_window_destroy(window);
682 ui_destroy(ui);
683}
684
685/** panel_cursor_top() moves cursor to the first entry */
686PCUT_TEST(cursor_top)
687{
688 ui_t *ui;
689 ui_window_t *window;
690 ui_wnd_params_t params;
691 panel_t *panel;
692 gfx_rect_t rect;
693 errno_t rc;
694
695 rc = ui_create_disp(NULL, &ui);
696 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
697
698 ui_wnd_params_init(&params);
699 params.caption = "Test";
700
701 rc = ui_window_create(ui, &params, &window);
702 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
703
704 rc = panel_create(window, &panel);
705 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
706
707 rect.p0.x = 0;
708 rect.p0.y = 0;
709 rect.p1.x = 10;
710 rect.p1.y = 4; // XXX Assuming this makes page size 2
711 panel_set_rect(panel, &rect);
712
[8c72f533]713 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
714
[be1d74c1]715 /* Add tree entries (more than page size, which is 2) */
716 rc = panel_entry_append(panel, "a", 1);
717 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
718
719 rc = panel_entry_append(panel, "b", 2);
720 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
721
722 rc = panel_entry_append(panel, "c", 3);
723 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
724
725 /* Cursor to the last entry and page start to the next-to-last entry */
726 panel->cursor = panel_last(panel);
727 panel->cursor_idx = 2;
728 panel->page = panel_prev(panel->cursor);
729 panel->page_idx = 1;
730
731 /* Move cursor to the top. This should scroll up. */
732 panel_cursor_top(panel);
733
734 /* Cursor and page start should now both be at the first entry */
735 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
736 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
737 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
738 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
739 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
740
741 panel_destroy(panel);
742 ui_window_destroy(window);
743 ui_destroy(ui);
744}
745
746/** panel_cursor_bottom() moves cursor to the last entry */
747PCUT_TEST(cursor_bottom)
748{
749 ui_t *ui;
750 ui_window_t *window;
751 ui_wnd_params_t params;
752 panel_t *panel;
753 gfx_rect_t rect;
754 errno_t rc;
755
756 rc = ui_create_disp(NULL, &ui);
757 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
758
759 ui_wnd_params_init(&params);
760 params.caption = "Test";
761
762 rc = ui_window_create(ui, &params, &window);
763 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
764
765 rc = panel_create(window, &panel);
766 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
767
768 rect.p0.x = 0;
769 rect.p0.y = 0;
770 rect.p1.x = 10;
771 rect.p1.y = 4; // XXX Assuming this makes page size 2
772 panel_set_rect(panel, &rect);
773
[8c72f533]774 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
775
[be1d74c1]776 /* Add tree entries (more than page size, which is 2) */
777 rc = panel_entry_append(panel, "a", 1);
778 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
779
780 rc = panel_entry_append(panel, "b", 2);
781 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
782
783 rc = panel_entry_append(panel, "c", 3);
784 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
785
786 /* Cursor and page start to the first entry */
787 panel->cursor = panel_first(panel);
788 panel->cursor_idx = 0;
789 panel->page = panel->cursor;
790 panel->page_idx = 0;
791
792 /* Move cursor to the bottom. This should scroll down. */
793 panel_cursor_bottom(panel);
794
795 /* Cursor should now be at the third and page at the second entry. */
796 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
797 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
798 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
799 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
800 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
801 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
802
803 panel_destroy(panel);
804 ui_window_destroy(window);
805 ui_destroy(ui);
806}
807
[8c72f533]808/** panel_page_up() moves one page up */
809PCUT_TEST(page_up)
810{
811 ui_t *ui;
812 ui_window_t *window;
813 ui_wnd_params_t params;
814 panel_t *panel;
815 gfx_rect_t rect;
816 errno_t rc;
817
818 rc = ui_create_disp(NULL, &ui);
819 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
820
821 ui_wnd_params_init(&params);
822 params.caption = "Test";
823
824 rc = ui_window_create(ui, &params, &window);
825 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
826
827 rc = panel_create(window, &panel);
828 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
829
830 rect.p0.x = 0;
831 rect.p0.y = 0;
832 rect.p1.x = 10;
833 rect.p1.y = 4; // XXX Assuming this makes page size 2
834 panel_set_rect(panel, &rect);
835
836 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
837
838 /* Add five entries (2 full pages, one partial) */
839 rc = panel_entry_append(panel, "a", 1);
840 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
841
842 rc = panel_entry_append(panel, "b", 2);
843 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
844
845 rc = panel_entry_append(panel, "c", 3);
846 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
847
848 rc = panel_entry_append(panel, "d", 4);
849 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
850
851 rc = panel_entry_append(panel, "e", 5);
852 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
853
854 /* Cursor to the last entry and page start to the next-to-last entry */
855 panel->cursor = panel_last(panel);
856 panel->cursor_idx = 4;
857 panel->page = panel_prev(panel->cursor);
858 panel->page_idx = 3;
859
860 /* Move one page up */
861 panel_page_up(panel);
862
863 /* Page should now start at second entry and cursor at third */
864 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
865 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
866 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
867 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
868 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
869 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
870
871 /* Move one page up again. */
872 panel_page_up(panel);
873
874 /* Cursor and page start should now both be at the first entry */
875 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
876 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
877 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
878 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
879 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
880
881 /* Moving further up should do nothing (we are at the top). */
882 panel_page_up(panel);
883
884 /* Cursor and page start should still be at the first entry */
885 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
886 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
887 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
888 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
889 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
890
891 panel_destroy(panel);
892 ui_window_destroy(window);
893 ui_destroy(ui);
894}
895
896/** panel_page_up() moves one page down */
897PCUT_TEST(page_down)
898{
899 ui_t *ui;
900 ui_window_t *window;
901 ui_wnd_params_t params;
902 panel_t *panel;
903 gfx_rect_t rect;
904 errno_t rc;
905
906 rc = ui_create_disp(NULL, &ui);
907 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
908
909 ui_wnd_params_init(&params);
910 params.caption = "Test";
911
912 rc = ui_window_create(ui, &params, &window);
913 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
914
915 rc = panel_create(window, &panel);
916 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
917
918 rect.p0.x = 0;
919 rect.p0.y = 0;
920 rect.p1.x = 10;
921 rect.p1.y = 4; // XXX Assuming this makes page size 2
922 panel_set_rect(panel, &rect);
923
924 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
925
926 /* Add five entries (2 full pages, one partial) */
927 rc = panel_entry_append(panel, "a", 1);
928 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
929
930 rc = panel_entry_append(panel, "b", 2);
931 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
932
933 rc = panel_entry_append(panel, "c", 3);
934 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
935
936 rc = panel_entry_append(panel, "d", 4);
937 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
938
939 rc = panel_entry_append(panel, "e", 5);
940 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
941
942 /* Cursor and page to the first entry */
943 panel->cursor = panel_first(panel);
944 panel->cursor_idx = 0;
945 panel->page = panel->cursor;
946 panel->page_idx = 0;
947
948 /* Move one page down */
949 panel_page_down(panel);
950
951 /* Page and cursor should point to the third entry */
952 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
953 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
954 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
955 PCUT_ASSERT_STR_EQUALS("c", panel->page->name);
956 PCUT_ASSERT_INT_EQUALS(3, panel->page->size);
957 PCUT_ASSERT_INT_EQUALS(2, panel->page_idx);
958
959 /* Move one page down again. */
960 panel_page_down(panel);
961
962 /* Cursor should point to last and page to next-to-last entry */
963 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
964 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
965 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
966 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
967 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
968 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
969
970 /* Moving further down should do nothing (we are at the bottom). */
971 panel_page_down(panel);
972
973 /* Cursor should still point to last and page to next-to-last entry */
974 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
975 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
976 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
977 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
978 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
979 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
980
981 panel_destroy(panel);
982 ui_window_destroy(window);
983 ui_destroy(ui);
984}
985
[6aa85c1]986PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.