1 /*
2 * Copyright (c) 2021-2022 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.h"
17
18 #include <climits>
19 #include <cstring>
20 #include <gtest/gtest.h>
21 #define private public
22
23 #include "res_config.h"
24 #include "resource_manager.h"
25 #include "resource_manager_impl.h"
26 #include "test_common.h"
27 #include "utils/errors.h"
28 #include "utils/string_utils.h"
29
30 using namespace OHOS::Global::Resource;
31 using namespace testing::ext;
32 namespace {
33 static const int NON_EXIST_ID = 1111;
34
35 static const char *g_nonExistName = "non_existent_name";
36 class ResourceManagerTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39
40 static void TearDownTestCase(void);
41
42 void SetUp();
43
44 void TearDown();
45
ResourceManagerTest()46 ResourceManagerTest() : rm(nullptr)
47 {}
48
~ResourceManagerTest()49 ~ResourceManagerTest()
50 {}
51
52 public:
53 ResourceManager *rm;
54
55 int GetResId(std::string name, ResType resType) const;
56
57 void TestStringByName(const char *name, const char *cmp) const;
58
59 void TestStringById(const char *name, const char *cmp) const;
60
61 void TestPluralStringById(int quantity, const char *cmp, bool format = false) const;
62
63 void TestPluralStringByName(int quantity, const char *cmp, bool format = false) const;
64
65 void AddResource(const char *language, const char *script, const char *region);
66 };
67
GetResId(std::string name,ResType resType) const68 int ResourceManagerTest::GetResId(std::string name, ResType resType) const
69 {
70 auto idv = ((ResourceManagerImpl *)rm)->hapManager_->GetResourceListByName(name.c_str(), resType);
71 if (idv == nullptr) {
72 return -1;
73 }
74
75 PrintIdValues(idv);
76 if (idv->GetLimitPathsConst().size() > 0) {
77 return idv->GetLimitPathsConst()[0]->GetIdItem()->id_;
78 }
79 return OBJ_NOT_FOUND;
80 }
81
TestStringByName(const char * name,const char * cmp) const82 void ResourceManagerTest::TestStringByName(const char *name, const char *cmp) const
83 {
84 RState rState;
85 std::string outValue;
86 rState = rm->GetStringByName(name, outValue);
87 ASSERT_EQ(SUCCESS, rState);
88 HILOG_DEBUG("%s : %s", name, outValue.c_str());
89 ASSERT_EQ(std::string(cmp), outValue);
90 }
91
TestStringById(const char * name,const char * cmp) const92 void ResourceManagerTest::TestStringById(const char *name, const char *cmp) const
93 {
94 RState rState;
95 std::string outValue;
96 int id = GetResId(name, ResType::STRING);
97 ASSERT_TRUE(id > 0);
98 rState = rm->GetStringById(id, outValue);
99 ASSERT_EQ(SUCCESS, rState);
100 ASSERT_EQ(std::string(cmp), outValue);
101 }
102
AddResource(const char * language,const char * script,const char * region)103 void ResourceManagerTest::AddResource(const char *language, const char *script, const char *region)
104 {
105 if (language != nullptr || region != nullptr) {
106 auto rc = CreateResConfig();
107 if (rc == nullptr) {
108 ASSERT_TRUE(false);
109 }
110 rc->SetLocaleInfo(language, script, region);
111 rm->UpdateResConfig(*rc);
112 delete rc;
113 }
114 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
115 ASSERT_TRUE(ret);
116 }
117
TestPluralStringById(int quantity,const char * cmp,bool format) const118 void ResourceManagerTest::TestPluralStringById(int quantity, const char *cmp, bool format) const
119 {
120 RState ret;
121 std::string outValue;
122 int id = GetResId("eat_apple", ResType::PLURALS);
123 if (format) {
124 ret = rm->GetPluralStringByIdFormat(outValue, id, quantity, quantity);
125 } else {
126 ret = rm->GetPluralStringById(id, quantity, outValue);
127 }
128
129 ASSERT_EQ(SUCCESS, ret);
130 ASSERT_EQ(std::string(cmp), outValue);
131 }
132
TestPluralStringByName(int quantity,const char * cmp,bool format) const133 void ResourceManagerTest::TestPluralStringByName(int quantity, const char *cmp, bool format) const
134 {
135 RState ret;
136 std::string outValue;
137 const char *name = "eat_apple";
138 if (format) {
139 ret = rm->GetPluralStringByNameFormat(outValue, name, quantity, quantity);
140 } else {
141 ret = rm->GetPluralStringByName(name, quantity, outValue);
142 }
143
144 ASSERT_EQ(SUCCESS, ret);
145 ASSERT_EQ(std::string(cmp), outValue);
146 }
147
SetUpTestCase(void)148 void ResourceManagerTest::SetUpTestCase(void)
149 {
150 // step 1: input testsuit setup step
151 g_logLevel = LOG_DEBUG;
152 }
153
TearDownTestCase(void)154 void ResourceManagerTest::TearDownTestCase(void)
155 {
156 // step 2: input testsuit teardown step
157 }
158
SetUp(void)159 void ResourceManagerTest::SetUp(void)
160 {
161 this->rm = CreateResourceManager();
162 }
163
TearDown(void)164 void ResourceManagerTest::TearDown(void)
165 {
166 delete this->rm;
167 }
168
169 /*
170 * @tc.name: ResourceManagerAddResourceTest001
171 * @tc.desc: Test AddResource function, file case.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(ResourceManagerTest, ResourceManagerAddResourceTest001, TestSize.Level1)
175 {
176 // success cases
177 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
178 ASSERT_TRUE(ret);
179 };
180
181 /*
182 * @tc.name: ResourceManagerAddResourceTest002
183 * @tc.desc: Test AddResource function, file case.
184 * @tc.type: FUNC
185 */
186 HWTEST_F(ResourceManagerTest, ResourceManagerAddResourceTest002, TestSize.Level1)
187 {
188 // error cases
189 // file not exist
190 bool ret = rm->AddResource("/data/test/do_not_exist.resources");
191 ASSERT_TRUE(!ret);
192 }
193
194 /*
195 * @tc.name: ResourceManagerAddResourceTest003
196 * @tc.desc: Test AddResource function, file case.
197 * @tc.type: FUNC
198 */
199 HWTEST_F(ResourceManagerTest, ResourceManagerAddResourceTest003, TestSize.Level1)
200 {
201 // error cases
202 // reload the same path
203 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
204 ASSERT_TRUE(ret);
205 ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
206 ASSERT_TRUE(!ret);
207 }
208
209 /*
210 * @tc.name: ResourceManagerUpdateResConfigTest001
211 * @tc.desc: Test UpdateResConfig function
212 * @tc.type: FUNC
213 */
214 HWTEST_F(ResourceManagerTest, ResourceManagerUpdateResConfigTest001, TestSize.Level1)
215 {
216 // success cases
217 RState state;
218 ResConfig *rc = CreateResConfig();
219 if (rc == nullptr) {
220 ASSERT_TRUE(false);
221 }
222 rc->SetLocaleInfo("en", nullptr, "US");
223 rc->SetDeviceType(DeviceType::DEVICE_CAR);
224 state = rm->UpdateResConfig(*rc);
225 delete rc;
226 EXPECT_EQ(SUCCESS, state);
227 }
228
229 /*
230 * @tc.name: ResourceManagerUpdateResConfigTest002
231 * @tc.desc: Test UpdateResConfig function
232 * @tc.type: FUNC
233 */
234 HWTEST_F(ResourceManagerTest, ResourceManagerUpdateResConfigTest002, TestSize.Level1)
235 {
236 // error cases
237 RState state;
238 ResConfig *rc = CreateResConfig();
239 if (rc == nullptr) {
240 ASSERT_TRUE(false);
241 }
242 state = rm->UpdateResConfig(*rc);
243 delete rc;
244 EXPECT_EQ(LOCALEINFO_IS_NULL, state);
245 }
246
247 /*
248 * @tc.name: ResourceManagerUpdateResConfigTest003
249 * @tc.desc: Test UpdateResConfig function
250 * @tc.type: FUNC
251 */
252 HWTEST_F(ResourceManagerTest, ResourceManagerUpdateResConfigTest003, TestSize.Level1)
253 {
254 // error cases
255 RState state;
256 ResConfig *rc = CreateResConfig();
257 if (rc == nullptr) {
258 ASSERT_TRUE(false);
259 }
260 rc->SetLocaleInfo(nullptr, nullptr, "US");
261 state = rm->UpdateResConfig(*rc);
262 delete rc;
263 EXPECT_EQ(LOCALEINFO_IS_NULL, state);
264 }
265
266 /*
267 * load a hap, defaultConfig set to en, then switch to zh
268 * @tc.name: ResourceManagerUpdateResConfigTest004
269 * @tc.desc: Test UpdateResConfig function
270 * @tc.type: FUNC
271 */
272 HWTEST_F(ResourceManagerTest, ResourceManagerUpdateResConfigTest004, TestSize.Level1)
273 {
274 // success case
275 bool ret = true;
276 RState state;
277 ResConfig *rc = CreateResConfig();
278 if (rc == nullptr) {
279 ASSERT_TRUE(false);
280 }
281 rc->SetLocaleInfo("en", nullptr, nullptr);
282 state = rm->UpdateResConfig(*rc);
283 EXPECT_EQ(SUCCESS, state);
284 ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
285 if (!ret) {
286 delete rc;
287 ASSERT_TRUE(false);
288 }
289 // update to another language, will trigger reload
290 // before reload:
291 TestStringByName("app_name", "App Name");
292
293 rc->SetLocaleInfo("zh", nullptr, nullptr);
294 state = rm->UpdateResConfig(*rc);
295 delete rc;
296 EXPECT_EQ(SUCCESS, state);
297 // after reload:
298 TestStringByName("app_name", "应用名称");
299 }
300
301 /*
302 * @tc.name: ResourceManagerUpdateResConfigTest005
303 * @tc.desc: Test UpdateResConfig function
304 * @tc.type: FUNC
305 */
306 HWTEST_F(ResourceManagerTest, ResourceManagerUpdateResConfigTest005, TestSize.Level1)
307 {
308 // error case
309 AddResource("zh", nullptr, nullptr);
310
311 // make a fake hapResource, then reload will fail
312 HapResource *hapResource = new HapResource("/data/test/non_exist", 0, nullptr, nullptr);
313 ((ResourceManagerImpl *)rm)->hapManager_->hapResources_.push_back(hapResource);
314 RState state;
315 ResConfig *rc = CreateResConfig();
316 if (rc == nullptr) {
317 ASSERT_TRUE(false);
318 }
319 rc->SetLocaleInfo("en", nullptr, "US");
320 state = rm->UpdateResConfig(*rc);
321 delete rc;
322 EXPECT_EQ(HAP_INIT_FAILED, state);
323 }
324
325 /*
326 * @tc.name: ResourceManagerGetResConfigTest001
327 * @tc.desc: Test GetResConfig function
328 * @tc.type: FUNC
329 */
330 HWTEST_F(ResourceManagerTest, ResourceManagerGetResConfigTest001, TestSize.Level1)
331 {
332 // success cases
333 ResConfigImpl rc;
334 rm->GetResConfig(rc);
335 EXPECT_EQ(nullptr, rc.GetLocaleInfo());
336 EXPECT_EQ(DIRECTION_NOT_SET, rc.GetDirection());
337 EXPECT_EQ(SCREEN_DENSITY_NOT_SET, rc.GetScreenDensity());
338 EXPECT_EQ(DEVICE_NOT_SET, rc.GetDeviceType());
339 }
340
341 /*
342 * @tc.name: ResourceManagerGetResConfigTest002
343 * @tc.desc: Test GetResConfig function
344 * @tc.type: FUNC
345 */
346 HWTEST_F(ResourceManagerTest, ResourceManagerGetResConfigTest002, TestSize.Level1)
347 {
348 // success cases
349 RState state;
350 {
351 ResConfig *rc = CreateResConfig();
352 if (rc == nullptr) {
353 ASSERT_TRUE(false);
354 }
355 rc->SetLocaleInfo("en", nullptr, "US");
356 rc->SetDeviceType(DeviceType::DEVICE_CAR);
357 state = rm->UpdateResConfig(*rc);
358 delete rc;
359 EXPECT_EQ(SUCCESS, state);
360 }
361
362 ResConfigImpl rc;
363 rm->GetResConfig(rc);
364 EXPECT_EQ("en", std::string(rc.GetLocaleInfo()->GetLanguage()));
365 EXPECT_EQ(DEVICE_CAR, rc.GetDeviceType());
366 }
367
368 /*
369 * @tc.name: ResourceManagerGetStringByIdTest001
370 * @tc.desc: Test GetStringById function
371 * @tc.type: FUNC
372 */
373 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByIdTest001, TestSize.Level1)
374 {
375 AddResource("en", nullptr, nullptr);
376
377 TestStringById("app_name", "App Name");
378
379 TestStringById("copyright_text", "XXXXXX All rights reserved. ©2011-2019");
380
381 TestStringById("string_ref", "XXXXXX All rights reserved. ©2011-2019");
382
383 TestStringById("string_ref2", "XXXXXX All rights reserved. ©2011-2019");
384 }
385
386 /*
387 * @tc.name: ResourceManagerGetStringByIdTest002
388 * @tc.desc: Test GetStringById function
389 * @tc.type: FUNC
390 */
391 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByIdTest002, TestSize.Level1)
392 {
393 AddResource("zh", nullptr, nullptr);
394
395 TestStringById("app_name", "应用名称");
396
397 TestStringById("copyright_text", "版权所有 ©2011-2019 XXXX有限公司保留一切权利");
398
399 TestStringById("string_ref", "$aaaaa");
400
401 TestStringById("string_ref2", "$aaaaa");
402 }
403
404 /*
405 * @tc.name: ResourceManagerGetStringByIdTest003
406 * @tc.desc: Test GetStringById function
407 * @tc.type: FUNC
408 */
409 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByIdTest003, TestSize.Level1)
410 {
411 AddResource("zh", nullptr, nullptr);
412
413 std::string outValue;
414 RState state = rm->GetStringById(NON_EXIST_ID, outValue);
415 ASSERT_EQ(NOT_FOUND, state);
416 }
417
418 /*
419 * @tc.name: ResourceManagerGetStringByNameTest001
420 * @tc.desc: Test GetStringByName function
421 * @tc.type: FUNC
422 */
423 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByNameTest001, TestSize.Level1)
424 {
425 AddResource("en", nullptr, nullptr);
426
427 TestStringByName("app_name", "App Name");
428
429 TestStringByName("copyright_text", "XXXXXX All rights reserved. ©2011-2019");
430
431 TestStringByName("string_ref", "XXXXXX All rights reserved. ©2011-2019");
432
433 TestStringByName("string_ref2", "XXXXXX All rights reserved. ©2011-2019");
434 }
435
436 /*
437 * @tc.name: ResourceManagerGetStringByNameTest002
438 * @tc.desc: Test GetStringByName function
439 * @tc.type: FUNC
440 */
441 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByNameTest002, TestSize.Level1)
442 {
443 AddResource("zh", nullptr, nullptr);
444
445 TestStringByName("app_name", "应用名称");
446
447 TestStringByName("copyright_text", "版权所有 ©2011-2019 XXXX有限公司保留一切权利");
448
449 TestStringByName("string_ref", "$aaaaa");
450
451 TestStringByName("string_ref2", "$aaaaa");
452 }
453
454 /*
455 * @tc.name: ResourceManagerGetStringByNameTest003
456 * @tc.desc: Test GetStringByName function
457 * @tc.type: FUNC
458 */
459 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringByNameTest003, TestSize.Level1)
460 {
461 AddResource("zh", nullptr, nullptr);
462
463 std::string outValue;
464 RState state = rm->GetStringByName(g_nonExistName, outValue);
465 ASSERT_EQ(NOT_FOUND, state);
466 }
467
468 /*
469 * @tc.name: ResourceManagerGetStringFormatByIdTest001
470 * @tc.desc: Test GetStringFormatById function
471 * @tc.type: FUNC
472 */
473 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringFormatByIdTest001, TestSize.Level1)
474 {
475 AddResource("zh", nullptr, nullptr);
476
477 const char *name = "app_name";
478 int id = GetResId(name, ResType::STRING);
479 ASSERT_TRUE(id > 0);
480 std::string outValue;
481 RState state = rm->GetStringFormatById(outValue, id, 101);
482 ASSERT_EQ(SUCCESS, state);
483 ASSERT_EQ("应用名称", outValue);
484 }
485
486 /*
487 * @tc.name: ResourceManagerGetStringFormatByIdTest002
488 * @tc.desc: Test GetStringFormatById function
489 * @tc.type: FUNC
490 */
491 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringFormatByIdTest002, TestSize.Level1)
492 {
493 AddResource("zh", nullptr, nullptr);
494
495 std::string outValue;
496 RState state = rm->GetStringFormatById(outValue, NON_EXIST_ID, 101);
497 ASSERT_EQ(NOT_FOUND, state);
498 }
499
500 /*
501 * @tc.name: ResourceManagerGetStringFormatByNameTest001
502 * @tc.desc: Test GetStringFormatByName function
503 * @tc.type: FUNC
504 */
505 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringFormatByNameTest001, TestSize.Level1)
506 {
507 AddResource("zh", nullptr, nullptr);
508
509 const char *name = "app_name";
510 std::string outValue;
511 RState state = rm->GetStringFormatByName(outValue, name, 101);
512 ASSERT_EQ(SUCCESS, state);
513 ASSERT_EQ("应用名称", outValue);
514 }
515
516 /*
517 * @tc.name: ResourceManagerGetStringFormatByNameTest002
518 * @tc.desc: Test GetStringFormatByName function
519 * @tc.type: FUNC
520 */
521 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringFormatByNameTest002, TestSize.Level1)
522 {
523 AddResource("zh", nullptr, nullptr);
524
525 std::string outValue;
526 RState state = rm->GetStringFormatByName(outValue, g_nonExistName, 101);
527 ASSERT_EQ(NOT_FOUND, state);
528 }
529
530 /*
531 * @tc.name: ResourceManagerGetStringArrayByIdTest001
532 * @tc.desc: Test GetStringArrayById function, file case.
533 * @tc.type: FUNC
534 */
535 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringArrayByIdTest001, TestSize.Level1)
536 {
537 AddResource("zh", nullptr, nullptr);
538
539 int id;
540 std::vector<std::string> outValue;
541 RState state;
542
543 id = GetResId("size", ResType::STRINGARRAY);
544 state = rm->GetStringArrayById(id, outValue);
545 ASSERT_EQ(SUCCESS, state);
546 ASSERT_EQ(static_cast<size_t>(4), outValue.size());
547 PrintVectorString(outValue);
548
549 // by name
550 state = rm->GetStringArrayByName("size", outValue);
551 ASSERT_EQ(SUCCESS, state);
552 ASSERT_EQ(static_cast<size_t>(4), outValue.size());
553 PrintVectorString(outValue);
554 }
555
556 /*
557 * @tc.name: ResourceManagerGetStringArrayByIdTest002
558 * @tc.desc: Test GetStringArrayById function, file case.
559 * @tc.type: FUNC
560 */
561 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringArrayByIdTest002, TestSize.Level1)
562 {
563 AddResource("zh", nullptr, nullptr);
564
565 RState state;
566 // error case
567 // not found case
568 std::vector<std::string> outValue;
569 state = rm->GetStringArrayById(NON_EXIST_ID, outValue);
570 ASSERT_EQ(NOT_FOUND, state);
571 }
572
573 /*
574 * @tc.name: ResourceManagerGetStringArrayByNameTest001
575 * @tc.desc: Test GetStringArrayByName function, file case.
576 * @tc.type: FUNC
577 */
578 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringArrayByNameTest001, TestSize.Level1)
579 {
580 AddResource("zh", nullptr, nullptr);
581
582 std::vector<std::string> outValue;
583 RState state;
584
585 // by name
586 state = rm->GetStringArrayByName("size", outValue);
587 ASSERT_EQ(SUCCESS, state);
588 ASSERT_EQ(static_cast<size_t>(4), outValue.size());
589 PrintVectorString(outValue);
590 }
591
592 /*
593 * @tc.name: ResourceManagerGetStringArrayByNameTest002
594 * @tc.desc: Test GetStringArrayByName function, file case.
595 * @tc.type: FUNC
596 */
597 HWTEST_F(ResourceManagerTest, ResourceManagerGetStringArrayByNameTest002, TestSize.Level1)
598 {
599 AddResource("zh", nullptr, nullptr);
600
601 RState state;
602 // error case
603 // not found case
604 std::vector<std::string> outValue;
605 state = rm->GetStringArrayByName(g_nonExistName, outValue);
606 ASSERT_EQ(NOT_FOUND, state);
607 }
608
609 /*
610 * @tc.name: ResourceManagerGetPatternByIdTest001
611 * @tc.desc: Test GetPatternById function, file case.
612 * @tc.type: FUNC
613 */
614 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByIdTest001, TestSize.Level1)
615 {
616 AddResource("zh", nullptr, nullptr);
617
618 int id;
619 std::map<std::string, std::string> outValue;
620 RState state;
621
622 id = GetResId("base", ResType::PATTERN);
623 ASSERT_TRUE(id > 0);
624 state = rm->GetPatternById(id, outValue);
625 ASSERT_EQ(SUCCESS, state);
626 ASSERT_EQ(static_cast<size_t>(3), outValue.size());
627 PrintMapString(outValue);
628 }
629
630 /*
631 * @tc.name: ResourceManagerGetPatternByIdTest002
632 * @tc.desc: Test GetPatternById function, file case.
633 * @tc.type: FUNC
634 */
635 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByIdTest002, TestSize.Level1)
636 {
637 AddResource("zh", nullptr, nullptr);
638
639 int id;
640 std::map<std::string, std::string> outValue;
641 RState state;
642
643 id = GetResId("child", ResType::PATTERN);
644 ASSERT_TRUE(id > 0);
645 state = rm->GetPatternById(id, outValue);
646 ASSERT_EQ(SUCCESS, state);
647 ASSERT_EQ(static_cast<size_t>(4), outValue.size());
648 PrintMapString(outValue);
649 }
650
651 /*
652 * @tc.name: ResourceManagerGetPatternByIdTest003
653 * @tc.desc: Test GetPatternById function, file case.
654 * @tc.type: FUNC
655 */
656 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByIdTest003, TestSize.Level1)
657 {
658 AddResource("zh", nullptr, nullptr);
659
660 int id;
661 std::map<std::string, std::string> outValue;
662 RState state;
663
664 id = GetResId("ccchild", ResType::PATTERN);
665 ASSERT_TRUE(id > 0);
666 state = rm->GetPatternById(id, outValue);
667 ASSERT_EQ(SUCCESS, state);
668 ASSERT_EQ(static_cast<size_t>(5), outValue.size());
669 PrintMapString(outValue);
670 }
671
672 /*
673 * @tc.name: ResourceManagerGetPatternByIdTest004
674 * @tc.desc: Test GetPatternById function, file case.
675 * @tc.type: FUNC
676 */
677 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByIdTest004, TestSize.Level1)
678 {
679 AddResource("zh", nullptr, nullptr);
680
681 std::map<std::string, std::string> outValue;
682 RState state;
683
684 // not found case
685 state = rm->GetPatternById(NON_EXIST_ID, outValue);
686 ASSERT_EQ(NOT_FOUND, state);
687 }
688
689 /*
690 * @tc.name: ResourceManagerGetPatternByNameTest001
691 * @tc.desc: Test GetPatternByName function, file case.
692 * @tc.type: FUNC
693 */
694 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByNameTest001, TestSize.Level1)
695 {
696 AddResource("zh", nullptr, nullptr);
697
698 std::map<std::string, std::string> outValue;
699 RState state;
700
701 state = rm->GetPatternByName("base", outValue);
702 ASSERT_EQ(SUCCESS, state);
703 ASSERT_EQ(static_cast<size_t>(3), outValue.size());
704 PrintMapString(outValue);
705 }
706
707 /*
708 * @tc.name: ResourceManagerGetPatternByNameTest002
709 * @tc.desc: Test GetPatternByName function, file case.
710 * @tc.type: FUNC
711 */
712 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByNameTest002, TestSize.Level1)
713 {
714 AddResource("zh", nullptr, nullptr);
715
716 std::map<std::string, std::string> outValue;
717 RState state;
718
719 state = rm->GetPatternByName("child", outValue);
720 ASSERT_EQ(SUCCESS, state);
721 ASSERT_EQ(static_cast<size_t>(4), outValue.size());
722 PrintMapString(outValue);
723 }
724
725 /*
726 * @tc.name: ResourceManagerGetPatternByNameTest003
727 * @tc.desc: Test GetPatternByName function, file case.
728 * @tc.type: FUNC
729 */
730 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByNameTest003, TestSize.Level1)
731 {
732 AddResource("zh", nullptr, nullptr);
733
734 std::map<std::string, std::string> outValue;
735 RState state;
736
737 state = rm->GetPatternByName("ccchild", outValue);
738 ASSERT_EQ(SUCCESS, state);
739 ASSERT_EQ(static_cast<size_t>(5), outValue.size());
740 PrintMapString(outValue);
741 }
742
743 /*
744 * @tc.name: ResourceManagerGetPatternByNameTest004
745 * @tc.desc: Test GetPatternByName function, file case.
746 * @tc.type: FUNC
747 */
748 HWTEST_F(ResourceManagerTest, ResourceManagerGetPatternByNameTest004, TestSize.Level1)
749 {
750 AddResource("zh", nullptr, nullptr);
751
752 std::map<std::string, std::string> outValue;
753 RState state;
754
755 // not found case
756 state = rm->GetPatternByName(g_nonExistName, outValue);
757 ASSERT_EQ(NOT_FOUND, state);
758 }
759
760 /*
761 * @tc.name: ResourceManagerGetPluralStringByIdTest001
762 * @tc.desc: Test GetPluralStringById function, file case.
763 * @tc.type: FUNC
764 */
765 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdTest001, TestSize.Level1)
766 {
767 AddResource("en", nullptr, "US");
768
769 int quantity = 1;
770 TestPluralStringById(quantity, "%d apple", false);
771
772 quantity = 101;
773 TestPluralStringById(quantity, "%d apples", false);
774 }
775
776 /*
777 * @tc.name: ResourceManagerGetPluralStringByIdTest002
778 * @tc.desc: Test GetPluralStringById function, file case.
779 * @tc.type: FUNC
780 */
781 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdTest002, TestSize.Level1)
782 {
783 AddResource("zh", nullptr, "CN");
784
785 int quantity = 1;
786 TestPluralStringById(quantity, "%d apples", false);
787
788 quantity = 101;
789 TestPluralStringById(quantity, "%d apples", false);
790 }
791
792 /*
793 * @tc.name: ResourceManagerGetPluralStringByIdTest003
794 * @tc.desc: Test GetPluralStringById function, file case.
795 * @tc.type: FUNC
796 */
797 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdTest003, TestSize.Level1)
798 {
799 AddResource("pl", nullptr, "PL");
800
801 int quantity = 1;
802 TestPluralStringById(quantity, "1 jabłko");
803
804 quantity = 2;
805 TestPluralStringById(quantity, "%d jabłka");
806
807 quantity = 23;
808 TestPluralStringById(quantity, "%d jabłka");
809
810 quantity = 12;
811 TestPluralStringById(quantity, "%d jabłek");
812 }
813
814 /*
815 * @tc.name: ResourceManagerGetPluralStringByIdTest004
816 * @tc.desc: Test GetPluralStringById function, file case.
817 * @tc.type: FUNC
818 */
819 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdTest004, TestSize.Level1)
820 {
821 AddResource("ar", nullptr, "SA");
822
823 int quantity = 0;
824 TestPluralStringById(quantity, "zero-0");
825 quantity = 1;
826 TestPluralStringById(quantity, "one-1");
827 quantity = 2;
828 TestPluralStringById(quantity, "two-2");
829 quantity = 5;
830 TestPluralStringById(quantity, "few-%d");
831 quantity = 12;
832 TestPluralStringById(quantity, "many-%d");
833 quantity = 500;
834 TestPluralStringById(quantity, "other-%d");
835 }
836
837 /*
838 * @tc.name: ResourceManagerGetPluralStringByIdTest005
839 * @tc.desc: Test GetPluralStringById function, file case.
840 * @tc.type: FUNC
841 */
842 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdTest005, TestSize.Level1)
843 {
844 AddResource("ar", nullptr, "SA");
845
846 RState state;
847 std::string outValue;
848 state = rm->GetPluralStringById(NON_EXIST_ID, 1, outValue);
849 ASSERT_EQ(NOT_FOUND, state);
850 }
851
852 /*
853 * @tc.name: ResourceManagerGetPluralStringByNameTest001
854 * @tc.desc: Test GetPluralStringByName function, file case.
855 * @tc.type: FUNC
856 */
857 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByNameTest001, TestSize.Level1)
858 {
859 AddResource("en", nullptr, "US");
860
861 int quantity = 1;
862 TestPluralStringByName(quantity, "%d apple", false);
863
864 quantity = 101;
865 TestPluralStringByName(quantity, "%d apples", false);
866 }
867
868 /*
869 * @tc.name: ResourceManagerGetPluralStringByNameTest002
870 * @tc.desc: Test GetPluralStringByName function, file case.
871 * @tc.type: FUNC
872 */
873 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByNameTest002, TestSize.Level1)
874 {
875 AddResource("ar", nullptr, "SA");
876
877 RState state;
878 std::string outValue;
879 state = rm->GetPluralStringByName(g_nonExistName, 1, outValue);
880 ASSERT_EQ(NOT_FOUND, state);
881 }
882
883 /*
884 * @tc.name: ResourceManagerGetPluralStringByIdFormatTest001
885 * @tc.desc: Test GetPluralStringByIdFormat function, file case.
886 * @tc.type: FUNC
887 */
888 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdFormatTest001, TestSize.Level1)
889 {
890 AddResource("zh", nullptr, "CN");
891
892 int quantity = 1;
893 TestPluralStringById(quantity, "1 apples", true);
894
895 quantity = 101;
896 TestPluralStringById(quantity, "101 apples", true);
897 }
898
899 /*
900 * @tc.name: ResourceManagerGetPluralStringByIdFormatTest002
901 * @tc.desc: Test GetPluralStringByIdFormat function, file case.
902 * @tc.type: FUNC
903 */
904 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByIdFormatTest002, TestSize.Level1)
905 {
906 AddResource("ar", nullptr, "SA");
907
908 RState state;
909 std::string outValue;
910 state = rm->GetPluralStringByIdFormat(outValue, NON_EXIST_ID, 1, 1);
911 ASSERT_EQ(NOT_FOUND, state);
912 }
913
914 /*
915 * @tc.name: ResourceManagerGetPluralStringByNameFormatTest001
916 * @tc.desc: Test GetPluralStringByNameFormat function, file case.
917 * @tc.type: FUNC
918 */
919 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByNameFormatTest001, TestSize.Level1)
920 {
921 AddResource("zh", nullptr, "CN");
922
923 int quantity = 1;
924 TestPluralStringByName(quantity, "1 apples", true);
925
926 quantity = 101;
927 TestPluralStringByName(quantity, "101 apples", true);
928 }
929
930 /*
931 * @tc.name: ResourceManagerGetPluralStringByNameFormatTest002
932 * @tc.desc: Test GetPluralStringByNameFormat function, file case.
933 * @tc.type: FUNC
934 */
935 HWTEST_F(ResourceManagerTest, ResourceManagerGetPluralStringByNameFormatTest002, TestSize.Level1)
936 {
937 AddResource("ar", nullptr, "SA");
938
939 RState state;
940 std::string outValue;
941 state = rm->GetPluralStringByNameFormat(outValue, g_nonExistName, 1, 1);
942 ASSERT_EQ(NOT_FOUND, state);
943 }
944
945 /*
946 * @tc.name: ResourceManagerGetThemeByIdTest001
947 * @tc.desc: Test GetThemeById
948 * @tc.type: FUNC
949 */
950 HWTEST_F(ResourceManagerTest, ResourceManagerGetThemeByIdTest001, TestSize.Level1)
951 {
952 AddResource("zh", nullptr, "CN");
953
954 std::map<std::string, std::string> outValue;
955 RState state;
956 int id = GetResId("app_theme", ResType::THEME);
957 ASSERT_TRUE(id > 0);
958 state = rm->GetThemeById(id, outValue);
959 ASSERT_EQ(SUCCESS, state);
960 PrintMapString(outValue);
961 }
962
963 /*
964 * @tc.name: ResourceManagerGetThemeByIdTest002
965 * @tc.desc: Test GetThemeById
966 * @tc.type: FUNC
967 */
968 HWTEST_F(ResourceManagerTest, ResourceManagerGetThemeByIdTest002, TestSize.Level1)
969 {
970 AddResource("zh", nullptr, "CN");
971
972 std::map<std::string, std::string> outValue;
973 RState state;
974 state = rm->GetThemeById(NON_EXIST_ID, outValue);
975 ASSERT_EQ(NOT_FOUND, state);
976 }
977
978 /*
979 * @tc.name: ResourceManagerGetThemeByNameTest001
980 * @tc.desc: Test GetThemeByName
981 * @tc.type: FUNC
982 */
983 HWTEST_F(ResourceManagerTest, ResourceManagerGetThemeByNameTest001, TestSize.Level1)
984 {
985 AddResource("zh", nullptr, "CN");
986
987 std::map<std::string, std::string> outValue;
988 RState state;
989 state = rm->GetThemeByName("app_theme", outValue);
990 ASSERT_EQ(SUCCESS, state);
991 PrintMapString(outValue);
992
993 state = rm->GetThemeByName("test_theme", outValue);
994 ASSERT_EQ(SUCCESS, state);
995 PrintMapString(outValue);
996 }
997
998 /*
999 * @tc.name: ResourceManagerGetThemeByNameTest002
1000 * @tc.desc: Test GetThemeByName
1001 * @tc.type: FUNC
1002 */
1003 HWTEST_F(ResourceManagerTest, ResourceManagerGetThemeByNameTest002, TestSize.Level1)
1004 {
1005 AddResource("zh", nullptr, "CN");
1006
1007 std::map<std::string, std::string> outValue;
1008 RState state;
1009 state = rm->GetThemeByName(g_nonExistName, outValue);
1010 ASSERT_EQ(NOT_FOUND, state);
1011 }
1012
1013 /*
1014 * @tc.name: ResourceManagerGetBooleanByIdTest001
1015 * @tc.desc: Test GetBooleanById
1016 * @tc.type: FUNC
1017 */
1018 HWTEST_F(ResourceManagerTest, ResourceManagerGetBooleanByIdTest001, TestSize.Level1)
1019 {
1020 AddResource("zh", nullptr, "CN");
1021
1022 bool outValue = true;
1023 RState state;
1024 int id = GetResId("boolean_1", ResType::BOOLEAN);
1025 ASSERT_TRUE(id > 0);
1026 state = rm->GetBooleanById(id, outValue);
1027 ASSERT_EQ(SUCCESS, state);
1028 EXPECT_TRUE(outValue);
1029
1030 id = GetResId("boolean_ref", ResType::BOOLEAN);
1031 ASSERT_TRUE(id > 0);
1032 state = rm->GetBooleanById(id, outValue);
1033 ASSERT_EQ(SUCCESS, state);
1034 EXPECT_TRUE(outValue);
1035 }
1036
1037 /*
1038 * @tc.name: ResourceManagerGetBooleanByIdTest002
1039 * @tc.desc: Test GetBooleanById
1040 * @tc.type: FUNC
1041 */
1042 HWTEST_F(ResourceManagerTest, ResourceManagerGetBooleanByIdTest002, TestSize.Level1)
1043 {
1044 AddResource("zh", nullptr, "CN");
1045
1046 bool outValue = true;
1047 RState state;
1048 state = rm->GetBooleanById(NON_EXIST_ID, outValue);
1049 ASSERT_EQ(NOT_FOUND, state);
1050 }
1051
1052 /*
1053 * @tc.name: ResourceManagerGetBooleanByNameTest001
1054 * @tc.desc: Test GetBooleanByName
1055 * @tc.type: FUNC
1056 */
1057 HWTEST_F(ResourceManagerTest, ResourceManagerGetBooleanByNameTest001, TestSize.Level1)
1058 {
1059 AddResource("zh", nullptr, "CN");
1060
1061 bool outValue = true;
1062 RState state;
1063 state = rm->GetBooleanByName("boolean_1", outValue);
1064 ASSERT_EQ(SUCCESS, state);
1065 EXPECT_TRUE(outValue);
1066
1067 state = rm->GetBooleanByName("boolean_ref", outValue);
1068 ASSERT_EQ(SUCCESS, state);
1069 EXPECT_TRUE(outValue);
1070 }
1071
1072 /*
1073 * @tc.name: ResourceManagerGetBooleanByNameTest002
1074 * @tc.desc: Test GetBooleanByName
1075 * @tc.type: FUNC
1076 */
1077 HWTEST_F(ResourceManagerTest, ResourceManagerGetBooleanByNameTest002, TestSize.Level1)
1078 {
1079 AddResource("zh", nullptr, "CN");
1080
1081 bool outValue = true;
1082 RState state;
1083 state = rm->GetBooleanByName(g_nonExistName, outValue);
1084 ASSERT_EQ(NOT_FOUND, state);
1085 }
1086
1087 /*
1088 * @tc.name: ResourceManagerGetIntegerByIdTest001
1089 * @tc.desc: Test GetIntegerById
1090 * @tc.type: FUNC
1091 */
1092 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntegerByIdTest001, TestSize.Level1)
1093 {
1094 AddResource("zh", nullptr, "CN");
1095
1096 int outValue;
1097 RState state;
1098 int id = GetResId("integer_1", ResType::INTEGER);
1099 ASSERT_TRUE(id > 0);
1100 state = rm->GetIntegerById(id, outValue);
1101 ASSERT_EQ(SUCCESS, state);
1102 EXPECT_EQ(101, outValue);
1103
1104 id = GetResId("integer_ref", ResType::INTEGER);
1105 ASSERT_TRUE(id > 0);
1106 state = rm->GetIntegerById(id, outValue);
1107 ASSERT_EQ(SUCCESS, state);
1108 EXPECT_EQ(101, outValue);
1109 }
1110
1111 /*
1112 * @tc.name: ResourceManagerGetIntegerByIdTest002
1113 * @tc.desc: Test GetIntegerById
1114 * @tc.type: FUNC
1115 */
1116 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntegerByIdTest002, TestSize.Level1)
1117 {
1118 AddResource("zh", nullptr, "CN");
1119
1120 int outValue;
1121 RState state;
1122 state = rm->GetIntegerById(NON_EXIST_ID, outValue);
1123 ASSERT_EQ(NOT_FOUND, state);
1124 }
1125
1126 /*
1127 * @tc.name: ResourceManagerGetIntegerByNameTest001
1128 * @tc.desc: Test GetIntegerByName
1129 * @tc.type: FUNC
1130 */
1131 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntegerByNameTest001, TestSize.Level1)
1132 {
1133 AddResource("zh", nullptr, "CN");
1134
1135 int outValue;
1136 RState state;
1137 state = rm->GetIntegerByName("integer_1", outValue);
1138 ASSERT_EQ(SUCCESS, state);
1139 EXPECT_EQ(101, outValue);
1140
1141 state = rm->GetIntegerByName("integer_ref", outValue);
1142 ASSERT_EQ(SUCCESS, state);
1143 EXPECT_EQ(101, outValue);
1144 }
1145
1146 /*
1147 * @tc.name: ResourceManagerGetIntegerByNameTest002
1148 * @tc.desc: Test GetIntegerByName
1149 * @tc.type: FUNC
1150 */
1151 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntegerByNameTest002, TestSize.Level1)
1152 {
1153 AddResource("zh", nullptr, "CN");
1154
1155 int outValue;
1156 RState state;
1157 state = rm->GetIntegerByName(g_nonExistName, outValue);
1158 ASSERT_EQ(NOT_FOUND, state);
1159 }
1160
1161 /*
1162 * @tc.name: ResourceManagerGetFloatByIdTest001
1163 * @tc.desc: Test GetFloatById
1164 * @tc.type: FUNC
1165 */
1166 HWTEST_F(ResourceManagerTest, ResourceManagerGetFloatByIdTest001, TestSize.Level1)
1167 {
1168 AddResource("zh", nullptr, "CN");
1169
1170 float outValue;
1171 RState state;
1172 int id = GetResId("width_appBar_backButton_touchTarget", ResType::FLOAT);
1173 ASSERT_TRUE(id > 0);
1174 state = rm->GetFloatById(id, outValue);
1175 ASSERT_EQ(SUCCESS, state);
1176 EXPECT_EQ(48, outValue); // 50vp
1177
1178 id = GetResId("float_ref", ResType::FLOAT);
1179 ASSERT_TRUE(id > 0);
1180 state = rm->GetFloatById(id, outValue);
1181 ASSERT_EQ(SUCCESS, state);
1182 EXPECT_EQ(707, outValue); // 48vp
1183 }
1184
1185 /*
1186 * @tc.name: ResourceManagerGetFloatByIdTest002
1187 * @tc.desc: Test GetFloatById
1188 * @tc.type: FUNC
1189 */
1190 HWTEST_F(ResourceManagerTest, ResourceManagerGetFloatByIdTest002, TestSize.Level1)
1191 {
1192 AddResource("zh", nullptr, "CN");
1193
1194 float outValue;
1195 RState state;
1196 state = rm->GetFloatById(NON_EXIST_ID, outValue);
1197 ASSERT_EQ(NOT_FOUND, state);
1198 }
1199
1200 /*
1201 * @tc.name: ResourceManagerGetFloatByNameTest001
1202 * @tc.desc: Test GetFloatByName
1203 * @tc.type: FUNC
1204 */
1205 HWTEST_F(ResourceManagerTest, ResourceManagerGetFloatByNameTest001, TestSize.Level1)
1206 {
1207 AddResource("zh", nullptr, "CN");
1208
1209 float outValue;
1210 RState state;
1211 state = rm->GetFloatByName("width_appBar_backButton_touchTarget", outValue);
1212 ASSERT_EQ(SUCCESS, state);
1213 EXPECT_EQ(48, outValue); // 50vp
1214
1215 state = rm->GetFloatByName("float_ref", outValue);
1216 ASSERT_EQ(SUCCESS, state);
1217 EXPECT_EQ(707, outValue); // 48vp
1218 }
1219
1220 /*
1221 * @tc.name: ResourceManagerGetFloatByNameTest002
1222 * @tc.desc: Test GetFloatByName
1223 * @tc.type: FUNC
1224 */
1225 HWTEST_F(ResourceManagerTest, ResourceManagerGetFloatByNameTest002, TestSize.Level1)
1226 {
1227 AddResource("zh", nullptr, "CN");
1228
1229 float outValue;
1230 RState state;
1231 state = rm->GetFloatByName(g_nonExistName, outValue);
1232 ASSERT_EQ(NOT_FOUND, state);
1233 }
1234
1235 /*
1236 * @tc.name: ResourceManagerGetIntArrayByIdTest001
1237 * @tc.desc: Test GetIntArrayById
1238 * @tc.type: FUNC
1239 */
1240 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntArrayByIdTest001, TestSize.Level1)
1241 {
1242 AddResource("zh", nullptr, "CN");
1243
1244 std::vector<int> outValue;
1245 RState state;
1246 int id = GetResId("intarray_1", ResType::INTARRAY);
1247 EXPECT_TRUE(id > 0);
1248 state = rm->GetIntArrayById(id, outValue);
1249 EXPECT_TRUE(state == SUCCESS);
1250 EXPECT_EQ(static_cast<uint32_t>(3), outValue.size());
1251 EXPECT_EQ(100, outValue[0]);
1252 EXPECT_EQ(200, outValue[1]);
1253 EXPECT_EQ(101, outValue[2]);
1254 }
1255
1256 /*
1257 * @tc.name: ResourceManagerGetIntArrayByIdTest002
1258 * @tc.desc: Test GetIntArrayById
1259 * @tc.type: FUNC
1260 */
1261 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntArrayByIdTest002, TestSize.Level1)
1262 {
1263 AddResource("zh", nullptr, "CN");
1264
1265 std::vector<int> outValue;
1266 RState state;
1267 state = rm->GetIntArrayById(NON_EXIST_ID, outValue);
1268 ASSERT_EQ(NOT_FOUND, state);
1269 }
1270
1271 /*
1272 * @tc.name: ResourceManagerGetIntArrayByNameTest001
1273 * @tc.desc: Test GetIntArrayByName
1274 * @tc.type: FUNC
1275 */
1276 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntArrayByNameTest001, TestSize.Level1)
1277 {
1278 AddResource("zh", nullptr, "CN");
1279
1280 std::vector<int> outValue;
1281 RState state;
1282 state = rm->GetIntArrayByName("intarray_1", outValue);
1283 EXPECT_TRUE(state == SUCCESS);
1284 EXPECT_EQ(static_cast<uint32_t>(3), outValue.size());
1285 EXPECT_EQ(100, outValue[0]);
1286 EXPECT_EQ(200, outValue[1]);
1287 EXPECT_EQ(101, outValue[2]);
1288 }
1289
1290 /*
1291 * @tc.name: ResourceManagerGetIntArrayByNameTest002
1292 * @tc.desc: Test GetIntArrayByName
1293 * @tc.type: FUNC
1294 */
1295 HWTEST_F(ResourceManagerTest, ResourceManagerGetIntArrayByNameTest002, TestSize.Level1)
1296 {
1297 AddResource("zh", nullptr, "CN");
1298
1299 std::vector<int> outValue;
1300 RState state;
1301 state = rm->GetIntArrayByName(g_nonExistName, outValue);
1302 ASSERT_EQ(NOT_FOUND, state);
1303 }
1304
1305 /*
1306 * @tc.name: ResourceManagerGetColorByIdTest001
1307 * @tc.desc: Test GetColorById
1308 * @tc.type: FUNC
1309 */
1310 HWTEST_F(ResourceManagerTest, ResourceManagerGetColorByIdTest001, TestSize.Level1)
1311 {
1312 AddResource("zh", nullptr, "CN");
1313
1314 uint32_t outValue;
1315 RState state;
1316 int id = GetResId("divider_color", ResType::COLOR);
1317 EXPECT_TRUE(id > 0);
1318 state = rm->GetColorById(id, outValue);
1319 EXPECT_TRUE(state == SUCCESS);
1320 EXPECT_EQ(static_cast<uint32_t>(268435456), outValue); // #10000000
1321
1322 id = GetResId("color_aboutPage_title_primary", ResType::COLOR);
1323 EXPECT_TRUE(id > 0);
1324 state = rm->GetColorById(id, outValue);
1325 EXPECT_TRUE(state == SUCCESS);
1326 EXPECT_EQ(4279834905, outValue); // #191919
1327 }
1328
1329 /*
1330 * @tc.name: ResourceManagerGetColorByIdTest002
1331 * @tc.desc: Test GetColorById
1332 * @tc.type: FUNC
1333 */
1334 HWTEST_F(ResourceManagerTest, ResourceManagerGetColorByIdTest002, TestSize.Level1)
1335 {
1336 AddResource("zh", nullptr, "CN");
1337
1338 uint32_t outValue;
1339 RState state;
1340 state = rm->GetColorById(NON_EXIST_ID, outValue);
1341 ASSERT_EQ(NOT_FOUND, state);
1342 }
1343
1344 /*
1345 * @tc.name: ResourceManagerGetColorByNameTest001
1346 * @tc.desc: Test GetColorByName
1347 * @tc.type: FUNC
1348 */
1349 HWTEST_F(ResourceManagerTest, ResourceManagerGetColorByNameTest001, TestSize.Level1)
1350 {
1351 AddResource("zh", nullptr, "CN");
1352
1353 uint32_t outValue;
1354 RState state;
1355 state = rm->GetColorByName("divider_color", outValue);
1356 EXPECT_TRUE(state == SUCCESS);
1357 EXPECT_EQ(static_cast<uint32_t>(268435456), outValue); // #10000000
1358
1359 state = rm->GetColorByName("color_aboutPage_title_primary", outValue);
1360 EXPECT_TRUE(state == SUCCESS);
1361 EXPECT_EQ(4279834905, outValue); // #191919
1362 }
1363
1364 /*
1365 * @tc.name: ResourceManagerGetColorByNameTest002
1366 * @tc.desc: Test GetColorByName
1367 * @tc.type: FUNC
1368 */
1369 HWTEST_F(ResourceManagerTest, ResourceManagerGetColorByNameTest002, TestSize.Level1)
1370 {
1371 AddResource("zh", nullptr, "CN");
1372
1373 uint32_t outValue;
1374 RState state;
1375 state = rm->GetColorByName(g_nonExistName, outValue);
1376 ASSERT_EQ(NOT_FOUND, state);
1377 }
1378
1379 /*
1380 * @tc.name: ResourceManagerGetProfileByIdTest001
1381 * @tc.desc: Test GetProfileById
1382 * @tc.type: FUNC
1383 */
1384 HWTEST_F(ResourceManagerTest, ResourceManagerGetProfileByIdTest001, TestSize.Level1)
1385 {
1386 AddResource("zh", nullptr, "CN");
1387
1388 HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr);
1389 tmp->Init();
1390 std::string res = tmp->GetResourcePath();
1391 res.append("entry/resources/base/profile/test_common.h");
1392
1393 std::string outValue;
1394 RState state;
1395 int id = GetResId("test_common", ResType::PROF);
1396 EXPECT_TRUE(id > 0);
1397 state = rm->GetProfileById(id, outValue);
1398 EXPECT_TRUE(state == SUCCESS);
1399 EXPECT_EQ(res, outValue);
1400 delete tmp;
1401 }
1402
1403 /*
1404 * @tc.name: ResourceManagerGetProfileByIdTest002
1405 * @tc.desc: Test GetProfileById
1406 * @tc.type: FUNC
1407 */
1408 HWTEST_F(ResourceManagerTest, ResourceManagerGetProfileByIdTest002, TestSize.Level1)
1409 {
1410 AddResource("zh", nullptr, "CN");
1411
1412 std::string outValue;
1413 RState state;
1414 state = rm->GetProfileById(NON_EXIST_ID, outValue);
1415 ASSERT_EQ(NOT_FOUND, state);
1416 }
1417
1418 /*
1419 * @tc.name: ResourceManagerGetProfileByNameTest001
1420 * @tc.desc: Test GetProfileByName
1421 * @tc.type: FUNC
1422 */
1423 HWTEST_F(ResourceManagerTest, ResourceManagerGetProfileByNameTest001, TestSize.Level1)
1424 {
1425 AddResource("zh", nullptr, "CN");
1426
1427 HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr);
1428 tmp->Init();
1429 std::string res = tmp->GetResourcePath();
1430 res.append("entry/resources/base/profile/test_common.h");
1431
1432 std::string outValue;
1433 RState state;
1434 state = rm->GetProfileByName("test_common", outValue);
1435 EXPECT_TRUE(state == SUCCESS);
1436 EXPECT_EQ(res, outValue);
1437 delete tmp;
1438 }
1439
1440 /*
1441 * @tc.name: ResourceManagerGetProfileByNameTest002
1442 * @tc.desc: Test GetProfileByName
1443 * @tc.type: FUNC
1444 */
1445 HWTEST_F(ResourceManagerTest, ResourceManagerGetProfileByNameTest002, TestSize.Level1)
1446 {
1447 AddResource("zh", nullptr, "CN");
1448
1449 std::string outValue;
1450 RState state;
1451 state = rm->GetProfileByName(g_nonExistName, outValue);
1452 ASSERT_EQ(NOT_FOUND, state);
1453 }
1454
1455 /*
1456 * @tc.name: ResourceManagerGetMediaByIdTest001
1457 * @tc.desc: Test GetMediaById
1458 * @tc.type: FUNC
1459 */
1460 HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest001, TestSize.Level1)
1461 {
1462 AddResource("zh", nullptr, "CN");
1463
1464 HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr);
1465 tmp->Init();
1466 std::string res = tmp->GetResourcePath();
1467 res.append("entry/resources/base/media/icon.png");
1468
1469 std::string outValue;
1470 RState state;
1471 int id = GetResId("icon", ResType::MEDIA);
1472 EXPECT_TRUE(id > 0);
1473 state = rm->GetMediaById(id, outValue);
1474 EXPECT_TRUE(state == SUCCESS);
1475 EXPECT_EQ(res, outValue);
1476 delete tmp;
1477 }
1478
1479 /*
1480 * @tc.name: ResourceManagerGetMediaByIdTest002
1481 * @tc.desc: Test GetMediaById
1482 * @tc.type: FUNC
1483 */
1484 HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByIdTest002, TestSize.Level1)
1485 {
1486 AddResource("zh", nullptr, "CN");
1487
1488 std::string outValue;
1489 RState state;
1490 state = rm->GetMediaById(NON_EXIST_ID, outValue);
1491 ASSERT_EQ(NOT_FOUND, state);
1492 }
1493
1494 /*
1495 * @tc.name: ResourceManagerGetMediaByNameTest001
1496 * @tc.desc: Test GetMediaByName
1497 * @tc.type: FUNC
1498 */
1499 HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest001, TestSize.Level1)
1500 {
1501 AddResource("zh", nullptr, "CN");
1502
1503 HapResource *tmp = new HapResource(FormatFullPath(g_resFilePath).c_str(), 0, nullptr, nullptr);
1504 tmp->Init();
1505 std::string res = tmp->GetResourcePath();
1506 res.append("entry/resources/base/media/icon.png");
1507
1508 std::string outValue;
1509 RState state;
1510 state = rm->GetMediaByName("icon", outValue);
1511 EXPECT_TRUE(state == SUCCESS);
1512 EXPECT_EQ(res, outValue);
1513 delete tmp;
1514 }
1515
1516 /*
1517 * @tc.name: ResourceManagerGetMediaByNameTest002
1518 * @tc.desc: Test GetMediaByName
1519 * @tc.type: FUNC
1520 */
1521 HWTEST_F(ResourceManagerTest, ResourceManagerGetMediaByNameTest002, TestSize.Level1)
1522 {
1523 AddResource("zh", nullptr, "CN");
1524
1525 std::string outValue;
1526 RState state;
1527 state = rm->GetMediaByName(g_nonExistName, outValue);
1528 ASSERT_EQ(NOT_FOUND, state);
1529 }
1530
1531 /*
1532 * @tc.name: ResourceManagerResolveReferenceTest001
1533 * @tc.desc: Test ResolveReference function, file case.
1534 * @tc.type: FUNC
1535 */
1536 HWTEST_F(ResourceManagerTest, ResourceManagerResolveReferenceTest001, TestSize.Level1)
1537 {
1538 ResConfig *rc = CreateResConfig();
1539 if (rc == nullptr) {
1540 ASSERT_TRUE(false);
1541 }
1542 rc->SetLocaleInfo("en", nullptr, "US");
1543 rm->UpdateResConfig(*rc);
1544
1545 rm->AddResource(FormatFullPath(g_resFilePath).c_str());
1546
1547 int id = GetResId("integer_1", ResType::INTEGER);
1548 std::string value(FormatString("$integer:%d", id));
1549 std::string outValue;
1550 RState ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1551 EXPECT_EQ(SUCCESS, ret);
1552 EXPECT_EQ(std::string("101"), outValue);
1553
1554 std::string copyright("XXXXXX All rights reserved. ©2011-2019");
1555 id = GetResId("copyright_text", ResType::STRING);
1556 value.assign(FormatString("$string:%d", id));
1557 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1558 EXPECT_EQ(SUCCESS, ret);
1559 EXPECT_EQ(copyright, outValue);
1560
1561 id = GetResId("string_ref", ResType::STRING);
1562 value.assign(FormatString("$string:%d", id));
1563 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1564 EXPECT_EQ(SUCCESS, ret);
1565 EXPECT_EQ(copyright, outValue);
1566
1567 id = GetResId("boolean_1", ResType::BOOLEAN);
1568 value.assign(FormatString("$boolean:%d", id));
1569 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1570 EXPECT_EQ(SUCCESS, ret);
1571 EXPECT_EQ(std::string("true"), outValue);
1572
1573 id = GetResId("grey_background", ResType::COLOR);
1574 value.assign(FormatString("$color:%d", id));
1575 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1576 EXPECT_EQ(SUCCESS, ret);
1577 EXPECT_EQ(std::string("#F5F5F5"), outValue);
1578
1579 id = GetResId("aboutPage_minHeight", ResType::FLOAT);
1580 value.assign(FormatString("$float:%d", id));
1581 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1582 EXPECT_EQ(SUCCESS, ret);
1583 EXPECT_EQ(std::string("707vp"), outValue);
1584
1585 id = GetResId("base", ResType::PATTERN);
1586 value.assign(FormatString("$pattern:%d", id));
1587 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1588 EXPECT_EQ(ERROR, ret);
1589
1590 // reload
1591 rc->SetLocaleInfo("zh", nullptr, "CN");
1592 rm->UpdateResConfig(*rc);
1593 delete rc;
1594
1595 id = GetResId("copyright_text", ResType::STRING);
1596 value.assign(FormatString("$string:%d", id));
1597 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1598 ASSERT_EQ(SUCCESS, ret);
1599 ASSERT_EQ(std::string("版权所有 ©2011-2019 XXXX有限公司保留一切权利"), outValue.c_str());
1600
1601 id = GetResId("string_ref", ResType::STRING);
1602 value.assign(FormatString("$string:%d", id));
1603 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1604 ASSERT_EQ(SUCCESS, ret);
1605 ASSERT_EQ(std::string("$aaaaa"), outValue.c_str());
1606
1607 // error case
1608 // wrong id
1609 value.assign(FormatString("$boolean:%d", NON_EXIST_ID));
1610 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1611 ASSERT_EQ(ERROR, ret);
1612 // wrong type
1613 id = GetResId("copyright_text", ResType::STRING);
1614 value.assign(FormatString("$boolean:%d", id));
1615 ret = ((ResourceManagerImpl *)rm)->ResolveReference(value, outValue);
1616 ASSERT_EQ(ERROR, ret);
1617 }
1618
1619 /*
1620 * @tc.name: ResourceManagerResolveParentReferenceTest001
1621 * @tc.desc: Test ResolveParentReference function, file case.
1622 * @tc.type: FUNC
1623 */
1624 HWTEST_F(ResourceManagerTest, ResourceManagerResolveParentReferenceTest001, TestSize.Level1)
1625 {
1626 rm->AddResource(FormatFullPath(g_resFilePath).c_str());
1627 int id;
1628 std::map<std::string, std::string> outValue;
1629 const IdItem *idItem;
1630 RState ret;
1631
1632 id = GetResId("base", ResType::PATTERN);
1633 ASSERT_TRUE(id > 0);
1634 idItem = ((ResourceManagerImpl *)rm)->hapManager_->FindResourceById(id);
1635 ASSERT_TRUE(idItem != nullptr);
1636 ret = ((ResourceManagerImpl *)rm)->ResolveParentReference(idItem, outValue);
1637 ASSERT_EQ(SUCCESS, ret);
1638 PrintMapString(outValue);
1639
1640 HILOG_DEBUG("=====");
1641 id = GetResId("child", ResType::PATTERN);
1642 ASSERT_TRUE(id > 0);
1643 idItem = ((ResourceManagerImpl *)rm)->hapManager_->FindResourceById(id);
1644 ASSERT_TRUE(idItem != nullptr);
1645 ret = ((ResourceManagerImpl *)rm)->ResolveParentReference(idItem, outValue);
1646 ASSERT_EQ(SUCCESS, ret);
1647 PrintMapString(outValue);
1648
1649 HILOG_DEBUG("=====");
1650 id = GetResId("ccchild", ResType::PATTERN);
1651 ASSERT_TRUE(id > 0);
1652 idItem = ((ResourceManagerImpl *)rm)->hapManager_->FindResourceById(id);
1653 ASSERT_TRUE(idItem != nullptr);
1654 ret = ((ResourceManagerImpl *)rm)->ResolveParentReference(idItem, outValue);
1655 ASSERT_EQ(SUCCESS, ret);
1656 PrintMapString(outValue);
1657
1658 // error case
1659 ret = ((ResourceManagerImpl *)rm)->ResolveParentReference(nullptr, outValue);
1660 ASSERT_EQ(ERROR, ret);
1661 // wrong resType
1662 IdItem *item = new IdItem;
1663 for (int i = 0; i < ResType::MAX_RES_TYPE; ++i) {
1664 if (i == ResType::THEME || i == ResType::PATTERN) {
1665 continue;
1666 }
1667 item->resType_ = (ResType) i;
1668 ret = ((ResourceManagerImpl *)rm)->ResolveParentReference(item, outValue);
1669 EXPECT_EQ(ERROR, ret);
1670 }
1671 delete item;
1672 }
1673
1674 /*
1675 * test res with same name in different resType
1676 * @tc.name: ResourceManagerSameNameTest001
1677 * @tc.desc: Test GetStringByName & GetBooleanByName & GetIntegerByName function, file case.
1678 * @tc.type: FUNC
1679 */
1680 HWTEST_F(ResourceManagerTest, ResourceManagerSameNameTest001, TestSize.Level1)
1681 {
1682 rm->AddResource(FormatFullPath(g_resFilePath).c_str());
1683 std::string outValue;
1684 std::string name;
1685 RState state;
1686
1687 state = rm->GetStringByName("same_name", outValue);
1688 EXPECT_TRUE(state == SUCCESS);
1689 EXPECT_EQ(std::string("StringSameName"), outValue);
1690
1691 bool outValueB = true;
1692 state = rm->GetBooleanByName("same_name", outValueB);
1693 EXPECT_TRUE(state == SUCCESS);
1694 EXPECT_EQ(false, outValueB);
1695
1696 int outValueI;
1697 state = rm->GetIntegerByName("same_name", outValueI);
1698 EXPECT_TRUE(state == SUCCESS);
1699 EXPECT_EQ(999, outValueI);
1700 }
1701 }
1702