2 * Copyright (c) 2023 Jiri Svoboda
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
29 #include <gfx/context.h>
30 #include <gfx/coord.h>
32 #include <pcut/pcut.h>
34 #include <ui/control.h>
35 #include <ui/scrollbar.h>
36 #include <ui/resource.h>
38 #include <ui/window.h>
39 #include "../private/pbutton.h"
40 #include "../private/scrollbar.h"
44 PCUT_TEST_SUITE(scrollbar
);
46 static void test_scrollbar_up(ui_scrollbar_t
*, void *);
47 static void test_scrollbar_down(ui_scrollbar_t
*, void *);
48 static void test_scrollbar_page_up(ui_scrollbar_t
*, void *);
49 static void test_scrollbar_page_down(ui_scrollbar_t
*, void *);
50 static void test_scrollbar_moved(ui_scrollbar_t
*, void *, gfx_coord_t
);
52 static ui_scrollbar_cb_t test_scrollbar_cb
= {
53 .up
= test_scrollbar_up
,
54 .down
= test_scrollbar_down
,
55 .page_up
= test_scrollbar_page_up
,
56 .page_down
= test_scrollbar_page_down
,
57 .moved
= test_scrollbar_moved
60 static ui_scrollbar_cb_t dummy_scrollbar_cb
= {
72 /** Create and destroy scrollbar */
73 PCUT_TEST(create_destroy
)
76 ui_window_t
*window
= NULL
;
77 ui_wnd_params_t params
;
78 ui_scrollbar_t
*scrollbar
= NULL
;
81 rc
= ui_create_disp(NULL
, &ui
);
82 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
84 ui_wnd_params_init(¶ms
);
85 params
.caption
= "Hello";
87 rc
= ui_window_create(ui
, ¶ms
, &window
);
88 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
89 PCUT_ASSERT_NOT_NULL(window
);
91 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
92 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
93 PCUT_ASSERT_NOT_NULL(scrollbar
);
95 ui_scrollbar_destroy(scrollbar
);
96 ui_window_destroy(window
);
100 /** ui_scrollbar_destroy() can take NULL argument (no-op) */
101 PCUT_TEST(destroy_null
)
103 ui_scrollbar_destroy(NULL
);
106 /** ui_scrollbar_ctl() returns control that has a working virtual destructor */
110 ui_window_t
*window
= NULL
;
111 ui_wnd_params_t params
;
112 ui_scrollbar_t
*scrollbar
= NULL
;
113 ui_control_t
*control
;
116 rc
= ui_create_disp(NULL
, &ui
);
117 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
119 ui_wnd_params_init(¶ms
);
120 params
.caption
= "Hello";
122 rc
= ui_window_create(ui
, ¶ms
, &window
);
123 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
124 PCUT_ASSERT_NOT_NULL(window
);
126 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
127 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
128 PCUT_ASSERT_NOT_NULL(scrollbar
);
130 control
= ui_scrollbar_ctl(scrollbar
);
131 PCUT_ASSERT_NOT_NULL(control
);
133 ui_control_destroy(control
);
134 ui_window_destroy(window
);
138 /** Set scrollbar rectangle sets internal field */
142 ui_window_t
*window
= NULL
;
143 ui_wnd_params_t params
;
144 ui_scrollbar_t
*scrollbar
= NULL
;
148 rc
= ui_create_disp(NULL
, &ui
);
149 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
151 ui_wnd_params_init(¶ms
);
152 params
.caption
= "Hello";
154 rc
= ui_window_create(ui
, ¶ms
, &window
);
155 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
156 PCUT_ASSERT_NOT_NULL(window
);
158 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
159 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
160 PCUT_ASSERT_NOT_NULL(scrollbar
);
167 ui_scrollbar_set_rect(scrollbar
, &rect
);
168 PCUT_ASSERT_INT_EQUALS(rect
.p0
.x
, scrollbar
->rect
.p0
.x
);
169 PCUT_ASSERT_INT_EQUALS(rect
.p0
.y
, scrollbar
->rect
.p0
.y
);
170 PCUT_ASSERT_INT_EQUALS(rect
.p1
.x
, scrollbar
->rect
.p1
.x
);
171 PCUT_ASSERT_INT_EQUALS(rect
.p1
.y
, scrollbar
->rect
.p1
.y
);
173 ui_scrollbar_destroy(scrollbar
);
174 ui_window_destroy(window
);
178 /** Paint scrollbar in graphics mode */
182 ui_window_t
*window
= NULL
;
183 ui_wnd_params_t params
;
184 ui_scrollbar_t
*scrollbar
;
187 rc
= ui_create_disp(NULL
, &ui
);
188 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
190 ui_wnd_params_init(¶ms
);
191 params
.caption
= "Hello";
193 rc
= ui_window_create(ui
, ¶ms
, &window
);
194 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
195 PCUT_ASSERT_NOT_NULL(window
);
197 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
198 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
200 rc
= ui_scrollbar_paint_gfx(scrollbar
);
201 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
203 ui_scrollbar_destroy(scrollbar
);
204 ui_window_destroy(window
);
208 /** Paint scrollbar in text mode */
209 PCUT_TEST(paint_text_horiz
)
212 ui_window_t
*window
= NULL
;
213 ui_wnd_params_t params
;
214 ui_scrollbar_t
*scrollbar
;
218 rc
= ui_create_disp(NULL
, &ui
);
219 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
221 ui_wnd_params_init(¶ms
);
222 params
.caption
= "Hello";
224 rc
= ui_window_create(ui
, ¶ms
, &window
);
225 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
226 PCUT_ASSERT_NOT_NULL(window
);
228 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
229 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
235 ui_scrollbar_set_rect(scrollbar
, &rect
);
237 rc
= ui_scrollbar_paint_text(scrollbar
);
238 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
240 ui_scrollbar_destroy(scrollbar
);
241 ui_window_destroy(window
);
245 /** ui_scrollbar_get_geom() returns scrollbar geometry */
249 ui_window_t
*window
= NULL
;
250 ui_wnd_params_t params
;
251 ui_scrollbar_t
*scrollbar
;
252 ui_scrollbar_geom_t geom
;
256 rc
= ui_create_disp(NULL
, &ui
);
257 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
259 ui_wnd_params_init(¶ms
);
260 params
.caption
= "Hello";
262 rc
= ui_window_create(ui
, ¶ms
, &window
);
263 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
264 PCUT_ASSERT_NOT_NULL(window
);
266 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
267 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
273 ui_scrollbar_set_rect(scrollbar
, &rect
);
275 ui_scrollbar_get_geom(scrollbar
, &geom
);
276 PCUT_ASSERT_INT_EQUALS(11, geom
.up_btn_rect
.p0
.x
);
277 PCUT_ASSERT_INT_EQUALS(11, geom
.up_btn_rect
.p0
.y
);
278 PCUT_ASSERT_INT_EQUALS(99, geom
.down_btn_rect
.p1
.x
);
279 PCUT_ASSERT_INT_EQUALS(29, geom
.down_btn_rect
.p1
.y
);
281 ui_scrollbar_destroy(scrollbar
);
282 ui_window_destroy(window
);
286 /** ui_scrollbar_trough_length() gives correct scrollbar trough length */
287 PCUT_TEST(trough_length
)
290 ui_window_t
*window
= NULL
;
291 ui_wnd_params_t params
;
292 ui_scrollbar_t
*scrollbar
;
297 rc
= ui_create_disp(NULL
, &ui
);
298 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
300 ui_wnd_params_init(¶ms
);
301 params
.caption
= "Hello";
303 rc
= ui_window_create(ui
, ¶ms
, &window
);
304 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
305 PCUT_ASSERT_NOT_NULL(window
);
307 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
308 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
310 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
316 ui_scrollbar_set_rect(scrollbar
, &rect
);
318 length
= ui_scrollbar_trough_length(scrollbar
);
320 /* Total length minus buttons */
321 PCUT_ASSERT_INT_EQUALS(110 - 10 - 2 * 21, length
);
323 ui_scrollbar_destroy(scrollbar
);
324 ui_window_destroy(window
);
328 /** ui_scrollbar_move_length() gives correct scrollbar move length */
329 PCUT_TEST(move_length
)
332 ui_window_t
*window
= NULL
;
333 ui_wnd_params_t params
;
334 ui_scrollbar_t
*scrollbar
;
339 rc
= ui_create_disp(NULL
, &ui
);
340 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
342 ui_wnd_params_init(¶ms
);
343 params
.caption
= "Hello";
345 rc
= ui_window_create(ui
, ¶ms
, &window
);
346 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
347 PCUT_ASSERT_NOT_NULL(window
);
349 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
350 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
352 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
358 ui_scrollbar_set_rect(scrollbar
, &rect
);
360 length
= ui_scrollbar_move_length(scrollbar
);
362 /* Total length minus buttons minus default thumb length */
363 PCUT_ASSERT_INT_EQUALS(110 - 10 - 2 * 21 - 21, length
);
365 ui_scrollbar_destroy(scrollbar
);
366 ui_window_destroy(window
);
370 /** ui_scrollbar_get_pos() returns scrollbar position */
374 ui_window_t
*window
= NULL
;
375 ui_wnd_params_t params
;
376 ui_scrollbar_t
*scrollbar
;
381 rc
= ui_create_disp(NULL
, &ui
);
382 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
384 ui_wnd_params_init(¶ms
);
385 params
.caption
= "Hello";
387 rc
= ui_window_create(ui
, ¶ms
, &window
);
388 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
389 PCUT_ASSERT_NOT_NULL(window
);
391 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
392 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
394 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
400 ui_scrollbar_set_rect(scrollbar
, &rect
);
403 pos
= ui_scrollbar_get_pos(scrollbar
);
404 PCUT_ASSERT_INT_EQUALS(42, pos
);
406 ui_scrollbar_destroy(scrollbar
);
407 ui_window_destroy(window
);
411 /** ui_scrollbar_set_thumb_length() sets thumb length */
412 PCUT_TEST(set_thumb_length
)
415 ui_window_t
*window
= NULL
;
416 ui_wnd_params_t params
;
417 ui_scrollbar_t
*scrollbar
;
421 rc
= ui_create_disp(NULL
, &ui
);
422 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
424 ui_wnd_params_init(¶ms
);
425 params
.caption
= "Hello";
427 rc
= ui_window_create(ui
, ¶ms
, &window
);
428 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
429 PCUT_ASSERT_NOT_NULL(window
);
431 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
432 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
434 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
440 ui_scrollbar_set_rect(scrollbar
, &rect
);
442 ui_scrollbar_set_thumb_length(scrollbar
, 42);
443 PCUT_ASSERT_INT_EQUALS(42, scrollbar
->thumb_len
);
445 ui_scrollbar_destroy(scrollbar
);
446 ui_window_destroy(window
);
450 /** ui_scrollbar_set_pos() sets thumb position */
454 ui_window_t
*window
= NULL
;
455 ui_wnd_params_t params
;
456 ui_scrollbar_t
*scrollbar
;
461 rc
= ui_create_disp(NULL
, &ui
);
462 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
464 ui_wnd_params_init(¶ms
);
465 params
.caption
= "Hello";
467 rc
= ui_window_create(ui
, ¶ms
, &window
);
468 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
469 PCUT_ASSERT_NOT_NULL(window
);
471 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
472 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
474 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
480 ui_scrollbar_set_rect(scrollbar
, &rect
);
482 ui_scrollbar_set_pos(scrollbar
, -1);
483 pos
= ui_scrollbar_get_pos(scrollbar
);
484 /* The value is clipped to the minimum possible position (0) */
485 PCUT_ASSERT_INT_EQUALS(0, pos
);
487 ui_scrollbar_set_pos(scrollbar
, 12);
488 pos
= ui_scrollbar_get_pos(scrollbar
);
489 /* The value is set to the requested value */
490 PCUT_ASSERT_INT_EQUALS(12, pos
);
492 ui_scrollbar_set_pos(scrollbar
, 42);
493 pos
= ui_scrollbar_get_pos(scrollbar
);
494 /* The value is clipped to the maximum possible position (37) */
495 PCUT_ASSERT_INT_EQUALS(37, pos
);
497 ui_scrollbar_destroy(scrollbar
);
498 ui_window_destroy(window
);
502 /** Press and release scrollbar thumb */
503 PCUT_TEST(thumb_press_release
)
506 ui_window_t
*window
= NULL
;
507 ui_wnd_params_t params
;
510 ui_scrollbar_t
*scrollbar
;
514 rc
= ui_create_disp(NULL
, &ui
);
515 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
517 ui_wnd_params_init(¶ms
);
518 params
.caption
= "Hello";
520 rc
= ui_window_create(ui
, ¶ms
, &window
);
521 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
522 PCUT_ASSERT_NOT_NULL(window
);
524 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
525 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
531 ui_scrollbar_set_rect(scrollbar
, &rect
);
534 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
536 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
541 ui_scrollbar_thumb_press(scrollbar
, &pos
);
542 PCUT_ASSERT_TRUE(scrollbar
->thumb_held
);
543 PCUT_ASSERT_FALSE(resp
.moved
);
548 ui_scrollbar_release(scrollbar
, &pos
);
549 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
550 PCUT_ASSERT_TRUE(resp
.moved
);
551 PCUT_ASSERT_INT_EQUALS(10, scrollbar
->pos
);
553 ui_scrollbar_destroy(scrollbar
);
554 ui_window_destroy(window
);
558 /** Press, update and release scrollbar */
559 PCUT_TEST(thumb_press_update_release
)
562 ui_window_t
*window
= NULL
;
563 ui_wnd_params_t params
;
566 ui_scrollbar_t
*scrollbar
;
570 rc
= ui_create_disp(NULL
, &ui
);
571 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
573 ui_wnd_params_init(¶ms
);
574 params
.caption
= "Hello";
576 rc
= ui_window_create(ui
, ¶ms
, &window
);
577 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
578 PCUT_ASSERT_NOT_NULL(window
);
580 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
581 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
587 ui_scrollbar_set_rect(scrollbar
, &rect
);
590 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
592 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
597 ui_scrollbar_thumb_press(scrollbar
, &pos
);
598 PCUT_ASSERT_TRUE(scrollbar
->thumb_held
);
599 PCUT_ASSERT_FALSE(resp
.moved
);
604 ui_scrollbar_update(scrollbar
, &pos
);
605 PCUT_ASSERT_TRUE(scrollbar
->thumb_held
);
606 PCUT_ASSERT_TRUE(resp
.moved
);
607 PCUT_ASSERT_INT_EQUALS(10, scrollbar
->pos
);
612 ui_scrollbar_release(scrollbar
, &pos
);
613 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
614 PCUT_ASSERT_TRUE(resp
.moved
);
615 PCUT_ASSERT_INT_EQUALS(20, scrollbar
->pos
);
617 ui_scrollbar_destroy(scrollbar
);
618 ui_window_destroy(window
);
622 /** Press and release upper trough */
623 PCUT_TEST(upper_trough_press_release
)
626 ui_window_t
*window
= NULL
;
627 ui_wnd_params_t params
;
630 ui_scrollbar_t
*scrollbar
;
634 rc
= ui_create_disp(NULL
, &ui
);
635 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
637 ui_wnd_params_init(¶ms
);
638 params
.caption
= "Hello";
640 rc
= ui_window_create(ui
, ¶ms
, &window
);
641 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
642 PCUT_ASSERT_NOT_NULL(window
);
644 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
645 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
651 ui_scrollbar_set_rect(scrollbar
, &rect
);
653 resp
.page_up
= false;
654 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
656 PCUT_ASSERT_FALSE(scrollbar
->upper_trough_held
);
658 ui_scrollbar_upper_trough_press(scrollbar
);
659 PCUT_ASSERT_TRUE(scrollbar
->upper_trough_held
);
660 PCUT_ASSERT_TRUE(resp
.page_up
);
662 /* Position does not matter here */
666 ui_scrollbar_release(scrollbar
, &pos
);
667 PCUT_ASSERT_FALSE(scrollbar
->upper_trough_held
);
669 ui_scrollbar_destroy(scrollbar
);
670 ui_window_destroy(window
);
674 /** Press and release lower trough */
675 PCUT_TEST(lower_trough_press_release
)
678 ui_window_t
*window
= NULL
;
679 ui_wnd_params_t params
;
682 ui_scrollbar_t
*scrollbar
;
686 rc
= ui_create_disp(NULL
, &ui
);
687 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
689 ui_wnd_params_init(¶ms
);
690 params
.caption
= "Hello";
692 rc
= ui_window_create(ui
, ¶ms
, &window
);
693 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
694 PCUT_ASSERT_NOT_NULL(window
);
696 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
697 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
703 ui_scrollbar_set_rect(scrollbar
, &rect
);
705 resp
.page_down
= false;
706 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
708 PCUT_ASSERT_FALSE(scrollbar
->lower_trough_held
);
710 ui_scrollbar_lower_trough_press(scrollbar
);
711 PCUT_ASSERT_TRUE(scrollbar
->lower_trough_held
);
712 PCUT_ASSERT_TRUE(resp
.page_down
);
714 /* Position does not matter here */
718 ui_scrollbar_release(scrollbar
, &pos
);
719 PCUT_ASSERT_FALSE(scrollbar
->lower_trough_held
);
721 ui_scrollbar_destroy(scrollbar
);
722 ui_window_destroy(window
);
726 /** Updating state of troughs when cursor or thumb moves */
727 PCUT_TEST(troughs_update
)
730 ui_window_t
*window
= NULL
;
731 ui_wnd_params_t params
;
734 ui_scrollbar_t
*scrollbar
;
737 rc
= ui_create_disp(NULL
, &ui
);
738 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
740 ui_wnd_params_init(¶ms
);
741 params
.caption
= "Hello";
743 rc
= ui_window_create(ui
, ¶ms
, &window
);
744 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
745 PCUT_ASSERT_NOT_NULL(window
);
747 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
748 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
754 ui_scrollbar_set_rect(scrollbar
, &rect
);
756 PCUT_ASSERT_FALSE(scrollbar
->lower_trough_inside
);
761 ui_scrollbar_troughs_update(scrollbar
, &pos
);
762 PCUT_ASSERT_TRUE(scrollbar
->lower_trough_inside
);
764 ui_scrollbar_destroy(scrollbar
);
765 ui_window_destroy(window
);
769 /** ui_scrollbar_up() delivers up event */
773 ui_window_t
*window
= NULL
;
774 ui_wnd_params_t params
;
775 ui_scrollbar_t
*scrollbar
;
779 rc
= ui_create_disp(NULL
, &ui
);
780 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
782 ui_wnd_params_init(¶ms
);
783 params
.caption
= "Hello";
785 rc
= ui_window_create(ui
, ¶ms
, &window
);
786 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
787 PCUT_ASSERT_NOT_NULL(window
);
789 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
790 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
792 /* Up with no callbacks set */
793 ui_scrollbar_up(scrollbar
);
795 /* Up with callback not implementing up */
796 ui_scrollbar_set_cb(scrollbar
, &dummy_scrollbar_cb
, NULL
);
797 ui_scrollbar_up(scrollbar
);
799 /* Up with real callback set */
801 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
802 ui_scrollbar_up(scrollbar
);
803 PCUT_ASSERT_TRUE(resp
.up
);
805 ui_scrollbar_destroy(scrollbar
);
806 ui_window_destroy(window
);
810 /** ui_scrollbar_down() delivers down event */
814 ui_window_t
*window
= NULL
;
815 ui_wnd_params_t params
;
816 ui_scrollbar_t
*scrollbar
;
820 rc
= ui_create_disp(NULL
, &ui
);
821 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
823 ui_wnd_params_init(¶ms
);
824 params
.caption
= "Hello";
826 rc
= ui_window_create(ui
, ¶ms
, &window
);
827 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
828 PCUT_ASSERT_NOT_NULL(window
);
830 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
831 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
833 /* Down with no callbacks set */
834 ui_scrollbar_down(scrollbar
);
836 /* Down with callback not implementing down */
837 ui_scrollbar_set_cb(scrollbar
, &dummy_scrollbar_cb
, NULL
);
838 ui_scrollbar_down(scrollbar
);
840 /* Down with real callback set */
842 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
843 ui_scrollbar_down(scrollbar
);
844 PCUT_ASSERT_TRUE(resp
.down
);
846 ui_scrollbar_destroy(scrollbar
);
847 ui_window_destroy(window
);
851 /** ui_scrollbar_page_up() delivers page up event */
855 ui_window_t
*window
= NULL
;
856 ui_wnd_params_t params
;
857 ui_scrollbar_t
*scrollbar
;
861 rc
= ui_create_disp(NULL
, &ui
);
862 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
864 ui_wnd_params_init(¶ms
);
865 params
.caption
= "Hello";
867 rc
= ui_window_create(ui
, ¶ms
, &window
);
868 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
869 PCUT_ASSERT_NOT_NULL(window
);
871 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
872 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
874 /* Page up with no callbacks set */
875 ui_scrollbar_page_up(scrollbar
);
877 /* Pge up with callback not implementing page up */
878 ui_scrollbar_set_cb(scrollbar
, &dummy_scrollbar_cb
, NULL
);
879 ui_scrollbar_page_up(scrollbar
);
881 /* Page up with real callback set */
882 resp
.page_up
= false;
883 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
884 ui_scrollbar_page_up(scrollbar
);
885 PCUT_ASSERT_TRUE(resp
.page_up
);
887 ui_scrollbar_destroy(scrollbar
);
888 ui_window_destroy(window
);
892 /** ui_scrollbar_page_down() delivers page down event */
896 ui_window_t
*window
= NULL
;
897 ui_wnd_params_t params
;
898 ui_scrollbar_t
*scrollbar
;
902 rc
= ui_create_disp(NULL
, &ui
);
903 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
905 ui_wnd_params_init(¶ms
);
906 params
.caption
= "Hello";
908 rc
= ui_window_create(ui
, ¶ms
, &window
);
909 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
910 PCUT_ASSERT_NOT_NULL(window
);
912 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
913 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
915 /* Page down with no callbacks set */
916 ui_scrollbar_page_down(scrollbar
);
918 /* Page down with callback not implementing page down */
919 ui_scrollbar_set_cb(scrollbar
, &dummy_scrollbar_cb
, NULL
);
920 ui_scrollbar_page_down(scrollbar
);
922 /* Page down with real callback set */
923 resp
.page_down
= false;
924 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
925 ui_scrollbar_page_down(scrollbar
);
926 PCUT_ASSERT_TRUE(resp
.page_down
);
928 ui_scrollbar_destroy(scrollbar
);
929 ui_window_destroy(window
);
933 /** ui_scrollbar_moved() delivers moved event */
937 ui_window_t
*window
= NULL
;
938 ui_wnd_params_t params
;
939 ui_scrollbar_t
*scrollbar
;
943 rc
= ui_create_disp(NULL
, &ui
);
944 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
946 ui_wnd_params_init(¶ms
);
947 params
.caption
= "Hello";
949 rc
= ui_window_create(ui
, ¶ms
, &window
);
950 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
951 PCUT_ASSERT_NOT_NULL(window
);
953 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
954 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
956 /* Moved with no callbacks set */
957 ui_scrollbar_moved(scrollbar
, 42);
959 /* Moved with callback not implementing moved */
960 ui_scrollbar_set_cb(scrollbar
, &dummy_scrollbar_cb
, NULL
);
961 ui_scrollbar_moved(scrollbar
, 42);
963 /* Moved with real callback set */
966 ui_scrollbar_set_cb(scrollbar
, &test_scrollbar_cb
, &resp
);
967 ui_scrollbar_moved(scrollbar
, 42);
968 PCUT_ASSERT_TRUE(resp
.moved
);
969 PCUT_ASSERT_INT_EQUALS(42, resp
.pos
);
971 ui_scrollbar_destroy(scrollbar
);
972 ui_window_destroy(window
);
976 /** ui_scrollbar_pos_event() detects thumb press/release */
977 PCUT_TEST(pos_event_press_release_thumb
)
980 ui_window_t
*window
= NULL
;
981 ui_wnd_params_t params
;
982 ui_scrollbar_t
*scrollbar
;
988 rc
= ui_create_disp(NULL
, &ui
);
989 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
991 ui_wnd_params_init(¶ms
);
992 params
.caption
= "Hello";
994 rc
= ui_window_create(ui
, ¶ms
, &window
);
995 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
996 PCUT_ASSERT_NOT_NULL(window
);
998 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
999 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1001 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
1007 ui_scrollbar_set_rect(scrollbar
, &rect
);
1009 /* Press outside is not claimed and does nothing */
1010 event
.type
= POS_PRESS
;
1013 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1014 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
1015 PCUT_ASSERT_EQUALS(ui_unclaimed
, claim
);
1017 /* Press inside thumb is claimed and depresses it */
1018 event
.type
= POS_PRESS
;
1021 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1022 PCUT_ASSERT_TRUE(scrollbar
->thumb_held
);
1023 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1025 /* Release outside (or anywhere) is claimed and relases thumb */
1026 event
.type
= POS_RELEASE
;
1029 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1030 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
1031 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1033 ui_scrollbar_destroy(scrollbar
);
1034 ui_window_destroy(window
);
1038 /** ui_scrollbar_pos_event() detects up button press/release */
1039 PCUT_TEST(pos_event_press_release_up_btn
)
1042 ui_window_t
*window
= NULL
;
1043 ui_wnd_params_t params
;
1044 ui_scrollbar_t
*scrollbar
;
1050 rc
= ui_create_disp(NULL
, &ui
);
1051 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1053 ui_wnd_params_init(¶ms
);
1054 params
.caption
= "Hello";
1056 rc
= ui_window_create(ui
, ¶ms
, &window
);
1057 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1058 PCUT_ASSERT_NOT_NULL(window
);
1060 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
1061 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1063 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
1069 ui_scrollbar_set_rect(scrollbar
, &rect
);
1071 /* Press inside up button is claimed and depresses it */
1072 event
.type
= POS_PRESS
;
1075 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1076 PCUT_ASSERT_TRUE(scrollbar
->up_btn
->held
);
1077 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1079 ui_scrollbar_destroy(scrollbar
);
1080 ui_window_destroy(window
);
1084 /** ui_scrollbar_pos_event() detects upper trough press/release */
1085 PCUT_TEST(pos_event_press_release_upper_trough
)
1088 ui_window_t
*window
= NULL
;
1089 ui_wnd_params_t params
;
1090 ui_scrollbar_t
*scrollbar
;
1096 rc
= ui_create_disp(NULL
, &ui
);
1097 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1099 ui_wnd_params_init(¶ms
);
1100 params
.caption
= "Hello";
1102 rc
= ui_window_create(ui
, ¶ms
, &window
);
1103 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1104 PCUT_ASSERT_NOT_NULL(window
);
1106 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
1107 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1109 PCUT_ASSERT_FALSE(scrollbar
->upper_trough_held
);
1115 ui_scrollbar_set_rect(scrollbar
, &rect
);
1117 /* Need to move thumb so that upper trough can be accessed */
1118 ui_scrollbar_set_pos(scrollbar
, 42);
1120 /* Press inside upper trough is claimed and depresses it */
1121 event
.type
= POS_PRESS
;
1124 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1125 PCUT_ASSERT_TRUE(scrollbar
->upper_trough_held
);
1126 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1128 /* Release outside (or anywhere) is claimed and relases upper trough */
1129 event
.type
= POS_RELEASE
;
1132 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1133 PCUT_ASSERT_FALSE(scrollbar
->upper_trough_held
);
1134 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1136 ui_scrollbar_destroy(scrollbar
);
1137 ui_window_destroy(window
);
1141 /** ui_scrollbar_pos_event() detects lower trough press/release */
1142 PCUT_TEST(pos_event_press_release_lower_trough
)
1145 ui_window_t
*window
= NULL
;
1146 ui_wnd_params_t params
;
1147 ui_scrollbar_t
*scrollbar
;
1153 rc
= ui_create_disp(NULL
, &ui
);
1154 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1156 ui_wnd_params_init(¶ms
);
1157 params
.caption
= "Hello";
1159 rc
= ui_window_create(ui
, ¶ms
, &window
);
1160 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1161 PCUT_ASSERT_NOT_NULL(window
);
1163 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
1164 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1166 PCUT_ASSERT_FALSE(scrollbar
->upper_trough_held
);
1172 ui_scrollbar_set_rect(scrollbar
, &rect
);
1174 /* Press inside lower trough is claimed and depresses it */
1175 event
.type
= POS_PRESS
;
1178 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1179 PCUT_ASSERT_TRUE(scrollbar
->lower_trough_held
);
1180 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1182 /* Release outside (or anywhere) is claimed and relases upper trough */
1183 event
.type
= POS_RELEASE
;
1186 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1187 PCUT_ASSERT_FALSE(scrollbar
->lower_trough_held
);
1188 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1190 ui_scrollbar_destroy(scrollbar
);
1191 ui_window_destroy(window
);
1195 /** ui_scrollbar_pos_event() detects down button press/release */
1196 PCUT_TEST(pos_event_press_relese_down_btn
)
1199 ui_window_t
*window
= NULL
;
1200 ui_wnd_params_t params
;
1201 ui_scrollbar_t
*scrollbar
;
1207 rc
= ui_create_disp(NULL
, &ui
);
1208 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1210 ui_wnd_params_init(¶ms
);
1211 params
.caption
= "Hello";
1213 rc
= ui_window_create(ui
, ¶ms
, &window
);
1214 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1215 PCUT_ASSERT_NOT_NULL(window
);
1217 rc
= ui_scrollbar_create(ui
, window
, ui_sbd_horiz
, &scrollbar
);
1218 PCUT_ASSERT_ERRNO_VAL(EOK
, rc
);
1220 PCUT_ASSERT_FALSE(scrollbar
->thumb_held
);
1226 ui_scrollbar_set_rect(scrollbar
, &rect
);
1228 /* Press inside down button is claimed and depresses it */
1229 event
.type
= POS_PRESS
;
1232 claim
= ui_scrollbar_pos_event(scrollbar
, &event
);
1233 PCUT_ASSERT_TRUE(scrollbar
->down_btn
->held
);
1234 PCUT_ASSERT_EQUALS(ui_claimed
, claim
);
1236 ui_scrollbar_destroy(scrollbar
);
1237 ui_window_destroy(window
);
1241 static void test_scrollbar_up(ui_scrollbar_t
*scrollbar
, void *arg
)
1243 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
1248 static void test_scrollbar_down(ui_scrollbar_t
*scrollbar
, void *arg
)
1250 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
1255 static void test_scrollbar_page_up(ui_scrollbar_t
*scrollbar
, void *arg
)
1257 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
1259 resp
->page_up
= true;
1262 static void test_scrollbar_page_down(ui_scrollbar_t
*scrollbar
, void *arg
)
1264 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
1266 resp
->page_down
= true;
1269 static void test_scrollbar_moved(ui_scrollbar_t
*scrollbar
, void *arg
, gfx_coord_t pos
)
1271 test_cb_resp_t
*resp
= (test_cb_resp_t
*) arg
;
1277 PCUT_EXPORT(scrollbar
);