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 "hap_manager_test.h"
17
18 #include <gtest/gtest.h>
19
20 #include "test_common.h"
21 #include "utils/string_utils.h"
22
23 #define private public
24
25 #include "hap_manager.h"
26
27 using namespace OHOS::Global::Resource;
28 using namespace testing::ext;
29 namespace {
30 class HapManagerTest : public testing::Test {
31 public:
32 static void SetUpTestCase(void);
33
34 static void TearDownTestCase(void);
35
36 void SetUp();
37
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void HapManagerTest::SetUpTestCase(void)
42 {
43 // step 1: input testsuit setup step
44 g_logLevel = LOG_DEBUG;
45 }
46
TearDownTestCase()47 void HapManagerTest::TearDownTestCase()
48 {
49 // step 2: input testsuit teardown step
50 }
51
SetUp()52 void HapManagerTest::SetUp()
53 {
54 // step 3: input testcase setup step
55 HILOG_DEBUG("HapManagerTest setup");
56 }
57
TearDown()58 void HapManagerTest::TearDown()
59 {
60 // step 4: input testcase teardown step
61 HILOG_DEBUG("HapManagerTest teardown");
62 }
63
64 /*
65 * this test shows how to load a hap, then find value list by id
66 * @tc.name: HapManagerFuncTest001
67 * @tc.desc: Test AddResourcePath & GetResourceList function, file case.
68 * @tc.type: FUNC
69 */
70 HWTEST_F(HapManagerTest, HapManagerFuncTest001, TestSize.Level1)
71 {
72 HapManager *hapManager = new HapManager(new ResConfigImpl);
73 bool ret = hapManager->AddResourcePath(FormatFullPath(g_resFilePath).c_str());
74
75 EXPECT_TRUE(ret);
76
77 int id = 16777217;
78 const HapResource::IdValues *idValues = hapManager->GetResourceList(id);
79 if (idValues == nullptr) {
80 delete hapManager;
81 ASSERT_TRUE(false);
82 }
83
84 PrintIdValues(idValues);
85 delete hapManager;
86 }
87
88 /*
89 * this test shows how to reload a hap
90 * @tc.name: HapManagerFuncTest002
91 * @tc.desc: Test UpdateResConfig & AddResourcePath function, file case.
92 * @tc.type: FUNC
93 */
94 HWTEST_F(HapManagerTest, HapManagerFuncTest002, TestSize.Level1)
95 {
96 ResConfig *rc = CreateResConfig();
97 if (rc == nullptr) {
98 ASSERT_TRUE(false);
99 }
100 rc->SetLocaleInfo("en", nullptr, "US");
101 std::string resPath = FormatFullPath(g_resFilePath);
102 const char *path = resPath.c_str();
103 HapManager *hapManager = new HapManager(new ResConfigImpl);
104 if (hapManager == nullptr) {
105 delete (rc);
106 ASSERT_TRUE(false);
107 }
108 hapManager->UpdateResConfig(*rc);
109 bool ret = hapManager->AddResourcePath(path);
110
111 EXPECT_TRUE(ret);
112
113 int id = 16777228;
114 const HapResource::IdValues *idValues = hapManager->GetResourceList(id);
115 if (idValues == nullptr) {
116 delete (hapManager);
117 delete (rc);
118 ASSERT_TRUE(false);
119 }
120
121 EXPECT_EQ(static_cast<size_t>(1), idValues->GetLimitPathsConst().size());
122 PrintIdValues(idValues);
123
124 // reload
125
126 ResConfig *rc2 = CreateResConfig();
127 if (rc2 == nullptr) {
128 delete (hapManager);
129 delete (rc);
130 ASSERT_TRUE(false);
131 }
132
133 rc2->SetLocaleInfo("zh", nullptr, "CN");
134 hapManager->UpdateResConfig(*rc2);
135 do {
136 idValues = hapManager->GetResourceList(id);
137 if (idValues == nullptr) {
138 EXPECT_TRUE(false);
139 break;
140 }
141
142 EXPECT_EQ(static_cast<size_t>(2), idValues->GetLimitPathsConst().size());
143
144 PrintIdValues(idValues);
145 } while (false);
146 delete (hapManager);
147 delete (rc2);
148 delete (rc);
149 }
150 }