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 "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
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 EXPECT_TRUE(false);
82 return;
83 }
84
85 PrintIdValues(idValues);
86 delete hapManager;
87 }
88
89 /*
90 * this test shows how to reload a hap
91 * @tc.name: HapManagerFuncTest002
92 * @tc.desc: Test UpdateResConfig & AddResourcePath function, file case.
93 * @tc.type: FUNC
94 */
95 HWTEST_F(HapManagerTest, HapManagerFuncTest002, TestSize.Level1)
96 {
97 ResConfig *rc = CreateResConfig();
98 if (rc == nullptr) {
99 EXPECT_TRUE(false);
100 return;
101 }
102 rc->SetLocaleInfo("en", nullptr, "US");
103 std::string resPath = FormatFullPath(g_resFilePath);
104 const char *path = resPath.c_str();
105 HapManager *hapManager = new HapManager(new ResConfigImpl);
106 if (hapManager == nullptr) {
107 delete (rc);
108 EXPECT_TRUE(false);
109 return;
110 }
111 hapManager->UpdateResConfig(*rc);
112 bool ret = hapManager->AddResourcePath(path);
113
114 EXPECT_TRUE(ret);
115
116 int id = 16777228;
117 const HapResource::IdValues *idValues = hapManager->GetResourceList(id);
118 if (idValues == nullptr) {
119 delete (hapManager);
120 delete (rc);
121 EXPECT_TRUE(false);
122 return;
123 }
124
125 EXPECT_EQ(static_cast<size_t>(1), idValues->GetLimitPathsConst().size());
126 PrintIdValues(idValues);
127
128 // reload
129
130 ResConfig* rc2 = CreateResConfig();
131 if (rc2 == nullptr) {
132 delete (hapManager);
133 delete (rc);
134 EXPECT_TRUE(false);
135 return;
136 }
137
138 rc2->SetLocaleInfo("zh", nullptr, "CN");
139 hapManager->UpdateResConfig(*rc2);
140 do {
141 idValues = hapManager->GetResourceList(id);
142 if (idValues == nullptr) {
143 EXPECT_TRUE(false);
144 break;
145 }
146
147 EXPECT_EQ(static_cast<size_t>(2), idValues->GetLimitPathsConst().size());
148
149 PrintIdValues(idValues);
150 } while (false);
151 delete (hapManager);
152 delete (rc2);
153 delete (rc);
154 }