add tests and refactoring

This commit is contained in:
lixianjing 2018-03-26 12:16:52 +08:00
parent 03ce6fb5d6
commit c262208307
10 changed files with 117 additions and 30 deletions

View File

@ -1,4 +1,4 @@
<window name="main" x="0" y="0" w="320" h="480"> <window name="main" x="0" y="0" w="320" h="480">
<image style="1:border" x="0" y="0" w="50%" h="50%" image="earth" draw_type="center"/> <image style="1:border" x="0" y="0" w="50%" h="50%" image="earth" draw_type="center"/>
<image style="1:border" x="0" y="50%" w="100%" h="80" image="earth" draw_type="scale_y"/> <image style="1:border" x="0" y="50%" w="100%" h="80" image="earth" draw_type="scale_h"/>
</window> </window>

View File

@ -1,4 +1,4 @@
<window name="main" x="0" y="0" w="320" h="480"> <window name="main" x="0" y="0" w="320" h="480">
<image style="1:border" x="0" y="0" w="50%" h="50%" image="earth" draw_type="center"/> <image style="1:border" x="0" y="0" w="50%" h="50%" image="earth" draw_type="center"/>
<image style="1:border" x="0" y="50%" w="50" h="50%" image="earth" draw_type="scale_x"/> <image style="1:border" x="0" y="50%" w="50" h="50%" image="earth" draw_type="scale_w"/>
</window> </window>

View File

@ -71,33 +71,44 @@ typedef enum _image_draw_type_t {
/** /**
* @const IMAGE_DRAW_CENTER * @const IMAGE_DRAW_CENTER
* *
*
*/ */
IMAGE_DRAW_CENTER = 0, IMAGE_DRAW_CENTER = 0,
/** /**
* @const IMAGE_DRAW_SCALE * @const IMAGE_DRAW_SCALE
* () *
* ()
*/ */
IMAGE_DRAW_SCALE, IMAGE_DRAW_SCALE,
/** /**
* @const IMAGE_DRAW_SCALE_AUTO * @const IMAGE_DRAW_SCALE_AUTO
* *
* ()
*/ */
IMAGE_DRAW_SCALE_AUTO, IMAGE_DRAW_SCALE_AUTO,
/** /**
* @const IMAGE_DRAW_SCALE_X * @const IMAGE_DRAW_SCALE_W
* *
*
*/ */
IMAGE_DRAW_SCALE_X, IMAGE_DRAW_SCALE_W,
/** /**
* @const IMAGE_DRAW_SCALE_Y * @const IMAGE_DRAW_SCALE_H
* *
*
*/ */
IMAGE_DRAW_SCALE_Y, IMAGE_DRAW_SCALE_H,
/** /**
* @const IMAGE_DRAW_REPEAT * @const IMAGE_DRAW_REPEAT
* *
*/ */
IMAGE_DRAW_REPEAT, IMAGE_DRAW_REPEAT,
/** /**
* @const IMAGE_DRAW_REPEAT_X * @const IMAGE_DRAW_REPEAT_X
* *
@ -108,19 +119,25 @@ typedef enum _image_draw_type_t {
* *
*/ */
IMAGE_DRAW_REPEAT_Y, IMAGE_DRAW_REPEAT_Y,
/** /**
* @const IMAGE_DRAW_9PATCH * @const IMAGE_DRAW_9PATCH
* 9 * 9
* 9445
*/ */
IMAGE_DRAW_9PATCH, IMAGE_DRAW_9PATCH,
/** /**
* @const IMAGE_DRAW_3PATCH_X * @const IMAGE_DRAW_3PATCH_X
* 3 * 3
* 3
*/ */
IMAGE_DRAW_3PATCH_X, IMAGE_DRAW_3PATCH_X,
/** /**
* @const IMAGE_DRAW_3PATCH_Y * @const IMAGE_DRAW_3PATCH_Y
* 3 * 3
* 3
*/ */
IMAGE_DRAW_3PATCH_Y IMAGE_DRAW_3PATCH_Y
} image_draw_type_t; } image_draw_type_t;

View File

