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 #define SWITCHES_CNT    10
10 
11 uint8_t value_changed_event_cnt = 0;
12 lv_obj_t * scr = NULL;
13 lv_obj_t * sw = NULL;
14 
setUp(void)15 void setUp(void)
16 {
17     /* Function run before every test */
18     scr = lv_scr_act();
19     sw = lv_switch_create(scr);
20 }
21 
tearDown(void)22 void tearDown(void)
23 {
24     /* Function run after every test */
25     value_changed_event_cnt = 0;
26 }
27 
mouse_click_on_switch(void)28 static void mouse_click_on_switch(void)
29 {
30     lv_test_mouse_click_at(sw->coords.x1, sw->coords.y1);
31 }
32 
event_handler(lv_event_t * e)33 static void event_handler(lv_event_t * e)
34 {
35     lv_event_code_t event = lv_event_get_code(e);
36 
37     if(LV_EVENT_VALUE_CHANGED == event) {
38         value_changed_event_cnt++;
39     }
40 
41 }
42 
test_switch_should_have_default_state_after_being_created(void)43 void test_switch_should_have_default_state_after_being_created(void)
44 {
45     lv_state_t state = lv_obj_get_state(sw);
46     TEST_ASSERT_EQUAL(state, LV_STATE_DEFAULT);
47 }
48 
test_switch_should_not_leak_memory_after_deletion(void)49 void test_switch_should_not_leak_memory_after_deletion(void)
50 {
51     size_t idx = 0;
52     uint32_t initial_available_memory = 0;
53     uint32_t final_available_memory = 0;
54     lv_mem_monitor_t monitor;
55     lv_obj_t * switches[SWITCHES_CNT] = {NULL};
56 
57     lv_mem_monitor(&monitor);
58     initial_available_memory = monitor.free_size;
59 
60     for(idx = 0; idx < SWITCHES_CNT; idx++) {
61         switches[idx] = lv_switch_create(scr);
62     }
63 
64     for(idx = 0; idx < SWITCHES_CNT; idx++) {
65         lv_obj_del(switches[idx]);
66     }
67 
68     lv_mem_monitor(&monitor);
69     final_available_memory = monitor.free_size;
70 
71     LV_HEAP_CHECK(TEST_ASSERT_LESS_OR_EQUAL(initial_available_memory, final_available_memory));
72 }
73 
test_switch_animation(void)74 void test_switch_animation(void)
75 {
76     lv_switch_t * anim_sw = (lv_switch_t *) sw;
77     int32_t initial_anim_state = anim_sw->anim_state;
78 
79     /* Trigger animation */
80     mouse_click_on_switch();
81     /* Wait some time  */
82     lv_test_indev_wait(50);
83 
84     int32_t checked_anim_state = anim_sw->anim_state;
85     TEST_ASSERT_GREATER_THAN(initial_anim_state, checked_anim_state);
86     TEST_ASSERT(lv_obj_has_state(sw, LV_STATE_CHECKED));
87 
88     mouse_click_on_switch();
89     lv_test_indev_wait(50);
90 
91     TEST_ASSERT_LESS_THAN(checked_anim_state, anim_sw->anim_state);
92     TEST_ASSERT_FALSE(lv_obj_has_state(sw, LV_STATE_CHECKED));
93 }
94 
test_switch_should_not_have_extra_draw_size_at_creation(void)95 void test_switch_should_not_have_extra_draw_size_at_creation(void)
96 {
97     lv_coord_t extra_size = _lv_obj_get_ext_draw_size(sw);
98 
99     TEST_ASSERT_EQUAL(0, extra_size);
100 }
101 
test_switch_should_update_extra_draw_size_after_editing_padding(void)102 void test_switch_should_update_extra_draw_size_after_editing_padding(void)
103 {
104     lv_coord_t pad = 6;
105     lv_coord_t actual = 0;
106     lv_coord_t expected = pad + _LV_SWITCH_KNOB_EXT_AREA_CORRECTION;
107 
108     static lv_style_t style_knob;
109     lv_style_init(&style_knob);
110     lv_style_set_pad_all(&style_knob, pad);
111 
112     lv_obj_remove_style_all(sw);
113     lv_obj_add_style(sw, &style_knob, LV_PART_KNOB);
114     lv_obj_center(sw);
115 
116     /* Get extra draw size */
117     actual = _lv_obj_get_ext_draw_size(sw);
118 
119     TEST_ASSERT_EQUAL(expected, actual);
120 }
121 
122 /* See #2330 for context */
test_switch_should_trigger_value_changed_event_only_once(void)123 void test_switch_should_trigger_value_changed_event_only_once(void)
124 {
125     lv_obj_add_event_cb(sw, event_handler, LV_EVENT_ALL, NULL);
126     mouse_click_on_switch();
127 
128     TEST_ASSERT_EQUAL(1, value_changed_event_cnt);
129 }
130 
131 /* See #2785 for context */
test_switch_should_state_change_when_event_bubbling_is_enabled(void)132 void test_switch_should_state_change_when_event_bubbling_is_enabled(void)
133 {
134     lv_obj_add_flag(sw, LV_OBJ_FLAG_EVENT_BUBBLE);
135     mouse_click_on_switch();
136 
137     TEST_ASSERT(lv_obj_has_state(sw, LV_STATE_CHECKED));
138 }
139 
140 #endif
141