test(line): add unit tests for line widget (#3104)

* test(line): Test y invert field

* test(line): Validate documented default values

* test(line): Assert line is not clickable after construction

* test(line): Test line size calculation

* chore(line): Refactor LV_EVENT_GET_SELF_SIZE handling

Return early when no point_array is set or point_num is 0

* chore(line): Cleanup LV_EVENT_DRAW_MAIN handling

Reduce scope of point variables

* test(line): Clean screen on tearDown

* test(line): Add initial test for extra draw size calculation

* test(line): Fix extra draw size test
This commit is contained in:
Carlos Diaz 2022-02-25 12:58:43 -06:00 committed by GitHub
parent 340d45cfa9
commit 406386dc3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 112 additions and 17 deletions

View File

@ -83,7 +83,7 @@ void lv_line_set_y_invert(lv_obj_t * obj, bool en)
lv_line_t * line = (lv_line_t *)obj;
if(line->y_inv == en) return;
line->y_inv = en == false ? 0 : 1;
line->y_inv = en ? 1U : 0U;
lv_obj_invalidate(obj);
}
@ -98,7 +98,7 @@ bool lv_line_get_y_invert(const lv_obj_t * obj)
lv_line_t * line = (lv_line_t *)obj;
return line->y_inv == 0 ? false : true;
return line->y_inv == 1U;
}
/**********************
@ -143,10 +143,12 @@ static void lv_line_event(const lv_obj_class_t * class_p, lv_event_t * e)
else if(code == LV_EVENT_GET_SELF_SIZE) {
lv_line_t * line = (lv_line_t *)obj;
if(line->point_num == 0 || line->point_array == NULL) return;
lv_point_t * p = lv_event_get_param(e);
lv_coord_t w = 0;
lv_coord_t h = 0;
if(line->point_num > 0) {
uint16_t i;
for(i = 0; i < line->point_num; i++) {
w = LV_MAX(line->point_array[i].x, w);
@ -159,7 +161,6 @@ static void lv_line_event(const lv_obj_class_t * class_p, lv_event_t * e)
p->x = w;
p->y = h;
}
}
else if(code == LV_EVENT_DRAW_MAIN) {
lv_line_t * line = (lv_line_t *)obj;
lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
@ -170,18 +171,17 @@ static void lv_line_event(const lv_obj_class_t * class_p, lv_event_t * e)
lv_obj_get_coords(obj, &area);
lv_coord_t x_ofs = area.x1 - lv_obj_get_scroll_x(obj);
lv_coord_t y_ofs = area.y1 - lv_obj_get_scroll_y(obj);
lv_point_t p1;
lv_point_t p2;
lv_coord_t h = lv_obj_get_height(obj);
uint16_t i;
lv_draw_line_dsc_t line_dsc;
lv_draw_line_dsc_init(&line_dsc);
lv_obj_init_draw_line_dsc(obj, LV_PART_MAIN, &line_dsc);
/*Read all points and draw the lines*/
uint16_t i;
for(i = 0; i < line->point_num - 1; i++) {
lv_point_t p1;
lv_point_t p2;
p1.x = line->point_array[i].x + x_ofs;
p2.x = line->point_array[i + 1].x + x_ofs;

View File

@ -0,0 +1,95 @@
#if LV_BUILD_TEST
#include "../lvgl.h"
#include "unity/unity.h"
static lv_obj_t * active_screen = NULL;
static lv_obj_t * line = NULL;
static const uint16_t default_point_num = 0U;
static const lv_coord_t initial_extra_draw_size = 5U;
static const lv_coord_t final_extra_draw_size = 10U;
void setUp(void)
{
active_screen = lv_scr_act();
line = lv_line_create(active_screen);
}
void tearDown(void)
{
lv_obj_clean(active_screen);
}
void test_line_should_have_valid_documented_default_values(void)
{
lv_line_t * line_ptr = (lv_line_t *) line;
TEST_ASSERT_EQUAL_UINT16(default_point_num, line_ptr->point_num);
TEST_ASSERT_NULL(line_ptr->point_array);
TEST_ASSERT_FALSE(lv_line_get_y_invert(line));
TEST_ASSERT_FALSE(lv_obj_has_flag(line, LV_OBJ_FLAG_CLICKABLE));
/* line doesn't have any points, so it's 0,0 in size */
TEST_ASSERT_EQUAL_UINT16(0U, lv_obj_get_self_width(line));
TEST_ASSERT_EQUAL_UINT16(0U, lv_obj_get_self_height(line));
}
void test_line_should_return_valid_y_invert(void)
{
lv_line_set_y_invert(line, true);
TEST_ASSERT_TRUE(lv_line_get_y_invert(line));
}
void test_line_size_should_be_updated_after_adding_points(void)
{
static lv_point_t points[] = { {5, 5} };
uint16_t point_cnt = (uint16_t) sizeof(points) / sizeof(lv_point_t);
lv_line_set_points(line, points, point_cnt);
lv_coord_t calculated_width = 0;
lv_coord_t calculated_height = 0;
/* Get the biggest coordinate on both axis */
uint16_t point_idx = 0;
for(point_idx = 0; point_idx < point_cnt; point_idx++) {
calculated_width = LV_MAX(points[point_idx].x, calculated_width);
calculated_height = LV_MAX(points[point_idx].y, calculated_height);
}
/* Add style line width */
lv_coord_t line_width = lv_obj_get_style_line_width(line, LV_PART_MAIN);
calculated_width += line_width;
calculated_height += line_width;
TEST_ASSERT_EQUAL_UINT16(calculated_width, lv_obj_get_self_width(line));
TEST_ASSERT_EQUAL_UINT16(calculated_height, lv_obj_get_self_height(line));
}
static void line_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
/* Set the new line extra draw size */
lv_event_set_ext_draw_size(e, initial_extra_draw_size);
}
}
void test_line_should_update_extra_draw_size_based_on_style(void)
{
/* Setup an event handler for line extra draw size event */
lv_obj_add_event_cb(line, line_event_cb, LV_EVENT_ALL, NULL);
/* Trigger the extra draw size event */
lv_obj_refresh_ext_draw_size(line);
TEST_ASSERT_EQUAL(initial_extra_draw_size, _lv_obj_get_ext_draw_size(line));
/* Update line width style, the event handler should set the extra draw size
* to the line width */
lv_obj_set_style_line_width(line, final_extra_draw_size, LV_PART_MAIN);
/* Trigger the extra draw size event */
lv_obj_refresh_ext_draw_size(line);
TEST_ASSERT_EQUAL(final_extra_draw_size, _lv_obj_get_ext_draw_size(line));
}
#endif