Editor needs to hide cursor when menu bar is activated
[helenos.git] / uspace / lib / ui / test / scrollbar.c
blobb24fc87f182085f6988de39f4e7903d70a70c7db
1 /*
2 * Copyright (c) 2023 Jiri Svoboda
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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>
31 #include <mem.h>
32 #include <pcut/pcut.h>
33 #include <stdbool.h>
34 #include <ui/control.h>
35 #include <ui/scrollbar.h>
36 #include <ui/resource.h>
37 #include <ui/ui.h>
38 #include <ui/window.h>
39 #include "../private/pbutton.h"
40 #include "../private/scrollbar.h"
42 PCUT_INIT;
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 = {
63 typedef struct {
64 bool up;
65 bool down;
66 bool page_up;
67 bool page_down;
68 bool moved;
69 gfx_coord_t pos;
70 } test_cb_resp_t;
72 /** Create and destroy scrollbar */
73 PCUT_TEST(create_destroy)
75 ui_t *ui = NULL;
76 ui_window_t *window = NULL;
77 ui_wnd_params_t params;
78 ui_scrollbar_t *scrollbar = NULL;
79 errno_t rc;
81 rc = ui_create_disp(NULL, &ui);
82 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
84 ui_wnd_params_init(&params);
85 params.caption = "Hello";
87 rc = ui_window_create(ui, &params, &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);
97 ui_destroy(ui);
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 */
107 PCUT_TEST(ctl)
109 ui_t *ui = NULL;
110 ui_window_t *window = NULL;
111 ui_wnd_params_t params;
112 ui_scrollbar_t *scrollbar = NULL;
113 ui_control_t *control;
114 errno_t rc;
116 rc = ui_create_disp(NULL, &ui);
117 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
119 ui_wnd_params_init(&params);
120 params.caption = "Hello";
122 rc = ui_window_create(ui, &params, &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);
135 ui_destroy(ui);
138 /** Set scrollbar rectangle sets internal field */
139 PCUT_TEST(set_rect)
141 ui_t *ui = NULL;
142 ui_window_t *window = NULL;
143 ui_wnd_params_t params;
144 ui_scrollbar_t *scrollbar = NULL;
145 gfx_rect_t rect;
146 errno_t rc;
148 rc = ui_create_disp(NULL, &ui);
149 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
151 ui_wnd_params_init(&params);
152 params.caption = "Hello";
154 rc = ui_window_create(ui, &params, &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);
162 rect.p0.x = 1;
163 rect.p0.y = 2;
164 rect.p1.x = 3;
165 rect.p1.y = 4;
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);
175 ui_destroy(ui);
178 /** Paint scrollbar in graphics mode */
179 PCUT_TEST(paint_gfx)
181 ui_t *ui = NULL;
182 ui_window_t *window = NULL;
183 ui_wnd_params_t params;
184 ui_scrollbar_t *scrollbar;
185 errno_t rc;
187 rc = ui_create_disp(NULL, &ui);
188 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
190 ui_wnd_params_init(&params);
191 params.caption = "Hello";
193 rc = ui_window_create(ui, &params, &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);
205 ui_destroy(ui);
208 /** Paint scrollbar in text mode */
209 PCUT_TEST(paint_text_horiz)
211 ui_t *ui = NULL;
212 ui_window_t *window = NULL;
213 ui_wnd_params_t params;
214 ui_scrollbar_t *scrollbar;
215 gfx_rect_t rect;
216 errno_t rc;
218 rc = ui_create_disp(NULL, &ui);
219 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
221 ui_wnd_params_init(&params);
222 params.caption = "Hello";
224 rc = ui_window_create(ui, &params, &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);
231 rect.p0.x = 1;
232 rect.p0.y = 1;
233 rect.p1.x = 10;
234 rect.p1.y = 2;
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);
242 ui_destroy(ui);
245 /** ui_scrollbar_get_geom() returns scrollbar geometry */
246 PCUT_TEST(get_geom)
248 ui_t *ui = NULL;
249 ui_window_t *window = NULL;
250 ui_wnd_params_t params;
251 ui_scrollbar_t *scrollbar;
252 ui_scrollbar_geom_t geom;
253 gfx_rect_t rect;
254 errno_t rc;
256 rc = ui_create_disp(NULL, &ui);
257 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
259 ui_wnd_params_init(&params);
260 params.caption = "Hello";
262 rc = ui_window_create(ui, &params, &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);
269 rect.p0.x = 10;
270 rect.p0.y = 10;
271 rect.p1.x = 100;
272 rect.p1.y = 30;
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);
283 ui_destroy(ui);
286 /** ui_scrollbar_trough_length() gives correct scrollbar trough length */
287 PCUT_TEST(trough_length)
289 ui_t *ui = NULL;
290 ui_window_t *window = NULL;
291 ui_wnd_params_t params;
292 ui_scrollbar_t *scrollbar;
293 gfx_coord_t length;
294 gfx_rect_t rect;
295 errno_t rc;
297 rc = ui_create_disp(NULL, &ui);
298 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
300 ui_wnd_params_init(&params);
301 params.caption = "Hello";
303 rc = ui_window_create(ui, &params, &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);
312 rect.p0.x = 10;
313 rect.p0.y = 20;
314 rect.p1.x = 110;
315 rect.p1.y = 120;
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);
325 ui_destroy(ui);
328 /** ui_scrollbar_move_length() gives correct scrollbar move length */
329 PCUT_TEST(move_length)
331 ui_t *ui = NULL;
332 ui_window_t *window = NULL;
333 ui_wnd_params_t params;
334 ui_scrollbar_t *scrollbar;
335 gfx_coord_t length;
336 gfx_rect_t rect;
337 errno_t rc;
339 rc = ui_create_disp(NULL, &ui);
340 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
342 ui_wnd_params_init(&params);
343 params.caption = "Hello";
345 rc = ui_window_create(ui, &params, &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);
354 rect.p0.x = 10;
355 rect.p0.y = 20;
356 rect.p1.x = 110;
357 rect.p1.y = 120;
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);
367 ui_destroy(ui);
370 /** ui_scrollbar_get_pos() returns scrollbar position */
371 PCUT_TEST(get_pos)
373 ui_t *ui = NULL;
374 ui_window_t *window = NULL;
375 ui_wnd_params_t params;
376 ui_scrollbar_t *scrollbar;
377 gfx_coord_t pos;
378 gfx_rect_t rect;
379 errno_t rc;
381 rc = ui_create_disp(NULL, &ui);
382 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
384 ui_wnd_params_init(&params);
385 params.caption = "Hello";
387 rc = ui_window_create(ui, &params, &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);
396 rect.p0.x = 10;
397 rect.p0.y = 20;
398 rect.p1.x = 110;
399 rect.p1.y = 120;
400 ui_scrollbar_set_rect(scrollbar, &rect);
402 scrollbar->pos = 42;
403 pos = ui_scrollbar_get_pos(scrollbar);
404 PCUT_ASSERT_INT_EQUALS(42, pos);
406 ui_scrollbar_destroy(scrollbar);
407 ui_window_destroy(window);
408 ui_destroy(ui);
411 /** ui_scrollbar_set_thumb_length() sets thumb length */
412 PCUT_TEST(set_thumb_length)
414 ui_t *ui = NULL;
415 ui_window_t *window = NULL;
416 ui_wnd_params_t params;
417 ui_scrollbar_t *scrollbar;
418 gfx_rect_t rect;
419 errno_t rc;
421 rc = ui_create_disp(NULL, &ui);
422 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
424 ui_wnd_params_init(&params);
425 params.caption = "Hello";
427 rc = ui_window_create(ui, &params, &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);
436 rect.p0.x = 10;
437 rect.p0.y = 20;
438 rect.p1.x = 110;
439 rect.p1.y = 120;
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);
447 ui_destroy(ui);
450 /** ui_scrollbar_set_pos() sets thumb position */
451 PCUT_TEST(set_pos)
453 ui_t *ui = NULL;
454 ui_window_t *window = NULL;
455 ui_wnd_params_t params;
456 ui_scrollbar_t *scrollbar;
457 gfx_coord_t pos;
458 gfx_rect_t rect;
459 errno_t rc;
461 rc = ui_create_disp(NULL, &ui);
462 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
464 ui_wnd_params_init(&params);
465 params.caption = "Hello";
467 rc = ui_window_create(ui, &params, &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);
476 rect.p0.x = 10;
477 rect.p0.y = 20;
478 rect.p1.x = 110;
479 rect.p1.y = 120;
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);
499 ui_destroy(ui);
502 /** Press and release scrollbar thumb */
503 PCUT_TEST(thumb_press_release)
505 ui_t *ui = NULL;
506 ui_window_t *window = NULL;
507 ui_wnd_params_t params;
508 gfx_coord2_t pos;
509 gfx_rect_t rect;
510 ui_scrollbar_t *scrollbar;
511 test_cb_resp_t resp;
512 errno_t rc;
514 rc = ui_create_disp(NULL, &ui);
515 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
517 ui_wnd_params_init(&params);
518 params.caption = "Hello";
520 rc = ui_window_create(ui, &params, &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);
527 rect.p0.x = 10;
528 rect.p0.y = 20;
529 rect.p1.x = 110;
530 rect.p1.y = 120;
531 ui_scrollbar_set_rect(scrollbar, &rect);
533 resp.moved = false;
534 ui_scrollbar_set_cb(scrollbar, &test_scrollbar_cb, &resp);
536 PCUT_ASSERT_FALSE(scrollbar->thumb_held);
538 pos.x = 11;
539 pos.y = 22;
541 ui_scrollbar_thumb_press(scrollbar, &pos);
542 PCUT_ASSERT_TRUE(scrollbar->thumb_held);
543 PCUT_ASSERT_FALSE(resp.moved);
545 pos.x = 21;
546 pos.y = 32;
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);
555 ui_destroy(ui);
558 /** Press, update and release scrollbar */
559 PCUT_TEST(thumb_press_update_release)
561 ui_t *ui = NULL;
562 ui_window_t *window = NULL;
563 ui_wnd_params_t params;
564 gfx_coord2_t pos;
565 gfx_rect_t rect;
566 ui_scrollbar_t *scrollbar;
567 test_cb_resp_t resp;
568 errno_t rc;
570 rc = ui_create_disp(NULL, &ui);
571 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
573 ui_wnd_params_init(&params);
574 params.caption = "Hello";
576 rc = ui_window_create(ui, &params, &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);
583 rect.p0.x = 10;
584 rect.p0.y = 20;
585 rect.p1.x = 110;
586 rect.p1.y = 120;
587 ui_scrollbar_set_rect(scrollbar, &rect);
589 resp.moved = false;
590 ui_scrollbar_set_cb(scrollbar, &test_scrollbar_cb, &resp);
592 PCUT_ASSERT_FALSE(scrollbar->thumb_held);
594 pos.x = 11;
595 pos.y = 22;
597 ui_scrollbar_thumb_press(scrollbar, &pos);
598 PCUT_ASSERT_TRUE(scrollbar->thumb_held);
599 PCUT_ASSERT_FALSE(resp.moved);
601 pos.x = 21;
602 pos.y = 32;
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);
609 pos.x = 31;
610 pos.y = 42;
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);
619 ui_destroy(ui);
622 /** Press and release upper trough */
623 PCUT_TEST(upper_trough_press_release)
625 ui_t *ui = NULL;
626 ui_window_t *window = NULL;
627 ui_wnd_params_t params;
628 gfx_coord2_t pos;
629 gfx_rect_t rect;
630 ui_scrollbar_t *scrollbar;
631 test_cb_resp_t resp;
632 errno_t rc;
634 rc = ui_create_disp(NULL, &ui);
635 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
637 ui_wnd_params_init(&params);
638 params.caption = "Hello";
640 rc = ui_window_create(ui, &params, &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);
647 rect.p0.x = 10;
648 rect.p0.y = 20;
649 rect.p1.x = 110;
650 rect.p1.y = 120;
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 */
663 pos.x = 11;
664 pos.y = 22;
666 ui_scrollbar_release(scrollbar, &pos);
667 PCUT_ASSERT_FALSE(scrollbar->upper_trough_held);
669 ui_scrollbar_destroy(scrollbar);
670 ui_window_destroy(window);
671 ui_destroy(ui);
674 /** Press and release lower trough */
675 PCUT_TEST(lower_trough_press_release)
677 ui_t *ui = NULL;
678 ui_window_t *window = NULL;
679 ui_wnd_params_t params;
680 gfx_coord2_t pos;
681 gfx_rect_t rect;
682 ui_scrollbar_t *scrollbar;
683 test_cb_resp_t resp;
684 errno_t rc;
686 rc = ui_create_disp(NULL, &ui);
687 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
689 ui_wnd_params_init(&params);
690 params.caption = "Hello";
692 rc = ui_window_create(ui, &params, &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);
699 rect.p0.x = 10;
700 rect.p0.y = 20;
701 rect.p1.x = 110;
702 rect.p1.y = 120;
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 */
715 pos.x = 11;
716 pos.y = 22;
718 ui_scrollbar_release(scrollbar, &pos);
719 PCUT_ASSERT_FALSE(scrollbar->lower_trough_held);
721 ui_scrollbar_destroy(scrollbar);
722 ui_window_destroy(window);
723 ui_destroy(ui);
726 /** Updating state of troughs when cursor or thumb moves */
727 PCUT_TEST(troughs_update)
729 ui_t *ui = NULL;
730 ui_window_t *window = NULL;
731 ui_wnd_params_t params;
732 gfx_coord2_t pos;
733 gfx_rect_t rect;
734 ui_scrollbar_t *scrollbar;
735 errno_t rc;
737 rc = ui_create_disp(NULL, &ui);
738 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
740 ui_wnd_params_init(&params);
741 params.caption = "Hello";
743 rc = ui_window_create(ui, &params, &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);
750 rect.p0.x = 10;
751 rect.p0.y = 20;
752 rect.p1.x = 110;
753 rect.p1.y = 120;
754 ui_scrollbar_set_rect(scrollbar, &rect);
756 PCUT_ASSERT_FALSE(scrollbar->lower_trough_inside);
758 pos.x = 60;
759 pos.y = 22;
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);
766 ui_destroy(ui);
769 /** ui_scrollbar_up() delivers up event */
770 PCUT_TEST(up)
772 ui_t *ui = NULL;
773 ui_window_t *window = NULL;
774 ui_wnd_params_t params;
775 ui_scrollbar_t *scrollbar;
776 test_cb_resp_t resp;
777 errno_t rc;
779 rc = ui_create_disp(NULL, &ui);
780 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
782 ui_wnd_params_init(&params);
783 params.caption = "Hello";
785 rc = ui_window_create(ui, &params, &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 */
800 resp.up = false;
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);
807 ui_destroy(ui);
810 /** ui_scrollbar_down() delivers down event */
811 PCUT_TEST(down)
813 ui_t *ui = NULL;
814 ui_window_t *window = NULL;
815 ui_wnd_params_t params;
816 ui_scrollbar_t *scrollbar;
817 test_cb_resp_t resp;
818 errno_t rc;
820 rc = ui_create_disp(NULL, &ui);
821 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
823 ui_wnd_params_init(&params);
824 params.caption = "Hello";
826 rc = ui_window_create(ui, &params, &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 */
841 resp.down = false;
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);
848 ui_destroy(ui);
851 /** ui_scrollbar_page_up() delivers page up event */
852 PCUT_TEST(page_up)
854 ui_t *ui = NULL;
855 ui_window_t *window = NULL;
856 ui_wnd_params_t params;
857 ui_scrollbar_t *scrollbar;
858 test_cb_resp_t resp;
859 errno_t rc;
861 rc = ui_create_disp(NULL, &ui);
862 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
864 ui_wnd_params_init(&params);
865 params.caption = "Hello";
867 rc = ui_window_create(ui, &params, &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);
889 ui_destroy(ui);
892 /** ui_scrollbar_page_down() delivers page down event */
893 PCUT_TEST(page_down)
895 ui_t *ui = NULL;
896 ui_window_t *window = NULL;
897 ui_wnd_params_t params;
898 ui_scrollbar_t *scrollbar;
899 test_cb_resp_t resp;
900 errno_t rc;
902 rc = ui_create_disp(NULL, &ui);
903 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
905 ui_wnd_params_init(&params);
906 params.caption = "Hello";
908 rc = ui_window_create(ui, &params, &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);
930 ui_destroy(ui);
933 /** ui_scrollbar_moved() delivers moved event */
934 PCUT_TEST(moved)
936 ui_t *ui = NULL;
937 ui_window_t *window = NULL;
938 ui_wnd_params_t params;
939 ui_scrollbar_t *scrollbar;
940 test_cb_resp_t resp;
941 errno_t rc;
943 rc = ui_create_disp(NULL, &ui);
944 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
946 ui_wnd_params_init(&params);
947 params.caption = "Hello";
949 rc = ui_window_create(ui, &params, &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 */
964 resp.moved = false;
965 resp.pos = 0;
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);
973 ui_destroy(ui);
976 /** ui_scrollbar_pos_event() detects thumb press/release */
977 PCUT_TEST(pos_event_press_release_thumb)
979 ui_t *ui = NULL;
980 ui_window_t *window = NULL;
981 ui_wnd_params_t params;
982 ui_scrollbar_t *scrollbar;
983 ui_evclaim_t claim;
984 pos_event_t event;
985 gfx_rect_t rect;
986 errno_t rc;
988 rc = ui_create_disp(NULL, &ui);
989 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
991 ui_wnd_params_init(&params);
992 params.caption = "Hello";
994 rc = ui_window_create(ui, &params, &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);
1003 rect.p0.x = 20;
1004 rect.p0.y = 10;
1005 rect.p1.x = 100;
1006 rect.p1.y = 30;
1007 ui_scrollbar_set_rect(scrollbar, &rect);
1009 /* Press outside is not claimed and does nothing */
1010 event.type = POS_PRESS;
1011 event.hpos = 1;
1012 event.vpos = 2;
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;
1019 event.hpos = 50;
1020 event.vpos = 20;
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;
1027 event.hpos = 41;
1028 event.vpos = 32;
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);
1035 ui_destroy(ui);
1038 /** ui_scrollbar_pos_event() detects up button press/release */
1039 PCUT_TEST(pos_event_press_release_up_btn)
1041 ui_t *ui = NULL;
1042 ui_window_t *window = NULL;
1043 ui_wnd_params_t params;
1044 ui_scrollbar_t *scrollbar;
1045 ui_evclaim_t claim;
1046 pos_event_t event;
1047 gfx_rect_t rect;
1048 errno_t rc;
1050 rc = ui_create_disp(NULL, &ui);
1051 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1053 ui_wnd_params_init(&params);
1054 params.caption = "Hello";
1056 rc = ui_window_create(ui, &params, &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);
1065 rect.p0.x = 20;
1066 rect.p0.y = 10;
1067 rect.p1.x = 100;
1068 rect.p1.y = 30;
1069 ui_scrollbar_set_rect(scrollbar, &rect);
1071 /* Press inside up button is claimed and depresses it */
1072 event.type = POS_PRESS;
1073 event.hpos = 30;
1074 event.vpos = 20;
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);
1081 ui_destroy(ui);
1084 /** ui_scrollbar_pos_event() detects upper trough press/release */
1085 PCUT_TEST(pos_event_press_release_upper_trough)
1087 ui_t *ui = NULL;
1088 ui_window_t *window = NULL;
1089 ui_wnd_params_t params;
1090 ui_scrollbar_t *scrollbar;
1091 ui_evclaim_t claim;
1092 pos_event_t event;
1093 gfx_rect_t rect;
1094 errno_t rc;
1096 rc = ui_create_disp(NULL, &ui);
1097 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1099 ui_wnd_params_init(&params);
1100 params.caption = "Hello";
1102 rc = ui_window_create(ui, &params, &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);
1111 rect.p0.x = 20;
1112 rect.p0.y = 10;
1113 rect.p1.x = 100;
1114 rect.p1.y = 30;
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;
1122 event.hpos = 50;
1123 event.vpos = 20;
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;
1130 event.hpos = 41;
1131 event.vpos = 32;
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);
1138 ui_destroy(ui);
1141 /** ui_scrollbar_pos_event() detects lower trough press/release */
1142 PCUT_TEST(pos_event_press_release_lower_trough)
1144 ui_t *ui = NULL;
1145 ui_window_t *window = NULL;
1146 ui_wnd_params_t params;
1147 ui_scrollbar_t *scrollbar;
1148 ui_evclaim_t claim;
1149 pos_event_t event;
1150 gfx_rect_t rect;
1151 errno_t rc;
1153 rc = ui_create_disp(NULL, &ui);
1154 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1156 ui_wnd_params_init(&params);
1157 params.caption = "Hello";
1159 rc = ui_window_create(ui, &params, &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);
1168 rect.p0.x = 20;
1169 rect.p0.y = 10;
1170 rect.p1.x = 100;
1171 rect.p1.y = 30;
1172 ui_scrollbar_set_rect(scrollbar, &rect);
1174 /* Press inside lower trough is claimed and depresses it */
1175 event.type = POS_PRESS;
1176 event.hpos = 70;
1177 event.vpos = 20;
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;
1184 event.hpos = 41;
1185 event.vpos = 32;
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);
1192 ui_destroy(ui);
1195 /** ui_scrollbar_pos_event() detects down button press/release */
1196 PCUT_TEST(pos_event_press_relese_down_btn)
1198 ui_t *ui = NULL;
1199 ui_window_t *window = NULL;
1200 ui_wnd_params_t params;
1201 ui_scrollbar_t *scrollbar;
1202 ui_evclaim_t claim;
1203 pos_event_t event;
1204 gfx_rect_t rect;
1205 errno_t rc;
1207 rc = ui_create_disp(NULL, &ui);
1208 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
1210 ui_wnd_params_init(&params);
1211 params.caption = "Hello";
1213 rc = ui_window_create(ui, &params, &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);
1222 rect.p0.x = 20;
1223 rect.p0.y = 10;
1224 rect.p1.x = 100;
1225 rect.p1.y = 30;
1226 ui_scrollbar_set_rect(scrollbar, &rect);
1228 /* Press inside down button is claimed and depresses it */
1229 event.type = POS_PRESS;
1230 event.hpos = 90;
1231 event.vpos = 20;
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);
1238 ui_destroy(ui);
1241 static void test_scrollbar_up(ui_scrollbar_t *scrollbar, void *arg)
1243 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
1245 resp->up = true;
1248 static void test_scrollbar_down(ui_scrollbar_t *scrollbar, void *arg)
1250 test_cb_resp_t *resp = (test_cb_resp_t *) arg;
1252 resp->down = true;
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;
1273 resp->moved = true;
1274 resp->pos = pos;
1277 PCUT_EXPORT(scrollbar);