1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 
4 #include "unity/unity.h"
5 #include "lv_test_indev.h"
6 
7 /* This function runs before each test */
8 void setUp(void);
9 
10 void test_arc_creation_successfull(void);
11 void test_arc_should_truncate_to_max_range_when_new_value_exceeds_it(void);
12 void test_arc_should_truncate_to_min_range_when_new_value_is_inferior(void);
13 void test_arc_should_update_value_after_updating_range(void);
14 void test_arc_should_update_angles_when_changing_to_symmetrical_mode(void);
15 void test_arc_should_update_angles_when_changing_to_symmetrical_mode_value_more_than_middle_range(void);
16 void test_arc_angles_when_reversed(void);
17 
18 static lv_obj_t * active_screen = NULL;
19 static lv_obj_t * arc = NULL;
20 static uint32_t event_cnt;
21 
22 static void dummy_event_cb(lv_event_t * e);
23 
setUp(void)24 void setUp(void)
25 {
26     active_screen = lv_scr_act();
27 }
28 
test_arc_creation_successfull(void)29 void test_arc_creation_successfull(void)
30 {
31     arc = lv_arc_create(active_screen);
32 
33     TEST_ASSERT_NOT_NULL(arc);
34 }
35 
test_arc_should_truncate_to_max_range_when_new_value_exceeds_it(void)36 void test_arc_should_truncate_to_max_range_when_new_value_exceeds_it(void)
37 {
38     /* Default max range is 100 */
39     int16_t value_after_truncation = 100;
40 
41     arc = lv_arc_create(active_screen);
42 
43     lv_arc_set_value(arc, 200);
44 
45     TEST_ASSERT_EQUAL_INT16(value_after_truncation, lv_arc_get_value(arc));
46 }
47 
test_arc_should_truncate_to_min_range_when_new_value_is_inferior(void)48 void test_arc_should_truncate_to_min_range_when_new_value_is_inferior(void)
49 {
50     /* Default min range is 100 */
51     int16_t value_after_truncation = 0;
52 
53     arc = lv_arc_create(active_screen);
54 
55     lv_arc_set_value(arc, 0);
56 
57     TEST_ASSERT_EQUAL_INT16(value_after_truncation, lv_arc_get_value(arc));
58 }
59 
test_arc_should_update_value_after_updating_range(void)60 void test_arc_should_update_value_after_updating_range(void)
61 {
62     int16_t value_after_updating_max_range = 50;
63     int16_t value_after_updating_min_range = 30;
64 
65     arc = lv_arc_create(active_screen);
66 
67     lv_arc_set_value(arc, 80);
68     lv_arc_set_range(arc, 1, 50);
69 
70     TEST_ASSERT_EQUAL_INT16(value_after_updating_max_range, lv_arc_get_value(arc));
71 
72     lv_arc_set_value(arc, 10);
73     lv_arc_set_range(arc, 30, 50);
74 
75     TEST_ASSERT_EQUAL_INT16(value_after_updating_min_range, lv_arc_get_value(arc));
76 }
77 
test_arc_should_update_angles_when_changing_to_symmetrical_mode(void)78 void test_arc_should_update_angles_when_changing_to_symmetrical_mode(void)
79 {
80     int16_t expected_angle_start = 135;
81     int16_t expected_angle_end = 270;
82 
83     /* start angle is 135, end angle is 45 at creation */
84     arc = lv_arc_create(active_screen);
85     lv_arc_set_mode(arc, LV_ARC_MODE_SYMMETRICAL);
86 
87     TEST_ASSERT_EQUAL_INT16(expected_angle_start, lv_arc_get_angle_start(arc));
88     TEST_ASSERT_EQUAL_INT16(expected_angle_end, lv_arc_get_angle_end(arc));
89 }
90 
test_arc_should_update_angles_when_changing_to_symmetrical_mode_value_more_than_middle_range(void)91 void test_arc_should_update_angles_when_changing_to_symmetrical_mode_value_more_than_middle_range(void)
92 {
93     int16_t expected_angle_start = 270;
94     int16_t expected_angle_end = 45;
95 
96     /* start angle is 135, end angle is 45 at creation */
97     arc = lv_arc_create(active_screen);
98     lv_arc_set_value(arc, 100);
99     lv_arc_set_mode(arc, LV_ARC_MODE_SYMMETRICAL);
100 
101     TEST_ASSERT_EQUAL_INT16(expected_angle_start, lv_arc_get_angle_start(arc));
102     TEST_ASSERT_EQUAL_INT16(expected_angle_end, lv_arc_get_angle_end(arc));
103 }
104 
105 /* See #2522 for more information */
test_arc_angles_when_reversed(void)106 void test_arc_angles_when_reversed(void)
107 {
108     uint16_t expected_start_angle = 54;
109     uint16_t expected_end_angle = 90;
110     int16_t expected_value = 40;
111 
112     lv_obj_t * arcBlack;
113     arcBlack = lv_arc_create(lv_scr_act());
114 
115     lv_arc_set_mode(arcBlack, LV_ARC_MODE_REVERSE);
116 
117     lv_arc_set_bg_angles(arcBlack, 0, 90);
118 
119     lv_arc_set_value(arcBlack, expected_value);
120 
121     TEST_ASSERT_EQUAL_UINT16(expected_start_angle, lv_arc_get_angle_start(arcBlack));
122     TEST_ASSERT_EQUAL_UINT16(expected_end_angle, lv_arc_get_angle_end(arcBlack));
123     TEST_ASSERT_EQUAL_INT16(expected_value, lv_arc_get_value(arcBlack));
124 }
125 
test_arc_click_area_with_adv_hittest(void)126 void test_arc_click_area_with_adv_hittest(void)
127 {
128     arc = lv_arc_create(lv_scr_act());
129     lv_obj_set_size(arc, 100, 100);
130     lv_obj_set_style_arc_width(arc, 10, 0);
131     lv_obj_add_flag(arc, LV_OBJ_FLAG_ADV_HITTEST);
132     lv_obj_add_event_cb(arc, dummy_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
133     lv_obj_set_ext_click_area(arc, 5);
134 
135     /*No click detected at the middle*/
136     event_cnt = 0;
137     lv_test_mouse_click_at(50, 50);
138     TEST_ASSERT_EQUAL_UINT32(0, event_cnt);
139 
140     /*No click close to the radius - bg_arc - ext_click_area*/
141     event_cnt = 0;
142     lv_test_mouse_click_at(83, 50);
143     TEST_ASSERT_EQUAL_UINT32(0, event_cnt);
144 
145     /*Click on the radius - bg_arc - ext_click_area*/
146     event_cnt = 0;
147     lv_test_mouse_click_at(86, 50);
148     TEST_ASSERT_GREATER_THAN(0, event_cnt);
149 
150     /*Click on the radius + ext_click_area*/
151     event_cnt = 0;
152     lv_test_mouse_click_at(104, 50);
153     TEST_ASSERT_GREATER_THAN(0, event_cnt);
154 
155     /*No click beyond to the radius + ext_click_area*/
156     event_cnt = 0;
157     lv_test_mouse_click_at(106, 50);
158     TEST_ASSERT_EQUAL_UINT32(0, event_cnt);
159 }
160 
161 /* Check value doesn't go to max when clicking on the other side of the arc */
test_arc_click_sustained_from_start_to_end_does_not_set_value_to_max(void)162 void test_arc_click_sustained_from_start_to_end_does_not_set_value_to_max(void)
163 {
164     arc = lv_arc_create(lv_scr_act());
165     lv_arc_set_value(arc, 0);
166 
167     lv_obj_set_size(arc, 100, 100);
168     lv_obj_center(arc);
169     lv_obj_add_event_cb(arc, dummy_event_cb, LV_EVENT_PRESSED, NULL);
170     event_cnt = 0;
171 
172     /* Click close to start angle */
173     event_cnt = 0;
174     lv_test_mouse_move_to(376, 285);
175     lv_test_mouse_press();
176     lv_test_indev_wait(50);
177     lv_test_mouse_release();
178     lv_test_indev_wait(50);
179 
180     TEST_ASSERT_EQUAL_UINT32(1, event_cnt);
181     TEST_ASSERT_EQUAL_UINT32(lv_arc_get_value(arc), lv_arc_get_min_value(arc));
182 
183     /* Click close to end angle */
184     event_cnt = 0;
185 
186     lv_test_mouse_move_to(376, 285);
187     lv_test_mouse_press();
188     lv_test_indev_wait(50);
189     lv_test_mouse_move_to(415, 281);
190     lv_test_indev_wait(50);
191     lv_test_mouse_release();
192     lv_test_indev_wait(50);
193 
194     TEST_ASSERT_EQUAL_UINT32(1, event_cnt);
195     TEST_ASSERT_NOT_EQUAL_UINT32(lv_arc_get_value(arc), lv_arc_get_max_value(arc));
196 
197     TEST_ASSERT_EQUAL_SCREENSHOT("arc_2.png");
198 }
199 
test_arc_basic_render(void)200 void test_arc_basic_render(void)
201 {
202     arc = lv_arc_create(lv_scr_act());
203     lv_obj_set_size(arc, 100, 100);
204     lv_obj_center(arc);
205     TEST_ASSERT_EQUAL_SCREENSHOT("arc_1.png");
206 }
207 
dummy_event_cb(lv_event_t * e)208 static void dummy_event_cb(lv_event_t * e)
209 {
210     LV_UNUSED(e);
211     event_cnt++;
212 }
213 
214 #endif
215