awtk/tests/assets_manager_test.cc

153 lines
4.7 KiB
C++
Raw Normal View History

2018-08-24 07:45:37 +08:00
#include "base/assets_manager.h"
2018-03-09 21:54:46 +08:00
#include "gtest/gtest.h"
2019-02-07 17:27:19 +08:00
TEST(AssetsManager, basic) {
2018-08-24 07:45:37 +08:00
const asset_info_t* null_res = NULL;
assets_manager_t* rm = assets_manager_create(10);
2018-03-09 21:54:46 +08:00
2018-08-24 07:45:37 +08:00
asset_info_t img1 = {ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_BMP, TRUE, 100, 0, "img1"};
asset_info_t img2 = {ASSET_TYPE_IMAGE, ASSET_TYPE_IMAGE_PNG, TRUE, 101, 0, "img2"};
asset_info_t ui1 = {ASSET_TYPE_UI, ASSET_TYPE_UI_BIN, TRUE, 102, 0, "ui1"};
asset_info_t ui2 = {ASSET_TYPE_UI, ASSET_TYPE_UI_XML, TRUE, 103, 0, "ui2"};
2018-03-09 21:54:46 +08:00
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_add(rm, &img1), RET_OK);
ASSERT_EQ(assets_manager_add(rm, &img2), RET_OK);
ASSERT_EQ(assets_manager_add(rm, &ui1), RET_OK);
ASSERT_EQ(assets_manager_add(rm, &ui2), RET_OK);
2018-03-09 21:54:46 +08:00
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_ref(rm, ASSET_TYPE_IMAGE, "img1"), &img1);
ASSERT_EQ(assets_manager_ref(rm, ASSET_TYPE_IMAGE, "img2"), &img2);
ASSERT_EQ(assets_manager_ref(rm, ASSET_TYPE_IMAGE, "img3"), null_res);
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_ref(rm, ASSET_TYPE_UI, "ui1"), &ui1);
ASSERT_EQ(assets_manager_ref(rm, ASSET_TYPE_UI, "ui2"), &ui2);
2018-08-24 07:45:37 +08:00
assets_manager_destroy(rm);
2018-03-09 21:54:46 +08:00
}
2018-04-30 08:13:36 +08:00
2019-02-07 17:27:19 +08:00
TEST(AssetsManager, file_image) {
2018-08-24 07:45:37 +08:00
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager_create(10);
2018-04-30 08:13:36 +08:00
2018-08-24 07:45:37 +08:00
r = assets_manager_ref(rm, ASSET_TYPE_IMAGE, "earth");
2018-04-30 08:13:36 +08:00
ASSERT_EQ(r != NULL, true);
2019-04-02 18:12:00 +08:00
ASSERT_EQ(r->refcount, 1);
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_unref(rm, r), RET_OK);
2018-04-30 08:13:36 +08:00
2018-08-24 07:45:37 +08:00
assets_manager_destroy(rm);
2018-04-30 08:13:36 +08:00
}
2018-05-04 14:52:15 +08:00
2019-02-07 17:27:19 +08:00
TEST(AssetsManager, file_script) {
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager_create(10);
r = assets_manager_ref(rm, ASSET_TYPE_SCRIPT, "dummy");
ASSERT_EQ(r != NULL, true);
2019-04-02 18:12:00 +08:00
ASSERT_EQ(r->refcount, 2);
r = assets_manager_find_in_cache(rm, ASSET_TYPE_SCRIPT, "dummy");
ASSERT_EQ(r != NULL, true);
2019-02-07 17:27:19 +08:00
ASSERT_EQ(assets_manager_unref(rm, r), RET_OK);
2019-04-02 18:12:00 +08:00
r = assets_manager_find_in_cache(rm, ASSET_TYPE_SCRIPT, "dummy");
ASSERT_EQ(r->refcount, 1);
2019-02-07 17:27:19 +08:00
assets_manager_destroy(rm);
}
TEST(AssetsManager, clearCache) {
2018-08-24 07:45:37 +08:00
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager_create(10);
2018-05-04 14:52:15 +08:00
2018-08-24 07:45:37 +08:00
r = assets_manager_find_in_cache(rm, ASSET_TYPE_IMAGE, "earth");
2018-05-04 14:52:15 +08:00
ASSERT_EQ(r == NULL, true);
2018-08-24 07:45:37 +08:00
r = assets_manager_ref(rm, ASSET_TYPE_IMAGE, "earth");
2018-05-04 14:52:15 +08:00
ASSERT_EQ(r != NULL, true);
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_unref(rm, r), RET_OK);
r = assets_manager_find_in_cache(rm, ASSET_TYPE_IMAGE, "earth");
2018-05-04 14:52:15 +08:00
ASSERT_EQ(r == NULL, true);
2018-08-24 07:45:37 +08:00
r = assets_manager_ref(rm, ASSET_TYPE_IMAGE, "earth");
2018-05-04 14:52:15 +08:00
ASSERT_EQ(r != NULL, true);
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_unref(rm, r), RET_OK);
2018-05-04 14:52:15 +08:00
2018-08-24 07:45:37 +08:00
ASSERT_EQ(assets_manager_clear_cache(rm, ASSET_TYPE_IMAGE), RET_OK);
2018-05-04 14:52:15 +08:00
2018-08-24 07:45:37 +08:00
r = assets_manager_find_in_cache(rm, ASSET_TYPE_IMAGE, "earth");
2018-05-04 14:52:15 +08:00
ASSERT_EQ(r == NULL, true);
2018-08-24 07:45:37 +08:00
assets_manager_destroy(rm);
2018-05-04 14:52:15 +08:00
}
2019-03-15 17:44:48 +08:00
TEST(AssetsManager, xml) {
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager();
#ifdef WITH_FS_RES
r = assets_manager_ref(rm, ASSET_TYPE_XML, "test");
ASSERT_EQ(r != NULL, true);
assets_manager_unref(rm, r);
#else
r = assets_manager_find_in_cache(rm, ASSET_TYPE_XML, "test");
ASSERT_EQ(r != NULL, true);
#endif /*WITH_FS_RES*/
}
2019-03-15 17:44:48 +08:00
TEST(AssetsManager, data) {
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager();
#ifdef WITH_FS_RES
r = assets_manager_ref(rm, ASSET_TYPE_DATA, "test.dat");
2019-03-15 17:44:48 +08:00
ASSERT_EQ(r != NULL, true);
assets_manager_unref(rm, r);
#else
r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "test.dat");
2019-03-15 17:44:48 +08:00
ASSERT_EQ(r != NULL, true);
2019-03-18 17:45:49 +08:00
#endif /*WITH_FS_RES*/
2019-03-15 17:44:48 +08:00
}
TEST(AssetsManager, json) {
2019-03-15 17:44:48 +08:00
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager();
#ifdef WITH_FS_RES
2019-03-22 12:02:10 +08:00
r = assets_manager_ref(rm, ASSET_TYPE_DATA, "com.zlg.app.json");
2019-03-15 17:44:48 +08:00
ASSERT_EQ(r != NULL, true);
2019-03-22 12:02:10 +08:00
ASSERT_EQ(strncmp((const char*)(r->data), "{}\n", 3), 0);
2019-03-15 17:44:48 +08:00
assets_manager_unref(rm, r);
#else
2019-03-22 12:02:10 +08:00
r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "com.zlg.app.json");
ASSERT_EQ(r != NULL, true);
2019-03-22 12:02:10 +08:00
ASSERT_EQ(strncmp((const char*)(r->data), "{}\n", 3), 0);
#endif /*WITH_FS_RES*/
}
2019-03-21 17:44:11 +08:00
TEST(AssetsManager, json_dupname) {
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager();
#ifdef WITH_FS_RES
r = assets_manager_ref(rm, ASSET_TYPE_DATA, "test.json");
ASSERT_EQ(r != NULL, true);
assets_manager_unref(rm, r);
#else
r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "test.json");
ASSERT_EQ(r != NULL, true);
#endif /*WITH_FS_RES*/
}
TEST(AssetsManager, any) {
const asset_info_t* r = NULL;
assets_manager_t* rm = assets_manager();
#ifdef WITH_FS_RES
2019-03-22 12:02:10 +08:00
r = assets_manager_ref(rm, ASSET_TYPE_DATA, "a-b-c.any");
ASSERT_EQ(r != NULL, true);
2019-03-22 12:02:10 +08:00
ASSERT_EQ(strncmp((const char*)(r->data), "abc\n", 4), 0);
assets_manager_unref(rm, r);
#else
2019-03-22 12:02:10 +08:00
r = assets_manager_find_in_cache(rm, ASSET_TYPE_DATA, "a-b-c.any");
2019-03-15 17:44:48 +08:00
ASSERT_EQ(r != NULL, true);
2019-03-22 12:02:10 +08:00
ASSERT_EQ(strncmp((const char*)(r->data), "abc\n", 4), 0);
2019-03-18 17:45:49 +08:00
#endif /*WITH_FS_RES*/
2019-03-15 17:44:48 +08:00
}