1 /*
2 * Copyright (c) 2024 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 #include <gtest/gtest.h>
16 
17 #include "feature/feature_system.h"
18 
19 using namespace testing::ext;
20 using namespace OHOS::DistributedData;
21 namespace DistributedDB {
22 struct AutoLaunchParam {
23 };
24 }
25 namespace OHOS::Test {
26 class FeatureSystemTest : public testing::Test {
27 };
28 
29 class MockFeature : public FeatureSystem::Feature {
30 public:
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply)31     int OnRemoteRequest(uint32_t code, OHOS::MessageParcel& data, OHOS::MessageParcel& reply) override
32     {
33         return E_OK;
34     }
35 };
36 
37 /**
38 * @tc.name: GetInstanceTest
39 * @tc.desc: getInstance
40 * @tc.type: FUNC
41 * @tc.require:
42 * @tc.author: MengYao
43 */
44 HWTEST_F(FeatureSystemTest, GetInstanceTest, TestSize.Level1)
45 {
46     FeatureSystem& featureSystem = FeatureSystem::GetInstance();
47     ASSERT_NE(&featureSystem, nullptr);
48 }
49 
50 /**
51 * @tc.name: RegisterCreatorTest And GetCreatorTest
52 * @tc.desc: registerCreatorTest And getCreator
53 * @tc.type: FUNC
54 * @tc.require:
55 * @tc.author: MengYao
56 */
57 HWTEST_F(FeatureSystemTest, RegisterCreatorAndGetCreatorTest, TestSize.Level1)
58 {
59     FeatureSystem& featureSystem = FeatureSystem::GetInstance();
60     std::string featureName = "MockFeature";
__anon932d15900102() 61     FeatureSystem::Creator creator = []() {
62         return std::shared_ptr<FeatureSystem::Feature>();
63     };
64 
65     int32_t result = featureSystem.RegisterCreator(featureName, creator);
66     EXPECT_EQ(result, E_OK);
67 
68     FeatureSystem::Creator registeredCreator = featureSystem.GetCreator(featureName);
69     EXPECT_NE(registeredCreator, nullptr);
70 }
71 
72 /**
73 * @tc.name: RegisterStaticActsTest And GetStaticActsTest
74 * @tc.desc: registerStaticActs And getStaticActs
75 * @tc.type: FUNC
76 * @tc.require:
77 * @tc.author: MengYao
78 */
79 HWTEST_F(FeatureSystemTest, GetStaticActsAndetStaticActsTest, TestSize.Level1)
80 {
81     FeatureSystem& featureSystem = FeatureSystem::GetInstance();
82     std::string staticActsName = "StaticActs";
83     std::shared_ptr<StaticActs> staticActs = std::make_shared<StaticActs>();
84 
85     int32_t result = featureSystem.RegisterStaticActs(staticActsName, staticActs);
86     EXPECT_EQ(result, E_OK);
87 
88     const OHOS::ConcurrentMap<std::string, std::shared_ptr<StaticActs>>& staticActsMap = featureSystem.GetStaticActs();
89     auto [success, staticActsPtr] = staticActsMap.Find("StaticActs");
90     EXPECT_NE(staticActsPtr, nullptr);
91 }
92 
93 /**
94 * @tc.name: GetFeatureNameTest
95 * @tc.desc: getFeatureNameTest
96 * @tc.type: FUNC
97 * @tc.require:
98 * @tc.author: MengYao
99 */
100 HWTEST_F(FeatureSystemTest, GetFeatureNameTest, TestSize.Level1)
101 {
102     FeatureSystem& featureSystem = FeatureSystem::GetInstance();
103     std::string featureName1 = "Feature1";
104     std::string featureName2 = "Feature2";
105 
__anon932d15900202() 106     featureSystem.RegisterCreator(featureName1, []() {
107         return nullptr;
108     });
__anon932d15900302() 109     featureSystem.RegisterCreator(featureName2, []() {
110         return nullptr;
111     });
112 
113     std::vector<std::string> featureNames = featureSystem.GetFeatureName(FeatureSystem::BIND_LAZY);
114 
115     EXPECT_EQ(featureNames[0], featureName1);
116     EXPECT_EQ(featureNames[1], featureName2);
117 }
118 
119 /**
120 * @tc.name: OnInitializeTest
121 * @tc.desc: onInitializeTest
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author: MengYao
125 */
126 HWTEST_F(FeatureSystemTest, OnInitializeTest, TestSize.Level1)
127 {
128     MockFeature feature;
129     int32_t result = feature.OnInitialize();
130     EXPECT_EQ(result, E_OK);
131 }
132 
133 /**
134 * @tc.name: OnBindTest
135 * @tc.desc: onBind
136 * @tc.type: FUNC
137 * @tc.require:
138 * @tc.author: MengYao
139 */
140 HWTEST_F(FeatureSystemTest, OnBindTest, TestSize.Level1)
141 {
142     MockFeature feature;
143     FeatureSystem::Feature::BindInfo bindInfo;
144     bindInfo.selfName = "Feature1";
145     bindInfo.selfTokenId = 123;
146     bindInfo.executors = std::shared_ptr<OHOS::ExecutorPool>();
147     int32_t result = feature.OnBind(bindInfo);
148     EXPECT_EQ(result, E_OK);
149 }
150 
151 /**
152 * @tc.name: OnAppExitTest
153 * @tc.desc: onAppExitTest
154 * @tc.type: FUNC
155 * @tc.require:
156 * @tc.author: MengYao
157 */
158 HWTEST_F(FeatureSystemTest, OnAppExitTest, TestSize.Level1)
159 {
160     MockFeature feature;
161     pid_t uid = 1;
162     pid_t pid = 2;
163     uint32_t tokenId = 3;
164     std::string bundleName = "com.example.app";
165     int32_t result = feature.OnAppExit(uid, pid, tokenId, bundleName);
166     EXPECT_EQ(result, E_OK);
167 }
168 
169 /**
170 * @tc.name: FeatureTest001
171 * @tc.desc: onAppUninstallTest and onAppUpdate and onAppInstall
172 * @tc.type: FUNC
173 * @tc.require:
174 * @tc.author: MengYao
175 */
176 HWTEST_F(FeatureSystemTest, FeatureTest001, TestSize.Level1)
177 {
178     FeatureSystem::Feature::BindInfo bindInfo;
179     std::string bundleName = "com.example.app";
180     int32_t user = 0;
181     int32_t index = 1;
182 
183     MockFeature mockFeature;
184 
185     int32_t ret = mockFeature.OnAppInstall(bundleName, user, index);
186     EXPECT_EQ(ret, E_OK);
187 
188     ret = mockFeature.OnAppUpdate(bundleName, user, index);
189     EXPECT_EQ(ret, E_OK);
190 
191     ret = mockFeature.OnAppUninstall(bundleName, user, index);
192     EXPECT_EQ(ret, E_OK);
193 }
194 
195 /**
196 * @tc.name: ResolveAutoLaunchTest
197 * @tc.desc: resolveAutoLaunch
198 * @tc.type: FUNC
199 * @tc.require:
200 * @tc.author: MengYao
201 */
202 HWTEST_F(FeatureSystemTest, ResolveAutoLaunchTest, TestSize.Level1)
203 {
204     FeatureSystem::Feature::BindInfo bindInfo;
205     std::string identifier = "example_identifier";
206     DistributedDB::AutoLaunchParam param;
207 
208     MockFeature mockFeature;
209     int32_t ret = mockFeature.ResolveAutoLaunch(identifier, param);
210 
211     EXPECT_EQ(ret, E_OK);
212 }
213 
214 /**
215 * @tc.name: OnUserChangeTest
216 * @tc.desc: onUserChange
217 * @tc.type: FUNC
218 * @tc.require:
219 * @tc.author: MengYao
220 */
221 HWTEST_F(FeatureSystemTest, OnUserChangeTest, TestSize.Level1)
222 {
223     FeatureSystem::Feature::BindInfo bindInfo;
224     uint32_t code = 1;
225     std::string user = "example_user";
226     std::string account = "example_account";
227 
228     MockFeature mockFeature;
229     int32_t ret = mockFeature.OnUserChange(code, user, account);
230 
231     EXPECT_EQ(ret, E_OK);
232 }
233 
234 /**
235 * @tc.name: FeatureTest002
236 * @tc.desc: online and offline and onReady and onSessionReady
237 * @tc.type: FUNC
238 * @tc.require:
239 * @tc.author: MengYao
240 */
241 HWTEST_F(FeatureSystemTest, FeatureTest002, TestSize.Level1)
242 {
243     FeatureSystem::Feature::BindInfo bindInfo;
244     std::string device = "example_device";
245 
246     MockFeature mockFeature;
247 
248     int32_t ret = mockFeature.Online(device);
249     EXPECT_EQ(ret, E_OK);
250 
251     ret = mockFeature.Offline(device);
252     EXPECT_EQ(ret, E_OK);
253 
254     ret = mockFeature.OnReady(device);
255     EXPECT_EQ(ret, E_OK);
256 
257     ret = mockFeature.OnSessionReady(device);
258     EXPECT_EQ(ret, E_OK);
259 }
260 
261 
262 /**
263 * @tc.name: OnScreenUnlockedTest
264 * @tc.desc: test OnScreenUnlocked
265 * @tc.type: FUNC
266 * @tc.require:
267 * @tc.author:
268 */
269 HWTEST_F(FeatureSystemTest, OnScreenUnlockedTest, TestSize.Level1)
270 {
271     MockFeature mockFeature;
272     int32_t user = 0;
273     int32_t ret = mockFeature.OnScreenUnlocked(user);
274     EXPECT_EQ(ret, E_OK);
275 }
276 } // namespace OHOS::Test