awtk/tests/image_manager_test.cc

153 lines
5.0 KiB
C++
Raw Normal View History

2018-06-27 13:46:42 +08:00
#include <stdlib.h>
2018-03-18 11:29:31 +08:00
#include "gtest/gtest.h"
#include "base/image_manager.h"
#include "image_loader/image_loader_stb.h"
#include <string>
using std::string;
2018-02-21 19:36:38 +08:00
TEST(ImageManager, basic) {
2018-03-18 11:29:31 +08:00
bitmap_t bmp;
2018-08-08 12:21:52 +08:00
memset(&bmp, 0x00, sizeof(bmp));
2018-12-29 12:17:06 +08:00
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "checked", &bmp), RET_OK);
2018-04-28 15:09:07 +08:00
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_OK);
2018-12-29 12:17:06 +08:00
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "not found", &bmp), RET_NOT_FOUND);
2018-04-28 15:09:07 +08:00
ASSERT_EQ(image_manager_lookup(image_manager(), "unchecked", &bmp), RET_NOT_FOUND);
2018-04-29 16:51:54 +08:00
2018-04-28 15:09:07 +08:00
ASSERT_EQ(image_manager_unload_unused(image_manager(), 0), RET_OK);
}
2020-10-19 17:06:42 +08:00
TEST(ImageManager, allloc) {
bitmap_t* bmp = bitmap_create();
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "checked", bmp), RET_OK);
2020-12-01 10:30:44 +08:00
bitmap_destroy_with_self(bmp);
2020-10-19 17:06:42 +08:00
bmp = bitmap_create();
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", bmp), RET_OK);
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "not found", bmp), RET_NOT_FOUND);
2020-12-01 10:30:44 +08:00
bitmap_destroy_with_self(bmp);
2020-10-19 17:06:42 +08:00
bmp = bitmap_create();
ASSERT_EQ(image_manager_lookup(image_manager(), "unchecked", bmp), RET_NOT_FOUND);
2020-12-01 10:30:44 +08:00
bitmap_destroy_with_self(bmp);
2020-10-19 17:06:42 +08:00
ASSERT_EQ(image_manager_unload_unused(image_manager(), 0), RET_OK);
}
2018-04-28 15:09:07 +08:00
TEST(ImageManager, unload) {
bitmap_t bmp;
2018-08-08 12:21:52 +08:00
memset(&bmp, 0x00, sizeof(bmp));
2018-04-28 15:09:07 +08:00
2019-02-18 11:29:51 +08:00
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "checked", &bmp), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_OK);
ASSERT_EQ(image_manager_unload_bitmap(image_manager(), &bmp), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_NOT_FOUND);
}
TEST(ImageManager, unload_unused) {
bitmap_t bmp;
memset(&bmp, 0x00, sizeof(bmp));
2018-12-29 12:17:06 +08:00
ASSERT_EQ(image_manager_get_bitmap(image_manager(), "checked", &bmp), RET_OK);
2018-04-28 15:09:07 +08:00
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_OK);
ASSERT_EQ(image_manager_unload_unused(image_manager(), 0), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_NOT_FOUND);
2018-04-29 16:51:54 +08:00
2018-04-28 15:09:07 +08:00
ASSERT_EQ(image_manager_unload_unused(image_manager(), 0), RET_OK);
}
TEST(ImageManager, add) {
bitmap_t bmp;
2018-08-08 12:21:52 +08:00
memset(&bmp, 0x00, sizeof(bmp));
2018-04-28 15:09:07 +08:00
bmp.w = 10;
bmp.h = 10;
ASSERT_EQ(image_manager_add(image_manager(), "checked", &bmp), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), "checked", &bmp), RET_OK);
ASSERT_EQ(image_manager_unload_unused(image_manager(), 0), RET_OK);
2018-02-21 19:36:38 +08:00
}
TEST(ImageManager, locale) {
bitmap_t bmp;
memset(&bmp, 0x00, sizeof(bmp));
image_manager_t* imm = image_manager_create();
assets_manager_t* am = assets_manager_create(0);
assets_manager_set_res_root(am, "tests/testdata");
image_manager_set_assets_manager(imm, am);
ASSERT_EQ(image_manager_get_bitmap(imm, "locale_$locale$", &bmp), RET_OK);
ASSERT_EQ(string(bmp.name), string("locale_en"));
ASSERT_EQ(image_manager_get_bitmap(imm, "locale1_$locale$", &bmp), RET_OK);
ASSERT_EQ(string(bmp.name), string("locale1_en_US"));
ASSERT_EQ(image_manager_get_bitmap(imm, "locale_$locale$", &bmp), RET_OK);
ASSERT_EQ(string(bmp.name), string("locale_en"));
ASSERT_EQ(image_manager_get_bitmap(imm, "locale1_$locale$", &bmp), RET_OK);
ASSERT_EQ(string(bmp.name), string("locale1_en_US"));
ASSERT_EQ(image_manager_unload_unused(imm, 0), RET_OK);
assets_manager_destroy(am);
image_manager_destroy(imm);
}
2019-06-13 15:15:12 +08:00
#ifdef WITH_FS_RES
TEST(ImageManager, fs) {
bitmap_t bmp;
2020-08-18 17:56:36 +08:00
const char* filename = "file://./res/assets/default/raw/images/xx/flag_CN.png";
2019-06-13 15:15:12 +08:00
memset(&bmp, 0x00, sizeof(bmp));
ASSERT_EQ(image_manager_get_bitmap(image_manager(), filename, &bmp), RET_OK);
ASSERT_EQ(image_manager_lookup(image_manager(), filename, &bmp), RET_OK);
ASSERT_EQ(image_manager_unload_bitmap(image_manager(), &bmp), RET_OK);
}
#endif /*WITH_FS_RES*/
TEST(ImageManager, limit) {
bitmap_t b1;
bitmap_t b2;
bitmap_t b3;
image_manager_t* imm = image_manager();
memset(&b1, 0x00, sizeof(b1));
memset(&b2, 0x00, sizeof(b1));
memset(&b3, 0x00, sizeof(b1));
2021-09-10 16:36:20 +08:00
bitmap_init(&b1, 10, 20, BITMAP_FMT_RGBA8888, NULL);
bitmap_init(&b2, 10, 30, BITMAP_FMT_RGBA8888, NULL);
bitmap_init(&b3, 10, 40, BITMAP_FMT_RGBA8888, NULL);
image_manager_unload_all(imm);
ASSERT_EQ(image_manager_add(imm, "b1", &b1), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(imm->images.size, 1u);
2021-09-10 16:36:20 +08:00
ASSERT_EQ(image_manager_add(imm, "b2", &b2), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(imm->images.size, 2u);
image_manager_set_max_mem_size_of_cached_images(imm, bitmap_get_mem_size(&b1));
2021-09-10 16:36:20 +08:00
ASSERT_EQ(image_manager_add(imm, "b3", &b3), RET_OK);
2021-09-16 16:06:55 +08:00
ASSERT_EQ(imm->images.size, 1u);
}
2022-07-05 07:57:16 +08:00
TEST(ImageManager, images_managers1) {
bitmap_t bmp;
assets_managers_set_applet_res_root("./tests/applets");
image_manager_t* imm = image_managers_ref("foo");
ASSERT_EQ(image_manager_get_bitmap(imm, "foo", &bmp), RET_OK);
image_managers_unref(imm);
}
TEST(ImageManager, images_managers2) {
bitmap_t bmp;
assets_managers_set_applet_res_root("./tests/applets");
image_manager_t* imm = image_managers_ref("bar");
ASSERT_EQ(image_manager_get_bitmap(imm, "bar", &bmp), RET_OK);
image_managers_unref(imm);
}