1 /*
2 * Copyright (c) 2023-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 "resource_manager_test_common.h"
17
18 using namespace OHOS::Global::Resource;
19 using namespace testing::ext;
20
ResourceManagerTestCommon(ResourceManager * rm)21 ResourceManagerTestCommon::ResourceManagerTestCommon(ResourceManager *rm) : rm(rm)
22 {}
23
ResourceManagerTestCommon(std::shared_ptr<ResourceManager> rm)24 ResourceManagerTestCommon::ResourceManagerTestCommon(std::shared_ptr<ResourceManager> rm)
25 {
26 this->rm = rm.get();
27 }
28
~ResourceManagerTestCommon()29 ResourceManagerTestCommon::~ResourceManagerTestCommon()
30 {}
31
SetUpTestCase(void)32 void ResourceManagerTestCommon::SetUpTestCase(void)
33 {
34 // step 1: input testsuit setup step
35 g_logLevel = LOG_DEBUG;
36 }
37
TearDownTestCase(void)38 void ResourceManagerTestCommon::TearDownTestCase(void)
39 {
40 // step 2: input testsuit teardown step
41 }
42
SetUp(void)43 void ResourceManagerTestCommon::SetUp(void)
44 {
45 this->rm = CreateResourceManager();
46 }
47
TearDown(void)48 void ResourceManagerTestCommon::TearDown(void)
49 {
50 delete this->rm;
51 }
52
GetResId(const std::string & name,ResType resType)53 int ResourceManagerTestCommon::GetResId(const std::string &name, ResType resType)
54 {
55 auto idValues = ((ResourceManagerImpl *)rm)->hapManager_->GetResourceListByName(name.c_str(), resType);
56 if (idValues.size() == 0) {
57 return -1;
58 }
59
60 PrintIdValues(idValues[0]);
61 for (auto &idValue : idValues) {
62 if (idValue->GetLimitPathsConst().size() > 0 && !idValue->GetLimitPathsConst()[0]->IsSystemResource()) {
63 return idValue->GetLimitPathsConst()[0]->GetIdItem()->id_;
64 }
65 }
66 return OBJ_NOT_FOUND;
67 }
68
TestStringByName(const char * name,const char * cmp)69 void ResourceManagerTestCommon::TestStringByName(const char *name, const char *cmp)
70 {
71 std::string outValue;
72 RState rState = rm->GetStringByName(name, outValue);
73 ASSERT_EQ(SUCCESS, rState);
74 RESMGR_HILOGD(RESMGR_TAG, "%s : %s", name, outValue.c_str());
75 ASSERT_EQ(std::string(cmp), outValue);
76 }
77
TestStringById(const char * name,const char * cmp)78 void ResourceManagerTestCommon::TestStringById(const char *name, const char *cmp)
79 {
80 std::string outValue;
81 int id = GetResId(name, ResType::STRING);
82 ASSERT_TRUE(id > 0);
83 RState rState = rm->GetStringById(id, outValue);
84 ASSERT_EQ(SUCCESS, rState);
85 ASSERT_EQ(std::string(cmp), outValue);
86 }
87
AddResource(const char * language,const char * script,const char * region)88 void ResourceManagerTestCommon::AddResource(const char *language, const char *script, const char *region)
89 {
90 if (language != nullptr || region != nullptr) {
91 auto rc = CreateResConfig();
92 if (rc == nullptr) {
93 EXPECT_TRUE(false);
94 return;
95 }
96 rc->SetLocaleInfo(language, script, region);
97 rm->UpdateResConfig(*rc);
98 delete rc;
99 }
100 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
101 ASSERT_TRUE(ret);
102 }
103
AddHapResource(const char * language,const char * script,const char * region)104 void ResourceManagerTestCommon::AddHapResource(const char *language, const char *script, const char *region)
105 {
106 if (language != nullptr || region != nullptr) {
107 auto rc = CreateResConfig();
108 if (rc == nullptr) {
109 EXPECT_TRUE(false);
110 return;
111 }
112 rc->SetLocaleInfo(language, script, region);
113 rm->UpdateResConfig(*rc);
114 delete rc;
115 }
116 bool ret = rm->AddResource(FormatFullPath(g_hapPath).c_str());
117 ASSERT_TRUE(ret);
118 }
119
AddColorModeResource(DeviceType deviceType,ColorMode colorMode,float screenDensity)120 void ResourceManagerTestCommon::AddColorModeResource(DeviceType deviceType, ColorMode colorMode,
121 float screenDensity)
122 {
123 auto rc = CreateResConfig();
124 if (rc == nullptr) {
125 EXPECT_TRUE(false);
126 return;
127 }
128 rc->SetLocaleInfo("zh", nullptr, nullptr);
129 rc->SetDeviceType(deviceType);
130 rc->SetColorMode(colorMode);
131 rc->SetScreenDensity(screenDensity);
132 rm->UpdateResConfig(*rc);
133 delete rc;
134 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
135 ASSERT_TRUE(ret);
136 }
137
TestPluralStringById(int quantity,const char * cmp,bool format)138 void ResourceManagerTestCommon::TestPluralStringById(int quantity, const char *cmp, bool format)
139 {
140 RState ret;
141 std::string outValue;
142 int id = GetResId("eat_apple", ResType::PLURALS);
143 ASSERT_TRUE(id > 0);
144 if (format) {
145 ret = rm->GetPluralStringByIdFormat(outValue, id, quantity, quantity);
146 } else {
147 ret = rm->GetPluralStringById(id, quantity, outValue);
148 }
149
150 ASSERT_EQ(SUCCESS, ret);
151 ASSERT_EQ(std::string(cmp), outValue);
152 }
153
TestPluralStringByName(int quantity,const char * cmp,bool format)154 void ResourceManagerTestCommon::TestPluralStringByName(int quantity, const char *cmp, bool format)
155 {
156 RState ret;
157 std::string outValue;
158 const char *name = "eat_apple";
159 if (format) {
160 ret = rm->GetPluralStringByNameFormat(outValue, name, quantity, quantity);
161 } else {
162 ret = rm->GetPluralStringByName(name, quantity, outValue);
163 }
164
165 ASSERT_EQ(SUCCESS, ret);
166 ASSERT_EQ(std::string(cmp), outValue);
167 }
168
TestFormatPluralStringById(const int id,double quantity,const char * cmp)169 void ResourceManagerTestCommon::TestFormatPluralStringById(const int id, double quantity, const char *cmp)
170 {
171 RState ret;
172 std::string outValue;
173 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> params;
174 params.push_back(std::make_tuple(ResourceManager::NapiValueType::NAPI_NUMBER, std::to_string(quantity)));
175 ret = rm->GetFormatPluralStringById(outValue, id, quantity, params);
176 ASSERT_EQ(SUCCESS, ret);
177 ASSERT_EQ(std::string(cmp), outValue);
178 }
179
TestFormatPluralStringByName(const std::string name,double quantity,const char * cmp)180 void ResourceManagerTestCommon::TestFormatPluralStringByName(const std::string name, double quantity, const char *cmp)
181 {
182 RState ret;
183 std::string outValue;
184 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> params;
185 params.push_back(std::make_tuple(ResourceManager::NapiValueType::NAPI_NUMBER, std::to_string(quantity)));
186 ret = rm->GetFormatPluralStringByName(outValue, name.c_str(), quantity, params);
187 ASSERT_EQ(SUCCESS, ret);
188 ASSERT_EQ(std::string(cmp), outValue);
189 }
190
TestGetFormatPluralStringById(std::string outValue,const int id,ResourceManager::Quantity quantity,...)191 RState ResourceManagerTestCommon::TestGetFormatPluralStringById(std::string outValue, const int id,
192 ResourceManager::Quantity quantity, ...)
193 {
194 RState state;
195 va_list args;
196 va_start(args, quantity);
197 state = rm->GetFormatPluralStringById(outValue, id, quantity, args);
198 va_end(args);
199 return state;
200 }
201
TestGetFormatPluralStringByName(std::string outValue,const char * name,ResourceManager::Quantity quantity,...)202 RState ResourceManagerTestCommon::TestGetFormatPluralStringByName(std::string outValue, const char *name,
203 ResourceManager::Quantity quantity, ...)
204 {
205 RState state;
206 va_list args;
207 va_start(args, quantity);
208 state = rm->GetFormatPluralStringByName(outValue, name, quantity, args);
209 va_end(args);
210 return state;
211 }
212
TestGetRawFilePathByName(const std::string & name,const std::string & cmp)213 void ResourceManagerTestCommon::TestGetRawFilePathByName(const std::string &name, const std::string &cmp)
214 {
215 std::string outValue;
216 rm->GetRawFilePathByName(name, outValue);
217 RESMGR_HILOGD(RESMGR_TAG, "%s : %s", name.c_str(), outValue.c_str());
218 ASSERT_EQ(cmp, outValue);
219 }
220
TestGetProfileById(HapResourceV1 * tmp)221 void ResourceManagerTestCommon::TestGetProfileById(HapResourceV1 *tmp)
222 {
223 tmp->Init(this->defaultResConfig);
224 std::string res = tmp->GetResourcePath();
225 res.append("entry/resources/base/profile/test_profile.json");
226
227 std::string outValue;
228 RState state;
229 int id = GetResId("test_profile", ResType::PROF);
230 EXPECT_TRUE(id > 0);
231 state = rm->GetProfileById(id, outValue);
232 EXPECT_TRUE(state == SUCCESS);
233 EXPECT_EQ(res, outValue);
234 }
235
TestGetProfileById(HapResourceV2 * tmp)236 void ResourceManagerTestCommon::TestGetProfileById(HapResourceV2 *tmp)
237 {
238 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
239 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
240 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
241 std::shared_ptr<MmapFile> mMap;
242 tmp->Init(keys, idMap, typeNameMap, mMap);
243 std::string res = tmp->GetResourcePath();
244 res.append("entry/resources/base/profile/test_profile.json");
245
246 std::string outValue;
247 RState state;
248 int id = GetResId("test_profile", ResType::PROF);
249 EXPECT_TRUE(id > 0);
250 state = rm->GetProfileById(id, outValue);
251 EXPECT_TRUE(state == SUCCESS);
252 EXPECT_EQ(res, outValue);
253 }
254
TestGetProfileByName(HapResourceV1 * tmp)255 void ResourceManagerTestCommon::TestGetProfileByName(HapResourceV1 *tmp)
256 {
257 tmp->Init(this->defaultResConfig);
258 std::string res = tmp->GetResourcePath();
259 res.append("entry/resources/base/profile/test_profile.json");
260
261 std::string outValue;
262 RState state = rm->GetProfileByName("test_profile", outValue);
263 EXPECT_TRUE(state == SUCCESS);
264 EXPECT_EQ(res, outValue);
265 }
266
TestGetProfileByName(HapResourceV2 * tmp)267 void ResourceManagerTestCommon::TestGetProfileByName(HapResourceV2 *tmp)
268 {
269 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
270 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
271 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
272 std::shared_ptr<MmapFile> mMap;
273 tmp->Init(keys, idMap, typeNameMap, mMap);
274 std::string res = tmp->GetResourcePath();
275 res.append("entry/resources/base/profile/test_profile.json");
276
277 std::string outValue;
278 RState state = rm->GetProfileByName("test_profile", outValue);
279 EXPECT_TRUE(state == SUCCESS);
280 EXPECT_EQ(res, outValue);
281 }
282
TestGetMediaById(HapResourceV1 * tmp)283 void ResourceManagerTestCommon::TestGetMediaById(HapResourceV1 *tmp)
284 {
285 tmp->Init(this->defaultResConfig);
286 std::string res = tmp->GetResourcePath();
287 res.append("entry/resources/base/media/icon1.png");
288
289 std::string outValue;
290 int id = GetResId("icon1", ResType::MEDIA);
291 EXPECT_TRUE(id > 0);
292 RState state = rm->GetMediaById(id, outValue);
293 EXPECT_TRUE(state == SUCCESS);
294 EXPECT_EQ(res, outValue);
295 }
296
TestGetMediaById(HapResourceV2 * tmp)297 void ResourceManagerTestCommon::TestGetMediaById(HapResourceV2 *tmp)
298 {
299 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
300 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
301 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
302 std::shared_ptr<MmapFile> mMap;
303 tmp->Init(keys, idMap, typeNameMap, mMap);
304 std::string res = tmp->GetResourcePath();
305 res.append("entry/resources/base/media/icon1.png");
306
307 std::string outValue;
308 int id = GetResId("icon1", ResType::MEDIA);
309 EXPECT_TRUE(id > 0);
310 RState state = rm->GetMediaById(id, outValue);
311 EXPECT_TRUE(state == SUCCESS);
312 EXPECT_EQ(res, outValue);
313 }
314
TestGetMediaWithDensityById(HapResourceV1 * tmp)315 void ResourceManagerTestCommon::TestGetMediaWithDensityById(HapResourceV1 *tmp)
316 {
317 tmp->Init(this->defaultResConfig);
318 std::string res = tmp->GetResourcePath();
319 res.append("entry/resources/sdpi/media/icon.png");
320
321 auto rc = CreateResConfig();
322 if (rc == nullptr) {
323 EXPECT_TRUE(false);
324 return;
325 }
326 rc->SetDeviceType(DEVICE_TV);
327 rc->SetColorMode(COLOR_MODE_NOT_SET);
328 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
329 rm->UpdateResConfig(*rc);
330 delete rc;
331
332 int density = 120;
333 std::string outValue;
334 int id = GetResId("icon", ResType::MEDIA);
335 EXPECT_TRUE(id > 0);
336 RState state = rm->GetMediaById(id, outValue, density);
337 EXPECT_TRUE(state == SUCCESS);
338 EXPECT_EQ(res, outValue);
339 }
340
TestGetMediaWithDensityById(HapResourceV2 * tmp)341 void ResourceManagerTestCommon::TestGetMediaWithDensityById(HapResourceV2 *tmp)
342 {
343 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
344 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
345 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
346 std::shared_ptr<MmapFile> mMap;
347 tmp->Init(keys, idMap, typeNameMap, mMap);
348 std::string res = tmp->GetResourcePath();
349 res.append("entry/resources/sdpi/media/icon.png");
350
351 auto rc = CreateResConfig();
352 if (rc == nullptr) {
353 EXPECT_TRUE(false);
354 return;
355 }
356 rc->SetDeviceType(DEVICE_TV);
357 rc->SetColorMode(COLOR_MODE_NOT_SET);
358 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
359 rm->UpdateResConfig(*rc);
360 delete rc;
361
362 int density = 120;
363 std::string outValue;
364 int id = GetResId("icon", ResType::MEDIA);
365 EXPECT_TRUE(id > 0);
366 RState state = rm->GetMediaById(id, outValue, density);
367 EXPECT_TRUE(state == SUCCESS);
368 EXPECT_EQ(res, outValue);
369 }
370
TestGetMediaByName(HapResourceV1 * tmp)371 void ResourceManagerTestCommon::TestGetMediaByName(HapResourceV1 *tmp)
372 {
373 tmp->Init(this->defaultResConfig);
374 std::string res = tmp->GetResourcePath();
375 res.append("entry/resources/base/media/icon1.png");
376
377 std::string outValue;
378 RState state = rm->GetMediaByName("icon1", outValue);
379 EXPECT_TRUE(state == SUCCESS);
380 EXPECT_EQ(res, outValue);
381 }
382
TestGetMediaByName(HapResourceV2 * tmp)383 void ResourceManagerTestCommon::TestGetMediaByName(HapResourceV2 *tmp)
384 {
385 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
386 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
387 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
388 std::shared_ptr<MmapFile> mMap;
389 tmp->Init(keys, idMap, typeNameMap, mMap);
390 std::string res = tmp->GetResourcePath();
391 res.append("entry/resources/base/media/icon1.png");
392
393 std::string outValue;
394 RState state = rm->GetMediaByName("icon1", outValue);
395 EXPECT_TRUE(state == SUCCESS);
396 EXPECT_EQ(res, outValue);
397 }
398
TestGetMediaWithDensityByName(HapResourceV1 * tmp)399 void ResourceManagerTestCommon::TestGetMediaWithDensityByName(HapResourceV1 *tmp)
400 {
401 tmp->Init(this->defaultResConfig);
402 std::string res = tmp->GetResourcePath();
403 res.append("entry/resources/sdpi/media/icon.png");
404
405 auto rc = CreateResConfig();
406 if (rc == nullptr) {
407 EXPECT_TRUE(false);
408 return;
409 }
410 rc->SetDeviceType(DEVICE_PHONE);
411 rc->SetColorMode(COLOR_MODE_NOT_SET);
412 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
413 rm->UpdateResConfig(*rc);
414 delete rc;
415
416 uint32_t density = 120;
417 std::string outValue;
418 RState state = rm->GetMediaByName("icon", outValue, density);
419 EXPECT_TRUE(state == SUCCESS);
420 EXPECT_EQ(res, outValue);
421 }
422
TestGetMediaWithDensityByName(HapResourceV2 * tmp)423 void ResourceManagerTestCommon::TestGetMediaWithDensityByName(HapResourceV2 *tmp)
424 {
425 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
426 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
427 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
428 std::shared_ptr<MmapFile> mMap;
429 tmp->Init(keys, idMap, typeNameMap, mMap);
430 std::string res = tmp->GetResourcePath();
431 res.append("entry/resources/sdpi/media/icon.png");
432
433 auto rc = CreateResConfig();
434 if (rc == nullptr) {
435 EXPECT_TRUE(false);
436 return;
437 }
438 rc->SetDeviceType(DEVICE_PHONE);
439 rc->SetColorMode(COLOR_MODE_NOT_SET);
440 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
441 rm->UpdateResConfig(*rc);
442 delete rc;
443
444 uint32_t density = 120;
445 std::string outValue;
446 RState state = rm->GetMediaByName("icon", outValue, density);
447 EXPECT_TRUE(state == SUCCESS);
448 EXPECT_EQ(res, outValue);
449 }
450
TestGetDrawableInfoById(HapResourceV1 * tmp)451 void ResourceManagerTestCommon::TestGetDrawableInfoById(HapResourceV1 *tmp)
452 {
453 tmp->Init(this->defaultResConfig);
454 int id = GetResId("icon1", ResType::MEDIA);
455 EXPECT_TRUE(id > 0);
456 std::string type;
457 size_t len;
458 std::unique_ptr<uint8_t[]> jsonBuf;
459 RState state = rm->GetDrawableInfoById(id, type, len, jsonBuf);
460 EXPECT_TRUE(state == SUCCESS);
461 EXPECT_TRUE(jsonBuf != nullptr);
462 }
463
TestGetDrawableInfoById(HapResourceV2 * tmp)464 void ResourceManagerTestCommon::TestGetDrawableInfoById(HapResourceV2 *tmp)
465 {
466 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
467 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
468 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
469 std::shared_ptr<MmapFile> mMap;
470 tmp->Init(keys, idMap, typeNameMap, mMap);
471 int id = GetResId("icon1", ResType::MEDIA);
472 EXPECT_TRUE(id > 0);
473 std::string type;
474 size_t len;
475 std::unique_ptr<uint8_t[]> jsonBuf;
476 RState state = rm->GetDrawableInfoById(id, type, len, jsonBuf);
477 EXPECT_TRUE(state == SUCCESS);
478 EXPECT_TRUE(jsonBuf != nullptr);
479 }
480
TestGetDrawableInfoWithDensityById(HapResourceV1 * tmp)481 void ResourceManagerTestCommon::TestGetDrawableInfoWithDensityById(HapResourceV1 *tmp)
482 {
483 tmp->Init(this->defaultResConfig);
484 auto rc = CreateResConfig();
485 if (rc == nullptr) {
486 EXPECT_TRUE(false);
487 return;
488 }
489 rc->SetDeviceType(DEVICE_TV);
490 rc->SetColorMode(COLOR_MODE_NOT_SET);
491 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
492 rm->UpdateResConfig(*rc);
493 delete rc;
494
495 int density = 120;
496 int id = GetResId("icon", ResType::MEDIA);
497 EXPECT_TRUE(id > 0);
498 std::string type;
499 size_t len;
500 std::unique_ptr<uint8_t[]> jsonBuf;
501 RState state = rm->GetDrawableInfoById(id, type, len, jsonBuf, density);
502 EXPECT_TRUE(state == SUCCESS);
503 EXPECT_TRUE(jsonBuf != nullptr);
504
505 // invalid density
506 int invalidDensity = 1000;
507 state = rm->GetDrawableInfoById(id, type, len, jsonBuf, invalidDensity);
508 EXPECT_TRUE(state == ERROR_CODE_INVALID_INPUT_PARAMETER);
509 }
510
TestGetDrawableInfoWithDensityById(HapResourceV2 * tmp)511 void ResourceManagerTestCommon::TestGetDrawableInfoWithDensityById(HapResourceV2 *tmp)
512 {
513 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
514 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
515 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
516 std::shared_ptr<MmapFile> mMap;
517 tmp->Init(keys, idMap, typeNameMap, mMap);
518 auto rc = CreateResConfig();
519 if (rc == nullptr) {
520 EXPECT_TRUE(false);
521 return;
522 }
523 rc->SetDeviceType(DEVICE_TV);
524 rc->SetColorMode(COLOR_MODE_NOT_SET);
525 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
526 rm->UpdateResConfig(*rc);
527 delete rc;
528
529 int density = 120;
530 int id = GetResId("icon", ResType::MEDIA);
531 EXPECT_TRUE(id > 0);
532 std::string type;
533 size_t len;
534 std::unique_ptr<uint8_t[]> jsonBuf;
535 RState state = rm->GetDrawableInfoById(id, type, len, jsonBuf, density);
536 EXPECT_TRUE(state == SUCCESS);
537 EXPECT_TRUE(jsonBuf != nullptr);
538
539 // invalid density
540 int invalidDensity = 1000;
541 state = rm->GetDrawableInfoById(id, type, len, jsonBuf, invalidDensity);
542 EXPECT_TRUE(state == ERROR_CODE_INVALID_INPUT_PARAMETER);
543 }
544
TestGetDrawableInfoByName(HapResourceV1 * tmp)545 void ResourceManagerTestCommon::TestGetDrawableInfoByName(HapResourceV1 *tmp)
546 {
547 tmp->Init(this->defaultResConfig);
548 std::string type;
549 size_t len;
550 std::unique_ptr<uint8_t[]> jsonBuf;
551 RState state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf);
552 EXPECT_TRUE(state == SUCCESS);
553 EXPECT_TRUE(jsonBuf != nullptr);
554 }
555
TestGetDrawableInfoByName(HapResourceV2 * tmp)556 void ResourceManagerTestCommon::TestGetDrawableInfoByName(HapResourceV2 *tmp)
557 {
558 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
559 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
560 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
561 std::shared_ptr<MmapFile> mMap;
562 tmp->Init(keys, idMap, typeNameMap, mMap);
563 std::string type;
564 size_t len;
565 std::unique_ptr<uint8_t[]> jsonBuf;
566 RState state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf);
567 EXPECT_TRUE(state == SUCCESS);
568 EXPECT_TRUE(jsonBuf != nullptr);
569 }
570
TestGetDrawableInfoWithDensityByName(HapResourceV1 * tmp)571 void ResourceManagerTestCommon::TestGetDrawableInfoWithDensityByName(HapResourceV1 *tmp)
572 {
573 tmp->Init(this->defaultResConfig);
574 auto rc = CreateResConfig();
575 if (rc == nullptr) {
576 EXPECT_TRUE(false);
577 return;
578 }
579 rc->SetDeviceType(DEVICE_PHONE);
580 rc->SetColorMode(COLOR_MODE_NOT_SET);
581 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
582 rm->UpdateResConfig(*rc);
583 delete rc;
584
585 uint32_t density = 120;
586 std::string type;
587 size_t len;
588 std::unique_ptr<uint8_t[]> jsonBuf;
589 RState state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf, density);
590 EXPECT_TRUE(state == SUCCESS);
591 EXPECT_TRUE(jsonBuf != nullptr);
592
593 // invalid density
594 int invalidDensity = 1000;
595 state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf, invalidDensity);
596 EXPECT_TRUE(state == ERROR_CODE_INVALID_INPUT_PARAMETER);
597 }
598
TestGetDrawableInfoWithDensityByName(HapResourceV2 * tmp)599 void ResourceManagerTestCommon::TestGetDrawableInfoWithDensityByName(HapResourceV2 *tmp)
600 {
601 std::unordered_map<uint32_t, std::shared_ptr<ResConfigImpl>> keys;
602 std::unordered_map<uint32_t, std::shared_ptr<IdValuesV2>> idMap;
603 std::unordered_map<uint32_t, std::unordered_map<std::string, std::shared_ptr<IdValuesV2>>> typeNameMap;
604 std::shared_ptr<MmapFile> mMap;
605 tmp->Init(keys, idMap, typeNameMap, mMap);
606 auto rc = CreateResConfig();
607 if (rc == nullptr) {
608 EXPECT_TRUE(false);
609 return;
610 }
611 rc->SetDeviceType(DEVICE_PHONE);
612 rc->SetColorMode(COLOR_MODE_NOT_SET);
613 rc->SetScreenDensity(SCREEN_DENSITY_NOT_SET);
614 rm->UpdateResConfig(*rc);
615 delete rc;
616
617 uint32_t density = 120;
618 std::string type;
619 size_t len;
620 std::unique_ptr<uint8_t[]> jsonBuf;
621 RState state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf, density);
622 EXPECT_TRUE(state == SUCCESS);
623 EXPECT_TRUE(jsonBuf != nullptr);
624
625 // invalid density
626 int invalidDensity = 1000;
627 state = rm->GetDrawableInfoByName("icon1", type, len, jsonBuf, invalidDensity);
628 EXPECT_TRUE(state == ERROR_CODE_INVALID_INPUT_PARAMETER);
629 }
630
TestGetStringFormatById(const char * name,const char * cmp)631 void ResourceManagerTestCommon::TestGetStringFormatById(const char *name, const char *cmp)
632 {
633 int id = GetResId(name, ResType::STRING);
634 ASSERT_TRUE(id > 0);
635 std::string outValue;
636 RState state = rm->GetStringFormatById(outValue, id, 101); // 101 means the format number
637 ASSERT_EQ(SUCCESS, state);
638 ASSERT_EQ(cmp, outValue);
639 }
640
TestGetStringFormatByName(const char * name,const char * cmp)641 void ResourceManagerTestCommon::TestGetStringFormatByName(const char *name, const char *cmp)
642 {
643 std::string outValue;
644 RState state = rm->GetStringFormatByName(outValue, name, 101); // 101 means the format number
645 ASSERT_EQ(SUCCESS, state);
646 ASSERT_EQ(cmp, outValue);
647 }
648
TestGetStringArrayById(const char * name)649 void ResourceManagerTestCommon::TestGetStringArrayById(const char *name)
650 {
651 std::vector<std::string> outValue;
652 int id = GetResId(name, ResType::STRINGARRAY);
653 RState state = rm->GetStringArrayById(id, outValue);
654 ASSERT_EQ(SUCCESS, state);
655 ASSERT_EQ(static_cast<size_t>(4), outValue.size()); // 4 means the size of string array resource
656 PrintVectorString(outValue);
657 }
658
TestGetStringFormatById(const char * name,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams,const char * cmp)659 void ResourceManagerTestCommon::TestGetStringFormatById(const char *name,
660 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams, const char *cmp)
661 {
662 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
663 ASSERT_TRUE(ret);
664 uint32_t id = GetResId(name, ResType::STRING);
665 ASSERT_TRUE(id > 0);
666 std::string outValue;
667 RState state = rm->GetStringFormatById(id, outValue, jsParams);
668 ASSERT_EQ(SUCCESS, state);
669 ASSERT_EQ(cmp, outValue);
670 }
671
TestGetStringFormatByIdWithVaArgs(const char * name,const char * cmp,...)672 void ResourceManagerTestCommon::TestGetStringFormatByIdWithVaArgs(const char *name, const char *cmp, ...)
673 {
674 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
675 ASSERT_TRUE(ret);
676 uint32_t id = GetResId(name, ResType::STRING);
677 ASSERT_TRUE(id > 0);
678 std::string outValue;
679 va_list args;
680 va_start(args, cmp);
681 RState state = rm->GetStringFormatById(outValue, id, args);
682 va_end(args);
683 ASSERT_EQ(SUCCESS, state);
684 ASSERT_EQ(cmp, outValue);
685 }
686
TestGetStringFormatByName(const char * name,std::vector<std::tuple<ResourceManager::NapiValueType,std::string>> & jsParams,const char * cmp)687 void ResourceManagerTestCommon::TestGetStringFormatByName(const char *name,
688 std::vector<std::tuple<ResourceManager::NapiValueType, std::string>> &jsParams, const char *cmp)
689 {
690 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
691 ASSERT_TRUE(ret);
692 std::string outValue;
693 RState state = rm->GetStringFormatByName(name, outValue, jsParams);
694 ASSERT_EQ(SUCCESS, state);
695 ASSERT_EQ(cmp, outValue);
696 }
697
TestGetStringFormatByNameWithVaArgs(const char * name,const char * cmp,...)698 void ResourceManagerTestCommon::TestGetStringFormatByNameWithVaArgs(const char *name, const char *cmp, ...)
699 {
700 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
701 ASSERT_TRUE(ret);
702 std::string outValue;
703 va_list args;
704 va_start(args, cmp);
705 RState state = rm->GetStringFormatByName(outValue, name, args);
706 va_end(args);
707 ASSERT_EQ(SUCCESS, state);
708 ASSERT_EQ(cmp, outValue);
709 }
710
TestGetStringArrayByName(const char * name)711 void ResourceManagerTestCommon::TestGetStringArrayByName(const char *name)
712 {
713 std::vector<std::string> outValue;
714 RState state = rm->GetStringArrayByName(name, outValue);
715 ASSERT_EQ(SUCCESS, state);
716 ASSERT_EQ(static_cast<size_t>(4), outValue.size()); // 4 means the size of string array resource
717 PrintVectorString(outValue);
718 }
719
TestGetPatternById(const char * name)720 void ResourceManagerTestCommon::TestGetPatternById(const char *name)
721 {
722 std::map<std::string, std::string> outValue;
723 int id = GetResId(name, ResType::PATTERN);
724 RState state = rm->GetPatternById(id, outValue);
725 ASSERT_EQ(SUCCESS, state);
726 ASSERT_EQ(static_cast<size_t>(3), outValue.size()); // 3 means the size of pattern resource
727 PrintMapString(outValue);
728 }
729
TestGetPatternByName(const char * name)730 void ResourceManagerTestCommon::TestGetPatternByName(const char *name)
731 {
732 std::map<std::string, std::string> outValue;
733 RState state = rm->GetPatternByName(name, outValue);
734 ASSERT_EQ(SUCCESS, state);
735 ASSERT_EQ(static_cast<size_t>(3), outValue.size()); // 3 means the size of pattern resource
736 PrintMapString(outValue);
737 }
738
TestGetThemeById(const char * name)739 void ResourceManagerTestCommon::TestGetThemeById(const char *name)
740 {
741 std::map<std::string, std::string> outValue;
742 int id = GetResId(name, ResType::THEME);
743 ASSERT_TRUE(id > 0);
744 RState state = rm->GetThemeById(id, outValue);
745 ASSERT_EQ(SUCCESS, state);
746 }
747
TestGetThemeByName(const char * appTheme,const char * testTheme)748 void ResourceManagerTestCommon::TestGetThemeByName(const char *appTheme, const char *testTheme)
749 {
750 std::map<std::string, std::string> outValue;
751 RState state = rm->GetThemeByName(appTheme, outValue);
752 ASSERT_EQ(SUCCESS, state);
753 PrintMapString(outValue);
754
755 state = rm->GetThemeByName(testTheme, outValue);
756 ASSERT_EQ(SUCCESS, state);
757 }
758
TestGetBooleanById(const char * boolean1,const char * booleanRef)759 void ResourceManagerTestCommon::TestGetBooleanById(const char* boolean1, const char* booleanRef)
760 {
761 bool outValue = true;
762 int id = GetResId(boolean1, ResType::BOOLEAN);
763 ASSERT_TRUE(id > 0);
764 RState state = rm->GetBooleanById(id, outValue);
765 ASSERT_EQ(SUCCESS, state);
766 EXPECT_TRUE(outValue);
767
768 id = GetResId(booleanRef, ResType::BOOLEAN);
769 ASSERT_TRUE(id > 0);
770 state = rm->GetBooleanById(id, outValue);
771 ASSERT_EQ(SUCCESS, state);
772 EXPECT_TRUE(outValue);
773 }
774
TestGetBooleanByName(const char * boolean1,const char * booleanRef)775 void ResourceManagerTestCommon::TestGetBooleanByName(const char* boolean1, const char* booleanRef)
776 {
777 bool outValue = true;
778 RState state = rm->GetBooleanByName(boolean1, outValue);
779 ASSERT_EQ(SUCCESS, state);
780 EXPECT_TRUE(outValue);
781
782 state = rm->GetBooleanByName(booleanRef, outValue);
783 ASSERT_EQ(SUCCESS, state);
784 EXPECT_TRUE(outValue);
785 }
786
TestGetIntegerById(const char * integer1,const char * integerRef)787 void ResourceManagerTestCommon::TestGetIntegerById(const char* integer1, const char* integerRef)
788 {
789 int outValue;
790 int id = GetResId(integer1, ResType::INTEGER);
791 ASSERT_TRUE(id > 0);
792 RState state = rm->GetIntegerById(id, outValue);
793 ASSERT_EQ(SUCCESS, state);
794 EXPECT_EQ(101, outValue); // 101 means the result of int resource
795
796 id = GetResId(integerRef, ResType::INTEGER);
797 ASSERT_TRUE(id > 0);
798 state = rm->GetIntegerById(id, outValue);
799 ASSERT_EQ(SUCCESS, state);
800 EXPECT_EQ(101, outValue); // 101 means the result of int resource
801 }
802
TestGetIntegerByName(const char * integer1,const char * integerRef)803 void ResourceManagerTestCommon::TestGetIntegerByName(const char* integer1, const char* integerRef)
804 {
805 int outValue;
806 RState state = rm->GetIntegerByName(integer1, outValue);
807 ASSERT_EQ(SUCCESS, state);
808 EXPECT_EQ(101, outValue); // 101 means the result of int resource
809
810 state = rm->GetIntegerByName(integerRef, outValue);
811 ASSERT_EQ(SUCCESS, state);
812 EXPECT_EQ(101, outValue); // 101 means the result of int resource
813 }
814
TestGetFloatById(const char * touchTarget,const char * floatRef)815 void ResourceManagerTestCommon::TestGetFloatById(const char* touchTarget, const char* floatRef)
816 {
817 float outValue;
818 int id = GetResId(touchTarget, ResType::FLOAT);
819 ASSERT_TRUE(id > 0);
820 RState state = rm->GetFloatById(id, outValue);
821 ASSERT_EQ(SUCCESS, state);
822 EXPECT_EQ(48, outValue); // 48vp
823
824 std::string unit;
825 state = rm->GetFloatById(id, outValue, unit);
826 ASSERT_EQ(SUCCESS, state);
827 EXPECT_EQ(48, outValue); // 48vp
828 EXPECT_EQ("vp", unit);
829
830 id = GetResId(floatRef, ResType::FLOAT);
831 ASSERT_TRUE(id > 0);
832 state = rm->GetFloatById(id, outValue);
833 ASSERT_EQ(SUCCESS, state);
834 EXPECT_EQ(707, outValue); // 707vp
835 }
836
TestGetFloatByName(const char * touchTarget,const char * floatRef)837 void ResourceManagerTestCommon::TestGetFloatByName(const char* touchTarget, const char* floatRef)
838 {
839 float outValue;
840 RState state = rm->GetFloatByName(touchTarget, outValue);
841 ASSERT_EQ(SUCCESS, state);
842 EXPECT_EQ(48, outValue); // 48vp
843
844 std::string unit;
845 state = rm->GetFloatByName(touchTarget, outValue, unit);
846 ASSERT_EQ(SUCCESS, state);
847 EXPECT_EQ(48, outValue); // 48vp
848 EXPECT_EQ("vp", unit);
849
850 state = rm->GetFloatByName(floatRef, outValue);
851 ASSERT_EQ(SUCCESS, state);
852 EXPECT_EQ(707, outValue); // 707vp
853 }
854
TestGetIntArrayById(const char * intarray1)855 void ResourceManagerTestCommon::TestGetIntArrayById(const char* intarray1)
856 {
857 std::vector<int> outValue;
858 int id = GetResId(intarray1, ResType::INTARRAY);
859 EXPECT_TRUE(id > 0);
860 RState state = rm->GetIntArrayById(id, outValue);
861 EXPECT_TRUE(state == SUCCESS);
862 EXPECT_EQ(static_cast<uint32_t>(3), outValue.size()); // 3 means the size of int array resource
863 EXPECT_EQ(100, outValue[0]); // 100 means the first value of int array
864 EXPECT_EQ(200, outValue[1]); // 200 means the second value of int array
865 EXPECT_EQ(101, outValue[2]); // 101 means the third value of int array
866 }
867
TestGetIntArrayByName(const char * intarray1)868 void ResourceManagerTestCommon::TestGetIntArrayByName(const char* intarray1)
869 {
870 std::vector<int> outValue;
871 RState state = rm->GetIntArrayByName(intarray1, outValue);
872 EXPECT_TRUE(state == SUCCESS);
873 EXPECT_EQ(static_cast<uint32_t>(3), outValue.size()); // 3 means the size of int array resource
874 EXPECT_EQ(100, outValue[0]); // 100 means the first value of int array
875 EXPECT_EQ(200, outValue[1]); // 200 means the second value of int array
876 EXPECT_EQ(101, outValue[2]); // 101 means the third value of int array
877 }
878
TestGetResourceLimitKeys(uint32_t expectedLimitKeys)879 void ResourceManagerTestCommon::TestGetResourceLimitKeys(uint32_t expectedLimitKeys)
880 {
881 uint32_t limitKeys = rm->GetResourceLimitKeys();
882 limitKeys &= expectedLimitKeys;
883 EXPECT_EQ(limitKeys, expectedLimitKeys);
884 }
885