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

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

Page up/down by mouse click on panel border

  • Property mode set to 100644
File size: 36.9 KB
RevLine 
[68b9e540]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>
[af2ea83]30#include <io/kbd_event.h>
31#include <io/pos_event.h>
[68b9e540]32#include <pcut/pcut.h>
[8e5f39d]33#include <stdio.h>
[b9d689b]34#include <ui/ui.h>
[8e5f39d]35#include <vfs/vfs.h>
[68b9e540]36#include "../panel.h"
37
38PCUT_INIT;
39
40PCUT_TEST_SUITE(panel);
41
[966a27d3]42/** Test response */
43typedef struct {
44 bool activate_req;
45 panel_t *activate_req_panel;
46} test_resp_t;
47
48static void test_panel_activate_req(void *, panel_t *);
49
50static panel_cb_t test_cb = {
51 .activate_req = test_panel_activate_req
52};
53
[68b9e540]54/** Create and destroy panel. */
55PCUT_TEST(create_destroy)
56{
57 panel_t *panel;
58 errno_t rc;
59
[fe5c7a1]60 rc = panel_create(NULL, true, &panel);
[68b9e540]61 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
62
63 panel_destroy(panel);
64}
65
[966a27d3]66/** panel_set_cb() sets callback */
67PCUT_TEST(set_cb)
68{
69 panel_t *panel;
70 errno_t rc;
71 test_resp_t resp;
72
73 rc = panel_create(NULL, true, &panel);
74 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
75
76 panel_set_cb(panel, &test_cb, &resp);
77 PCUT_ASSERT_EQUALS(&test_cb, panel->cb);
78 PCUT_ASSERT_EQUALS(&resp, panel->cb_arg);
79
80 panel_destroy(panel);
81}
82
[af2ea83]83/** Test panel_entry_paint() */
84PCUT_TEST(entry_paint)
85{
86 ui_t *ui;
87 ui_window_t *window;
88 ui_wnd_params_t params;
89 panel_t *panel;
[75357c5]90 panel_entry_attr_t attr;
[af2ea83]91 errno_t rc;
92
93 rc = ui_create_disp(NULL, &ui);
94 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
95
96 ui_wnd_params_init(&params);
97 params.caption = "Test";
98
99 rc = ui_window_create(ui, &params, &window);
100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
101
[fe5c7a1]102 rc = panel_create(window, true, &panel);
[af2ea83]103 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
104
[75357c5]105 panel_entry_attr_init(&attr);
106 attr.name = "a";
107 attr.size = 1;
108
109 rc = panel_entry_append(panel, &attr);
[af2ea83]110 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
111
112 rc = panel_entry_paint(panel_first(panel), 0);
113 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
114
115 panel_destroy(panel);
116 ui_window_destroy(window);
117 ui_destroy(ui);
118}
119
[0e5ed803]120/** Test panel_paint() */
121PCUT_TEST(paint)
122{
123 ui_t *ui;
124 ui_window_t *window;
125 ui_wnd_params_t params;
126 panel_t *panel;
127 errno_t rc;
128
129 rc = ui_create_disp(NULL, &ui);
130 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
131
132 ui_wnd_params_init(&params);
133 params.caption = "Test";
134
135 rc = ui_window_create(ui, &params, &window);
136 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
137
[fe5c7a1]138 rc = panel_create(window, true, &panel);
[0e5ed803]139 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
140
141 rc = panel_paint(panel);
142 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
143
144 panel_destroy(panel);
145 ui_window_destroy(window);
146 ui_destroy(ui);
147}
148
149/** panel_ctl() returns a valid UI control */
150PCUT_TEST(ctl)
151{
152 panel_t *panel;
153 ui_control_t *control;
154 errno_t rc;
155
[fe5c7a1]156 rc = panel_create(NULL, true, &panel);
[0e5ed803]157 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
158
159 control = panel_ctl(panel);
160 PCUT_ASSERT_NOT_NULL(control);
161
162 panel_destroy(panel);
163}
164
[af2ea83]165/** Test panel_kbd_event() */
166PCUT_TEST(kbd_event)
167{
168 panel_t *panel;
169 ui_evclaim_t claimed;
170 kbd_event_t event;
171 errno_t rc;
172
[fe5c7a1]173 /* Active panel should claim events */
174
175 rc = panel_create(NULL, true, &panel);
[af2ea83]176 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
177
[fe5c7a1]178 event.type = KEY_PRESS;
[0e125698]179 event.key = KC_ESCAPE;
[fe5c7a1]180 event.mods = 0;
181 event.c = '\0';
182
183 claimed = panel_kbd_event(panel, &event);
184 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
185
186 panel_destroy(panel);
187
188 /* Inactive panel should not claim events */
189
190 rc = panel_create(NULL, false, &panel);
191 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
[af2ea83]192
193 event.type = KEY_PRESS;
[0e125698]194 event.key = KC_ESCAPE;
[af2ea83]195 event.mods = 0;
196 event.c = '\0';
197
198 claimed = panel_kbd_event(panel, &event);
199 PCUT_ASSERT_EQUALS(ui_unclaimed, claimed);
200
201 panel_destroy(panel);
202}
203
[0e5ed803]204/** Test panel_pos_event() */
205PCUT_TEST(pos_event)
206{
[8922b76]207 ui_t *ui;
208 ui_window_t *window;
209 ui_wnd_params_t params;
[0e5ed803]210 panel_t *panel;
211 ui_evclaim_t claimed;
212 pos_event_t event;
[8922b76]213 gfx_rect_t rect;
214 panel_entry_attr_t attr;
[0e5ed803]215 errno_t rc;
216
[8922b76]217 rc = ui_create_disp(NULL, &ui);
218 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
219
220 ui_wnd_params_init(&params);
221 params.caption = "Test";
222
223 rc = ui_window_create(ui, &params, &window);
224 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
225
226 rc = panel_create(window, true, &panel);
[0e5ed803]227 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
228
[8922b76]229 rect.p0.x = 0;
230 rect.p0.y = 0;
231 rect.p1.x = 10;
232 rect.p1.y = 10;
233 panel_set_rect(panel, &rect);
234
235 panel_entry_attr_init(&attr);
236 attr.name = "a";
237 attr.size = 1;
238 rc = panel_entry_append(panel, &attr);
239 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
240
241 attr.name = "b";
242 attr.size = 2;
243 rc = panel_entry_append(panel, &attr);
244 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
245
246 attr.name = "c";
247 attr.size = 3;
248 rc = panel_entry_append(panel, &attr);
249 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
250
251 panel->cursor = panel_first(panel);
252 panel->cursor_idx = 0;
253 panel->page = panel_first(panel);
254 panel->page_idx = 0;
255
[af2ea83]256 event.pos_id = 0;
257 event.type = POS_PRESS;
258 event.btn_num = 1;
[8922b76]259
260 /* Clicking on the middle entry should select it */
261 event.hpos = 1;
262 event.vpos = 2;
[af2ea83]263
[0e5ed803]264 claimed = panel_pos_event(panel, &event);
[8922b76]265 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
266
267 PCUT_ASSERT_NOT_NULL(panel->cursor);
268 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
269 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
270
271 /* Clicking below the last entry should select it */
272 event.hpos = 1;
273 event.vpos = 4;
274 claimed = panel_pos_event(panel, &event);
275 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
276
277 PCUT_ASSERT_NOT_NULL(panel->cursor);
278 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
279 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
[0e5ed803]280
[5e221a77]281 /* Clicking on the top edge should do a page-up */
282 event.hpos = 1;
283 event.vpos = 0;
284 claimed = panel_pos_event(panel, &event);
285 PCUT_ASSERT_EQUALS(ui_claimed, claimed);
286
287 PCUT_ASSERT_NOT_NULL(panel->cursor);
288 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
289 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
290
[0e5ed803]291 panel_destroy(panel);
[8922b76]292 ui_window_destroy(window);
293 ui_destroy(ui);
[0e5ed803]294}
295
296/** panel_set_rect() sets internal field */
297PCUT_TEST(set_rect)
298{
299 panel_t *panel;
300 gfx_rect_t rect;
301 errno_t rc;
302
[fe5c7a1]303 rc = panel_create(NULL, true, &panel);
[0e5ed803]304 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
305
306 rect.p0.x = 1;
307 rect.p0.y = 2;
308 rect.p1.x = 3;
309 rect.p1.y = 4;
310
311 panel_set_rect(panel, &rect);
312 PCUT_ASSERT_INT_EQUALS(rect.p0.x, panel->rect.p0.x);
313 PCUT_ASSERT_INT_EQUALS(rect.p0.y, panel->rect.p0.y);
314 PCUT_ASSERT_INT_EQUALS(rect.p1.x, panel->rect.p1.x);
315 PCUT_ASSERT_INT_EQUALS(rect.p1.y, panel->rect.p1.y);
316
317 panel_destroy(panel);
318}
319
[3e6c51f]320/** panel_page_size() returns correct size */
321PCUT_TEST(page_size)
322{
323 panel_t *panel;
324 gfx_rect_t rect;
325 errno_t rc;
326
[fe5c7a1]327 rc = panel_create(NULL, true, &panel);
[3e6c51f]328 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
329
330 rect.p0.x = 10;
331 rect.p0.y = 20;
332 rect.p1.x = 30;
333 rect.p1.y = 40;
334
335 panel_set_rect(panel, &rect);
336
337 /* NOTE If page size changes, we have problems elsewhere in the tests */
338 PCUT_ASSERT_INT_EQUALS(18, panel_page_size(panel));
339
340 panel_destroy(panel);
341}
342
[fe5c7a1]343/** panel_is_active() returns panel activity state */
344PCUT_TEST(is_active)
345{
346 panel_t *panel;
347 errno_t rc;
348
349 rc = panel_create(NULL, true, &panel);
350 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
351 PCUT_ASSERT_TRUE(panel_is_active(panel));
352 panel_destroy(panel);
353
354 rc = panel_create(NULL, false, &panel);
355 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
356 PCUT_ASSERT_FALSE(panel_is_active(panel));
357 panel_destroy(panel);
358}
359
360/** panel_activate() activates panel */
361PCUT_TEST(activate)
362{
363 ui_t *ui;
364 ui_window_t *window;
365 ui_wnd_params_t params;
366 panel_t *panel;
367 errno_t rc;
368
369 rc = ui_create_disp(NULL, &ui);
370 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
371
372 ui_wnd_params_init(&params);
373 params.caption = "Test";
374
375 rc = ui_window_create(ui, &params, &window);
376 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
377
378 rc = panel_create(window, false, &panel);
379 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
380
381 PCUT_ASSERT_FALSE(panel_is_active(panel));
[0e125698]382 rc = panel_activate(panel);
383 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
[fe5c7a1]384 PCUT_ASSERT_TRUE(panel_is_active(panel));
385
386 panel_destroy(panel);
387 ui_window_destroy(window);
388 ui_destroy(ui);
389}
390
391/** panel_deactivate() deactivates panel */
392PCUT_TEST(deactivate)
393{
394 ui_t *ui;
395 ui_window_t *window;
396 ui_wnd_params_t params;
397 panel_t *panel;
398 errno_t rc;
399
400 rc = ui_create_disp(NULL, &ui);
401 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
402
403 ui_wnd_params_init(&params);
404 params.caption = "Test";
405
406 rc = ui_window_create(ui, &params, &window);
407 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
408
409 rc = panel_create(window, true, &panel);
410 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
411
412 PCUT_ASSERT_TRUE(panel_is_active(panel));
413 panel_deactivate(panel);
414 PCUT_ASSERT_FALSE(panel_is_active(panel));
415
416 panel_destroy(panel);
417 ui_window_destroy(window);
418 ui_destroy(ui);
419}
420
[3b67e95]421/** panel_entry_append() appends new entry */
422PCUT_TEST(entry_append)
423{
424 panel_t *panel;
[75357c5]425 panel_entry_attr_t attr;
[3b67e95]426 errno_t rc;
427
[fe5c7a1]428 rc = panel_create(NULL, true, &panel);
[3b67e95]429 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
430
[75357c5]431 panel_entry_attr_init(&attr);
432
433 attr.name = "a";
434 attr.size = 1;
435 rc = panel_entry_append(panel, &attr);
[3b67e95]436 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
437
438 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
439
[75357c5]440 attr.name = "b";
441 attr.size = 2;
442 rc = panel_entry_append(panel, &attr);
[3b67e95]443 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
444
445 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
446
447 panel_destroy(panel);
448}
449
450/** panel_entry_delete() deletes entry */
451PCUT_TEST(entry_delete)
452{
453 panel_t *panel;
454 panel_entry_t *entry;
[75357c5]455 panel_entry_attr_t attr;
[3b67e95]456 errno_t rc;
457
[fe5c7a1]458 rc = panel_create(NULL, true, &panel);
[3b67e95]459 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
460
[75357c5]461 attr.name = "a";
462 attr.size = 1;
463 rc = panel_entry_append(panel, &attr);
[3b67e95]464 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
465
[75357c5]466 attr.name = "b";
467 attr.size = 2;
468 rc = panel_entry_append(panel, &attr);
[3b67e95]469 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
470
471 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
472
473 entry = panel_first(panel);
474 panel_entry_delete(entry);
475
476 PCUT_ASSERT_INT_EQUALS(1, list_count(&panel->entries));
477
478 entry = panel_first(panel);
479 panel_entry_delete(entry);
480
481 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
482
483 panel_destroy(panel);
484}
485
[8e5f39d]486/** panel_clear_entries() removes all entries from panel */
487PCUT_TEST(clear_entries)
488{
489 panel_t *panel;
[75357c5]490 panel_entry_attr_t attr;
[8e5f39d]491 errno_t rc;
492
[fe5c7a1]493 rc = panel_create(NULL, true, &panel);
[8e5f39d]494 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
495
[75357c5]496 panel_entry_attr_init(&attr);
497 attr.name = "a";
498 attr.size = 1;
499 rc = panel_entry_append(panel, &attr);
[8e5f39d]500 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
501
[75357c5]502 attr.name = "a";
503 attr.size = 2;
504 rc = panel_entry_append(panel, &attr);
[8e5f39d]505 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
506
507 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
508
509 panel_clear_entries(panel);
510 PCUT_ASSERT_INT_EQUALS(0, list_count(&panel->entries));
511
512 panel_destroy(panel);
513}
514
515/** panel_read_dir() reads the contents of a directory */
516PCUT_TEST(read_dir)
517{
518 panel_t *panel;
519 panel_entry_t *entry;
520 char buf[L_tmpnam];
521 char *fname;
522 char *p;
523 errno_t rc;
524 FILE *f;
525 int rv;
526
527 /* Create name for temporary directory */
528 p = tmpnam(buf);
529 PCUT_ASSERT_NOT_NULL(p);
530
531 /* Create temporary directory */
532 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
533 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
534
535 rv = asprintf(&fname, "%s/%s", p, "a");
536 PCUT_ASSERT_TRUE(rv >= 0);
537
538 f = fopen(fname, "wb");
539 PCUT_ASSERT_NOT_NULL(f);
540
541 rv = fprintf(f, "X");
542 PCUT_ASSERT_TRUE(rv >= 0);
543
544 rv = fclose(f);
545 PCUT_ASSERT_INT_EQUALS(0, rv);
546
[fe5c7a1]547 rc = panel_create(NULL, true, &panel);
[8e5f39d]548 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
549
550 rc = panel_read_dir(panel, p);
551 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
552
[0e125698]553 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
[8e5f39d]554
555 entry = panel_first(panel);
556 PCUT_ASSERT_NOT_NULL(entry);
[0e125698]557 PCUT_ASSERT_STR_EQUALS("..", entry->name);
558
559 entry = panel_next(entry);
560 PCUT_ASSERT_NOT_NULL(entry);
[8e5f39d]561 PCUT_ASSERT_STR_EQUALS("a", entry->name);
[0e125698]562 PCUT_ASSERT_INT_EQUALS(1, entry->size);
[8e5f39d]563
564 panel_destroy(panel);
565
566 rv = remove(fname);
567 PCUT_ASSERT_INT_EQUALS(0, rv);
568
569 rv = remove(p);
570 PCUT_ASSERT_INT_EQUALS(0, rv);
[9468680]571
[8e5f39d]572 free(fname);
573}
574
[9468680]575/** When moving to parent directory from a subdir, we seek to the
576 * coresponding entry
577 */
578PCUT_TEST(read_dir_up)
579{
580 panel_t *panel;
581 char buf[L_tmpnam];
582 char *subdir_a;
583 char *subdir_b;
584 char *subdir_c;
585 char *p;
586 errno_t rc;
587 int rv;
588
589 /* Create name for temporary directory */
590 p = tmpnam(buf);
591 PCUT_ASSERT_NOT_NULL(p);
592
593 /* Create temporary directory */
594 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
595 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
596
597 /* Create some subdirectories */
598
599 rv = asprintf(&subdir_a, "%s/%s", p, "a");
600 PCUT_ASSERT_TRUE(rv >= 0);
601 rc = vfs_link_path(subdir_a, KIND_DIRECTORY, NULL);
602 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
603
604 rv = asprintf(&subdir_b, "%s/%s", p, "b");
605 PCUT_ASSERT_TRUE(rv >= 0);
606 rc = vfs_link_path(subdir_b, KIND_DIRECTORY, NULL);
607 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
608
609 rv = asprintf(&subdir_c, "%s/%s", p, "c");
610 PCUT_ASSERT_TRUE(rv >= 0);
611 rc = vfs_link_path(subdir_c, KIND_DIRECTORY, NULL);
612 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
613
614 rc = panel_create(NULL, true, &panel);
615 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
616
617 /* Start in subdirectory "b" */
618 rc = panel_read_dir(panel, subdir_b);
619 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
620
621 /* Now go up (into p) */
622
623 rc = panel_read_dir(panel, "..");
624 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
625
626 PCUT_ASSERT_NOT_NULL(panel->cursor);
627 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
628
629 panel_destroy(panel);
630
631 rv = remove(subdir_a);
632 PCUT_ASSERT_INT_EQUALS(0, rv);
633
634 rv = remove(subdir_b);
635 PCUT_ASSERT_INT_EQUALS(0, rv);
636
637 rv = remove(subdir_c);
638 PCUT_ASSERT_INT_EQUALS(0, rv);
639
640 rv = remove(p);
641 PCUT_ASSERT_INT_EQUALS(0, rv);
642
643 free(subdir_a);
644 free(subdir_b);
645 free(subdir_c);
646}
647
[03c4b23]648/** panel_sort() sorts panel entries */
649PCUT_TEST(sort)
650{
651 panel_t *panel;
652 panel_entry_t *entry;
[75357c5]653 panel_entry_attr_t attr;
[03c4b23]654 errno_t rc;
655
656 rc = panel_create(NULL, true, &panel);
657 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
658
[75357c5]659 panel_entry_attr_init(&attr);
660
661 attr.name = "b";
662 attr.size = 1;
663 rc = panel_entry_append(panel, &attr);
[03c4b23]664 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
665
[75357c5]666 attr.name = "c";
667 attr.size = 3;
668 rc = panel_entry_append(panel, &attr);
[03c4b23]669 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
670
[75357c5]671 attr.name = "a";
672 attr.size = 2;
673 rc = panel_entry_append(panel, &attr);
[03c4b23]674 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
675
676 rc = panel_sort(panel);
677 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
678
679 entry = panel_first(panel);
680 PCUT_ASSERT_STR_EQUALS("a", entry->name);
681 PCUT_ASSERT_INT_EQUALS(2, entry->size);
682
683 entry = panel_next(entry);
684 PCUT_ASSERT_STR_EQUALS("b", entry->name);
685 PCUT_ASSERT_INT_EQUALS(1, entry->size);
686
687 entry = panel_next(entry);
688 PCUT_ASSERT_STR_EQUALS("c", entry->name);
689 PCUT_ASSERT_INT_EQUALS(3, entry->size);
690
691 panel_destroy(panel);
692}
693
694/** panel_entry_ptr_cmp compares two indirectly referenced entries */
695PCUT_TEST(entry_ptr_cmp)
696{
697 panel_t *panel;
698 panel_entry_t *a, *b;
[75357c5]699 panel_entry_attr_t attr;
[03c4b23]700 int rel;
701 errno_t rc;
702
703 rc = panel_create(NULL, true, &panel);
704 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
705
[75357c5]706 panel_entry_attr_init(&attr);
707
708 attr.name = "a";
709 attr.size = 2;
710 rc = panel_entry_append(panel, &attr);
[03c4b23]711 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
712
[75357c5]713 attr.name = "b";
714 attr.size = 1;
715 rc = panel_entry_append(panel, &attr);
[03c4b23]716 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
717
718 a = panel_first(panel);
719 PCUT_ASSERT_NOT_NULL(a);
720 b = panel_next(a);
721 PCUT_ASSERT_NOT_NULL(b);
722
723 /* a < b */
724 rel = panel_entry_ptr_cmp(&a, &b);
725 PCUT_ASSERT_TRUE(rel < 0);
726
727 /* b > a */
728 rel = panel_entry_ptr_cmp(&b, &a);
729 PCUT_ASSERT_TRUE(rel > 0);
730
731 /* a == a */
732 rel = panel_entry_ptr_cmp(&a, &a);
733 PCUT_ASSERT_INT_EQUALS(0, rel);
734
735 panel_destroy(panel);
736}
737
[3b67e95]738/** panel_first() returns valid entry or @c NULL as appropriate */
739PCUT_TEST(first)
740{
741 panel_t *panel;
742 panel_entry_t *entry;
[75357c5]743 panel_entry_attr_t attr;
[3b67e95]744 errno_t rc;
745
[fe5c7a1]746 rc = panel_create(NULL, true, &panel);
[3b67e95]747 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
748
[75357c5]749 panel_entry_attr_init(&attr);
750
[3b67e95]751 entry = panel_first(panel);
752 PCUT_ASSERT_NULL(entry);
753
754 /* Add one entry */
[75357c5]755 attr.name = "a";
756 attr.size = 1;
757 rc = panel_entry_append(panel, &attr);
[3b67e95]758 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
759
760 /* Now try getting it */
761 entry = panel_first(panel);
762 PCUT_ASSERT_NOT_NULL(entry);
763 PCUT_ASSERT_STR_EQUALS("a", entry->name);
764 PCUT_ASSERT_INT_EQUALS(1, entry->size);
765
766 /* Add another entry */
[75357c5]767 attr.name = "b";
[9468680]768 attr.size = 2;
[75357c5]769 rc = panel_entry_append(panel, &attr);
[3b67e95]770 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
771
772 /* We should still get the first entry */
773 entry = panel_first(panel);
774 PCUT_ASSERT_NOT_NULL(entry);
775 PCUT_ASSERT_STR_EQUALS("a", entry->name);
776 PCUT_ASSERT_INT_EQUALS(1, entry->size);
777
778 panel_destroy(panel);
779}
780
[af2ea83]781/** panel_last() returns valid entry or @c NULL as appropriate */
782PCUT_TEST(last)
783{
784 panel_t *panel;
785 panel_entry_t *entry;
[75357c5]786 panel_entry_attr_t attr;
[af2ea83]787 errno_t rc;
788
[fe5c7a1]789 rc = panel_create(NULL, true, &panel);
[af2ea83]790 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
791
[75357c5]792 panel_entry_attr_init(&attr);
793
[af2ea83]794 entry = panel_last(panel);
795 PCUT_ASSERT_NULL(entry);
796
797 /* Add one entry */
[75357c5]798 attr.name = "a";
799 attr.size = 1;
800 rc = panel_entry_append(panel, &attr);
[af2ea83]801 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
802
803 /* Now try getting it */
804 entry = panel_last(panel);
805 PCUT_ASSERT_NOT_NULL(entry);
806 PCUT_ASSERT_STR_EQUALS("a", entry->name);
807 PCUT_ASSERT_INT_EQUALS(1, entry->size);
808
809 /* Add another entry */
[75357c5]810 attr.name = "b";
811 attr.size = 2;
812 rc = panel_entry_append(panel, &attr);
[af2ea83]813 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
814
815 /* We should get new entry now */
816 entry = panel_last(panel);
817 PCUT_ASSERT_NOT_NULL(entry);
[75357c5]818 attr.name = "b";
819 attr.size = 2;
[af2ea83]820 PCUT_ASSERT_STR_EQUALS("b", entry->name);
821 PCUT_ASSERT_INT_EQUALS(2, entry->size);
822
823 panel_destroy(panel);
824}
825
[3b67e95]826/** panel_next() returns the next entry or @c NULL as appropriate */
827PCUT_TEST(next)
828{
829 panel_t *panel;
830 panel_entry_t *entry;
[75357c5]831 panel_entry_attr_t attr;
[3b67e95]832 errno_t rc;
833
[fe5c7a1]834 rc = panel_create(NULL, true, &panel);
[3b67e95]835 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
836
[75357c5]837 panel_entry_attr_init(&attr);
838
[3b67e95]839 /* Add one entry */
[75357c5]840 attr.name = "a";
841 attr.size = 1;
842 rc = panel_entry_append(panel, &attr);
[3b67e95]843 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
844
845 /* Now try getting its successor */
846 entry = panel_first(panel);
847 PCUT_ASSERT_NOT_NULL(entry);
848
849 entry = panel_next(entry);
850 PCUT_ASSERT_NULL(entry);
851
852 /* Add another entry */
[75357c5]853 attr.name = "b";
854 attr.size = 2;
855 rc = panel_entry_append(panel, &attr);
[3b67e95]856 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
857
858 /* Try getting the successor of first entry again */
859 entry = panel_first(panel);
860 PCUT_ASSERT_NOT_NULL(entry);
861
862 entry = panel_next(entry);
863 PCUT_ASSERT_NOT_NULL(entry);
864 PCUT_ASSERT_STR_EQUALS("b", entry->name);
865 PCUT_ASSERT_INT_EQUALS(2, entry->size);
866
867 panel_destroy(panel);
868}
869
[af2ea83]870/** panel_prev() returns the previous entry or @c NULL as appropriate */
871PCUT_TEST(prev)
872{
873 panel_t *panel;
874 panel_entry_t *entry;
[75357c5]875 panel_entry_attr_t attr;
[af2ea83]876 errno_t rc;
877
[fe5c7a1]878 rc = panel_create(NULL, true, &panel);
[af2ea83]879 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
880
[75357c5]881 panel_entry_attr_init(&attr);
882
[af2ea83]883 /* Add one entry */
[75357c5]884 attr.name = "a";
885 attr.size = 1;
886 rc = panel_entry_append(panel, &attr);
[af2ea83]887 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
888
889 /* Now try getting its predecessor */
890 entry = panel_last(panel);
891 PCUT_ASSERT_NOT_NULL(entry);
892
893 entry = panel_prev(entry);
894 PCUT_ASSERT_NULL(entry);
895
896 /* Add another entry */
[75357c5]897 attr.name = "b";
898 attr.size = 2;
899 rc = panel_entry_append(panel, &attr);
[af2ea83]900 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
901
902 /* Try getting the predecessor of the new entry */
903 entry = panel_last(panel);
904 PCUT_ASSERT_NOT_NULL(entry);
905
906 entry = panel_prev(entry);
907 PCUT_ASSERT_NOT_NULL(entry);
908 PCUT_ASSERT_STR_EQUALS("a", entry->name);
909 PCUT_ASSERT_INT_EQUALS(1, entry->size);
910
911 panel_destroy(panel);
912}
913
[8922b76]914/** panel_page_nth_entry() .. */
915PCUT_TEST(page_nth_entry)
916{
917 panel_t *panel;
918 panel_entry_t *entry;
919 panel_entry_attr_t attr;
920 size_t idx;
921 errno_t rc;
922
923 rc = panel_create(NULL, true, &panel);
924 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
925
926 panel_entry_attr_init(&attr);
927
928 /* Add some entries */
929 attr.name = "a";
930 attr.size = 1;
931 rc = panel_entry_append(panel, &attr);
932 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
933
934 attr.name = "b";
935 attr.size = 2;
936 rc = panel_entry_append(panel, &attr);
937 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
938
939 attr.name = "c";
940 attr.size = 3;
941 rc = panel_entry_append(panel, &attr);
942 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
943
944 panel->page = panel_next(panel_first(panel));
945 panel->page_idx = 1;
946
947 entry = panel_page_nth_entry(panel, 0, &idx);
948 PCUT_ASSERT_STR_EQUALS("b", entry->name);
949 PCUT_ASSERT_INT_EQUALS(1, idx);
950
951 entry = panel_page_nth_entry(panel, 1, &idx);
952 PCUT_ASSERT_STR_EQUALS("c", entry->name);
953 PCUT_ASSERT_INT_EQUALS(2, idx);
954
955 entry = panel_page_nth_entry(panel, 2, &idx);
956 PCUT_ASSERT_STR_EQUALS("c", entry->name);
957 PCUT_ASSERT_INT_EQUALS(2, idx);
958
959 entry = panel_page_nth_entry(panel, 3, &idx);
960 PCUT_ASSERT_STR_EQUALS("c", entry->name);
961 PCUT_ASSERT_INT_EQUALS(2, idx);
962
963 panel_destroy(panel);
964}
965
[af2ea83]966/** panel_cursor_move() ... */
967PCUT_TEST(cursor_move)
968{
969}
970
971/** panel_cursor_up() moves cursor one entry up */
972PCUT_TEST(cursor_up)
973{
974 ui_t *ui;
975 ui_window_t *window;
976 ui_wnd_params_t params;
977 panel_t *panel;
[75357c5]978 panel_entry_attr_t attr;
[af2ea83]979 gfx_rect_t rect;
980 errno_t rc;
981
982 rc = ui_create_disp(NULL, &ui);
983 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
984
985 ui_wnd_params_init(&params);
986 params.caption = "Test";
987
988 rc = ui_window_create(ui, &params, &window);
989 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
990
[fe5c7a1]991 rc = panel_create(window, true, &panel);
[af2ea83]992 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
993
994 rect.p0.x = 0;
995 rect.p0.y = 0;
996 rect.p1.x = 10;
997 rect.p1.y = 4; // XXX Assuming this makes page size 2
998 panel_set_rect(panel, &rect);
999
[3e6c51f]1000 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1001
[af2ea83]1002 /* Add tree entries (more than page size, which is 2) */
[75357c5]1003
1004 panel_entry_attr_init(&attr);
1005
1006 attr.name = "a";
1007 attr.size = 1;
1008 rc = panel_entry_append(panel, &attr);
[af2ea83]1009 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1010
[75357c5]1011 attr.name = "b";
1012 attr.size = 2;
1013 rc = panel_entry_append(panel, &attr);
[af2ea83]1014 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1015
[75357c5]1016 attr.name = "c";
1017 attr.size = 3;
1018 rc = panel_entry_append(panel, &attr);
[af2ea83]1019 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1020
1021 /* Cursor to the last entry and page start to the next-to-last entry */
1022 panel->cursor = panel_last(panel);
1023 panel->cursor_idx = 2;
1024 panel->page = panel_prev(panel->cursor);
1025 panel->page_idx = 1;
1026
1027 /* Move cursor one entry up */
1028 panel_cursor_up(panel);
1029
1030 /* Cursor and page start should now both be at the second entry */
1031 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
1032 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
1033 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
1034 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1035 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1036
1037 /* Move cursor one entry up. This should scroll up. */
1038 panel_cursor_up(panel);
1039
1040 /* Cursor and page start should now both be at the first entry */
1041 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1042 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1043 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1044 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1045 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1046
1047 /* Moving further up should do nothing (we are at the top). */
1048 panel_cursor_up(panel);
1049
1050 /* Cursor and page start should still be at the first entry */
1051 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1052 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1053 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1054 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1055 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1056
1057 panel_destroy(panel);
1058 ui_window_destroy(window);
1059 ui_destroy(ui);
1060}
1061
1062/** panel_cursor_down() moves cursor one entry down */
1063PCUT_TEST(cursor_down)
1064{
1065 ui_t *ui;
1066 ui_window_t *window;
1067 ui_wnd_params_t params;
1068 panel_t *panel;
[75357c5]1069 panel_entry_attr_t attr;
[af2ea83]1070 gfx_rect_t rect;
1071 errno_t rc;
1072
1073 rc = ui_create_disp(NULL, &ui);
1074 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1075
1076 ui_wnd_params_init(&params);
1077 params.caption = "Test";
1078
1079 rc = ui_window_create(ui, &params, &window);
1080 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1081
[fe5c7a1]1082 rc = panel_create(window, true, &panel);
[af2ea83]1083 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1084
1085 rect.p0.x = 0;
1086 rect.p0.y = 0;
1087 rect.p1.x = 10;
1088 rect.p1.y = 4; // XXX Assuming this makes page size 2
1089 panel_set_rect(panel, &rect);
1090
[3e6c51f]1091 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1092
[af2ea83]1093 /* Add tree entries (more than page size, which is 2) */
[75357c5]1094
1095 panel_entry_attr_init(&attr);
1096
1097 attr.name = "a";
1098 attr.size = 1;
1099 rc = panel_entry_append(panel, &attr);
[af2ea83]1100 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1101
[75357c5]1102 attr.name = "b";
1103 attr.size = 2;
1104 rc = panel_entry_append(panel, &attr);
[af2ea83]1105 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1106
[75357c5]1107 attr.name = "c";
1108 attr.size = 3;
1109 rc = panel_entry_append(panel, &attr);
[af2ea83]1110 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1111
1112 /* Cursor and page start to the first entry */
1113 panel->cursor = panel_first(panel);
1114 panel->cursor_idx = 0;
1115 panel->page = panel->cursor;
1116 panel->page_idx = 0;
1117
1118 /* Move cursor one entry down */
1119 panel_cursor_down(panel);
1120
1121 /* Cursor should now be at the second entry, page stays the same */
1122 PCUT_ASSERT_STR_EQUALS("b", panel->cursor->name);
1123 PCUT_ASSERT_INT_EQUALS(2, panel->cursor->size);
1124 PCUT_ASSERT_INT_EQUALS(1, panel->cursor_idx);
1125 PCUT_ASSERT_EQUALS(panel_first(panel), panel->page);
1126 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1127
1128 /* Move cursor one entry down. This should scroll down. */
1129 panel_cursor_down(panel);
1130
1131 /* Cursor should now be at the third and page at the second entry. */
1132 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1133 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1134 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1135 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
1136 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
1137 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1138
1139 /* Moving further down should do nothing (we are at the bottom). */
1140 panel_cursor_down(panel);
1141
1142 /* Cursor should still be at the third and page at the second entry. */
1143 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1144 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1145 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1146 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
1147 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
1148 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1149
1150 panel_destroy(panel);
1151 ui_window_destroy(window);
1152 ui_destroy(ui);
1153}
1154
1155/** panel_cursor_top() moves cursor to the first entry */
1156PCUT_TEST(cursor_top)
1157{
1158 ui_t *ui;
1159 ui_window_t *window;
1160 ui_wnd_params_t params;
1161 panel_t *panel;
[75357c5]1162 panel_entry_attr_t attr;
[af2ea83]1163 gfx_rect_t rect;
1164 errno_t rc;
1165
1166 rc = ui_create_disp(NULL, &ui);
1167 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1168
1169 ui_wnd_params_init(&params);
1170 params.caption = "Test";
1171
1172 rc = ui_window_create(ui, &params, &window);
1173 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1174
[fe5c7a1]1175 rc = panel_create(window, true, &panel);
[af2ea83]1176 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1177
1178 rect.p0.x = 0;
1179 rect.p0.y = 0;
1180 rect.p1.x = 10;
1181 rect.p1.y = 4; // XXX Assuming this makes page size 2
1182 panel_set_rect(panel, &rect);
1183
[3e6c51f]1184 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1185
[af2ea83]1186 /* Add tree entries (more than page size, which is 2) */
[75357c5]1187
1188 panel_entry_attr_init(&attr);
1189
1190 attr.name = "a";
1191 attr.size = 1;
1192 rc = panel_entry_append(panel, &attr);
[af2ea83]1193 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1194
[75357c5]1195 attr.name = "b";
1196 attr.size = 2;
1197 rc = panel_entry_append(panel, &attr);
[af2ea83]1198 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1199
[75357c5]1200 attr.name = "c";
1201 attr.size = 3;
1202 rc = panel_entry_append(panel, &attr);
[af2ea83]1203 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1204
1205 /* Cursor to the last entry and page start to the next-to-last entry */
1206 panel->cursor = panel_last(panel);
1207 panel->cursor_idx = 2;
1208 panel->page = panel_prev(panel->cursor);
1209 panel->page_idx = 1;
1210
1211 /* Move cursor to the top. This should scroll up. */
1212 panel_cursor_top(panel);
1213
1214 /* Cursor and page start should now both be at the first entry */
1215 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1216 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1217 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1218 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1219 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1220
1221 panel_destroy(panel);
1222 ui_window_destroy(window);
1223 ui_destroy(ui);
1224}
1225
1226/** panel_cursor_bottom() moves cursor to the last entry */
1227PCUT_TEST(cursor_bottom)
1228{
1229 ui_t *ui;
1230 ui_window_t *window;
1231 ui_wnd_params_t params;
1232 panel_t *panel;
[75357c5]1233 panel_entry_attr_t attr;
[af2ea83]1234 gfx_rect_t rect;
1235 errno_t rc;
1236
1237 rc = ui_create_disp(NULL, &ui);
1238 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1239
1240 ui_wnd_params_init(&params);
1241 params.caption = "Test";
1242
1243 rc = ui_window_create(ui, &params, &window);
1244 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1245
[fe5c7a1]1246 rc = panel_create(window, true, &panel);
[af2ea83]1247 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1248
1249 rect.p0.x = 0;
1250 rect.p0.y = 0;
1251 rect.p1.x = 10;
1252 rect.p1.y = 4; // XXX Assuming this makes page size 2
1253 panel_set_rect(panel, &rect);
1254
[3e6c51f]1255 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1256
[af2ea83]1257 /* Add tree entries (more than page size, which is 2) */
[75357c5]1258
1259 panel_entry_attr_init(&attr);
1260
1261 attr.name = "a";
1262 attr.size = 1;
1263 rc = panel_entry_append(panel, &attr);
[af2ea83]1264 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1265
[75357c5]1266 attr.name = "b";
1267 attr.size = 2;
1268 rc = panel_entry_append(panel, &attr);
[af2ea83]1269 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1270
[75357c5]1271 attr.name = "c";
1272 attr.size = 3;
1273 rc = panel_entry_append(panel, &attr);
[af2ea83]1274 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1275
1276 /* Cursor and page start to the first entry */
1277 panel->cursor = panel_first(panel);
1278 panel->cursor_idx = 0;
1279 panel->page = panel->cursor;
1280 panel->page_idx = 0;
1281
1282 /* Move cursor to the bottom. This should scroll down. */
1283 panel_cursor_bottom(panel);
1284
1285 /* Cursor should now be at the third and page at the second entry. */
1286 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1287 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1288 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1289 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
1290 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
1291 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1292
1293 panel_destroy(panel);
1294 ui_window_destroy(window);
1295 ui_destroy(ui);
1296}
1297
[3e6c51f]1298/** panel_page_up() moves one page up */
1299PCUT_TEST(page_up)
1300{
1301 ui_t *ui;
1302 ui_window_t *window;
1303 ui_wnd_params_t params;
1304 panel_t *panel;
[75357c5]1305 panel_entry_attr_t attr;
[3e6c51f]1306 gfx_rect_t rect;
1307 errno_t rc;
1308
1309 rc = ui_create_disp(NULL, &ui);
1310 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1311
1312 ui_wnd_params_init(&params);
1313 params.caption = "Test";
1314
1315 rc = ui_window_create(ui, &params, &window);
1316 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1317
[fe5c7a1]1318 rc = panel_create(window, true, &panel);
[3e6c51f]1319 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1320
1321 rect.p0.x = 0;
1322 rect.p0.y = 0;
1323 rect.p1.x = 10;
1324 rect.p1.y = 4; // XXX Assuming this makes page size 2
1325 panel_set_rect(panel, &rect);
1326
1327 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1328
1329 /* Add five entries (2 full pages, one partial) */
[75357c5]1330
1331 panel_entry_attr_init(&attr);
1332
1333 attr.name = "a";
1334 attr.size = 1;
1335 rc = panel_entry_append(panel, &attr);
[3e6c51f]1336 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1337
[75357c5]1338 attr.name = "b";
1339 attr.size = 2;
1340 rc = panel_entry_append(panel, &attr);
[3e6c51f]1341 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1342
[75357c5]1343 attr.name = "c";
1344 attr.size = 3;
1345 rc = panel_entry_append(panel, &attr);
[3e6c51f]1346 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1347
[75357c5]1348 attr.name = "d";
1349 attr.size = 4;
1350 rc = panel_entry_append(panel, &attr);
[3e6c51f]1351 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1352
[75357c5]1353 attr.name = "e";
1354 attr.size = 5;
1355 rc = panel_entry_append(panel, &attr);
[3e6c51f]1356 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1357
1358 /* Cursor to the last entry and page start to the next-to-last entry */
1359 panel->cursor = panel_last(panel);
1360 panel->cursor_idx = 4;
1361 panel->page = panel_prev(panel->cursor);
1362 panel->page_idx = 3;
1363
1364 /* Move one page up */
1365 panel_page_up(panel);
1366
1367 /* Page should now start at second entry and cursor at third */
1368 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1369 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1370 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1371 PCUT_ASSERT_STR_EQUALS("b", panel->page->name);
1372 PCUT_ASSERT_INT_EQUALS(2, panel->page->size);
1373 PCUT_ASSERT_INT_EQUALS(1, panel->page_idx);
1374
1375 /* Move one page up again. */
1376 panel_page_up(panel);
1377
1378 /* Cursor and page start should now both be at the first entry */
1379 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1380 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1381 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1382 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1383 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1384
1385 /* Moving further up should do nothing (we are at the top). */
1386 panel_page_up(panel);
1387
1388 /* Cursor and page start should still be at the first entry */
1389 PCUT_ASSERT_STR_EQUALS("a", panel->cursor->name);
1390 PCUT_ASSERT_INT_EQUALS(1, panel->cursor->size);
1391 PCUT_ASSERT_INT_EQUALS(0, panel->cursor_idx);
1392 PCUT_ASSERT_EQUALS(panel->cursor, panel->page);
1393 PCUT_ASSERT_INT_EQUALS(0, panel->page_idx);
1394
1395 panel_destroy(panel);
1396 ui_window_destroy(window);
1397 ui_destroy(ui);
1398}
1399
1400/** panel_page_up() moves one page down */
1401PCUT_TEST(page_down)
1402{
1403 ui_t *ui;
1404 ui_window_t *window;
1405 ui_wnd_params_t params;
1406 panel_t *panel;
[75357c5]1407 panel_entry_attr_t attr;
[3e6c51f]1408 gfx_rect_t rect;
1409 errno_t rc;
1410
1411 rc = ui_create_disp(NULL, &ui);
1412 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1413
1414 ui_wnd_params_init(&params);
1415 params.caption = "Test";
1416
1417 rc = ui_window_create(ui, &params, &window);
1418 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1419
[fe5c7a1]1420 rc = panel_create(window, true, &panel);
[3e6c51f]1421 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1422
1423 rect.p0.x = 0;
1424 rect.p0.y = 0;
1425 rect.p1.x = 10;
1426 rect.p1.y = 4; // XXX Assuming this makes page size 2
1427 panel_set_rect(panel, &rect);
1428
1429 PCUT_ASSERT_INT_EQUALS(2, panel_page_size(panel));
1430
1431 /* Add five entries (2 full pages, one partial) */
[75357c5]1432
1433 panel_entry_attr_init(&attr);
1434
1435 attr.name = "a";
1436 attr.size = 1;
1437 rc = panel_entry_append(panel, &attr);
[3e6c51f]1438 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1439
[75357c5]1440 attr.name = "b";
1441 attr.size = 2;
1442 rc = panel_entry_append(panel, &attr);
[3e6c51f]1443 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1444
[75357c5]1445 attr.name = "c";
1446 attr.size = 3;
1447 rc = panel_entry_append(panel, &attr);
[3e6c51f]1448 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1449
[75357c5]1450 attr.name = "d";
1451 attr.size = 4;
1452 rc = panel_entry_append(panel, &attr);
[3e6c51f]1453 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1454
[75357c5]1455 attr.name = "e";
1456 attr.size = 5;
1457 rc = panel_entry_append(panel, &attr);
[3e6c51f]1458 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1459
1460 /* Cursor and page to the first entry */
1461 panel->cursor = panel_first(panel);
1462 panel->cursor_idx = 0;
1463 panel->page = panel->cursor;
1464 panel->page_idx = 0;
1465
1466 /* Move one page down */
1467 panel_page_down(panel);
1468
1469 /* Page and cursor should point to the third entry */
1470 PCUT_ASSERT_STR_EQUALS("c", panel->cursor->name);
1471 PCUT_ASSERT_INT_EQUALS(3, panel->cursor->size);
1472 PCUT_ASSERT_INT_EQUALS(2, panel->cursor_idx);
1473 PCUT_ASSERT_STR_EQUALS("c", panel->page->name);
1474 PCUT_ASSERT_INT_EQUALS(3, panel->page->size);
1475 PCUT_ASSERT_INT_EQUALS(2, panel->page_idx);
1476
1477 /* Move one page down again. */
1478 panel_page_down(panel);
1479
1480 /* Cursor should point to last and page to next-to-last entry */
1481 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1482 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1483 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1484 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1485 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1486 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1487
1488 /* Moving further down should do nothing (we are at the bottom). */
1489 panel_page_down(panel);
1490
1491 /* Cursor should still point to last and page to next-to-last entry */
1492 PCUT_ASSERT_STR_EQUALS("e", panel->cursor->name);
1493 PCUT_ASSERT_INT_EQUALS(5, panel->cursor->size);
1494 PCUT_ASSERT_INT_EQUALS(4, panel->cursor_idx);
1495 PCUT_ASSERT_STR_EQUALS("d", panel->page->name);
1496 PCUT_ASSERT_INT_EQUALS(4, panel->page->size);
1497 PCUT_ASSERT_INT_EQUALS(3, panel->page_idx);
1498
1499 panel_destroy(panel);
1500 ui_window_destroy(window);
1501 ui_destroy(ui);
1502}
1503
[0e125698]1504/** panel_open() opens a directory entry */
1505PCUT_TEST(open)
1506{
1507 ui_t *ui;
1508 ui_window_t *window;
1509 ui_wnd_params_t params;
1510 panel_t *panel;
1511 panel_entry_t *entry;
1512 char buf[L_tmpnam];
1513 char *sdname;
1514 char *p;
1515 errno_t rc;
1516 int rv;
1517
1518 rc = ui_create_disp(NULL, &ui);
1519 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1520
1521 ui_wnd_params_init(&params);
1522 params.caption = "Test";
1523
1524 rc = ui_window_create(ui, &params, &window);
1525 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1526
1527 /* Create name for temporary directory */
1528 p = tmpnam(buf);
1529 PCUT_ASSERT_NOT_NULL(p);
1530
1531 /* Create temporary directory */
1532 rc = vfs_link_path(p, KIND_DIRECTORY, NULL);
1533 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1534
1535 rv = asprintf(&sdname, "%s/%s", p, "a");
1536 PCUT_ASSERT_TRUE(rv >= 0);
1537
1538 /* Create sub-directory */
1539 rc = vfs_link_path(sdname, KIND_DIRECTORY, NULL);
1540 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1541
1542 rc = panel_create(window, true, &panel);
1543 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1544
1545 rc = panel_read_dir(panel, p);
1546 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1547 PCUT_ASSERT_STR_EQUALS(p, panel->dir);
1548
1549 PCUT_ASSERT_INT_EQUALS(2, list_count(&panel->entries));
1550
1551 entry = panel_first(panel);
1552 PCUT_ASSERT_NOT_NULL(entry);
1553 PCUT_ASSERT_STR_EQUALS("..", entry->name);
1554
1555 entry = panel_next(entry);
1556 PCUT_ASSERT_NOT_NULL(entry);
1557 PCUT_ASSERT_STR_EQUALS("a", entry->name);
1558 PCUT_ASSERT_TRUE(entry->isdir);
1559
1560 rc = panel_open(panel, entry);
1561 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1562
1563 PCUT_ASSERT_STR_EQUALS(sdname, panel->dir);
1564
1565 panel_destroy(panel);
1566 ui_window_destroy(window);
1567 ui_destroy(ui);
1568
1569 rv = remove(sdname);
1570 PCUT_ASSERT_INT_EQUALS(0, rv);
1571
1572 rv = remove(p);
1573 PCUT_ASSERT_INT_EQUALS(0, rv);
1574
1575 free(sdname);
1576}
1577
[966a27d3]1578/** panel_activate_req() sends activation request */
1579PCUT_TEST(activate_req)
1580{
1581 panel_t *panel;
1582 errno_t rc;
1583 test_resp_t resp;
1584
1585 rc = panel_create(NULL, true, &panel);
1586 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1587
1588 panel_set_cb(panel, &test_cb, &resp);
1589
1590 resp.activate_req = false;
1591 resp.activate_req_panel = NULL;
1592
1593 panel_activate_req(panel);
1594 PCUT_ASSERT_TRUE(resp.activate_req);
1595 PCUT_ASSERT_EQUALS(panel, resp.activate_req_panel);
1596
1597 panel_destroy(panel);
1598}
1599
1600static void test_panel_activate_req(void *arg, panel_t *panel)
1601{
1602 test_resp_t *resp = (test_resp_t *)arg;
1603
1604 resp->activate_req = true;
1605 resp->activate_req_panel = panel;
1606}
1607
[68b9e540]1608PCUT_EXPORT(panel);
Note: See TracBrowser for help on using the repository browser.