awtk/tests/widget_factory_test.cc

70 lines
2.0 KiB
C++
Raw Normal View History

2018-06-27 13:46:42 +08:00
#include "gtest/gtest.h"
#include "tkc/mem.h"
#include "widgets/button.h"
#include "base/widget_factory.h"
typedef struct _custom_widget_t {
widget_t base;
} custom_widget_t;
2018-08-01 12:53:59 +08:00
static const widget_vtable_t s_custom_vtable = {};
widget_t* custom_widget_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
widget_t* widget = NULL;
custom_widget_t* c = TKMEM_ZALLOC(custom_widget_t);
return_value_if_fail(c != NULL, NULL);
widget = WIDGET(c);
2018-08-01 12:53:59 +08:00
widget_init(widget, parent, &s_custom_vtable, x, y, w, h);
return widget;
}
TEST(WidgetFactory, button) {
widget_t* button = NULL;
widget_factory_t* factory = widget_factory_create();
2019-08-20 07:44:53 +08:00
widget_factory_register(factory, "button", button_create);
button = widget_factory_create_widget(factory, "button", NULL, 10, 20, 30, 40);
ASSERT_EQ(button != NULL, true);
ASSERT_EQ(button->x, 10);
ASSERT_EQ(button->y, 20);
ASSERT_EQ(button->w, 30);
ASSERT_EQ(button->h, 40);
2018-08-04 11:58:43 +08:00
widget_destroy(button);
widget_factory_destroy(factory);
}
TEST(WidgetFactory, custom) {
widget_t* custom = NULL;
widget_factory_t* factory = widget_factory_create();
custom = widget_factory_create_widget(factory, "custom", NULL, 10, 20, 30, 40);
ASSERT_EQ(custom, WIDGET(NULL));
widget_factory_register(factory, "custom", custom_widget_create);
custom = widget_factory_create_widget(factory, "custom", NULL, 10, 20, 30, 40);
ASSERT_EQ(custom != NULL, true);
ASSERT_EQ(custom->x, 10);
ASSERT_EQ(custom->y, 20);
ASSERT_EQ(custom->w, 30);
ASSERT_EQ(custom->h, 40);
2018-08-04 11:58:43 +08:00
widget_destroy(custom);
widget_factory_destroy(factory);
}
2021-07-15 07:08:11 +08:00
TEST(WidgetFactory, over_write) {
widget_t* custom = NULL;
widget_factory_t* factory = widget_factory_create();
widget_factory_register(factory, "button", button_create);
widget_factory_register(factory, "button", custom_widget_create);
custom = widget_factory_create_widget(factory, "button", NULL, 10, 20, 30, 40);
ASSERT_EQ(custom->vt, &s_custom_vtable);
widget_destroy(custom);
widget_factory_destroy(factory);
}