1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 
4 #include "unity/unity.h"
5 #include "lv_test_indev.h"
6 
7 static lv_obj_t * active_screen = NULL;
8 static lv_obj_t * slider = NULL;
9 static lv_obj_t * sliderRangeMode = NULL;
10 static lv_obj_t * sliderNormalMode = NULL;
11 static lv_obj_t * sliderSymmetricalMode = NULL;
12 static lv_group_t * g = NULL;
13 
setUp(void)14 void setUp(void)
15 {
16     active_screen = lv_scr_act();
17     slider = lv_slider_create(active_screen);
18     sliderRangeMode = lv_slider_create(active_screen);
19     sliderNormalMode = lv_slider_create(active_screen);
20     sliderSymmetricalMode = lv_slider_create(active_screen);
21 
22     lv_slider_set_mode(sliderRangeMode, LV_SLIDER_MODE_RANGE);
23     lv_slider_set_mode(sliderNormalMode, LV_SLIDER_MODE_NORMAL);
24     lv_slider_set_mode(sliderSymmetricalMode, LV_SLIDER_MODE_SYMMETRICAL);
25 
26     g = lv_group_create();
27     lv_indev_set_group(lv_test_encoder_indev, g);
28 }
29 
tearDown(void)30 void tearDown(void)
31 {
32     lv_obj_clean(active_screen);
33 }
34 
test_textarea_should_have_valid_documented_default_values(void)35 void test_textarea_should_have_valid_documented_default_values(void)
36 {
37     lv_coord_t objw = lv_obj_get_width(slider);
38     lv_coord_t objh = lv_obj_get_height(slider);
39 
40     /* Horizontal slider */
41     TEST_ASSERT_TRUE(objw >= objh);
42     TEST_ASSERT_FALSE(lv_obj_has_flag(slider, LV_OBJ_FLAG_SCROLL_CHAIN));
43     TEST_ASSERT_FALSE(lv_obj_has_flag(slider, LV_OBJ_FLAG_SCROLLABLE));
44 }
45 
test_slider_event_keys_right_and_up_increment_value_by_one(void)46 void test_slider_event_keys_right_and_up_increment_value_by_one(void)
47 {
48     char key = LV_KEY_RIGHT;
49     lv_slider_set_value(slider, 10, LV_ANIM_OFF);
50     int32_t value = lv_slider_get_value(slider);
51 
52     lv_event_send(slider, LV_EVENT_KEY, (void *) &key);
53 
54     int32_t new_value = lv_slider_get_value(slider);
55     TEST_ASSERT_EQUAL_INT32(value + 1, new_value);
56 
57     key = LV_KEY_UP;
58     lv_event_send(slider, LV_EVENT_KEY, (void *) &key);
59     TEST_ASSERT_EQUAL_INT32(new_value + 1, lv_slider_get_value(slider));
60 }
61 
test_slider_event_keys_left_and_down_decrement_value_by_one(void)62 void test_slider_event_keys_left_and_down_decrement_value_by_one(void)
63 {
64     char key = LV_KEY_LEFT;
65     lv_slider_set_value(slider, 10, LV_ANIM_OFF);
66     int32_t value = lv_slider_get_value(slider);
67 
68     lv_event_send(slider, LV_EVENT_KEY, (void *) &key);
69 
70     int32_t new_value = lv_slider_get_value(slider);
71     TEST_ASSERT_EQUAL_INT32(value - 1, new_value);
72 
73     key = LV_KEY_DOWN;
74     lv_event_send(slider, LV_EVENT_KEY, (void *) &key);
75     TEST_ASSERT_EQUAL_INT32(new_value - 1, lv_slider_get_value(slider));
76 }
77 
test_slider_event_invalid_key_should_not_change_values(void)78 void test_slider_event_invalid_key_should_not_change_values(void)
79 {
80     char key = LV_KEY_ENTER;
81     lv_slider_set_value(slider, 10, LV_ANIM_OFF);
82     int32_t value = lv_slider_get_value(slider);
83 
84     lv_event_send(slider, LV_EVENT_KEY, (void *) &key);
85 
86     TEST_ASSERT_EQUAL_INT32(value, lv_slider_get_value(slider));
87 }
88 
test_slider_range_mode_should_leave_edit_mode_if_released(void)89 void test_slider_range_mode_should_leave_edit_mode_if_released(void)
90 {
91     lv_slider_t * ptr = (lv_slider_t *) sliderRangeMode;
92 
93     /* Setup group and encoder indev */
94     lv_group_add_obj(g, sliderNormalMode);
95     lv_group_set_editing(g, true);
96 
97     lv_test_encoder_click();
98 
99     /* Always executed when handling LV_EVENT_RELEASED or
100      * LV_EVENT_PRESS_LOST */
101     TEST_ASSERT_FALSE(ptr->dragging);
102     TEST_ASSERT_NULL(ptr->value_to_set);
103     TEST_ASSERT_EQUAL(0U, ptr->left_knob_focus);
104 
105     /* Group leaved edit mode */
106     TEST_ASSERT_FALSE(lv_group_get_editing(g));
107 }
108 
test_slider_range_mode_should_not_leave_edit_mode_if_released_with_no_left_knob_focus(void)109 void test_slider_range_mode_should_not_leave_edit_mode_if_released_with_no_left_knob_focus(void)
110 {
111     lv_slider_t * ptr = (lv_slider_t *) sliderRangeMode;
112 
113     /* Setup group and encoder indev */
114     lv_group_add_obj(g, sliderRangeMode);
115     lv_group_set_editing(g, true);
116 
117     lv_test_encoder_release();
118     lv_test_indev_wait(50);
119 
120     /* Always executed when handling LV_EVENT_RELEASED or
121      * LV_EVENT_PRESS_LOST */
122     TEST_ASSERT_FALSE(ptr->dragging);
123     TEST_ASSERT_NULL(ptr->value_to_set);
124 
125     TEST_ASSERT(lv_group_get_editing(g));
126 }
127 
test_slider_normal_mode_should_leave_edit_mode_if_released(void)128 void test_slider_normal_mode_should_leave_edit_mode_if_released(void)
129 {
130     lv_slider_t * ptr = (lv_slider_t *) sliderNormalMode;
131     ptr->left_knob_focus = 1;
132 
133     /* Setup group and encoder indev */
134     lv_group_add_obj(g, sliderNormalMode);
135     lv_group_set_editing(g, true);
136 
137     lv_test_encoder_click();
138 
139     /* Always executed when handling LV_EVENT_RELEASED or
140      * LV_EVENT_PRESS_LOST */
141     TEST_ASSERT_FALSE(ptr->dragging);
142     TEST_ASSERT_NULL(ptr->value_to_set);
143     TEST_ASSERT_EQUAL(0U, ptr->left_knob_focus);
144 
145     /* Group leaved edit mode */
146     TEST_ASSERT_FALSE(lv_group_get_editing(g));
147 }
148 
test_ranged_mode_adjust_with_encoder(void)149 void test_ranged_mode_adjust_with_encoder(void)
150 {
151     lv_slider_set_value(sliderRangeMode, 90, LV_ANIM_OFF);
152     lv_slider_set_left_value(sliderRangeMode, 10, LV_ANIM_OFF);
153 
154     /* Setup group and encoder indev */
155     lv_group_add_obj(g, sliderRangeMode);
156     lv_group_set_editing(g, false);
157 
158     /*Go the edit mode*/
159     lv_test_encoder_click();
160 
161     /*Adjust the right knob*/
162     lv_test_encoder_turn(-10);
163     TEST_ASSERT_EQUAL(80, lv_slider_get_value(sliderRangeMode));  /*Updated?*/
164     TEST_ASSERT_EQUAL(10, lv_slider_get_left_value(sliderRangeMode));     /*Maintained?*/
165 
166     /*Focus the left knob*/
167     lv_test_encoder_click();
168 
169     /*Adjust the left knob*/
170     lv_test_encoder_turn(5);
171     TEST_ASSERT_EQUAL(80, lv_slider_get_value(sliderRangeMode));  /*Maintained?*/
172     TEST_ASSERT_EQUAL(15, lv_slider_get_left_value(sliderRangeMode));  /*Updated?*/
173 
174 }
175 
test_normal_mode_slider_hit_test(void)176 void test_normal_mode_slider_hit_test(void)
177 {
178     /* Validate if point 0,0 can click in the slider */
179     lv_point_t point = {
180         .x = 0,
181         .y = 0
182     };
183 
184     lv_hit_test_info_t info = {
185         .res = false,
186         .point = &point
187     };
188 
189     lv_slider_set_value(sliderNormalMode, 100, LV_ANIM_OFF);
190     lv_event_send(sliderNormalMode, LV_EVENT_HIT_TEST, (void *) &info);
191 
192     /* point can click slider */
193     TEST_ASSERT(info.res);
194 }
195 
test_slider_range_event_hit_test(void)196 void test_slider_range_event_hit_test(void)
197 {
198     /* Validate if point 0,0 can click in the slider */
199     lv_point_t point = {
200         .x = 0,
201         .y = 0
202     };
203 
204     lv_hit_test_info_t info = {
205         .res = false,
206         .point = &point
207     };
208     lv_event_send(sliderRangeMode, LV_EVENT_HIT_TEST, (void *) &info);
209 
210     /* point can click slider in the left knob */
211     TEST_ASSERT(info.res);
212 }
213 
214 #endif
215