mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-02 03:58:33 +08:00
add tests and refactoring
This commit is contained in:
parent
03ce6fb5d6
commit
c262208307
@ -1,4 +1,4 @@
|
||||
<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="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>
|
@ -1,4 +1,4 @@
|
||||
<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="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>
|
@ -71,33 +71,44 @@ typedef enum _image_draw_type_t {
|
||||
/**
|
||||
* @const IMAGE_DRAW_CENTER
|
||||
* 居中显示。
|
||||
* 将图片按原大小显示在目标矩形的中央。
|
||||
*/
|
||||
IMAGE_DRAW_CENTER = 0,
|
||||
|
||||
/**
|
||||
* @const IMAGE_DRAW_SCALE
|
||||
* 缩放至目标矩形大小(不保证宽高成比例)。
|
||||
* 缩放显示。
|
||||
* 将图片缩放至目标矩形的大小(不保证宽高成比例)。
|
||||
*/
|
||||
IMAGE_DRAW_SCALE,
|
||||
|
||||
/**
|
||||
* @const 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
|
||||
* 平铺显示。
|
||||
*/
|
||||
IMAGE_DRAW_REPEAT,
|
||||
|
||||
/**
|
||||
* @const IMAGE_DRAW_REPEAT_X
|
||||
* 水平方向平铺显示,垂直方向缩放。
|
||||
@ -108,19 +119,25 @@ typedef enum _image_draw_type_t {
|
||||
* 垂直方向平铺显示,水平方向缩放。
|
||||
*/
|
||||
IMAGE_DRAW_REPEAT_Y,
|
||||
|
||||
/**
|
||||
* @const IMAGE_DRAW_9PATCH
|
||||
* 9宫格显示。
|
||||
* 将图片分成等大小的9块,4个角按原大小显示在目标矩形的4个角,左右上下和中间5块分别缩放显示在对应的目标区域。
|
||||
*/
|
||||
IMAGE_DRAW_9PATCH,
|
||||
|
||||
/**
|
||||
* @const IMAGE_DRAW_3PATCH_X
|
||||
* 水平方向3宫格显示。
|
||||
* 将图片在水平方向上分成等大小的3块,左右两块按原大小显示在目标矩形的左右,中间一块缩放显示在目标区域中间剩余部分。
|
||||
*/
|
||||
IMAGE_DRAW_3PATCH_X,
|
||||
|
||||
/**
|
||||
* @const IMAGE_DRAW_3PATCH_Y
|
||||
* 垂直方向3宫格显示。
|
||||
* 将图片在垂直方向上分成等大小的3块,上下两块按原大小显示在目标矩形的上下,中间一块缩放显示在目标区域中间剩余部分。
|
||||
*/
|
||||
IMAGE_DRAW_3PATCH_Y
|
||||
} image_draw_type_t;
|
||||
|
@ -809,7 +809,7 @@ ret_t canvas_test_paint(canvas_t* c, bool_t pressed, xy_t x, xy_t y) {
|
||||
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 d;
|
||||
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);
|
||||
}
|
||||
|
||||
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 d;
|
||||
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);
|
||||
case IMAGE_DRAW_SCALE_AUTO: {
|
||||
return canvas_draw_image_scale(c, img, dst);
|
||||
case IMAGE_DRAW_SCALE_X:
|
||||
return canvas_draw_image_scale_x(c, img, dst);
|
||||
case IMAGE_DRAW_SCALE_Y:
|
||||
return canvas_draw_image_scale_y(c, img, dst);
|
||||
case IMAGE_DRAW_SCALE_W:
|
||||
return canvas_draw_image_scale_w(c, img, dst);
|
||||
case IMAGE_DRAW_SCALE_H:
|
||||
return canvas_draw_image_scale_h(c, img, dst);
|
||||
case IMAGE_DRAW_REPEAT:
|
||||
return canvas_draw_image_repeat(c, img, dst);
|
||||
case IMAGE_DRAW_REPEAT_X:
|
||||
|
@ -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_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_x(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_w(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);
|
||||
|
||||
|
@ -74,8 +74,8 @@ static const key_type_value_t resource_type_value[] = {
|
||||
|
||||
static const key_type_value_t image_draw_type_name_value[] = {
|
||||
{"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_y", 0, IMAGE_DRAW_SCALE_Y}, {"repeat", 0, IMAGE_DRAW_REPEAT},
|
||||
{"scale_auto", 0, IMAGE_DRAW_SCALE_AUTO}, {"scale_w", 0, IMAGE_DRAW_SCALE_W},
|
||||
{"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},
|
||||
{"9patch", 0, IMAGE_DRAW_9PATCH}, {"3patch_x", 0, IMAGE_DRAW_3PATCH_X},
|
||||
{"3patch_y", 0, IMAGE_DRAW_3PATCH_Y}};
|
||||
|
@ -106,6 +106,12 @@ typedef enum _event_type_t {
|
||||
*/
|
||||
EVT_PROP_CHANGED,
|
||||
|
||||
/**
|
||||
* @const EVT_VALUE_CHANGED
|
||||
* 控件的值改变事件名。
|
||||
*/
|
||||
EVT_VALUE_CHANGED,
|
||||
|
||||
/**
|
||||
* @const EVT_PAINT
|
||||
* 绘制事件名。
|
||||
|
@ -619,7 +619,7 @@ TEST(Canvas, draw_image_repeat) {
|
||||
lcd_destroy(lcd);
|
||||
}
|
||||
|
||||
TEST(Canvas, draw_image_scale_x) {
|
||||
TEST(Canvas, draw_image_scale_w) {
|
||||
rect_t r;
|
||||
rect_t d;
|
||||
canvas_t c;
|
||||
@ -637,25 +637,25 @@ TEST(Canvas, draw_image_scale_x) {
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
canvas_end_frame(&c);
|
||||
@ -663,7 +663,7 @@ TEST(Canvas, draw_image_scale_x) {
|
||||
lcd_destroy(lcd);
|
||||
}
|
||||
|
||||
TEST(Canvas, draw_image_scale_y) {
|
||||
TEST(Canvas, draw_image_scale_h) {
|
||||
rect_t r;
|
||||
rect_t d;
|
||||
canvas_t c;
|
||||
@ -681,25 +681,25 @@ TEST(Canvas, draw_image_scale_y) {
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
lcd_log_reset(lcd);
|
||||
rect_init(r, 0, 0, img.w, img.h);
|
||||
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);");
|
||||
|
||||
canvas_end_frame(&c);
|
||||
|
46
tests/check_button_test.cc
Normal file
46
tests/check_button_test.cc
Normal 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);
|
||||
}
|
@ -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_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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user