#include "base/dialog.h" #include "ui_loader/ui_builder_default.h" #include "ui_loader/ui_loader_xml.h" #include "gtest/gtest.h" TEST(UILoaderXML, basic) { widget_t* ok = NULL; widget_t* cancel = NULL; ui_loader_t* loader = xml_ui_loader(); ui_builder_t* builder = ui_builder_default_create(""); const char* str = "\ \ \ "; ASSERT_EQ(ui_loader_load(loader, (const uint8_t*)str, strlen(str), builder), RET_OK); ASSERT_EQ(tk_str_eq(widget_get_type(builder->root), WIDGET_TYPE_DIALOG), TRUE); ASSERT_EQ(widget_count_children(builder->root), 2); ok = widget_lookup(builder->root, "ok", TRUE); cancel = widget_lookup(builder->root, "cancel", TRUE); ASSERT_EQ(ok != NULL, true); ASSERT_EQ(tk_str_eq(widget_get_type(ok), WIDGET_TYPE_BUTTON), true); ASSERT_EQ(cancel != NULL, true); ASSERT_EQ(tk_str_eq(widget_get_type(cancel), WIDGET_TYPE_LABEL), true); ASSERT_EQ(strcmp(widget_get_child(DIALOG(builder->root)->client, 0)->name, "ok"), 0); ASSERT_EQ(strcmp(widget_get_child(DIALOG(builder->root)->client, 1)->name, "cancel"), 0); widget_destroy(builder->root); ui_builder_destroy(builder); } TEST(UILoaderXML, attr) { widget_t* b1 = NULL; widget_t* b2 = NULL; widget_t* b3 = NULL; widget_t* b4 = NULL; ui_loader_t* loader = xml_ui_loader(); ui_builder_t* builder = ui_builder_default_create(""); const char* str = "\ \ "; ASSERT_EQ(ui_loader_load(loader, (const uint8_t*)str, strlen(str), builder), RET_OK); ASSERT_EQ(builder->root != NULL, true); widget_layout(builder->root); b1 = widget_lookup(builder->root, "b1", TRUE); ASSERT_EQ(b1 != NULL, true); ASSERT_EQ(b1->x, 10); ASSERT_EQ(b1->y, 10); ASSERT_EQ(b1->w, 80); ASSERT_EQ(b1->h, 20); ASSERT_EQ(wcscmp(b1->text.str, L"ac\"&"), 0); wh_t parent_w = b1->parent->w; wh_t parent_h = b1->parent->h; b2 = widget_lookup(builder->root, "b2", TRUE); ASSERT_EQ(b2 != NULL, true); ASSERT_EQ(b2->x, 40); ASSERT_EQ(b2->y, parent_h * 10 / 100); ASSERT_EQ(b2->w, 320); ASSERT_EQ(b2->h, parent_h * 20 / 100); b3 = widget_lookup(builder->root, "b3", TRUE); ASSERT_EQ(b3 != NULL, true); ASSERT_EQ(b3->x, 160); ASSERT_EQ(b3->y, (parent_h - b3->h) / 2); ASSERT_EQ(b3->w, 80); ASSERT_EQ(b3->h, 20); b4 = widget_lookup(builder->root, "b4", TRUE); ASSERT_EQ(b4 != NULL, true); ASSERT_EQ(b4->x, parent_w - b4->w); ASSERT_EQ(b4->y, parent_h - b4->h); ASSERT_EQ(b4->w, 80); ASSERT_EQ(b4->h, 20); widget_destroy(builder->root); ui_builder_destroy(builder); } TEST(UILoaderXML, prop1) { widget_t* root = NULL; ui_loader_t* loader = xml_ui_loader(); ui_builder_t* builder = ui_builder_default_create(""); const char* str = "\ 1\ 2\ 3\ 4\ 123]]>123\ "; ASSERT_EQ(ui_loader_load(loader, (const uint8_t*)str, strlen(str), builder), RET_OK); root = builder->root; ASSERT_EQ(builder->root != NULL, true); ASSERT_EQ(root->x, 1); ASSERT_EQ(root->y, 2); ASSERT_EQ(root->w, 3); ASSERT_EQ(root->h, 4); ASSERT_STREQ(root->tr_text, "123123"); ui_builder_destroy(builder); widget_destroy(root); } TEST(UILoaderXML, prop2) { widget_t* root = NULL; ui_loader_t* loader = xml_ui_loader(); ui_builder_t* builder = ui_builder_default_create(""); const char* str = "\ 1\ 2\ 3\ 4\ 123]]>123\ "; ASSERT_EQ(ui_loader_load(loader, (const uint8_t*)str, strlen(str), builder), RET_OK); root = builder->root; ASSERT_EQ(builder->root != NULL, true); ASSERT_EQ(root->x, 1); ASSERT_EQ(root->y, 2); ASSERT_EQ(root->w, 3); ASSERT_EQ(root->h, 4); ASSERT_STREQ(root->tr_text, "123123"); widget_destroy(root); ui_builder_destroy(builder); } TEST(UILoaderXML, ui_loader_load_widget_from_xml) { const char* str = "\ 1\ 2\ 3\ 4\ 123]]>123\ "; widget_t* root = ui_loader_load_widget_from_xml(NULL, str, strlen(str)); ASSERT_EQ(root != NULL, true); ASSERT_EQ(root->x, 1); ASSERT_EQ(root->y, 2); ASSERT_EQ(root->w, 3); ASSERT_EQ(root->h, 4); ASSERT_STREQ(root->tr_text, "123123"); widget_t* button = widget_lookup(root, "b3", TRUE); ASSERT_EQ(button != NULL, TRUE); widget_destroy(root); }