aboutsummaryrefslogtreecommitdiff
path: root/tests/src/test_cases/test_hover.c
blob: 66cbcfb0c20663e0aa89a86f72cbc9b0fc49dd0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#if LV_BUILD_TEST
#include "../lvgl.h"
#include "../lv_test_indev.h"
#include "unity/unity.h"

#define TEST_HOVER_COUNTS   20

typedef struct _test_hover_t {
    /* data */
    char id[32];
    uint32_t counts;
} test_hover_t;

static test_hover_t label_hovered;
static test_hover_t btn_hovered;

static const lv_point_t pointer1[] = {
    {0, 0}, {110, 20}, {150, 26}, {120, 19}, {0, 0},
};

static const lv_point_t pointer2[] = {
    {0, 0}, {60, 100}, {80, 100}, {120, 120}, {0, 0},
};

void setUp(void)
{
    /* Function run before every test */
}

void tearDown(void)
{
    /* Function run after every test */
    lv_obj_clean(lv_screen_active());
}

static void hovered_event_cb(lv_event_t * e)
{
    test_hover_t * hover = lv_event_get_user_data(e);
    lv_log("Object(ID:%s) hovered %u/%d times.\n", hover->id, hover->counts, TEST_HOVER_COUNTS);
}

static void test_move_mouse(lv_point_t * point, uint8_t size)
{
    lv_point_t * p = point;

    for(uint8_t j = 0; j < TEST_HOVER_COUNTS; j++) {
        for(uint8_t i = 0; i < size; i++) {
            lv_test_mouse_move_to(p[i].x, p[i].y);
            lv_test_indev_wait(50);
        }
    }
}

void test_hover_basic(void)
{
    lv_obj_t * label = lv_label_create(lv_screen_active());
    lv_obj_set_size(label, 200, 20);
    lv_label_set_text(label, "Clickable text can be hovered!");
    lv_obj_set_pos(label, 100, 20);
    lv_obj_add_flag(label, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_text_color(label, lv_color_hex(0x5be1b6), LV_PART_MAIN | LV_STATE_HOVERED);

    /*Set hover callback*/
    lv_obj_stringify_id(label, label_hovered.id, sizeof(label_hovered.id));
    label_hovered.counts = 0;
    lv_obj_add_event_cb(label, hovered_event_cb, LV_EVENT_HOVER_OVER, &label_hovered);

    lv_obj_t * btn = lv_button_create(lv_screen_active());
    lv_obj_set_pos(btn, 64, 100);
    lv_obj_set_size(btn, 128, 48);
    lv_obj_set_style_bg_opa(btn, 128, LV_PART_MAIN | LV_STATE_HOVERED);

    /*Set hover callback*/
    lv_obj_stringify_id(btn, btn_hovered.id, sizeof(btn_hovered.id));
    btn_hovered.counts = 0;
    lv_obj_add_event_cb(btn, hovered_event_cb, LV_EVENT_HOVER_OVER, &btn_hovered);

    test_move_mouse((lv_point_t *)pointer1, 5);
    test_move_mouse((lv_point_t *)pointer2, 5);
}

void test_hover_delete(void)
{
    for(int i = 0; i < 4; i++) {
        lv_obj_t * btn = lv_button_create(lv_screen_active());
        lv_obj_set_size(btn, 200, 100);

        lv_test_mouse_move_to(i * 10, 50);
        lv_test_indev_wait(50);

        lv_obj_delete(btn);  /*No crash while deleting the hovered button*/
        lv_test_indev_wait(50);
    }
}


#endif