diff --git a/frameworks/resmgr/include/hap_manager.h b/frameworks/resmgr/include/hap_manager.h index fa934fc1e42ce00f9ca2dfd302ed52b68d461ab9..bc7d6b6d7363cb0851cf80883349310721b0f765 100644 --- a/frameworks/resmgr/include/hap_manager.h +++ b/frameworks/resmgr/include/hap_manager.h @@ -85,17 +85,20 @@ public: /** * Find best resource path by resource id * @param id the resource id + * @param density the input screen density * @return the best resource path */ - const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id); + const HapResource::ValueUnderQualifierDir *FindQualifierValueById(uint32_t id, uint32_t density = 0); /** * Find best resource path by resource name * @param name the resource name * @param resType the resource type + * @param density the input screen density * @return the best resource path */ - const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType); + const HapResource::ValueUnderQualifierDir *FindQualifierValueByName(const char *name, const ResType resType, + uint32_t density = 0); /** * Find the raw file path diff --git a/frameworks/resmgr/include/res_config_impl.h b/frameworks/resmgr/include/res_config_impl.h index 35735b185ff1783f76ebbbdd8c4e29140c60d788..a39ff8ee53c3894e693a7d43f4a32160cc458c50 100644 --- a/frameworks/resmgr/include/res_config_impl.h +++ b/frameworks/resmgr/include/res_config_impl.h @@ -37,9 +37,10 @@ public: * Whether this resConfig more match request resConfig * @param other the other resConfig * @param request the request resConfig + * @param density the input screen density * @return true if this resConfig more match request resConfig than other resConfig, else false */ - bool IsMoreSuitable(const ResConfigImpl *other, const ResConfigImpl *request) const; + bool IsMoreSuitable(const ResConfigImpl *other, const ResConfigImpl *request, uint32_t density = 0) const; /** * Set locale information @@ -135,7 +136,7 @@ public: virtual ~ResConfigImpl(); private: - bool IsMoreSpecificThan(const ResConfigImpl *other) const; + bool IsMoreSpecificThan(const ResConfigImpl *other, uint32_t density = 0) const; bool CopyLocale(ResConfig &other); @@ -149,10 +150,12 @@ private: int IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, uint32_t requestMcc, uint32_t requestMnc) const; - int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity) const; + int IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, uint32_t density = 0) const; bool IsDensityMoreSuitable(int thisDistance, int otherDistance) const; + int IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density = 0) const; + private: ResLocale *resLocale_; Direction direction_; diff --git a/frameworks/resmgr/include/resource_manager_impl.h b/frameworks/resmgr/include/resource_manager_impl.h index dd245db5c519eeae75dfd6ef7c5084677bf02edc..900b8f65909fade622a5557a9acdfc7ceb0d4d04 100644 --- a/frameworks/resmgr/include/resource_manager_impl.h +++ b/frameworks/resmgr/include/resource_manager_impl.h @@ -299,13 +299,31 @@ public: virtual RState GetMediaById(uint32_t id, std::string &outValue); /** - * Get the PROF resource by resource name + * Get the MEDIA resource by resource id with density + * @param id the resource id + * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity + * @param outValue the obtain resource path write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ + virtual RState GetMediaById(uint32_t id, uint32_t density, std::string &outValue); + + /** + * Get the MEDIA resource by resource name * @param name the resource name * @param outValue the obtain resource path write to * @return SUCCESS if resource exist, else NOT_FOUND */ virtual RState GetMediaByName(const char *name, std::string &outValue); + /** + * Get the MEDIA resource by resource name with density + * @param name the resource name + * @param density the screen density, within the area of OHOS::Global::Resource::ScreenDensity + * @param outValue the obtain resource path write to + * @return SUCCESS if resource exist, else NOT_FOUND + */ + virtual RState GetMediaByName(const char *name, uint32_t density, std::string &outValue); + /** * Get the raw file path by resource name * @param name the resource name @@ -366,6 +384,8 @@ private: RState ResolveParentReference(const IdItem *idItem, std::map &outValue); + bool IsDensityValid(uint32_t density); + HapManager *hapManager_; float fontRatio_ = 0.0f; diff --git a/frameworks/resmgr/src/hap_manager.cpp b/frameworks/resmgr/src/hap_manager.cpp index 1bad0ba11d8d08748543e13c55a7402ae78bd24a..85b55d4deb0338031a3966339e6d569964f71c1e 100644 --- a/frameworks/resmgr/src/hap_manager.cpp +++ b/frameworks/resmgr/src/hap_manager.cpp @@ -148,7 +148,7 @@ const IdItem *HapManager::FindResourceByName(const char *name, const ResType res } const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueByName( - const char *name, const ResType resType) + const char *name, const ResType resType, uint32_t density) { std::vector candidates = this->GetResourceListByName(name, resType); if (candidates.size() == 0) { @@ -172,7 +172,7 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueByName( result = paths[i]; continue; } - if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { + if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig, density)) { bestResConfig = resConfig; result = paths[i]; } @@ -181,7 +181,7 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueByName( return result; } -const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueById(uint32_t id) +const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueById(uint32_t id, uint32_t density) { std::vector candidates = this->GetResourceList(id); if (candidates.size() == 0) { @@ -209,7 +209,7 @@ const HapResource::ValueUnderQualifierDir *HapManager::FindQualifierValueById(ui result = paths[i]; continue; } - if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig)) { + if (!bestResConfig->IsMoreSuitable(resConfig, currentResConfig, density)) { bestResConfig = resConfig; result = paths[i]; } diff --git a/frameworks/resmgr/src/res_config_impl.cpp b/frameworks/resmgr/src/res_config_impl.cpp index 7aceb3e70a07e6cf67b3d04f0f726f57f40cc558..6a5c117243544ba14e06054720f47304c8cd423e 100644 --- a/frameworks/resmgr/src/res_config_impl.cpp +++ b/frameworks/resmgr/src/res_config_impl.cpp @@ -18,6 +18,7 @@ #include #endif #include "locale_matcher.h" +#include "res_common.h" #include "res_locale.h" #include "utils/utils.h" #ifdef SUPPORT_GRAPHICS @@ -321,7 +322,7 @@ bool ResConfigImpl::IsColorModeMatch(ColorMode colorMode) const * */ bool ResConfigImpl::IsMoreSuitable(const ResConfigImpl *other, - const ResConfigImpl *request) const + const ResConfigImpl *request, uint32_t density) const { if (request != nullptr && other != nullptr) { int ret = IsMccMncMoreSuitable(other->mcc_, other->mnc_, request->mcc_, request->mnc_); @@ -350,12 +351,12 @@ bool ResConfigImpl::IsMoreSuitable(const ResConfigImpl *other, request->colorMode_ != ColorMode::COLOR_MODE_NOT_SET) { return this->colorMode_ != ColorMode::COLOR_MODE_NOT_SET; } - ret = IsDensityMoreSuitable(other->screenDensity_, request->screenDensity_); + ret = IsDensityMoreSuitable(other->screenDensity_, request->screenDensity_, density); if (ret != 0) { return ret > 0; } } - return this->IsMoreSpecificThan(other); + return this->IsMoreSpecificThan(other, density); } /** @@ -400,22 +401,38 @@ int ResConfigImpl::IsMccMncMoreSuitable(uint32_t otherMcc, uint32_t otherMnc, ui * return 0 * */ -int ResConfigImpl::IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity) const +int ResConfigImpl::IsDensityMoreSuitable(ScreenDensity otherDensity, ScreenDensity requestDensity, + uint32_t density) const { int ret = 0; - if (requestDensity != ScreenDensity::SCREEN_DENSITY_NOT_SET && - this->screenDensity_ != otherDensity) { - int thisDistance = this->screenDensity_ - requestDensity; - int otherDistance = otherDensity - requestDensity; - if (IsDensityMoreSuitable(thisDistance, otherDistance)) { - // the density of this resConfig is suitable than other resConfig - ret = 1; - } else { - // the density of other resConfig mcc/mnc is suitable than this resConfig - ret = -1; + int thisDistance; + int otherDistance; + if (density == ScreenDensity::SCREEN_DENSITY_NOT_SET) { + if (requestDensity != ScreenDensity::SCREEN_DENSITY_NOT_SET && + this->screenDensity_ != otherDensity) { + thisDistance = this->screenDensity_ - requestDensity; + otherDistance = otherDensity - requestDensity; + if (IsDensityMoreSuitable(thisDistance, otherDistance)) { + // the density of this resConfig is suitable than other resConfig + ret = 1; + } else { + // the density of other resConfig is suitable than this resConfig + ret = -1; + } + } + } else { + if (this->screenDensity_ != otherDensity) { + thisDistance = this->screenDensity_ - density; + otherDistance = otherDensity - density; + if (IsDensityMoreSuitable(thisDistance, otherDistance)) { + // the density of this resConfig is suitable than other resConfig + ret = 1; + } else { + // the density of other resConfig is suitable than this resConfig + ret = -1; + } } } - return ret; } @@ -456,7 +473,7 @@ bool ResConfigImpl::IsCompletedScript() const return isCompletedScript_; } -bool ResConfigImpl::IsMoreSpecificThan(const ResConfigImpl *other) const +bool ResConfigImpl::IsMoreSpecificThan(const ResConfigImpl *other, uint32_t density) const { if (other == nullptr) { return true; @@ -488,12 +505,53 @@ bool ResConfigImpl::IsMoreSpecificThan(const ResConfigImpl *other) const if (this->colorMode_ != other->colorMode_) { return (this->colorMode_ != ColorMode::COLOR_MODE_NOT_SET); } - if (this->screenDensity_ != other->screenDensity_) { - return (this->screenDensity_ != ScreenDensity::SCREEN_DENSITY_NOT_SET); + int ret = IsDensityMoreSpecificThan(other->screenDensity_, density); + if (ret != 0) { + return ret > 0; } + return true; } +int ResConfigImpl::IsDensityMoreSpecificThan(ScreenDensity otherDensity, uint32_t density) const +{ + int ret = 0; + if (density == SCREEN_DENSITY_NOT_SET) { + if (this->screenDensity_ != otherDensity) { + if (this->screenDensity_ != ScreenDensity::SCREEN_DENSITY_NOT_SET) { + // the density of this resConfig is suitable than other resConfig + ret = 1; + } else { + // the density of other resConfig is suitable than this resConfig + ret = -1; + } + } + } else { + if ((this->screenDensity_ != ScreenDensity::SCREEN_DENSITY_NOT_SET) && + (otherDensity == ScreenDensity::SCREEN_DENSITY_NOT_SET)) { + // the density of this resConfig is suitable than other resConfig + ret = 1; + } + if ((this->screenDensity_ == ScreenDensity::SCREEN_DENSITY_NOT_SET) && + (otherDensity != ScreenDensity::SCREEN_DENSITY_NOT_SET)) { + // the density of other resConfig is suitable than this resConfig + ret = -1; + } + if (this->screenDensity_ != otherDensity) { + int thisDistance = this->screenDensity_ - density; + int otherDistance = otherDensity - density; + if (IsDensityMoreSuitable(thisDistance, otherDistance)) { + // the density of this resConfig is suitable than other resConfig + ret = 1; + } else { + // the density of other resConfig is suitable than this resConfig + ret = -1; + } + } + } + return ret; +} + ResConfig *CreateResConfig() { ResConfigImpl *temp = new(std::nothrow) ResConfigImpl; diff --git a/frameworks/resmgr/src/resource_manager_impl.cpp b/frameworks/resmgr/src/resource_manager_impl.cpp index 5b136d9bec5077d08a0cf7a8c181b4639d1ad227..5cb4202065d2417f5a7c20470d20891b11525423 100644 --- a/frameworks/resmgr/src/resource_manager_impl.cpp +++ b/frameworks/resmgr/src/resource_manager_impl.cpp @@ -631,6 +631,20 @@ RState ResourceManagerImpl::GetMediaById(uint32_t id, std::string &outValue) return GetRawFile(qd, ResType::MEDIA, outValue); } +RState ResourceManagerImpl::GetMediaById(uint32_t id, uint32_t density, std::string &outValue) +{ + if (!IsDensityValid(density)) { + HILOG_ERROR("density invalid"); + return NOT_SUPPORT_SEP; + } + auto qualifierDir = hapManager_->FindQualifierValueById(id, density); + if (qualifierDir == nullptr) { + HILOG_ERROR("find qualifier value by media id error"); + return NOT_FOUND; + } + return GetRawFile(qualifierDir, ResType::MEDIA, outValue); +} + RState ResourceManagerImpl::GetMediaByName(const char *name, std::string &outValue) { auto qd = hapManager_->FindQualifierValueByName(name, ResType::MEDIA); @@ -640,6 +654,20 @@ RState ResourceManagerImpl::GetMediaByName(const char *name, std::string &outVal return GetRawFile(qd, ResType::MEDIA, outValue); } +RState ResourceManagerImpl::GetMediaByName(const char *name, uint32_t density, std::string &outValue) +{ + if (!IsDensityValid(density)) { + HILOG_ERROR("density invalid"); + return NOT_SUPPORT_SEP; + } + auto qualifierDir = hapManager_->FindQualifierValueByName(name, ResType::MEDIA, density); + if (qualifierDir == nullptr) { + HILOG_ERROR("find qualifier value by media name error"); + return NOT_FOUND; + } + return GetRawFile(qualifierDir, ResType::MEDIA, outValue); +} + RState ResourceManagerImpl::GetRawFile(const HapResource::ValueUnderQualifierDir *vuqd, const ResType resType, std::string &outValue) { @@ -769,6 +797,22 @@ std::vector ResourceManagerImpl::GetResourcePaths() { return this->hapManager_->GetResourcePaths(); } + +bool ResourceManagerImpl::IsDensityValid(uint32_t density) +{ + switch (density) { + case SCREEN_DENSITY_NOT_SET: + case SCREEN_DENSITY_SDPI: + case SCREEN_DENSITY_MDPI: + case SCREEN_DENSITY_LDPI: + case SCREEN_DENSITY_XLDPI: + case SCREEN_DENSITY_XXLDPI: + case SCREEN_DENSITY_XXXLDPI: + return true; + default: + return false; + } +} } // namespace Resource } // namespace Global } // namespace OHOS diff --git a/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp b/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp index 0f67d4f4f7f4fd2a46d964e3561eb97a57cb21b4..956dec35fbed23cfb5d4b40e73ef9e7dc55a3cd7 100644 --- a/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp +++ b/frameworks/resmgr/test/unittest/common/resource_manager_test.cpp @@ -2390,7 +2390,7 @@ HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest001, TestSize.Level HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); tmp->Init(); std::string res = tmp->GetResourcePath(); - res.append("entry/resources/base/media/icon.png"); + res.append("entry/resources/zh_CN-sdpi/media/icon.png"); std::string outValue; RState state; @@ -2417,41 +2417,1430 @@ HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest002, TestSize.Level ASSERT_EQ(NOT_FOUND, state); } +/* + * @tc.name: ResourceManagerGetMediaByIdTest003 + * @tc.desc: Test GetMediaById, to match sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest003, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TV); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 120; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest004 + * @tc.desc: Test GetMediaById, to match mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest004, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TV); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 160; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest005 + * @tc.desc: Test GetMediaById, to match ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest005, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 240; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest006 + * @tc.desc: Test GetMediaById, to match xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest006, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 320; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest007 + * @tc.desc: Test GetMediaById, to match xxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest007, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 480; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest008 + * @tc.desc: Test GetMediaById, to match xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest008, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 640; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest009 + * @tc.desc: Test GetMediaById, to match unsupport density + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest009, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + uint32_t density1 = 420; + uint32_t density2 = 800; + uint32_t density3 = 10; + std::string outValue; + RState state1; + RState state2; + RState state3; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state1 = rm->GetMediaById(id, density1, outValue); + state2 = rm->GetMediaById(id, density2, outValue); + state3 = rm->GetMediaById(id, density3, outValue); + EXPECT_TRUE(state1 == NOT_SUPPORT_SEP); + EXPECT_TRUE(state2 == NOT_SUPPORT_SEP); + EXPECT_TRUE(state3 == NOT_SUPPORT_SEP); +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest010 + * @tc.desc: Test GetMediaById, to match with no density param + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest010, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + uint32_t density = 0; + std::string outValue1; + std::string outValue2; + RState state1; + RState state2; + int id = GetResId("icon", ResType::MEDIA); + state1 = rm->GetMediaById(id, density, outValue1); + state2 = rm->GetMediaById(id, outValue2); + EXPECT_TRUE(state1 == SUCCESS); + EXPECT_TRUE(state2 == SUCCESS); + EXPECT_EQ(outValue1, outValue2); +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest011 + * @tc.desc: Test GetMediaById, to match zh_CN-sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest011, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 120; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest012 + * @tc.desc: Test GetMediaById, to match zh_CN-mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest012, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 160; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest013 + * @tc.desc: Test GetMediaById, to match zh_CN-ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest013, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 240; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest014 + * @tc.desc: Test GetMediaById, to match zh_CN-xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest014, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 320; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest015 + * @tc.desc: Test GetMediaById, to match zh_CN-xxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest015, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 480; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest016 + * @tc.desc: Test GetMediaById, to match zh_CN-xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest016, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 640; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest017 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest017, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 120; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest018 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest018, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 160; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest019 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest019, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 240; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest020 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest020, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 320; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest021 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-xxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest021, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 480; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByIdTest022 + * @tc.desc: Test GetMediaById, to match mcc460_mnc101-zh_CN-phone-dark-xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest022, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 640; + std::string outValue; + RState state; + int id = GetResId("icon", ResType::MEDIA); + EXPECT_TRUE(id > 0); + state = rm->GetMediaById(id, density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + /* * @tc.name: ResourceManagerGetMediaByNameTest001 * @tc.desc: Test GetMediaByName * @tc.type: FUNC */ -HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest001, TestSize.Level1) +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest001, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-sdpi/media/icon.png"); + + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest002 + * @tc.desc: Test GetMediaByName + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest002, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + std::string outValue; + RState state; + state = rm->GetMediaByName(g_nonExistName, outValue); + ASSERT_EQ(NOT_FOUND, state); +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest003 + * @tc.desc: Test GetMediaByName, to match sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest003, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 120; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest004 + * @tc.desc: Test GetMediaByName, to match mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest004, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 160; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest005 + * @tc.desc: Test GetMediaByName, to match ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest005, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 240; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest006 + * @tc.desc: Test GetMediaByName, to match xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest006, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 320; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest007 + * @tc.desc: Test GetMediaByName, to match xxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest007, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 480; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest008 + * @tc.desc: Test GetMediaByName, to match xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest008, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + uint32_t density = 640; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest009 + * @tc.desc: Test GetMediaByName, to match unsupport density + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest009, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + uint32_t density1 = 420; + uint32_t density2 = 800; + uint32_t density3 = 10; + std::string outValue; + RState state1; + RState state2; + RState state3; + state1 = rm->GetMediaByName("icon", density1, outValue); + state2 = rm->GetMediaByName("icon", density2, outValue); + state3 = rm->GetMediaByName("icon", density3, outValue); + EXPECT_TRUE(state1 == NOT_SUPPORT_SEP); + EXPECT_TRUE(state2 == NOT_SUPPORT_SEP); + EXPECT_TRUE(state3 == NOT_SUPPORT_SEP); +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest010 + * @tc.desc: Test GetMediaByName, to match with no density param + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest010, TestSize.Level1) +{ + AddResource("en", nullptr, "US"); + + uint32_t density = 0; + std::string outValue1; + std::string outValue2; + RState state1; + RState state2; + state1 = rm->GetMediaByName("icon", density, outValue1); + state2 = rm->GetMediaByName("icon", outValue2); + EXPECT_TRUE(state1 == SUCCESS); + EXPECT_TRUE(state2 == SUCCESS); + EXPECT_EQ(outValue1, outValue2); +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest011 + * @tc.desc: Test GetMediaByName, to match zh_CN-sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest011, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 120; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest012 + * @tc.desc: Test GetMediaByName, to match zh_CN-mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest012, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 160; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest013 + * @tc.desc: Test GetMediaByName, to match zh_CN-ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest013, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 240; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest014 + * @tc.desc: Test GetMediaByName, to match zh_CN-xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest014, TestSize.Level1) { AddResource("zh", nullptr, "CN"); HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); tmp->Init(); std::string res = tmp->GetResourcePath(); - res.append("entry/resources/base/media/icon.png"); + res.append("entry/resources/zh_CN-xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + int density = 320; std::string outValue; RState state; - state = rm->GetMediaByName("icon", outValue); + state = rm->GetMediaByName("icon", density, outValue); EXPECT_TRUE(state == SUCCESS); EXPECT_EQ(res, outValue); delete tmp; } /* - * @tc.name: ResourceManagerGetMediaByNameTest002 - * @tc.desc: Test GetMediaByName + * @tc.name: ResourceManagerGetMediaByNameTest015 + * @tc.desc: Test GetMediaByName, to match zh_CN-xxldpi determinder * @tc.type: FUNC */ -HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest002, TestSize.Level1) +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest015, TestSize.Level1) { AddResource("zh", nullptr, "CN"); + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 480; std::string outValue; RState state; - state = rm->GetMediaByName(g_nonExistName, outValue); - ASSERT_EQ(NOT_FOUND, state); + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest016 + * @tc.desc: Test GetMediaByName, to match zh_CN-xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest016, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/zh_CN-xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetDeviceType(DEVICE_TABLET); + rc->SetColorMode(COLOR_MODE_NOT_SET); + rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 640; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest017 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-sdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest017, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-sdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 120; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest018 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-mdpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest018, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-mdpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 160; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest019 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-ldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest019, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-ldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 240; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest020 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-xldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest020, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 320; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest021 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-xxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest021, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 480; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; +} + +/* + * @tc.name: ResourceManagerGetMediaByNameTest022 + * @tc.desc: Test GetMediaByName, to match mcc460_mnc101-zh_CN-phone-dark-xxxldpi determinder + * @tc.type: FUNC + */ +HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest022, TestSize.Level1) +{ + AddResource("zh", nullptr, "CN"); + + HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr); + tmp->Init(); + std::string res = tmp->GetResourcePath(); + res.append("entry/resources/mcc460_mnc101-zh_CN-phone-dark-xxxldpi/media/icon.png"); + + auto rc = CreateResConfig(); + if (rc == nullptr) { + EXPECT_TRUE(false); + return; + } + rc->SetMcc(460); + rc->SetMnc(101); + rc->SetLocaleInfo("zh", nullptr, "CN"); + rc->SetDeviceType(DEVICE_PHONE); + rc->SetColorMode(DARK); + rc->SetScreenDensity(SCREEN_DENSITY_XXXLDPI); + rm->UpdateResConfig(*rc); + delete rc; + + int density = 640; + std::string outValue; + RState state; + state = rm->GetMediaByName("icon", density, outValue); + EXPECT_TRUE(state == SUCCESS); + EXPECT_EQ(res, outValue); + delete tmp; } /* diff --git a/frameworks/resmgr/test/unittest/common/resource_manager_test.h b/frameworks/resmgr/test/unittest/common/resource_manager_test.h index cccfc9d7e262667b055118733c44b66410a861a4..fa0d5f830df1282bfb5d3c5bb7fff2b185ed15a6 100644 --- a/frameworks/resmgr/test/unittest/common/resource_manager_test.h +++ b/frameworks/resmgr/test/unittest/common/resource_manager_test.h @@ -126,8 +126,48 @@ int ResourceManagerGetProfileByNameTest001(void); int ResourceManagerGetProfileByNameTest002(void); int ResourceManagerGetMediaByIdTest001(void); int ResourceManagerGetMediaByIdTest002(void); +int ResourceManagerGetMediaByIdTest003(void); +int ResourceManagerGetMediaByIdTest004(void); +int ResourceManagerGetMediaByIdTest005(void); +int ResourceManagerGetMediaByIdTest006(void); +int ResourceManagerGetMediaByIdTest007(void); +int ResourceManagerGetMediaByIdTest008(void); +int ResourceManagerGetMediaByIdTest009(void); +int ResourceManagerGetMediaByIdTest010(void); +int ResourceManagerGetMediaByIdTest011(void); +int ResourceManagerGetMediaByIdTest012(void); +int ResourceManagerGetMediaByIdTest013(void); +int ResourceManagerGetMediaByIdTest014(void); +int ResourceManagerGetMediaByIdTest015(void); +int ResourceManagerGetMediaByIdTest016(void); +int ResourceManagerGetMediaByIdTest017(void); +int ResourceManagerGetMediaByIdTest018(void); +int ResourceManagerGetMediaByIdTest019(void); +int ResourceManagerGetMediaByIdTest020(void); +int ResourceManagerGetMediaByIdTest021(void); +int ResourceManagerGetMediaByIdTest022(void); int ResourceManagerGetMediaByNameTest001(void); int ResourceManagerGetMediaByNameTest002(void); +int ResourceManagerGetMediaByNameTest003(void); +int ResourceManagerGetMediaByNameTest004(void); +int ResourceManagerGetMediaByNameTest005(void); +int ResourceManagerGetMediaByNameTest006(void); +int ResourceManagerGetMediaByNameTest007(void); +int ResourceManagerGetMediaByNameTest008(void); +int ResourceManagerGetMediaByNameTest009(void); +int ResourceManagerGetMediaByNameTest010(void); +int ResourceManagerGetMediaByNameTest011(void); +int ResourceManagerGetMediaByNameTest012(void); +int ResourceManagerGetMediaByNameTest013(void); +int ResourceManagerGetMediaByNameTest014(void); +int ResourceManagerGetMediaByNameTest015(void); +int ResourceManagerGetMediaByNameTest016(void); +int ResourceManagerGetMediaByNameTest017(void); +int ResourceManagerGetMediaByNameTest018(void); +int ResourceManagerGetMediaByNameTest019(void); +int ResourceManagerGetMediaByNameTest020(void); +int ResourceManagerGetMediaByNameTest021(void); +int ResourceManagerGetMediaByNameTest022(void); int ResourceManagerResolveReferenceTest001(void); int ResourceManagerResolveParentReferenceTest001(void); int ResourceManagerSameNameTest001(void); diff --git a/interfaces/inner_api/include/resource_manager.h b/interfaces/inner_api/include/resource_manager.h index b288c203d5627500d5032e497205475524f29eaf..8c5f046519ec157033564d2b411f64ddf4b91347 100644 --- a/interfaces/inner_api/include/resource_manager.h +++ b/interfaces/inner_api/include/resource_manager.h @@ -114,8 +114,12 @@ public: virtual RState GetMediaById(uint32_t id, std::string &outValue) = 0; + virtual RState GetMediaById(uint32_t id, uint32_t density, std::string &outValue) = 0; + virtual RState GetMediaByName(const char *name, std::string &outValue) = 0; + virtual RState GetMediaByName(const char *name, uint32_t density, std::string &outValue) = 0; + virtual RState GetRawFilePathByName(const std::string &name, std::string &outValue) = 0; virtual RState GetRawFileDescriptor(const std::string &name, RawFileDescriptor &descriptor) = 0; diff --git a/test/resource/data/all.hap b/test/resource/data/all.hap index 467ad248bc0407c2c36d764fd28b7bdc5f917239..8b4ea975ef485c1a260e12a46a8ca12cf8d84a8a 100644 Binary files a/test/resource/data/all.hap and b/test/resource/data/all.hap differ diff --git a/test/resource/data/all/assets/entry/resources.index b/test/resource/data/all/assets/entry/resources.index index c9a14647255f26b1c44fe6b785ab31039cad69d2..312fce6dc499efc75ddf0abe13c330470a6b9a6d 100644 Binary files a/test/resource/data/all/assets/entry/resources.index and b/test/resource/data/all/assets/entry/resources.index differ