diff --git a/src/layouts/flex/lv_flex.c b/src/layouts/flex/lv_flex.c index 168f9de90..65a1d048e 100644 --- a/src/layouts/flex/lv_flex.c +++ b/src/layouts/flex/lv_flex.c @@ -472,7 +472,6 @@ static void place_content(lv_flex_align_t place, int32_t max_size, int32_t conte { if(item_cnt <= 1) { switch(place) { - case LV_FLEX_ALIGN_SPACE_BETWEEN: case LV_FLEX_ALIGN_SPACE_AROUND: case LV_FLEX_ALIGN_SPACE_EVENLY: place = LV_FLEX_ALIGN_CENTER; @@ -492,7 +491,7 @@ static void place_content(lv_flex_align_t place, int32_t max_size, int32_t conte *start_pos += max_size - content_size; break; case LV_FLEX_ALIGN_SPACE_BETWEEN: - *gap = (int32_t)(max_size - content_size) / (int32_t)(item_cnt - 1); + if(item_cnt > 1) *gap = (int32_t)(max_size - content_size) / (int32_t)(item_cnt - 1); break; case LV_FLEX_ALIGN_SPACE_AROUND: *gap += (int32_t)(max_size - content_size) / (int32_t)(item_cnt); diff --git a/tests/ref_imgs/align_flex_center1.png b/tests/ref_imgs/align_flex_center1.png new file mode 100644 index 000000000..3e1b37365 Binary files /dev/null and b/tests/ref_imgs/align_flex_center1.png differ diff --git a/tests/ref_imgs/align_flex_center2.png b/tests/ref_imgs/align_flex_center2.png new file mode 100644 index 000000000..729d02088 Binary files /dev/null and b/tests/ref_imgs/align_flex_center2.png differ diff --git a/tests/ref_imgs/align_flex_space_around1.png b/tests/ref_imgs/align_flex_space_around1.png new file mode 100644 index 000000000..3e1b37365 Binary files /dev/null and b/tests/ref_imgs/align_flex_space_around1.png differ diff --git a/tests/ref_imgs/align_flex_space_around2.png b/tests/ref_imgs/align_flex_space_around2.png new file mode 100644 index 000000000..509436507 Binary files /dev/null and b/tests/ref_imgs/align_flex_space_around2.png differ diff --git a/tests/ref_imgs/align_flex_space_between1.png b/tests/ref_imgs/align_flex_space_between1.png new file mode 100644 index 000000000..9dfebbf26 Binary files /dev/null and b/tests/ref_imgs/align_flex_space_between1.png differ diff --git a/tests/ref_imgs/align_flex_space_between2.png b/tests/ref_imgs/align_flex_space_between2.png new file mode 100644 index 000000000..79d86bb5a Binary files /dev/null and b/tests/ref_imgs/align_flex_space_between2.png differ diff --git a/tests/ref_imgs/align_flex_space_evenly1.png b/tests/ref_imgs/align_flex_space_evenly1.png new file mode 100644 index 000000000..3e1b37365 Binary files /dev/null and b/tests/ref_imgs/align_flex_space_evenly1.png differ diff --git a/tests/ref_imgs/align_flex_space_evenly2.png b/tests/ref_imgs/align_flex_space_evenly2.png new file mode 100644 index 000000000..fa63d506e Binary files /dev/null and b/tests/ref_imgs/align_flex_space_evenly2.png differ diff --git a/tests/src/test_cases/test_align_flex.c b/tests/src/test_cases/test_align_flex.c new file mode 100644 index 000000000..91bcaf72d --- /dev/null +++ b/tests/src/test_cases/test_align_flex.c @@ -0,0 +1,80 @@ +#if LV_BUILD_TEST +#include "../lvgl.h" + +#include "unity/unity.h" + +static lv_obj_t * active_screen = NULL; + +void setUp(void) +{ + active_screen = lv_screen_active(); +} + +void tearDown(void) +{ + lv_obj_clean(active_screen); +} + +static void simple_style(lv_obj_t * obj) +{ + lv_obj_set_style_radius(obj, 0, LV_PART_MAIN); + lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF); + lv_obj_set_style_border_width(obj, 0, LV_PART_MAIN); + lv_obj_set_style_pad_all(obj, 0, LV_PART_MAIN); + lv_obj_set_style_margin_all(obj, 0, LV_PART_MAIN); + lv_obj_set_style_outline_pad(obj, 0, LV_PART_MAIN); + lv_obj_set_style_outline_width(obj, 0, LV_PART_MAIN); +} + +static lv_obj_t * create_row(lv_flex_align_t main_place, int n_children) +{ + lv_obj_t * row = lv_obj_create(lv_screen_active()); + lv_obj_set_size(row, LV_PCT(100), LV_PCT(100)); + lv_obj_set_flex_flow(row, LV_FLEX_FLOW_ROW); + lv_obj_set_flex_align(row, main_place, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); + simple_style(row); + for(int i = 0; i < n_children; i++) { + lv_obj_t * child = lv_obj_create(row); + lv_obj_set_size(child, 40, 40); + lv_obj_set_style_bg_color(child, lv_palette_main(LV_PALETTE_BLUE), 0); + simple_style(child); + } + return row; +} + +void test_align(void) +{ + create_row(LV_FLEX_ALIGN_CENTER, 1); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_center1.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_CENTER, 2); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_center2.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_EVENLY, 1); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_evenly1.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_EVENLY, 2); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_evenly2.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_AROUND, 1); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_around1.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_AROUND, 2); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_around2.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_BETWEEN, 1); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_between1.png"); + lv_obj_clean(active_screen); + + create_row(LV_FLEX_ALIGN_SPACE_BETWEEN, 2); + TEST_ASSERT_EQUAL_SCREENSHOT("align_flex_space_between2.png"); + lv_obj_clean(active_screen); +} + +#endif