• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_resource_test.h"
17 
18 #include <climits>
19 #include <gtest/gtest.h>
20 
21 #include "hap_parser.h"
22 #include "hap_resource.h"
23 #include "test_common.h"
24 #include "utils/date_utils.h"
25 #include "utils/errors.h"
26 #include "utils/string_utils.h"
27 
28 #define private public
29 
30 using namespace OHOS::Global::Resource;
31 using namespace testing::ext;
32 namespace {
33 class HapResourceTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36 
37     static void TearDownTestCase(void);
38 
39     void SetUp();
40 
41     void TearDown();
42 };
43 
SetUpTestCase(void)44 void HapResourceTest::SetUpTestCase(void)
45 {
46     // step 1: input testsuit setup step
47     g_logLevel = LOG_DEBUG;
48 }
49 
TearDownTestCase(void)50 void HapResourceTest::TearDownTestCase(void)
51 {
52     // step 2: input testsuit teardown step
53 }
54 
SetUp()55 void HapResourceTest::SetUp()
56 {
57     // step 3: input testcase setup step
58 }
59 
TearDown()60 void HapResourceTest::TearDown()
61 {
62     // step 4: input testcase teardown step
63 }
64 
65 /*
66  * this test shows how to load a hap, defaultConfig set to null
67  * @tc.name: HapResourceFuncTest001
68  * @tc.desc: Test Load & GetIdValues & GetIdValuesByName function, file case.
69  * @tc.type: FUNC
70  */
71 HWTEST_F(HapResourceTest, HapResourceFuncTest001, TestSize.Level1)
72 {
73     auto start = CurrentTimeUsec();
74     const HapResource *pResource = HapResource::LoadFromIndex(FormatFullPath(g_resFilePath).c_str(), nullptr);
75     auto cost = CurrentTimeUsec() - start;
76     HILOG_DEBUG("load cost: %ld us.", cost);
77 
78     if (pResource == nullptr) {
79         ASSERT_TRUE(false);
80     }
81     EXPECT_EQ(static_cast<size_t>(80), pResource->IdSize());
82 
83     int id = pResource->GetIdByName("app_name", ResType::STRING);
84     start = CurrentTimeUsec();
85     auto idValues = pResource->GetIdValues(id);
86     cost = CurrentTimeUsec() - start;
87     EXPECT_EQ(static_cast<size_t>(3), idValues->GetLimitPathsConst().size());
88     HILOG_DEBUG("GetIdValues by id cost: %ld us.", cost);
89     PrintIdValues(idValues);
90     {
91         auto limitPath = idValues->GetLimitPathsConst()[0];
92         EXPECT_TRUE(limitPath->GetFolder() == "en_US");
93         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
94         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "App Name");
95     }
96     {
97         auto limitPath = idValues->GetLimitPathsConst()[1];
98         EXPECT_TRUE(limitPath->GetFolder() == "zh_CN");
99         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
100         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "应用名称");
101     }
102     {
103         auto limitPath = idValues->GetLimitPathsConst()[2];
104         EXPECT_TRUE(limitPath->GetFolder() == "default");
105         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
106         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "About");
107     }
108 
109     std::string name = std::string("app_name");
110     start = CurrentTimeUsec();
111     auto idValues2 = pResource->GetIdValuesByName(name, ResType::STRING);
112     cost = CurrentTimeUsec() - start;
113     EXPECT_EQ(static_cast<size_t>(3), idValues2->GetLimitPathsConst().size());
114     HILOG_DEBUG("GetIdValues by name cost: %ld us.", cost);
115     PrintIdValues(idValues);
116     {
117         auto limitPath = idValues->GetLimitPathsConst()[0];
118         EXPECT_TRUE(limitPath->GetFolder() == "en_US");
119         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
120         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "App Name");
121     }
122     {
123         auto limitPath = idValues->GetLimitPathsConst()[1];
124         EXPECT_TRUE(limitPath->GetFolder() == "zh_CN");
125         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
126         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "应用名称");
127     }
128     {
129         auto limitPath = idValues->GetLimitPathsConst()[2];
130         EXPECT_TRUE(limitPath->GetFolder() == "default");
131         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
132         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "About");
133     }
134 
135     delete (pResource);
136 }
137 
138 /*
139  * load a hap, set config en_US
140  * @tc.name: HapResourceFuncTest002
141  * @tc.desc: Test Load & GetIdValues & GetIdValuesByName function, file case.
142  * @tc.type: FUNC
143  */
144 HWTEST_F(HapResourceTest, HapResourceFuncTest002, TestSize.Level1)
145 {
146     ResConfigImpl *rc = new ResConfigImpl;
147     rc->SetLocaleInfo("en", nullptr, "US");
148     std::string resPath = FormatFullPath(g_resFilePath);
149     const char *path = resPath.c_str();
150 
151     auto start = CurrentTimeUsec();
152     const HapResource *pResource = HapResource::LoadFromIndex(path, rc);
153     auto cost = CurrentTimeUsec() - start;
154     HILOG_DEBUG("load cost: %ld us.", cost);
155 
156     if (pResource == nullptr) {
157         delete rc;
158         ASSERT_TRUE(false);
159     }
160 
161     EXPECT_EQ(static_cast<size_t>(79), pResource->IdSize());
162 
163     int id = pResource->GetIdByName("app_name", ResType::STRING);
164     start = CurrentTimeUsec();
165     auto idValues = pResource->GetIdValues(id);
166     cost = CurrentTimeUsec() - start;
167     EXPECT_EQ(static_cast<size_t>(2), idValues->GetLimitPathsConst().size());
168     HILOG_DEBUG("GetIdValues by id cost: %ld us.", cost);
169     PrintIdValues(idValues);
170 
171     {
172         auto limitPath = idValues->GetLimitPathsConst()[0];
173         EXPECT_TRUE(limitPath->GetFolder() == "en_US");
174         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
175         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "App Name");
176     }
177 
178     {
179         auto limitPath = idValues->GetLimitPathsConst()[1];
180         EXPECT_TRUE(limitPath->GetFolder() == "default");
181         EXPECT_TRUE(limitPath->GetIdItem()->name_ == "app_name");
182         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "About");
183     }
184 
185     std::string name = std::string("app_name");
186     start = CurrentTimeUsec();
187     auto idValues2 = pResource->GetIdValuesByName(name, ResType::STRING);
188     cost = CurrentTimeUsec() - start;
189     EXPECT_EQ(static_cast<size_t>(2), idValues2->GetLimitPathsConst().size());
190     HILOG_DEBUG("GetIdValues by name cost: %ld us.", cost);
191     PrintIdValues(idValues);
192 
193     {
194         auto limitPath = idValues2->GetLimitPathsConst()[0];
195         EXPECT_TRUE(limitPath->GetFolder() == "en_US");
196         EXPECT_TRUE(limitPath->GetIdItem()->id_ == static_cast<uint32_t>(id));
197         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "App Name");
198     }
199 
200     {
201         auto limitPath = idValues->GetLimitPathsConst()[1];
202         EXPECT_TRUE(limitPath->GetFolder() == "default");
203         EXPECT_TRUE(limitPath->GetIdItem()->id_ == static_cast<uint32_t>(id));
204         EXPECT_TRUE(limitPath->GetIdItem()->value_ == "About");
205     }
206     delete pResource;
207     delete rc;
208 }
209 
210 /*
211  * load a hap, get a value which is ref
212  * @tc.name: HapResourceFuncTest003
213  * @tc.desc: Test GetIdValuesByName function, file case.
214  * @tc.type: FUNC
215  */
216 HWTEST_F(HapResourceTest, HapResourceFuncTest003, TestSize.Level1)
217 {
218     auto start = CurrentTimeUsec();
219     const HapResource *pResource = HapResource::LoadFromIndex(FormatFullPath(g_resFilePath).c_str(), nullptr);
220     auto cost = CurrentTimeUsec() - start;
221     HILOG_DEBUG("load cost: %ld us.", cost);
222 
223     if (pResource == nullptr) {
224         ASSERT_TRUE(false);
225     }
226 
227     auto idv = pResource->GetIdValuesByName(std::string("integer_ref"), ResType::INTEGER);
228     PrintIdValues(idv);
229 
230     idv = pResource->GetIdValuesByName(std::string("string_ref"), ResType::STRING);
231     PrintIdValues(idv);
232 
233     // ref propagation
234     idv = pResource->GetIdValuesByName(std::string("string_ref2"), ResType::STRING);
235     PrintIdValues(idv);
236 
237     idv = pResource->GetIdValuesByName(std::string("boolean_ref"), ResType::BOOLEAN);
238     PrintIdValues(idv);
239 
240     idv = pResource->GetIdValuesByName(std::string("color_ref"), ResType::COLOR);
241     PrintIdValues(idv);
242 
243     idv = pResource->GetIdValuesByName(std::string("float_ref"), ResType::FLOAT);
244     PrintIdValues(idv);
245 
246     // ref in array ,
247     idv = pResource->GetIdValuesByName(std::string("intarray_1"), ResType::INTARRAY);
248     PrintIdValues(idv);
249 
250     // "parent":   was ref too
251     idv = pResource->GetIdValuesByName(std::string("child"), ResType::PATTERN);
252     PrintIdValues(idv);
253 }
254 
LoadFromHap(const char * hapPath,const ResConfigImpl * defaultConfig)255 ResDesc *LoadFromHap(const char *hapPath, const ResConfigImpl *defaultConfig)
256 {
257     std::string errOut;
258     void *buf = nullptr;
259     size_t bufLen;
260     int32_t out = HapParser::ReadIndexFromFile(hapPath,
261                                                &buf, bufLen, errOut);
262     if (out != OK) {
263         HILOG_ERROR("ReadIndexFromFile failed! retcode:%d,err:%s", out, errOut.c_str());
264         return nullptr;
265     }
266     HILOG_DEBUG("extract success, bufLen:%zu", bufLen);
267 
268     ResDesc *resDesc = new ResDesc();
269     out = HapParser::ParseResHex((char *)buf, bufLen, *resDesc, defaultConfig);
270     if (out != OK) {
271         delete (resDesc);
272         free(buf);
273         HILOG_ERROR("ParseResHex failed! retcode:%d", out);
274         return nullptr;
275     } else {
276         HILOG_DEBUG("ParseResHex success:\n%s", resDesc->ToString().c_str());
277     }
278     free(buf);
279     // construct hapresource
280     return resDesc;
281 }
282 
283 /*
284  * @tc.name: HapResourceFuncTest004
285  * @tc.desc: Test HapParser::ReadIndexFromFile function, file case.
286  * @tc.type: FUNC
287  */
288 HWTEST_F(HapResourceTest, HapResourceFuncTest004, TestSize.Level1)
289 {
290     // 1. normal case
291     ResDesc *resDesc = LoadFromHap(FormatFullPath("all.hap").c_str(), nullptr);
292     ASSERT_TRUE(resDesc != nullptr);
293 
294     // 2. hap file exists, config.json does not exist
295     resDesc = LoadFromHap(FormatFullPath("err-config.json-1.hap").c_str(), nullptr);
296     ASSERT_TRUE(resDesc == nullptr);
297 
298     // 3. hap file exists, config.json error: missing "moduleName"
299     resDesc = LoadFromHap(FormatFullPath("err-config.json-2.hap").c_str(), nullptr);
300     ASSERT_TRUE(resDesc == nullptr);
301 }
302 }