1 /*
2 * Copyright (c) 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 <gtest/gtest.h>
17 #include "theme_pack_resource.h"
18
19 using namespace OHOS::Global::Resource;
20 using namespace testing::ext;
21
22 namespace {
23 class ThemeResourceTest : public testing::Test {
24 protected:
25 void SetUp() override;
26 void TearDown() override;
27 };
28
SetUp()29 void ThemeResourceTest::SetUp()
30 {}
31
TearDown()32 void ThemeResourceTest::TearDown()
33 {}
34
35 /*
36 * @tc.name: ThemeResourceInitThemeResTest001
37 * @tc.desc: Test InitThemeRes function, file case.
38 * @tc.type: FUNC
39 */
40 HWTEST_F(ThemeResourceTest, ThemeResourceInitThemeResTest001, TestSize.Level1)
41 {
42 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
43 EXPECT_TRUE(themeResource != nullptr);
44 std::pair<std::string, std::string> bundleInfo;
45 cJSON *root = nullptr;
46 std::shared_ptr<ThemeConfig> themeConfig = std::make_shared<ThemeConfig>();
47 std::string resTypeStr = "";
48 themeResource->InitThemeRes(bundleInfo, root, themeConfig, resTypeStr);
49 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
50 }
51
52 /*
53 * @tc.name: ThemeResourceInitThemeResTest002
54 * @tc.desc: Test InitThemeRes function, file case.
55 * @tc.type: FUNC
56 */
57 HWTEST_F(ThemeResourceTest, ThemeResourceInitThemeResTest002, TestSize.Level1)
58 {
59 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
60 ASSERT_TRUE(themeResource != nullptr);
61 std::pair<std::string, std::string> bundleInfo;
62 cJSON *root = cJSON_CreateObject();
63 ASSERT_TRUE(root != nullptr);
64 cJSON *item = cJSON_CreateObject();
65 ASSERT_TRUE(item != nullptr);
66 cJSON_AddItemToObject(item, "name1", cJSON_CreateString("aaaaa"));
67 cJSON_AddItemToObject(root, "color", item);
68 root->type = cJSON_Array;
69 std::shared_ptr<ThemeConfig> themeConfig = std::make_shared<ThemeConfig>();
70 std::string resTypeStr = "color";
71 themeResource->InitThemeRes(bundleInfo, root, themeConfig, resTypeStr);
72 cJSON_Delete(root);
73 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
74 }
75
76 /*
77 * @tc.name: ThemeResourceInitThemeResTest003
78 * @tc.desc: Test InitThemeRes function, file case.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(ThemeResourceTest, ThemeResourceInitThemeResTest003, TestSize.Level1)
82 {
83 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
84 ASSERT_TRUE(themeResource != nullptr);
85 std::pair<std::string, std::string> bundleInfo;
86 cJSON *root = cJSON_CreateObject();
87 ASSERT_TRUE(root != nullptr);
88 cJSON *item = cJSON_CreateObject();
89 ASSERT_TRUE(item != nullptr);
90 cJSON_AddItemToObject(item, "name", cJSON_CreateString("aaaaa"));
91 cJSON_AddItemToObject(item, "value1", cJSON_CreateString("bbbbb"));
92 cJSON_AddItemToObject(root, "color", item);
93 root->type = cJSON_Array;
94 std::shared_ptr<ThemeConfig> themeConfig = std::make_shared<ThemeConfig>();
95 std::string resTypeStr = "color";
96 themeResource->InitThemeRes(bundleInfo, root, themeConfig, resTypeStr);
97 cJSON_Delete(root);
98 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
99 }
100
101 /*
102 * @tc.name: ThemeResourceInitThemeResTest004
103 * @tc.desc: Test InitThemeRes function, file case.
104 * @tc.type: FUNC
105 */
106 HWTEST_F(ThemeResourceTest, ThemeResourceInitThemeResTest004, TestSize.Level1)
107 {
108 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
109 ASSERT_TRUE(themeResource != nullptr);
110 std::pair<std::string, std::string> bundleInfo;
111 cJSON *root = cJSON_CreateObject();
112 ASSERT_TRUE(root != nullptr);
113 std::shared_ptr<ThemeConfig> themeConfig = std::make_shared<ThemeConfig>();
114 std::string resTypeStr = "string";
115 themeResource->InitThemeRes(bundleInfo, root, themeConfig, resTypeStr);
116 cJSON_Delete(root);
117 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
118 }
119
120 /*
121 * @tc.name: ThemeResourceParseJsonTest001
122 * @tc.desc: Test ParseJson function, file case.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(ThemeResourceTest, ThemeResourceParseJsonTest001, TestSize.Level1)
126 {
127 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
128 EXPECT_TRUE(themeResource != nullptr);
129 std::string bundleName = "ohos.global.test.all";
130 std::string moduleName = "entry";
131 std::string jsonPath = "/data/test/theme/dark/media/foreground.png";
132 themeResource->ParseJson(bundleName, moduleName, jsonPath);
133 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
134 }
135
136 /*
137 * @tc.name: ThemeResourceParseJsonTest002
138 * @tc.desc: Test ParseJson function, file case.
139 * @tc.type: FUNC
140 */
141 HWTEST_F(ThemeResourceTest, ThemeResourceParseJsonTest002, TestSize.Level1)
142 {
143 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
144 EXPECT_TRUE(themeResource != nullptr);
145 std::string bundleName = "ohos.global.test.all";
146 std::string moduleName = "entry";
147 std::string jsonPath = "/data/test/theme/horizontal/media/foreground.png";
148 themeResource->ParseJson(bundleName, moduleName, jsonPath);
149 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
150 }
151
152 /*
153 * @tc.name: ThemeResourceParseJsonTest003
154 * @tc.desc: Test ParseJson function, file case.
155 * @tc.type: FUNC
156 */
157 HWTEST_F(ThemeResourceTest, ThemeResourceParseJsonTest003, TestSize.Level1)
158 {
159 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
160 EXPECT_TRUE(themeResource != nullptr);
161 std::string bundleName = "ohos.global.test.all";
162 std::string moduleName = "entry";
163 std::string jsonPath = "/data/test/theme/horizontal-dark/media/foreground.png";
164 themeResource->ParseJson(bundleName, moduleName, jsonPath);
165 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
166 }
167
168 /*
169 * @tc.name: ThemeResourceParseJsonTest004
170 * @tc.desc: Test ParseJson function, file case.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(ThemeResourceTest, ThemeResourceParseJsonTest004, TestSize.Level1)
174 {
175 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
176 EXPECT_TRUE(themeResource != nullptr);
177 std::string bundleName = "ohos.global.test.all";
178 std::string moduleName = "entry";
179 std::string jsonPath = "/data/test/theme/base/media/foreground.png";
180 themeResource->ParseJson(bundleName, moduleName, jsonPath);
181 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
182 }
183
184 /*
185 * @tc.name: ThemeResourceParseJsonTest005
186 * @tc.desc: Test ParseJson function, file case.
187 * @tc.type: FUNC
188 */
189 HWTEST_F(ThemeResourceTest, ThemeResourceParseJsonTest005, TestSize.Level1)
190 {
191 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
192 EXPECT_TRUE(themeResource != nullptr);
193 std::string bundleName = "ohos.global.test.all";
194 std::string moduleName = "entry";
195 std::string jsonPath = "data/test/foreground.png";
196 themeResource->ParseJson(bundleName, moduleName, jsonPath);
197 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
198 }
199
200 /*
201 * @tc.name: ThemeResourceParseIconTest001
202 * @tc.desc: Test ParseIcon function, file case.
203 * @tc.type: FUNC
204 */
205 HWTEST_F(ThemeResourceTest, ThemeResourceParseIconTest001, TestSize.Level1)
206 {
207 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
208 EXPECT_TRUE(themeResource != nullptr);
209 std::string bundleName = "ohos.global.test.all";
210 std::string moduleName = "entry";
211 std::string iconPath = "data/test";
212 themeResource->ParseIcon(bundleName, moduleName, iconPath);
213 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
214 }
215
216 /*
217 * @tc.name: ThemeResourceParseIconTest002
218 * @tc.desc: Test ParseIcon function, file case.
219 * @tc.type: FUNC
220 */
221 HWTEST_F(ThemeResourceTest, ThemeResourceParseIconTest002, TestSize.Level1)
222 {
223 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
224 EXPECT_TRUE(themeResource != nullptr);
225 std::string bundleName = "ohos.global.test.all";
226 std::string moduleName = "entry";
227 std::string iconPath = "foreground.png";
228 themeResource->ParseIcon(bundleName, moduleName, iconPath);
229 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
230 }
231
232 /*
233 * @tc.name: ThemeResourceParseIconTest003
234 * @tc.desc: Test ParseIcon function, file case.
235 * @tc.type: FUNC
236 */
237 HWTEST_F(ThemeResourceTest, ThemeResourceParseIconTest003, TestSize.Level1)
238 {
239 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
240 EXPECT_TRUE(themeResource != nullptr);
241 std::string bundleName = "ohos.global.test.all";
242 std::string moduleName = "entry";
243 std::string iconPath = "foreground.png/";
244 themeResource->ParseIcon(bundleName, moduleName, iconPath);
245 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
246 }
247
248 /*
249 * @tc.name: ThemeResourceGetThemeValuesTest001
250 * @tc.desc: Test GetThemeValues function, file case.
251 * @tc.type: FUNC
252 */
253 HWTEST_F(ThemeResourceTest, ThemeResourceGetThemeValuesTest001, TestSize.Level1)
254 {
255 std::shared_ptr<ThemeResource> themeResource = std::make_shared<ThemeResource>("");
256 EXPECT_TRUE(themeResource != nullptr);
257 std::pair<std::string, std::string> bundInfo;
258 ResType resType = ResType::VALUES;
259 std::string name("test");
260 themeResource->GetThemeValues(bundInfo, resType, name);
261 EXPECT_TRUE(themeResource->themeValueVec_.size() == 0);
262 }
263
264 /*
265 * @tc.name: ThemeResourceLoadThemeIconResourceTest001
266 * @tc.desc: Test LoadThemeIconResource function, file case.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(ThemeResourceTest, ThemeResourceLoadThemeIconResourceTest001, TestSize.Level1)
270 {
271 std::string iconPath("");
272 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
273 EXPECT_TRUE(themeResource == nullptr);
274
275 iconPath = "abc";
276 themeResource = ThemeResource::LoadThemeIconResource(iconPath);
277 EXPECT_TRUE(themeResource != nullptr);
278 }
279
280 /*
281 * @tc.name: ThemeResourceLoadThemeIconResourceTest002
282 * @tc.desc: Test LoadThemeIconResource function, file case.
283 * @tc.type: FUNC
284 */
285 HWTEST_F(ThemeResourceTest, ThemeResourceLoadThemeIconResourceTest002, TestSize.Level1)
286 {
287 std::string iconPath("/data/test/theme/themereource/");
288 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
289 EXPECT_TRUE(themeResource != nullptr);
290 }
291
292 /*
293 * @tc.name: ThemeResourceLoadThemeIconResourceTest003
294 * @tc.desc: Test LoadThemeIconResource function, file case.
295 * @tc.type: FUNC
296 */
297 HWTEST_F(ThemeResourceTest, ThemeResourceLoadThemeIconResourceTest003, TestSize.Level1)
298 {
299 std::string iconPath("/data/test/theme/icons/dynamic_icons/");
300 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
301 EXPECT_TRUE((themeResource != nullptr && themeResource->iconValues_.size() != 0));
302 }
303
304 /*
305 * @tc.name: ThemeResourceGetThemeAppIconByAbilityNameTest001
306 * @tc.desc: Test GetThemeAppIconByAbilityName function, file case.
307 * @tc.type: FUNC
308 */
309 HWTEST_F(ThemeResourceTest, ThemeResourceGetThemeAppIconByAbilityNameTest001, TestSize.Level1)
310 {
311 std::string iconPath("/data/test/theme/icons/dynamic_icons");
312 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
313 ASSERT_TRUE(themeResource != nullptr);
314 std::pair<std::string, std::string> bundleInfo;
315 bundleInfo.first = "ohos.global.test";
316 bundleInfo.second = "entry";
317 std::string iconName = "foreground";
318 std::string abilityName = "MainAbility";
319 std::string result = themeResource->GetThemeAppIconByAbilityName(bundleInfo, iconName, abilityName);
320 EXPECT_EQ(result, std::string(""));
321 }
322
323 /*
324 * @tc.name: ThemeResourceGetThemeAppIconByAbilityNameTest002
325 * @tc.desc: Test GetThemeAppIconByAbilityName function, file case.
326 * @tc.type: FUNC
327 */
328 HWTEST_F(ThemeResourceTest, ThemeResourceGetThemeAppIconByAbilityNameTest002, TestSize.Level1)
329 {
330 std::string iconPath("/data/test/theme/icons/ohos.global.test.all");
331 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
332 ASSERT_TRUE(themeResource != nullptr);
333 std::pair<std::string, std::string> bundleInfo;
334 bundleInfo.first = "ohos.global.test";
335 bundleInfo.second = "entry";
336 std::string iconName = "foreground";
337 std::string abilityName = "MainAbility";
338 std::string result = themeResource->GetThemeAppIconByAbilityName(bundleInfo, iconName, abilityName);
339 EXPECT_EQ(result, std::string(""));
340 }
341
342 /*
343 * @tc.name: ThemeResourceHasIconInThemeTest001
344 * @tc.desc: Test HasIconInTheme function, file case.
345 * @tc.type: FUNC
346 */
347 HWTEST_F(ThemeResourceTest, ThemeResourceHasIconInThemeTest001, TestSize.Level1)
348 {
349 std::string iconPath("/data/test/theme/icons/dynamic_icons");
350 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeIconResource(iconPath);
351 ASSERT_TRUE(themeResource != nullptr);
352 std::string bundleName = "ohos.global.test.all";
353 bool result = themeResource->HasIconInTheme(bundleName);
354 EXPECT_FALSE(result);
355 }
356
357 /*
358 * @tc.name: ThemeResourceLoadThemeResourceTest001
359 * @tc.desc: Test LoadThemeResource function, file case.
360 * @tc.type: FUNC
361 */
362 HWTEST_F(ThemeResourceTest, ThemeResourceLoadThemeResourceTest001, TestSize.Level1)
363 {
364 std::string rootDir = "";
365 std::shared_ptr<ThemeResource> themeResource = ThemeResource::LoadThemeResource(rootDir);
366 EXPECT_TRUE(themeResource == nullptr);
367 }
368 }
369