1 #if LV_BUILD_TEST
2 #include "../lvgl.h"
3 
4 #include "unity/unity.h"
5 
6 #include "lv_test_helpers.h"
7 #include "lv_test_indev.h"
8 
9 void test_checkbox_creation_successfull(void);
10 void test_checkbox_should_call_event_handler_on_click_when_enabled(void);
11 void test_checkbox_should_have_default_text_when_created(void);
12 void test_checkbox_should_return_dinamically_allocated_text(void);
13 void test_checkbox_should_allocate_memory_for_static_text(void);
14 
15 static lv_obj_t * active_screen = NULL;
16 static lv_obj_t * checkbox = NULL;
17 
18 static volatile bool event_called = false;
19 
event_handler(lv_event_t * e)20 static void event_handler(lv_event_t * e)
21 {
22     lv_event_code_t code = lv_event_get_code(e);
23 
24     if(LV_EVENT_VALUE_CHANGED == code) {
25         event_called = true;
26     }
27 }
28 
test_checkbox_creation_successfull(void)29 void test_checkbox_creation_successfull(void)
30 {
31     active_screen = lv_scr_act();
32     checkbox = lv_checkbox_create(active_screen);
33 
34     TEST_ASSERT_NOT_NULL(checkbox);
35 }
36 
test_checkbox_should_call_event_handler_on_click_when_enabled(void)37 void test_checkbox_should_call_event_handler_on_click_when_enabled(void)
38 {
39     active_screen = lv_scr_act();
40     checkbox = lv_checkbox_create(active_screen);
41 
42     lv_obj_add_state(checkbox, LV_STATE_CHECKED);
43     lv_obj_add_event_cb(checkbox, event_handler, LV_EVENT_ALL, NULL);
44 
45     lv_test_mouse_click_at(checkbox->coords.x1, checkbox->coords.y1);
46 
47     TEST_ASSERT_TRUE(event_called);
48 
49     event_called = false;
50 }
51 
test_checkbox_should_have_default_text_when_created(void)52 void test_checkbox_should_have_default_text_when_created(void)
53 {
54     const char * default_text = "Check box";
55 
56     active_screen = lv_scr_act();
57     checkbox = lv_checkbox_create(active_screen);
58 
59     TEST_ASSERT_EQUAL_STRING(default_text, lv_checkbox_get_text(checkbox));
60     TEST_ASSERT_NOT_NULL(lv_checkbox_get_text(checkbox));
61 }
62 
test_checkbox_should_return_dinamically_allocated_text(void)63 void test_checkbox_should_return_dinamically_allocated_text(void)
64 {
65     const char * message = "Hello World!";
66 
67     active_screen = lv_scr_act();
68     checkbox = lv_checkbox_create(active_screen);
69 
70     lv_checkbox_set_text(checkbox, message);
71 
72     TEST_ASSERT_EQUAL_STRING(message, lv_checkbox_get_text(checkbox));
73     TEST_ASSERT_NOT_NULL(lv_checkbox_get_text(checkbox));
74 }
75 
test_checkbox_should_allocate_memory_for_static_text(void)76 void test_checkbox_should_allocate_memory_for_static_text(void)
77 {
78     uint32_t initial_available_memory = 0;
79     const char * static_text = "Keep me while you exist";
80 
81     lv_mem_monitor_t m1;
82     lv_mem_monitor(&m1);
83 
84     active_screen = lv_scr_act();
85     checkbox = lv_checkbox_create(active_screen);
86 
87     initial_available_memory = m1.free_size;
88 
89     lv_checkbox_set_text_static(checkbox, static_text);
90 
91     lv_mem_monitor(&m1);
92 
93     LV_HEAP_CHECK(TEST_ASSERT_LESS_THAN(initial_available_memory, m1.free_size));
94 }
95 
96 #endif
97