add style test

This commit is contained in:
Gabor Kiss-Vamosi
2020-01-16 14:27:12 +01:00
parent 805af47113
commit fae87aa3a3
12 changed files with 1290 additions and 0 deletions

BIN
tests/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

BIN
tests/icon2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

339
tests/lv_test_assert.c Normal file
View File

@@ -0,0 +1,339 @@
/**
* @file lv_test_assert.c
*
* Copyright 2002-2010 Guillaume Cottenceau.
*
* This software may be freely redistributed under the terms
* of the X11 license.
*
*/
/*********************
* INCLUDES
*********************/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#define PNG_DEBUG 3
#include <png.h>
#include "lv_test_assert.h"
#include "../lvgl.h"
/*********************
* DEFINES
*********************/
#define REF_IMGS_PATH "lvgl/tests/lv_test_ref_imgs/"
/**********************
* TYPEDEFS
**********************/
typedef struct {
int width, height;
png_byte color_type;
png_byte bit_depth;
png_structp png_ptr;
png_infop info_ptr;
int number_of_passes;
png_bytep * row_pointers;
}png_img_t;
/**********************
* STATIC PROTOTYPES
**********************/
void read_png_file(png_img_t * p, const char* file_name);
void write_png_file(png_img_t * p, const char* file_name);
void png_release(png_img_t * p);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_test_print(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stdout, s, args);
fprintf(stdout, "\n");
va_end(args);
}
void lv_test_exit(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
void lv_test_error(const char * s, ...)
{
va_list args;
va_start(args, s);
vfprintf(stderr, s, args);
fprintf(stderr, "\n");
va_end(args);
exit(1);
}
void lv_test_assert_int_eq(int32_t n_ref, int32_t n_act, const char * s)
{
if(n_ref != n_act) {
lv_test_error(" FAIL: %s. (Expected: %d, Actual: %d)", s, n_ref, n_act);
} else {
lv_test_print(" PASS: %s. (Expected: %d)", s, n_ref);
}
}
void lv_test_assert_int_gt(int32_t n_ref, int32_t n_act, const char * s)
{
if(n_act <= n_ref) {
lv_test_error(" FAIL: %s. (Expected: > %d, Actual: %d)", s, n_ref, n_act);
} else {
lv_test_print(" PASS: %s. (Expected: > %d, , Actual: %d)", s, n_ref, n_act);
}
}
void lv_test_assert_int_lt(int32_t n_ref, int32_t n_act, const char * s)
{
if(n_act >= n_ref) {
lv_test_error(" FAIL: %s. (Expected: < %d, Actual: %d)", s, n_ref, n_act);
} else {
lv_test_print(" PASS: %s. (Expected: < %d, , Actual: %d)", s, n_ref, n_act);
}
}
void lv_test_assert_str_eq(const char * s_ref, const char * s_act, const char * s)
{
if(strcmp(s_ref, s_act) != 0) {
lv_test_error(" FAIL: %s. (Expected: %s, Actual: %s)", s, s_ref, s_act);
} else {
lv_test_print(" PASS: %s. (Expected: %s)", s, s_ref);
}
}
void lv_test_assert_ptr_eq(const void * p_ref, const void * p_act, const char * s)
{
if(p_ref != p_act) {
lv_test_error(" FAIL: %s. (Expected: 0x%lx, Actual: 0x%lx)", s, p_ref, p_act);
} else {
lv_test_print(" PASS: %s. (Expected: 0x%lx)", s, p_ref);
}
}
void lv_test_assert_color_eq(lv_color_t c_ref, lv_color_t c_act, const char * s)
{
if(c_ref.full != c_act.full) {
lv_test_error(" FAIL: %s. (Expected: R:%02x, G:%02x, B:%02x, Actual: R:%02x, G:%02x, B:%02x)", s,
c_ref.ch.red, c_ref.ch.green, c_ref.ch.blue,
c_act.ch.red, c_act.ch.green, c_act.ch.blue);
} else {
lv_test_print(" PASS: %s. (Expected: R:%02x, G:%02x, B:%02x)", s,
c_ref.ch.red, c_ref.ch.green, c_ref.ch.blue);
}
}
void lv_test_assert_img_eq(const char * fn_ref, const char * s)
{
char fn_ref_full[512];
sprintf(fn_ref_full, "%s%s", REF_IMGS_PATH, fn_ref);
png_img_t p;
read_png_file(&p, fn_ref_full);
uint8_t * screen_buf;
lv_disp_t * disp = lv_disp_get_default();
lv_refr_now(disp);
screen_buf = disp->driver.buffer->buf1;
bool err = false;
int x, y, i_buf = 0;
for (y=0; y<p.height; y++) {
png_byte* row = p.row_pointers[y];
for (x=0; x<p.width; x++) {
const png_byte* ptr_ref = &(row[x*3]);
uint8_t * ptr_act = &(screen_buf[i_buf*4]);
uint8_t tmp = ptr_act[0];
ptr_act[0] = ptr_act[2];
ptr_act[2] = tmp;
if(memcmp(ptr_act, ptr_ref, 3) != 0) {
err = true;
break;
}
i_buf++;
}
if(err) break;
}
png_release(&p);
if(err) {
lv_test_error(" FAIL: %s. (Expected: %s)", s, fn_ref);
} else {
lv_test_print(" PASS: %s. (Expected: %s)", s, fn_ref);
}
}
/**********************
* STATIC FUNCTIONS
**********************/
void read_png_file(png_img_t * p, const char* file_name)
{
char header[8]; // 8 is the maximum size that can be checked
/* open file and test for it being a png */
FILE *fp = fopen(file_name, "rb");
if (!fp)
lv_test_exit("[read_png_file] File %s could not be opened for reading", file_name);
fread(header, 1, 8, fp);
if (png_sig_cmp((png_const_bytep)header, 0, 8))
lv_test_exit("[read_png_file] File %s is not recognized as a PNG file", file_name);
/* initialize stuff */
p->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!p->png_ptr)
lv_test_exit("[read_png_file] png_create_read_struct failed");
p->info_ptr = png_create_info_struct(p->png_ptr);
if (!p->info_ptr)
lv_test_exit("[read_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[read_png_file] Error during init_io");
png_init_io(p->png_ptr, fp);
png_set_sig_bytes(p->png_ptr, 8);
png_read_info(p->png_ptr, p->info_ptr);
p->width = png_get_image_width(p->png_ptr, p->info_ptr);
p->height = png_get_image_height(p->png_ptr, p->info_ptr);
p->color_type = png_get_color_type(p->png_ptr, p->info_ptr);
p->bit_depth = png_get_bit_depth(p->png_ptr, p->info_ptr);
p->number_of_passes = png_set_interlace_handling(p->png_ptr);
png_read_update_info(p->png_ptr, p->info_ptr);
/* read file */
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[read_png_file] Error during read_image");
p->row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * p->height);
int y;
for (y=0; y<p->height; y++)
p->row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(p->png_ptr,p->info_ptr));
png_read_image(p->png_ptr, p->row_pointers);
fclose(fp);
}
void write_png_file(png_img_t * p, const char* file_name)
{
/* create file */
FILE *fp = fopen(file_name, "wb");
if (!fp)
lv_test_exit("[write_png_file] File %s could not be opened for writing", file_name);
/* initialize stuff */
p->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!p->png_ptr)
lv_test_exit("[write_png_file] png_create_write_struct failed");
p->info_ptr = png_create_info_struct(p->png_ptr);
if (!p->info_ptr)
lv_test_exit("[write_png_file] png_create_info_struct failed");
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[write_png_file] Error during init_io");
png_init_io(p->png_ptr, fp);
/* write header */
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[write_png_file] Error during writing header");
png_set_IHDR(p->png_ptr, p->info_ptr, p->width, p->height,
p->bit_depth, p->color_type, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(p->png_ptr, p->info_ptr);
/* write bytes */
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[write_png_file] Error during writing bytes");
png_write_image(p->png_ptr, p->row_pointers);
/* end write */
if (setjmp(png_jmpbuf(p->png_ptr)))
lv_test_exit("[write_png_file] Error during end of write");
png_write_end(p->png_ptr, NULL);
fclose(fp);
}
void png_release(png_img_t * p)
{
int y;
for (y=0; y<p->height; y++)
free(p->row_pointers[y]);
free(p->row_pointers);
}
void process_file(png_img_t * p)
{
if (png_get_color_type(p->png_ptr, p->info_ptr) == PNG_COLOR_TYPE_RGB)
lv_test_exit("[process_file] input file is PNG_COLOR_TYPE_RGB but must be PNG_COLOR_TYPE_RGBA "
"(lacks the alpha channel)");
if (png_get_color_type(p->png_ptr, p->info_ptr) != PNG_COLOR_TYPE_RGBA)
lv_test_exit("[process_file] color_type of input file must be PNG_COLOR_TYPE_RGBA (%d) (is %d)",
PNG_COLOR_TYPE_RGBA, png_get_color_type(p->png_ptr, p->info_ptr));
int x, y;
for (y=0; y<p->height; y++) {
png_byte* row = p->row_pointers[y];
for (x=0; x<p->width; x++) {
png_byte* ptr = &(row[x*4]);
printf("Pixel at position [ %d - %d ] has RGBA values: %d - %d - %d - %d\n",
x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
/* set red value to 0 and green value to the blue one */
ptr[0] = 0;
ptr[1] = ptr[2];
}
}
}

51
tests/lv_test_assert.h Normal file
View File

@@ -0,0 +1,51 @@
/**
* @file lv_test_assert.h
*
*/
#ifndef LV_TEST_ASSERT_H
#define LV_TEST_ASSERT_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include <stdbool.h>
#include <stdint.h>
#include "../lvgl.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void lv_test_print(const char * s, ...);
void lv_test_exit(const char * s, ...);
void lv_test_error(const char * s, ...);
void lv_test_assert_int_eq(int32_t n1, int32_t n2, const char * s);
void lv_test_assert_int_gt(int32_t n_ref, int32_t n_act, const char * s);
void lv_test_assert_int_lt(int32_t n_ref, int32_t n_act, const char * s);
void lv_test_assert_str_eq(const char * str1, const char * str2, const char * s);
void lv_test_assert_ptr_eq(const void * p_ref, const void * p_act, const char * s);
void lv_test_assert_color_eq(lv_color_t c_ref, lv_color_t c_act, const char * s);
void lv_test_assert_img_eq(const char * ref_img_fn, const char * s);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_TEST_ASSERT_H*/

View File

@@ -0,0 +1,52 @@
/**
* @file lv_test_core.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../lv_test_assert.h"
#include "lv_test_obj.h"
#include "lv_test_style.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_test_core(void)
{
lv_test_print("");
lv_test_print("*******************");
lv_test_print("Start lv_core tests");
lv_test_print("*******************");
lv_test_obj();
lv_test_style();
}
/**********************
* STATIC FUNCTIONS
**********************/

View File

@@ -0,0 +1,37 @@
/**
* @file lv_test_core.h
*
*/
#ifndef LV_TEST_CORE_H
#define LV_TEST_CORE_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_TEST_CORE_H*/

View File

@@ -0,0 +1,87 @@
/**
* @file lv_test_obj.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../../lvgl.h"
#include "../lv_test_assert.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void create_delete_change_parent(void);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_test_obj(void)
{
lv_test_print("");
lv_test_print("==================");
lv_test_print("Start lv_obj tests");
lv_test_print("==================");
create_delete_change_parent();
}
/**********************
* STATIC FUNCTIONS
**********************/
static void create_delete_change_parent(void)
{
lv_test_print("");
lv_test_print("Create, delete, change parent of a simple object:");
lv_test_print("-------------------------------------------------");
lv_test_print("Create an object on the default screen");
lv_test_assert_int_eq(0, lv_obj_count_children(lv_scr_act()), "Screen's children count before creation");
lv_obj_t * obj = lv_obj_create(lv_scr_act(), NULL);
lv_test_assert_int_eq(1, lv_obj_count_children(lv_scr_act()), "Screen's children count after creation");
lv_test_assert_int_eq(0, lv_obj_count_children(obj), "New object's children count after creation");
lv_test_assert_img_eq("lv_test_obj_1_1.png", "Draw test");
lv_test_print("Delete the created object");
lv_obj_del(obj);
obj = NULL;
lv_test_assert_int_eq(0, lv_obj_count_children(lv_scr_act()), "Screen's children count after delete");
lv_test_print("Create two objects");
lv_obj_t * obj_parent = lv_obj_create(lv_scr_act(), NULL);
lv_obj_t * obj_child = lv_obj_create(lv_scr_act(), NULL);
lv_test_assert_int_eq(2, lv_obj_count_children(lv_scr_act()), "Screen's children count after creation");
lv_test_print("Change the parent of the second object to the first");
lv_obj_set_parent(obj_child, obj_parent);
lv_test_assert_int_eq(1, lv_obj_count_children(lv_scr_act()), "Screen's children count after parent change");
lv_test_assert_int_eq(1, lv_obj_count_children(obj_parent), "Parent object's children count after parent change");
lv_test_assert_int_eq(0, lv_obj_count_children(obj_child), "Child object's children count after parent change");
lv_test_print("Remove the parent object");
lv_obj_del(obj_parent);
lv_test_assert_int_eq(0, lv_obj_count_children(lv_scr_act()), "Screen's children count after delete");
}

View File

@@ -0,0 +1,38 @@
/**
* @file lv_test_obj.h
*
*/
#ifndef LV_TEST_OBJ_H
#define LV_TEST_OBJ_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void lv_test_obj(void);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_TEST_OBJ_H*/

View File

@@ -0,0 +1,583 @@
/**
* @file lv_test_style.c
*
*/
/*********************
* INCLUDES
*********************/
#include "../../lvgl.h"
#include "../lv_test_assert.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void empty_style(void);
static void add_remove_read_prop(void);
static void cascade(void);
static void states(void);
static void mem_leak(void);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_test_style(void)
{
lv_test_print("");
lv_test_print("====================");
lv_test_print("Start lv_style tests");
lv_test_print("====================");
empty_style();
add_remove_read_prop();
cascade();
states();
mem_leak();
}
/**********************
* STATIC FUNCTIONS
**********************/
static void empty_style(void)
{
lv_test_print("");
lv_test_print("Test empty styles:");
lv_test_print("-----------------");
lv_style_list_t style_list;
lv_style_list_init(&style_list);
lv_res_t found;
lv_style_int_t value;
lv_opa_t opa;
void * ptr;
lv_color_t color;
lv_test_print("Get a properties from an empty style");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a 'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a 'color' property");
}
static void add_remove_read_prop(void)
{
lv_test_print("");
lv_test_print("Add, remove and read properties:");
lv_test_print("--------------------------------");
lv_style_list_t style_list;
lv_style_list_init(&style_list);
lv_style_t style;
lv_style_init(&style);
lv_style_list_add_style(&style_list, &style);
lv_res_t found;
lv_style_int_t value;
lv_opa_t opa;
void * ptr;
lv_color_t color;
lv_test_print("Add an empty style and read properties");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a non existing 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a non existing 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a non existing 'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_INV, found, "Get a non existing 'color' property");
lv_test_print("Set properties and read back the values");
lv_style_set_int(&style, LV_STYLE_LINE_SPACE, 5);
lv_style_set_opa(&style, LV_STYLE_BG_OPA, LV_OPA_50);
lv_style_set_ptr(&style, LV_STYLE_FONT, LV_FONT_DEFAULT);
lv_style_set_color(&style, LV_STYLE_BG_COLOR, LV_COLOR_RED);
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an existing 'int' property");
lv_test_assert_int_eq(5, value, "Get the value of an 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an existing 'opa' property");
lv_test_assert_int_eq(LV_OPA_50, opa, "Get the value of an 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an existing 'ptr' property");
lv_test_assert_ptr_eq(LV_FONT_DEFAULT, ptr, "Get the value of a 'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an existing 'color' property");
lv_test_assert_color_eq(LV_COLOR_RED, color, "Get the value of a 'color' property");
lv_test_print("Reset the the style");
lv_style_reset(&style);
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'int' property from a reseted style");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'opa' property from a reseted style");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'ptr' property from a reseted style");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_INV, found, "Get an 'color' property from a reseted style");
/*Clean-up*/
lv_style_list_reset(&style_list);
}
static void cascade(void)
{
lv_test_print("");
lv_test_print("Cascade style styles:");
lv_test_print("----------------------");
lv_style_list_t style_list;
lv_style_list_init(&style_list);
lv_style_t style_first;
lv_style_init(&style_first);
lv_style_list_add_style(&style_list, &style_first);
lv_style_t style_second;
lv_style_init(&style_second);
lv_style_list_add_style(&style_list, &style_second);
lv_res_t found;
lv_style_int_t value;
lv_opa_t opa;
void * ptr;
lv_color_t color;
lv_test_print("Read properties set only in the firstly added style");
lv_style_set_int(&style_first, LV_STYLE_LINE_SPACE, 5);
lv_style_set_opa(&style_first, LV_STYLE_BG_OPA, LV_OPA_50);
lv_style_set_ptr(&style_first, LV_STYLE_FONT, LV_FONT_DEFAULT);
lv_style_set_color(&style_first, LV_STYLE_BG_COLOR, LV_COLOR_RED);
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property");
lv_test_assert_int_eq(5, value, "Get the value of an 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'opa' property");
lv_test_assert_int_eq(LV_OPA_50, opa, "Get the value of an 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'ptr' property");
lv_test_assert_ptr_eq(LV_FONT_DEFAULT, ptr, "Get the value of a 'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'color' property");
lv_test_assert_color_eq(LV_COLOR_RED, color, "Get the value of a 'color' property");
lv_test_print("Overwrite the properties from the second style");
lv_style_set_int(&style_second, LV_STYLE_LINE_SPACE, 10);
lv_style_set_opa(&style_second, LV_STYLE_BG_OPA, LV_OPA_60);
lv_style_set_ptr(&style_second, LV_STYLE_FONT, LV_FONT_DEFAULT + 1);
lv_style_set_color(&style_second, LV_STYLE_BG_COLOR, LV_COLOR_BLUE);
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an overwritten 'int' property");
lv_test_assert_int_eq(10, value, "Get the value of an overwritten 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an overwritten 'opa' property");
lv_test_assert_int_eq(LV_OPA_60, opa, "Get the value of an overwritten 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an overwritten 'ptr' property");
lv_test_assert_ptr_eq(LV_FONT_DEFAULT + 1, ptr, "Get the value of an overwritten 'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an overwritten 'color' property");
lv_test_assert_color_eq(LV_COLOR_BLUE, color, "Get the value of an overwritten 'color' property");
lv_test_print("Overwrite the properties with the local style");
lv_style_list_set_local_int(&style_list, LV_STYLE_LINE_SPACE, 20);
lv_style_list_set_local_opa(&style_list, LV_STYLE_BG_OPA, LV_OPA_70);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_FONT, LV_FONT_DEFAULT + 2);
lv_style_list_set_local_color(&style_list, LV_STYLE_BG_COLOR, LV_COLOR_LIME);
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a local 'int' property");
lv_test_assert_int_eq(20, value, "Get the value of a local 'int' property");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a local 'opa' property");
lv_test_assert_int_eq(LV_OPA_70, opa, "Get the value of a local 'opa' property");
found = lv_style_list_get_ptr(&style_list, LV_STYLE_FONT, &ptr);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a local 'ptr' property");
lv_test_assert_ptr_eq(LV_FONT_DEFAULT + 2, ptr, "Get the value of a local'ptr' property");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a local 'color' property");
lv_test_assert_color_eq(LV_COLOR_LIME, color, "Get the value of a local'color' property");
/*Clean-up*/
lv_style_list_reset(&style_list);
}
static void states(void)
{
lv_test_print("");
lv_test_print("Test style states:");
lv_test_print("------------------");
lv_style_list_t style_list;
lv_style_list_init(&style_list);
lv_style_t style_first;
lv_style_init(&style_first);
lv_style_list_add_style(&style_list, &style_first);
lv_style_t style_second;
lv_style_init(&style_second);
lv_style_list_add_style(&style_list, &style_second);
lv_test_print("Test state precedence in 1 style");
lv_style_set_int(&style_first, LV_STYLE_LINE_SPACE, 5);
lv_style_set_int(&style_first, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_CHECKED, 6);
lv_style_set_int(&style_first, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_PRESSED, 7);
lv_res_t found;
lv_style_int_t value;
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in normal state");
lv_test_assert_int_eq(5, value, "Get the value of an 'int' property in normal state");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_CHECKED, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in checked state");
lv_test_assert_int_eq(6, value, "Get the value of an 'int' in checked state");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_PRESSED, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in pressed state");
lv_test_assert_int_eq(7, value, "Get the value of an 'int' in pressed state");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_HOVER, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in hover (unspecified) state");
lv_test_assert_int_eq(5, value, "Get the value of an 'int' in hover (unspecified) state");
found = lv_style_list_get_int(&style_list, LV_STYLE_LINE_SPACE | LV_STYLE_STATE_CHECKED | LV_STYLE_STATE_PRESSED | LV_STYLE_STATE_HOVER, &value);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in checked pressed hovered state");
lv_test_assert_int_eq(7, value, "Get the value of an 'int' in checked pressed hovered state");
lv_test_print("Test state precedence in 1 style with combined states");
lv_style_set_opa(&style_first, LV_STYLE_BG_OPA, LV_OPA_50);
lv_style_set_opa(&style_first, LV_STYLE_BG_OPA | LV_STYLE_STATE_CHECKED | LV_STYLE_STATE_PRESSED, LV_OPA_60);
lv_opa_t opa;
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA , &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'opa' property in normal state");
lv_test_assert_int_eq(LV_OPA_50, opa, "Get the value of an 'int' in normal state");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA | LV_STYLE_STATE_CHECKED, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'opa' property in checked (unspecified) state");
lv_test_assert_int_eq(LV_OPA_50, opa, "Get the value of an 'int' in checked (unspecified) state");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA | LV_STYLE_STATE_CHECKED | LV_STYLE_STATE_PRESSED, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'opa' property in checked pressed state");
lv_test_assert_int_eq(LV_OPA_60, opa, "Get the value of an 'int' in checked pressed state");
found = lv_style_list_get_opa(&style_list, LV_STYLE_BG_OPA | LV_STYLE_STATE_CHECKED | LV_STYLE_STATE_PRESSED | LV_STYLE_STATE_HOVER, &opa);
lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'opa' property in checked pressed hovered state");
lv_test_assert_int_eq(LV_OPA_60, opa, "Get the value of an 'int' in checked pressed hovered state");
lv_test_print("Test state precedence in 2 styles");
lv_style_set_color(&style_first, LV_STYLE_BG_COLOR, LV_COLOR_YELLOW);
lv_style_set_color(&style_first, LV_STYLE_BG_COLOR | LV_STYLE_STATE_HOVER, LV_COLOR_RED);
lv_style_set_color(&style_second, LV_STYLE_BG_COLOR | LV_STYLE_STATE_CHECKED, LV_COLOR_LIME);
lv_style_set_color(&style_second, LV_STYLE_BG_COLOR | LV_STYLE_STATE_HOVER | LV_STYLE_STATE_PRESSED, LV_COLOR_BLUE);
lv_color_t color;
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in normal state");
lv_test_assert_color_eq(LV_COLOR_YELLOW, color, "Get the value of a 'color' property in normal state");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR | LV_STYLE_STATE_HOVER, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in hovered state");
lv_test_assert_color_eq(LV_COLOR_RED, color, "Get the value of a 'color' in hovered state");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR | LV_STYLE_STATE_CHECKED, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in checked state");
lv_test_assert_color_eq(LV_COLOR_LIME, color, "Get the value of a 'color' in checked state");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR | LV_STYLE_STATE_HOVER | LV_STYLE_STATE_PRESSED, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in hover pressed state");
lv_test_assert_color_eq(LV_COLOR_BLUE, color, "Get the value of a 'color' in hover pressed state");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR | LV_STYLE_STATE_EDIT, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in edit (unspecified) state");
lv_test_assert_color_eq(LV_COLOR_YELLOW, color, "Get the value of a 'color' in edit (unspecified) state");
found = lv_style_list_get_color(&style_list, LV_STYLE_BG_COLOR | LV_STYLE_STATE_CHECKED | LV_STYLE_STATE_EDIT, &color);
lv_test_assert_int_eq(LV_RES_OK, found, "Get a 'color' property in checked edit state");
lv_test_assert_color_eq(LV_COLOR_LIME, color, "Get the value of a 'color' in checked edit state");
/*Clean-up*/
lv_style_list_reset(&style_list);
}
static void mem_leak(void)
{
lv_test_print("");
lv_test_print("Test style set, add, remove memory leak");
lv_test_print("----------------------------------------");
lv_mem_monitor_t mon_start;
lv_mem_monitor_t mon_end;
lv_style_list_t style_list;
lv_style_list_init(&style_list);
lv_style_t style1;
lv_style_init(&style1);
lv_style_t style2;
lv_style_init(&style2);
lv_style_t style3;
lv_style_init(&style3);
uint32_t i;
lv_test_print("Set style properties");
lv_mem_monitor(&mon_start);
for(i = 0; i < 100; i++) {
lv_style_set_color(&style2, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR | LV_STYLE_STATE_EDIT, LV_COLOR_BLUE);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR, LV_COLOR_BLUE);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR | LV_STYLE_STATE_EDIT | LV_STYLE_STATE_FOCUS, LV_COLOR_GREEN);
lv_style_set_color(&style3, LV_STYLE_BG_COLOR, LV_COLOR_RED);
lv_style_set_color(&style3, LV_STYLE_IMAGE_RECOLOR, LV_COLOR_RED);
lv_style_reset(&style1);
lv_style_reset(&style2);
lv_style_reset(&style3);
}
lv_test_assert_int_eq(LV_RES_OK, lv_mem_test(), "Memory integrity check");
lv_mem_defrag();
lv_mem_monitor(&mon_end);
lv_test_assert_int_lt(sizeof(void*) * 8, mon_start.free_size - mon_end.free_size, "Style memory leak");
lv_test_print("Use local style");
lv_mem_monitor(&mon_start);
for(i = 0; i < 100; i++) {
lv_style_list_set_local_ptr(&style_list, LV_STYLE_FONT | LV_STYLE_STATE_PRESSED, LV_FONT_DEFAULT);
lv_style_list_reset(&style_list);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_FONT | LV_STYLE_STATE_PRESSED, NULL);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_reset(&style_list);
}
lv_style_list_reset(&style_list);
lv_test_assert_int_eq(LV_RES_OK, lv_mem_test(), "Memory integrity check");
lv_mem_defrag();
lv_mem_monitor(&mon_end);
lv_test_assert_int_lt(sizeof(void*) * 8, mon_start.free_size - mon_end.free_size, "Style memory leak");
lv_test_print("Add styles");
lv_mem_monitor(&mon_start);
for(i = 0; i < 100; i++) {
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style2, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_list_add_style(&style_list, &style1);
lv_style_list_remove_style(&style_list, &style1);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style3);
lv_style_list_remove_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style1);
lv_style_list_reset(&style_list);
lv_style_reset(&style1);
lv_style_reset(&style2);
lv_style_reset(&style3);
}
lv_test_assert_int_eq(LV_RES_OK, lv_mem_test(), "Memory integrity check");
lv_mem_defrag();
lv_mem_monitor(&mon_end);
lv_test_assert_int_lt(sizeof(void*) * 8, mon_start.free_size - mon_end.free_size, "Style memory leak");
lv_test_print("Add styles and use local style");
lv_mem_monitor(&mon_start);
for(i = 0; i < 100; i++) {
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style2, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style3, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
if(i % 2 == 0) lv_style_list_set_local_color(&style_list, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_list_add_style(&style_list, &style1);
lv_style_list_remove_style(&style_list, &style1);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style3);
lv_style_list_remove_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style1);
if(i % 2 != 0) lv_style_list_set_local_color(&style_list, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_list_reset(&style_list);
lv_style_reset(&style1);
lv_style_reset(&style2);
lv_style_reset(&style3);
}
lv_test_assert_int_eq(LV_RES_OK, lv_mem_test(), "Memory integrity check");
lv_mem_defrag();
lv_mem_monitor(&mon_end);
lv_test_assert_int_lt(sizeof(void*) * 8, mon_start.free_size - mon_end.free_size, "Style memory leak");
lv_test_print("Complex test");
lv_mem_monitor(&mon_start);
for(i = 0; i < 100; i++) {
if(i % 2 == 0) {
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_CLOSE);
}
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR, LV_COLOR_RED);
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR | LV_STYLE_STATE_EDIT, LV_COLOR_BLUE);
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR, LV_COLOR_BLUE);
lv_style_set_color(&style1, LV_STYLE_LINE_COLOR | LV_STYLE_STATE_EDIT | LV_STYLE_STATE_FOCUS, LV_COLOR_GREEN);
lv_style_list_add_style(&style_list, &style1);
if(i % 4 == 0) {
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_CLOSE);
}
lv_style_list_remove_style(&style_list, &style1);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_10);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_20);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_30);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_40);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_50);
lv_style_set_opa(&style2, LV_STYLE_TEXT_OPA, LV_OPA_60);
lv_style_list_add_style(&style_list, &style2);
if(i % 8 == 0) {
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_CLOSE);
}
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_remove_style(&style_list, &style2);
lv_style_set_int(&style3, LV_STYLE_PAD_LEFT, 10);
lv_style_set_int(&style3, LV_STYLE_PAD_RIGHT, 20);
lv_style_set_int(&style3, LV_STYLE_PAD_LEFT, 11);
lv_style_set_int(&style3, LV_STYLE_PAD_RIGHT, 21);
lv_style_set_int(&style3, LV_STYLE_PAD_LEFT, 12);
lv_style_set_int(&style3, LV_STYLE_PAD_RIGHT, 22);
lv_style_set_int(&style3, LV_STYLE_PAD_LEFT, 12);
lv_style_set_int(&style3, LV_STYLE_PAD_RIGHT, 23);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_FONT | LV_STYLE_STATE_PRESSED, LV_FONT_DEFAULT);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_FONT | LV_STYLE_STATE_PRESSED, NULL);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_set_local_ptr(&style_list, LV_STYLE_PATTERN_IMAGE, LV_SYMBOL_OK);
lv_style_list_add_style(&style_list, &style3);
lv_style_list_add_style(&style_list, &style2);
lv_style_list_add_style(&style_list, &style1);
lv_style_list_reset(&style_list);
lv_style_reset(&style1);
lv_style_reset(&style2);
lv_style_reset(&style3);
}
lv_style_list_reset(&style_list);
lv_style_reset(&style1);
lv_style_reset(&style2);
lv_style_reset(&style3);
lv_test_assert_int_eq(LV_RES_OK, lv_mem_test(), "Memory integrity check");
lv_mem_defrag();
lv_mem_monitor(&mon_end);
lv_test_assert_int_lt(sizeof(void*) * 8, mon_start.free_size - mon_end.free_size, "Style memory leak");
}

