1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 
7 static lv_obj_t * active_screen = NULL;
8 static lv_obj_t * line = NULL;
9 
10 static const uint16_t default_point_num = 0U;
11 static const int32_t initial_extra_draw_size = 5U;
12 static const int32_t final_extra_draw_size = 10U;
13 
setUp(void)14 void setUp(void)
15 {
16     active_screen = lv_screen_active();
17     line = lv_line_create(active_screen);
18 }
19 
tearDown(void)20 void tearDown(void)
21 {
22     lv_obj_clean(active_screen);
23 }
24 
test_line_should_have_valid_documented_default_values(void)25 void test_line_should_have_valid_documented_default_values(void)
26 {
27     TEST_ASSERT_EQUAL_UINT16(default_point_num, lv_line_get_point_count(line));
28     TEST_ASSERT_NULL(lv_line_get_points(line));
29     TEST_ASSERT_FALSE(lv_line_get_y_invert(line));
30     TEST_ASSERT_FALSE(lv_obj_has_flag(line, LV_OBJ_FLAG_CLICKABLE));
31     /* line doesn't have any points, so it's 0,0 in size */
32     TEST_ASSERT_EQUAL_UINT16(0U, lv_obj_get_self_width(line));
33     TEST_ASSERT_EQUAL_UINT16(0U, lv_obj_get_self_height(line));
34 }
35 
test_line_should_return_valid_y_invert(void)36 void test_line_should_return_valid_y_invert(void)
37 {
38     lv_line_set_y_invert(line, true);
39     TEST_ASSERT_TRUE(lv_line_get_y_invert(line));
40 }
41 
test_line_size_should_be_updated_after_adding_points(void)42 void test_line_size_should_be_updated_after_adding_points(void)
43 {
44     static lv_point_precise_t points[] = { {5, 5} };
45     uint16_t point_cnt = (uint16_t) sizeof(points) / sizeof(lv_point_precise_t);
46     lv_line_set_points(line, points, point_cnt);
47 
48     int32_t calculated_width = 0;
49     int32_t calculated_height = 0;
50 
51     /* Get the biggest coordinate on both axis */
52     uint16_t point_idx = 0;
53     for(point_idx = 0; point_idx < point_cnt; point_idx++) {
54         calculated_width = (int32_t)LV_MAX(points[point_idx].x, calculated_width);
55         calculated_height = (int32_t)LV_MAX(points[point_idx].y, calculated_height);
56     }
57 
58     TEST_ASSERT_EQUAL_UINT16(calculated_width, lv_obj_get_self_width(line));
59     TEST_ASSERT_EQUAL_UINT16(calculated_height, lv_obj_get_self_height(line));
60 }
61 
line_event_cb(lv_event_t * e)62 static void line_event_cb(lv_event_t * e)
63 {
64     lv_event_code_t code = lv_event_get_code(e);
65 
66     if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
67         /* Set the new line extra draw size */
68         lv_event_set_ext_draw_size(e, initial_extra_draw_size);
69     }
70 }
71 
test_line_should_update_extra_draw_size_based_on_style(void)72 void test_line_should_update_extra_draw_size_based_on_style(void)
73 {
74     /* Setup an event handler for line extra draw size event */
75     lv_obj_add_event_cb(line, line_event_cb, LV_EVENT_ALL, NULL);
76     /* Trigger the extra draw size event */
77     lv_obj_refresh_ext_draw_size(line);
78 
79     TEST_ASSERT_EQUAL(initial_extra_draw_size, lv_obj_get_ext_draw_size(line));
80 
81     /* Update line width style, the event handler should set the extra draw size
82      * to the line width */
83     lv_obj_set_style_line_width(line, final_extra_draw_size, LV_PART_MAIN);
84 
85     /* Trigger the extra draw size event */
86     lv_obj_refresh_ext_draw_size(line);
87 
88     TEST_ASSERT_EQUAL(final_extra_draw_size, lv_obj_get_ext_draw_size(line));
89 }
90 
test_line_basic_render(void)91 void test_line_basic_render(void)
92 {
93     static lv_point_precise_t points[] = { {5, 5},
94         {100, 5},    /*Horizontal*/
95         {100, 100},  /*Vertical*/
96         {120, 5},    /*Steep*/
97         {200, 20},   /*Flat*/
98     };
99     uint16_t point_cnt = (uint16_t) sizeof(points) / sizeof(lv_point_precise_t);
100     lv_line_set_points(line, points, point_cnt);
101     lv_obj_set_pos(line, 10, 10);
102 
103     line = lv_line_create(active_screen);
104     lv_line_set_points(line, points, point_cnt);
105     lv_obj_set_pos(line, 400, 0);
106     lv_obj_set_style_line_width(line, 5, LV_PART_MAIN);
107     lv_obj_set_style_line_dash_gap(line, 3, LV_PART_MAIN);
108     lv_obj_set_style_line_dash_width(line, 10, LV_PART_MAIN);
109 
110     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/line_1.png");
111 }
112 
test_line_dash_gap(void)113 void test_line_dash_gap(void)
114 {
115     static lv_point_precise_t line_points1[3] = { {50, 50}, {250, 50}, {250, 250} };
116     static lv_point_precise_t line_points2[3] = { {50, 250}, {50, 50}, {250, 50} };
117 
118     lv_obj_t * line1;
119     line1 = lv_line_create(lv_screen_active());
120     lv_line_set_points(line1, line_points1, 3);
121     lv_obj_set_style_line_width(line1, 1, LV_PART_MAIN);
122     lv_obj_set_style_line_dash_width(line1, 1, LV_PART_MAIN);
123     lv_obj_set_style_line_dash_gap(line1, 1, LV_PART_MAIN);
124 
125     lv_obj_align(line1, LV_ALIGN_LEFT_MID, 0, 0);
126 
127     lv_obj_t * line2;
128     line2 = lv_line_create(lv_screen_active());
129     lv_line_set_points(line2, line_points2, 3);
130     lv_obj_set_style_line_width(line2, 2, LV_PART_MAIN);
131     lv_obj_set_style_line_dash_width(line2, 2, LV_PART_MAIN);
132     lv_obj_set_style_line_dash_gap(line2, 2, LV_PART_MAIN);
133 
134     lv_obj_center(line2);
135 
136     lv_obj_t * line3;
137     line3 = lv_line_create(lv_screen_active());
138     lv_line_set_points(line3, line_points2, 3);
139     lv_obj_set_style_line_width(line3, 5, LV_PART_MAIN);
140     lv_obj_set_style_line_dash_width(line3, 3, LV_PART_MAIN);
141     lv_obj_set_style_line_dash_gap(line3, 1, LV_PART_MAIN);
142 
143     lv_obj_align(line3, LV_ALIGN_RIGHT_MID, 0, 0);
144 
145     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/line_2.png");
146 }
147 
test_line_point_array_getters_and_setters(void)148 void test_line_point_array_getters_and_setters(void)
149 {
150     const lv_point_precise_t points[3] = {{10, 20}, {30, 40}, {50, 60}};
151     lv_line_set_points(line, points, 3);
152     TEST_ASSERT_EQUAL_UINT32(3, lv_line_get_point_count(line));
153     TEST_ASSERT_FALSE(lv_line_is_point_array_mutable(line));
154     TEST_ASSERT_EQUAL_PTR(points, lv_line_get_points(line));
155     TEST_ASSERT_NULL(lv_line_get_points_mutable(line));
156 
157     lv_point_precise_t points_mutable[2] = {{10, 20}, {30, 40}};
158     lv_line_set_points_mutable(line, points_mutable, 2);
159     TEST_ASSERT_EQUAL_UINT32(2, lv_line_get_point_count(line));
160     TEST_ASSERT_TRUE(lv_line_is_point_array_mutable(line));
161     TEST_ASSERT_EQUAL_PTR(points_mutable, lv_line_get_points(line));
162     TEST_ASSERT_EQUAL_PTR(points_mutable, lv_line_get_points_mutable(line));
163 }
164 
165 #endif
166