@ -809,7 +809,7 @@ ret_t canvas_test_paint(canvas_t* c, bool_t pressed, xy_t x, xy_t y) {
return RET_OK; return RET_OK;
} }
ret_t canvas_draw_image_scale_x(canvas_t* c, bitmap_t* img, rect_t* dst) { ret_t canvas_draw_image_scale_w(canvas_t* c, bitmap_t* img, rect_t* dst) {
rect_t s; rect_t s;
rect_t d; rect_t d;
wh_t src_h = 0; wh_t src_h = 0;
@ -832,7 +832,7 @@ ret_t canvas_draw_image_scale_x(canvas_t* c, bitmap_t* img, rect_t* dst) {
return canvas_draw_image(c, img, &s, &d); return canvas_draw_image(c, img, &s, &d);
} }
ret_t canvas_draw_image_scale_y(canvas_t* c, bitmap_t* img, rect_t* dst) { ret_t canvas_draw_image_scale_h(canvas_t* c, bitmap_t* img, rect_t* dst) {
rect_t s; rect_t s;
rect_t d; rect_t d;
wh_t src_w = 0; wh_t src_w = 0;
@ -911,10 +911,10 @@ ret_t canvas_draw_image_ex(canvas_t* c, bitmap_t* img, image_draw_type_t draw_ty
return canvas_draw_image(c, img, &src, dst); return canvas_draw_image(c, img, &src, dst);
case IMAGE_DRAW_SCALE_AUTO: { case IMAGE_DRAW_SCALE_AUTO: {
return canvas_draw_image_scale(c, img, dst); return canvas_draw_image_scale(c, img, dst);
case IMAGE_DRAW_SCALE_X: case IMAGE_DRAW_SCALE_W:
return canvas_draw_image_scale_x(c, img, dst); return canvas_draw_image_scale_w(c, img, dst);
case IMAGE_DRAW_SCALE_Y: case IMAGE_DRAW_SCALE_H:
return canvas_draw_image_scale_y(c, img, dst); return canvas_draw_image_scale_h(c, img, dst);
case IMAGE_DRAW_REPEAT: case IMAGE_DRAW_REPEAT:
return canvas_draw_image_repeat(c, img, dst); return canvas_draw_image_repeat(c, img, dst);
case IMAGE_DRAW_REPEAT_X: case IMAGE_DRAW_REPEAT_X:

View File

@ -91,8 +91,8 @@ ret_t canvas_draw_image_repeat(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_repeat_x(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_repeat_x(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_repeat_y(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_repeat_y(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_scale(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_scale(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_scale_x(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_scale_w(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_scale_y(canvas_t* c, bitmap_t* img, rect_t* dst); ret_t canvas_draw_image_scale_h(canvas_t* c, bitmap_t* img, rect_t* dst);
ret_t canvas_draw_image_ex(canvas_t* c, bitmap_t* img, image_draw_type_t draw_type, rect_t* dst); ret_t canvas_draw_image_ex(canvas_t* c, bitmap_t* img, image_draw_type_t draw_type, rect_t* dst);

View File

@ -74,8 +74,8 @@ static const key_type_value_t resource_type_value[] = {
static const key_type_value_t image_draw_type_name_value[] = { static const key_type_value_t image_draw_type_name_value[] = {
{"center", 0, IMAGE_DRAW_CENTER}, {"scale", 0, IMAGE_DRAW_SCALE}, {"center", 0, IMAGE_DRAW_CENTER}, {"scale", 0, IMAGE_DRAW_SCALE},
{"scale_auto", 0, IMAGE_DRAW_SCALE_AUTO}, {"scale_x", 0, IMAGE_DRAW_SCALE_X}, {"scale_auto", 0, IMAGE_DRAW_SCALE_AUTO}, {"scale_w", 0, IMAGE_DRAW_SCALE_W},
{"scale_y", 0, IMAGE_DRAW_SCALE_Y}, {"repeat", 0, IMAGE_DRAW_REPEAT}, {"scale_h", 0, IMAGE_DRAW_SCALE_H}, {"repeat", 0, IMAGE_DRAW_REPEAT},
{"repeat_x", 0, IMAGE_DRAW_REPEAT_X}, {"repeat_y", 0, IMAGE_DRAW_REPEAT_Y}, {"repeat_x", 0, IMAGE_DRAW_REPEAT_X}, {"repeat_y", 0, IMAGE_DRAW_REPEAT_Y},
{"9patch", 0, IMAGE_DRAW_9PATCH}, {"3patch_x", 0, IMAGE_DRAW_3PATCH_X}, {"9patch", 0, IMAGE_DRAW_9PATCH}, {"3patch_x", 0, IMAGE_DRAW_3PATCH_X},
{"3patch_y", 0, IMAGE_DRAW_3PATCH_Y}}; {"3patch_y", 0, IMAGE_DRAW_3PATCH_Y}};

View File

@ -106,6 +106,12 @@ typedef enum _event_type_t {
*/ */
EVT_PROP_CHANGED, EVT_PROP_CHANGED,
/**
* @const EVT_VALUE_CHANGED
*
*/
EVT_VALUE_CHANGED,
/** /**
* @const EVT_PAINT * @const EVT_PAINT
* *

View File

@ -619,7 +619,7 @@ TEST(Canvas, draw_image_repeat) {
lcd_destroy(lcd); lcd_destroy(lcd);
} }
TEST(Canvas, draw_image_scale_x) { TEST(Canvas, draw_image_scale_w) {
rect_t r; rect_t r;
rect_t d; rect_t d;
canvas_t c; canvas_t c;
@ -637,25 +637,25 @@ TEST(Canvas, draw_image_scale_x) {
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w / 2, img.h / 2); rect_init(d, 0, 0, img.w / 2, img.h / 2);
ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_w(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w + 10, img.h / 2); rect_init(d, 0, 0, img.w + 10, img.h / 2);
ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_w(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,12,0,0,42,16);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,12,0,0,42,16);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10);
ASSERT_EQ(canvas_draw_image_scale_x(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_w(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,64,64);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,64,64);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10);
ASSERT_EQ(canvas_draw_image_ex(&c, &img, IMAGE_DRAW_SCALE_X, &d), RET_OK); ASSERT_EQ(canvas_draw_image_ex(&c, &img, IMAGE_DRAW_SCALE_W, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,64,64);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,64,64);");
canvas_end_frame(&c); canvas_end_frame(&c);
@ -663,7 +663,7 @@ TEST(Canvas, draw_image_scale_x) {
lcd_destroy(lcd); lcd_destroy(lcd);
} }
TEST(Canvas, draw_image_scale_y) { TEST(Canvas, draw_image_scale_h) {
rect_t r; rect_t r;
rect_t d; rect_t d;
canvas_t c; canvas_t c;
@ -681,25 +681,25 @@ TEST(Canvas, draw_image_scale_y) {
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w / 2, img.h / 2); rect_init(d, 0, 0, img.w / 2, img.h / 2);
ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_h(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w + 10, img.h / 2); rect_init(d, 0, 0, img.w + 10, img.h / 2);
ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_h(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,32,32,0,0,16,16);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10);
ASSERT_EQ(canvas_draw_image_scale_y(&c, &img, &d), RET_OK); ASSERT_EQ(canvas_draw_image_scale_h(&c, &img, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,27,32,0,0,64,74);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,27,32,0,0,64,74);");
lcd_log_reset(lcd); lcd_log_reset(lcd);
rect_init(r, 0, 0, img.w, img.h); rect_init(r, 0, 0, img.w, img.h);
rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10); rect_init(d, 0, 0, img.w * 2, img.h * 2 + 10);
ASSERT_EQ(canvas_draw_image_ex(&c, &img, IMAGE_DRAW_SCALE_Y, &d), RET_OK); ASSERT_EQ(canvas_draw_image_ex(&c, &img, IMAGE_DRAW_SCALE_H, &d), RET_OK);
ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,27,32,0,0,64,74);"); ASSERT_EQ(lcd_log_get_commands(lcd), "dg(0,0,27,32,0,0,64,74);");
canvas_end_frame(&c); canvas_end_frame(&c);

View File

@ -0,0 +1,46 @@
#include "gtest/gtest.h"
#include "base/check_button.h"
static ret_t on_changed(void* ctx, event_t* e) {
int* count = (int*)ctx;
if(e->type == EVT_VALUE_CHANGED) {
*count += 1;
}
return RET_OK;
}
TEST(CheckButton, basic) {
uint32_t id = 0;
int count = 0;
widget_t* button = check_button_create(NULL, 0, 0, 100, 100);
ASSERT_EQ(CHECK_BUTTON(button)->radio, FALSE);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
id = widget_on(button, EVT_VALUE_CHANGED, on_changed, &count);
ASSERT_EQ(id > 0, TRUE);
ASSERT_EQ(check_button_set_value(button, TRUE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, TRUE);
ASSERT_EQ(count, 1);
ASSERT_EQ(check_button_set_value(button, FALSE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
ASSERT_EQ(count, 2);
ASSERT_EQ(widget_off(button, id), RET_OK);
ASSERT_EQ(check_button_set_value(button, TRUE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, TRUE);
ASSERT_EQ(count, 2);
ASSERT_EQ(check_button_set_value(button, FALSE), RET_OK);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
ASSERT_EQ(count, 2);
}
TEST(CheckButton, radio) {
widget_t* button = check_button_create_radio(NULL, 0, 0, 100, 100);
ASSERT_EQ(CHECK_BUTTON(button)->radio, TRUE);
ASSERT_EQ(CHECK_BUTTON(button)->value, FALSE);
}

View File

@ -56,3 +56,21 @@ TEST(ThemeGen, state) {
ASSERT_EQ(style_get_int(&style, STYLE_ID_BG_COLOR, 0), 0xff00ffff); ASSERT_EQ(style_get_int(&style, STYLE_ID_BG_COLOR, 0), 0xff00ffff);
ASSERT_EQ(style_get_int(&style, STYLE_ID_FG_COLOR, 0), 0x7f00ffff); ASSERT_EQ(style_get_int(&style, STYLE_ID_FG_COLOR, 0), 0x7f00ffff);
} }
TEST(ThemeGen, style_type) {
uint8_t buff[1024];
theme_t theme;
style_t style;
const char* str =
"<button style_type=\"1:yellow\" state=\"over\" bg-color=\"yellow\" fg-color=\"#fafbfc\" font-name=\"sans\" font-size=\"12\" />\
<button style_type=\"1:yellow\" state=\"pressed\" bg-color=\"rgb(255,255,0)\" fg-color=\"rgba(255,255,0,0.5)\" border-color=\"#ff00ff\" />";
xml_gen_buff(str, buff, sizeof(buff));
theme.data = buff;
style.data = theme_find_style(&theme, WIDGET_BUTTON, 1, WIDGET_STATE_OVER);
ASSERT_EQ(style.data != NULL, true);
style.data = theme_find_style(&theme, WIDGET_BUTTON, 1, WIDGET_STATE_PRESSED);
ASSERT_EQ(style.data != NULL, true);
}