1 /*
2 * Copyright (c) 2025-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <memory>
17 #include "gtest/gtest.h"
18 #include "hap_resource.h"
19 #include "resource_manager_test_common.h"
20 #include "system_resource_manager.h"
21
22 using namespace testing::ext;
23 using namespace OHOS::Global::Resource;
24
25 namespace {
26 class SystemResourceManagerTest : public testing::Test {
27 protected:
28 void SetUp() override;
29 void TearDown() override;
30
31 static constexpr int refCountTwo = 2;
32 static constexpr int refCountFour = 4;
33 };
34
SetUp()35 void SystemResourceManagerTest::SetUp()
36 {
37 SystemResourceManager::ReleaseSystemResourceManager();
38 }
39
TearDown()40 void SystemResourceManagerTest::TearDown()
41 {}
42
43 HWTEST_F(SystemResourceManagerTest, CreateSystemResourceTest, TestSize.Level1)
44 {
45 {
46 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
47 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 1);
48 }
49 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 0);
50 }
51
52 HWTEST_F(SystemResourceManagerTest, CreateSystemResourceTest002, TestSize.Level1)
53 {
54 std::shared_ptr<SystemResourceManager> sysResMgr = std::make_shared<SystemResourceManager>();
55 EXPECT_TRUE(sysResMgr != nullptr);
56 }
57
58 HWTEST_F(SystemResourceManagerTest, GetSystemResourceManagerNoSandBoxTest, TestSize.Level1)
59 {
60 {
61 SystemResourceManager::GetSystemResourceManagerNoSandBox();
62 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 1);
63 }
64 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 1);
65 SystemResourceManager::ReleaseSystemResourceManager();
66 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), 0);
67 }
68
69 HWTEST_F(SystemResourceManagerTest, DoubleCreateSystemResourceTest, TestSize.Level1)
70 {
71 std::weak_ptr<HapResource> weakResource;
72 {
73 std::shared_ptr<ResourceManager> resMgr1(CreateResourceManager());
74 std::shared_ptr<ResourceManager> resMgr2(CreateResourceManager());
75 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), refCountTwo);
76 auto sysResMgr = SystemResourceManager::weakResourceManager_.lock();
77 ASSERT_TRUE(sysResMgr != nullptr);
78 auto hapResources = sysResMgr->hapManager_->GetHapResource();
79 ASSERT_GT(hapResources.size(), 0);
80 weakResource = hapResources[0];
81 EXPECT_EQ(weakResource.use_count(), refCountFour);
82 }
83 EXPECT_EQ(weakResource.use_count(), 0);
84 }
85
86 HWTEST_F(SystemResourceManagerTest, SystemResourceCreateBeforeGet, TestSize.Level1)
87 {
88 std::weak_ptr<HapResource> weakResource;
89 {
90 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
91 SystemResourceManager::GetSystemResourceManagerNoSandBox();
92 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), refCountTwo);
93 auto sysResMgr = SystemResourceManager::weakResourceManager_.lock();
94 ASSERT_TRUE(sysResMgr != nullptr);
95 auto hapResources = sysResMgr->hapManager_->GetHapResource();
96 ASSERT_GT(hapResources.size(), 0);
97 weakResource = hapResources[0];
98 EXPECT_EQ(weakResource.use_count(), refCountFour);
99 }
100 EXPECT_EQ(weakResource.use_count(), refCountTwo);
101 SystemResourceManager::ReleaseSystemResourceManager();
102 EXPECT_EQ(weakResource.use_count(), 0);
103 }
104
105 HWTEST_F(SystemResourceManagerTest, SystemResourceCreateAfterGet, TestSize.Level1)
106 {
107 std::weak_ptr<HapResource> weakResource;
108 {
109 SystemResourceManager::GetSystemResourceManagerNoSandBox();
110 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
111 EXPECT_EQ(SystemResourceManager::weakResourceManager_.use_count(), refCountTwo);
112 auto sysResMgr = SystemResourceManager::weakResourceManager_.lock();
113 ASSERT_TRUE(sysResMgr != nullptr);
114 auto hapResources = sysResMgr->hapManager_->GetHapResource();
115 ASSERT_GT(hapResources.size(), 0);
116 weakResource = hapResources[0];
117 EXPECT_EQ(weakResource.use_count(), refCountFour);
118 }
119 EXPECT_EQ(weakResource.use_count(), refCountTwo);
120 SystemResourceManager::ReleaseSystemResourceManager();
121 EXPECT_EQ(weakResource.use_count(), 0);
122 }
123
124 HWTEST_F(SystemResourceManagerTest, AddSystemResourceTest, TestSize.Level1)
125 {
126 ResourceManagerImpl *appResMgr = nullptr;
127 bool result = SystemResourceManager::AddSystemResource(appResMgr);
128 EXPECT_FALSE(result);
129 }
130
131 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest001, TestSize.Level1)
132 {
133 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
134 ASSERT_TRUE(resMgr != nullptr);
135 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
136 ASSERT_TRUE(sysResMgr != nullptr);
137 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
138 ASSERT_TRUE(ret);
139 ResConfigImpl rc;
140 rc.SetLocaleInfo("zh", nullptr, "CN");
141 rc.SetColorMode(ColorMode::DARK);
142 resMgr->UpdateResConfig(rc);
143 std::string rmOutValue;
144 resMgr->GetStringByName("ohos_desc_write_calendar", rmOutValue);
145 std::string sysOutValue;
146 sysResMgr->GetStringByName("ohos_desc_write_calendar", sysOutValue);
147 EXPECT_EQ(rmOutValue, sysOutValue);
148 }
149
150 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest002, TestSize.Level1)
151 {
152 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
153 ASSERT_TRUE(resMgr != nullptr);
154 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
155 ASSERT_TRUE(sysResMgr != nullptr);
156 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
157 ASSERT_TRUE(ret);
158 ResConfigImpl rc;
159 rc.SetLocaleInfo("en", nullptr, "US");
160 rc.SetColorMode(ColorMode::DARK);
161 resMgr->UpdateResConfig(rc);
162 uint32_t rmOutValue;
163 resMgr->GetColorByName("ohos_id_color_foregroud", rmOutValue);
164 uint32_t sysOutValue;
165 sysResMgr->GetColorByName("ohos_id_color_foregroud", sysOutValue);
166 EXPECT_EQ(rmOutValue, sysOutValue);
167 }
168
169 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest003, TestSize.Level1)
170 {
171 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
172 ASSERT_TRUE(resMgr != nullptr);
173 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
174 ASSERT_TRUE(sysResMgr != nullptr);
175 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
176 ASSERT_TRUE(ret);
177 ResConfigImpl rc;
178 rc.SetLocaleInfo("en", nullptr, "US");
179 rc.SetScreenDensity(2.00);
180 rc.SetDeviceType(DeviceType::DEVICE_WEARABLE);
181 resMgr->UpdateResConfig(rc);
182 uint32_t rmOutValue;
183 resMgr->GetColorByName("ohos_id_color_text_primary_contrary", rmOutValue);
184 uint32_t sysOutValue;
185 sysResMgr->GetColorByName("ohos_id_color_text_primary_contrary", sysOutValue);
186 EXPECT_EQ(rmOutValue, sysOutValue);
187 }
188
189 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest004, TestSize.Level1)
190 {
191 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
192 ASSERT_TRUE(resMgr != nullptr);
193 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
194 ASSERT_TRUE(sysResMgr != nullptr);
195 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
196 ASSERT_TRUE(ret);
197 ResConfigImpl rc;
198 rc.SetLocaleInfo("en", nullptr, "US");
199 rc.SetScreenDensity(3.25);
200 resMgr->UpdateResConfig(rc);
201 float rmOutValue;
202 resMgr->GetFloatByName("ohos_fa_corner_radius_card", rmOutValue);
203 float sysOutValue;
204 sysResMgr->GetFloatByName("ohos_fa_corner_radius_card", sysOutValue);
205 EXPECT_EQ(rmOutValue, sysOutValue);
206 }
207
208 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest005, TestSize.Level1)
209 {
210 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
211 ASSERT_TRUE(resMgr != nullptr);
212 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
213 ASSERT_TRUE(sysResMgr != nullptr);
214 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
215 ASSERT_TRUE(ret);
216 ResConfigImpl rc;
217 rc.SetLocaleInfo("en", nullptr, "US");
218 rc.SetDirection(Direction::DIRECTION_VERTICAL);
219 resMgr->UpdateResConfig(rc);
220 uint32_t rmOutValue;
221 resMgr->GetSymbolByName("ohos_wifi", rmOutValue);
222 uint32_t sysOutValue;
223 sysResMgr->GetSymbolByName("ohos_wifi", sysOutValue);
224 EXPECT_EQ(rmOutValue, sysOutValue);
225 }
226
227 HWTEST_F(SystemResourceManagerTest, CreateSysResourceManagerTest006, TestSize.Level1)
228 {
229 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
230 ASSERT_TRUE(resMgr != nullptr);
231 auto sysResMgr = SystemResourceManager::CreateSysResourceManager();
232 ASSERT_TRUE(sysResMgr != nullptr);
233 bool ret = resMgr->AddResource(FormatFullPath(g_resFilePath).c_str());
234 ASSERT_TRUE(ret);
235 ResConfigImpl rc;
236 rc.SetLocaleInfo("en", nullptr, "US");
237 rc.SetMcc(460);
238 rc.SetMnc(001);
239 resMgr->UpdateResConfig(rc);
240 int rmOutValue;
241 resMgr->GetIntegerByName("ohos_id_alpha_content_primary", rmOutValue);
242 int sysOutValue;
243 sysResMgr->GetIntegerByName("ohos_id_alpha_content_primary", sysOutValue);
244 EXPECT_EQ(rmOutValue, sysOutValue);
245 }
246 } // namespace