1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 #include "../../lvgl_private.h"
4 
5 #include "unity/unity.h"
6 
7 #include "lv_test_helpers.h"
8 #include "lv_test_indev.h"
9 
10 void test_checkbox_creation_successful(void);
11 void test_checkbox_should_call_event_handler_on_click_when_enabled(void);
12 void test_checkbox_should_have_default_text_when_created(void);
13 void test_checkbox_should_return_dynamically_allocated_text(void);
14 void test_checkbox_should_allocate_memory_for_static_text(void);
15 
16 static lv_obj_t * active_screen = NULL;
17 static lv_obj_t * checkbox = NULL;
18 
19 static volatile bool event_called = false;
20 
event_handler(lv_event_t * e)21 static void event_handler(lv_event_t * e)
22 {
23     lv_event_code_t code = lv_event_get_code(e);
24 
25     if(LV_EVENT_VALUE_CHANGED == code) {
26         event_called = true;
27     }
28 }
29 
test_checkbox_creation_successful(void)30 void test_checkbox_creation_successful(void)
31 {
32     active_screen = lv_screen_active();
33     checkbox = lv_checkbox_create(active_screen);
34 
35     TEST_ASSERT_NOT_NULL(checkbox);
36 }
37 
test_checkbox_should_call_event_handler_on_click_when_enabled(void)38 void test_checkbox_should_call_event_handler_on_click_when_enabled(void)
39 {
40     active_screen = lv_screen_active();
41     checkbox = lv_checkbox_create(active_screen);
42 
43     lv_obj_add_state(checkbox, LV_STATE_CHECKED);
44     lv_obj_add_event_cb(checkbox, event_handler, LV_EVENT_ALL, NULL);
45 
46     lv_test_mouse_click_at(checkbox->coords.x1, checkbox->coords.y1);
47 
48     TEST_ASSERT_TRUE(event_called);
49 
50     event_called = false;
51 }
52 
test_checkbox_should_have_default_text_when_created(void)53 void test_checkbox_should_have_default_text_when_created(void)
54 {
55     const char * default_text = "Check box";
56 
57     active_screen = lv_screen_active();
58     checkbox = lv_checkbox_create(active_screen);
59 
60     TEST_ASSERT_EQUAL_STRING(default_text, lv_checkbox_get_text(checkbox));
61     TEST_ASSERT_NOT_NULL(lv_checkbox_get_text(checkbox));
62 }
63 
test_checkbox_should_return_dynamically_allocated_text(void)64 void test_checkbox_should_return_dynamically_allocated_text(void)
65 {
66     const char * message = "Hello World!";
67 
68     active_screen = lv_screen_active();
69     checkbox = lv_checkbox_create(active_screen);
70 
71     lv_checkbox_set_text(checkbox, message);
72 
73     TEST_ASSERT_EQUAL_STRING(message, lv_checkbox_get_text(checkbox));
74     TEST_ASSERT_NOT_NULL(lv_checkbox_get_text(checkbox));
75 }
76 
test_checkbox_should_allocate_memory_for_static_text(void)77 void test_checkbox_should_allocate_memory_for_static_text(void)
78 {
79     size_t initial_available_memory = 0;
80     const char * static_text = "Keep me while you exist";
81 
82     lv_mem_monitor_t m1;
83     lv_mem_monitor(&m1);
84 
85     active_screen = lv_screen_active();
86     checkbox = lv_checkbox_create(active_screen);
87 
88     initial_available_memory = m1.free_size;
89 
90     lv_checkbox_set_text_static(checkbox, static_text);
91 
92     lv_mem_monitor(&m1);
93 
94     LV_UNUSED(initial_available_memory);
95     LV_HEAP_CHECK(TEST_ASSERT_LESS_THAN(initial_available_memory, m1.free_size));
96 }
97 
test_checkbox_rtl(void)98 void test_checkbox_rtl(void)
99 {
100     const char * message =
101         "מעבד, או בשמו המלא יחידת עיבוד מרכזית (באנגלית: CPU - Central Processing Unit).";
102 
103     lv_obj_t * screen = lv_obj_create(lv_screen_active());
104     lv_obj_remove_style_all(screen);
105     lv_obj_set_size(screen, 800, 480);
106     lv_obj_center(screen);
107     lv_obj_set_style_bg_color(screen, lv_color_white(), 0);
108     lv_obj_set_style_bg_opa(screen, LV_OPA_100, 0);
109     lv_obj_set_style_pad_all(screen, 0, 0);
110 
111     lv_obj_t * test_checkbox = lv_checkbox_create(active_screen);
112 
113     lv_checkbox_set_text(test_checkbox, message);
114     lv_obj_set_style_text_font(test_checkbox, &lv_font_dejavu_16_persian_hebrew, 0);
115     lv_obj_center(test_checkbox);
116     lv_obj_set_style_base_dir(test_checkbox, LV_BASE_DIR_RTL, 0);
117 
118     TEST_ASSERT_EQUAL_SCREENSHOT("widgets/checkbox_rtl_1.png");
119 }
120 
121 #endif
122