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