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