View File

@@ -0,0 +1,38 @@
/**
* @file lv_test_style.h
*
*/
#ifndef LV_TEST_STYLE_H
#define LV_TEST_STYLE_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
void lv_test_style(void);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*LV_TEST_STYLE_H*/

65
tests/lv_test_main.c Normal file
View File

@@ -0,0 +1,65 @@
#include "../lvgl.h"
#include <stdio.h>
#include <sys/time.h>
#if LV_BUILD_TEST == 0
static void hal_init(void);
static void dummy_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p);
int main2(void)
{
printf("Call lv_init...\n");
lv_init();
hal_init();
lv_test_core();
printf("Exit with success!\n");
return 0;
}
static void hal_init(void)
{
static lv_disp_buf_t disp_buf;
lv_color_t * disp_buf1 = malloc(LV_HOR_RES * LV_VER_RES * sizeof(lv_color_t));
lv_disp_buf_init(&disp_buf, disp_buf1, NULL, LV_HOR_RES* LV_VER_RES);
lv_disp_drv_t disp_drv;
lv_disp_drv_init(&disp_drv);
disp_drv.buffer = &disp_buf;
disp_drv.flush_cb = dummy_flush_cb;
lv_disp_drv_register(&disp_drv);
}
static void dummy_flush_cb(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
{
lv_disp_flush_ready(disp_drv);
}
uint32_t custom_tick_get(void)
{
static uint64_t start_ms = 0;
if(start_ms == 0) {
struct timeval tv_start;
gettimeofday(&tv_start, NULL);
start_ms = (tv_start.tv_sec * 1000000 + tv_start.tv_usec) / 1000;
}
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
uint64_t now_ms;
now_ms = (tv_now.tv_sec * 1000000 + tv_now.tv_usec) / 1000;
uint32_t time_ms = now_ms - start_ms;
return time_ms;
}
#endif

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB