1 /* 2 * Copyright (c) 2023 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 #ifndef OHOS_MOCK_BUNDLE_MANAGER_SERVICE_H 17 #define OHOS_MOCK_BUNDLE_MANAGER_SERVICE_H 18 19 #include "bundle_installer_interface.h" 20 #include "bundle_mgr_interface.h" 21 #include "gmock/gmock.h" 22 #include "iremote_object.h" 23 #include "iremote_stub.h" 24 #include "mock_overlay_manager.h" 25 26 namespace OHOS { 27 constexpr int32_t BASE_USER_RANGE = 200000; 28 class MockBundleManagerService : public IRemoteStub<AppExecFwk::IBundleMgr> { 29 public: MockBundleManagerService()30 MockBundleManagerService() {}; ~MockBundleManagerService()31 virtual ~MockBundleManagerService() {}; 32 MOCK_METHOD0(GetBundleInstaller, sptr<AppExecFwk::IBundleInstaller>()); 33 MOCK_METHOD2( 34 GetHapModuleInfo, bool(const AppExecFwk::AbilityInfo &abilityInfo, AppExecFwk::HapModuleInfo &hapModuleInfo)); 35 MOCK_METHOD3(GetHapModuleInfo, 36 bool(const AppExecFwk::AbilityInfo &abilityInfo, int32_t userId, AppExecFwk::HapModuleInfo &hapModuleInfo)); 37 MOCK_METHOD1(GetAppType, std::string(const std::string &bundleName)); 38 MOCK_METHOD3(GetBaseSharedBundleInfos, 39 ErrCode(const std::string &bundleName, std::vector<AppExecFwk::BaseSharedBundleInfo> &baseSharedBundleInfos, 40 AppExecFwk::GetDependentBundleInfoFlag flag)); 41 MOCK_METHOD2(GetBundleInfoForSelf, ErrCode(int32_t flags, AppExecFwk::BundleInfo &bundleInfo)); 42 GetOverlayManagerProxy()43 sptr<AppExecFwk::IOverlayManager> GetOverlayManagerProxy() 44 { 45 sptr<AppExecFwk::IOverlayManager> overlayModuleProxy = 46 new (std::nothrow) AppExecFwk::OverlayManagerProxy(nullptr); 47 return overlayModuleProxy; 48 } 49 GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids)50 bool GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector<int> &gids) 51 { 52 return true; 53 } 54 GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)55 bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, 56 AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override 57 { 58 if (bundleName == "test_contextImpl") { 59 bundleInfo.name = "test_contextImpl"; 60 bundleInfo.applicationInfo.name = "test_contextImpl"; 61 AppExecFwk::HapModuleInfo moduleInfo1; 62 moduleInfo1.moduleName = "test_moduleName"; 63 bundleInfo.hapModuleInfos.push_back(moduleInfo1); 64 } 65 return true; 66 } 67 GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)68 bool GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, const int userId, 69 AppExecFwk::ApplicationInfo &appInfo) 70 { 71 if (appName.empty()) { 72 return false; 73 } 74 appInfo.name = appName; 75 appInfo.bundleName = appName; 76 appInfo.uid = userId * BASE_USER_RANGE; 77 if (appName.compare("com.test.crowdtest") == 0) { 78 appInfo.appDistributionType = "crowdtesting"; 79 appInfo.crowdtestDeadline = 0; 80 } 81 if (appName.compare("com.test.atomicservice") == 0) { 82 appInfo.bundleType = AppExecFwk::BundleType::ATOMIC_SERVICE; 83 } 84 return true; 85 } 86 87 bool GetBundleInfos(const AppExecFwk::BundleFlag flag, std::vector<AppExecFwk::BundleInfo> &bundleInfos, 88 int32_t userId = AppExecFwk::Constants::UNSPECIFIED_USERID) 89 { 90 OHOS::AppExecFwk::BundleInfo bundleInfo; 91 bundleInfo.name = "com.ix.residentservcie"; 92 bundleInfo.isKeepAlive = true; 93 bundleInfo.applicationInfo.process = "com.ix.residentservcie"; 94 95 OHOS::AppExecFwk::HapModuleInfo hapModuleInfo; 96 hapModuleInfo.isModuleJson = true; 97 hapModuleInfo.mainElementName = "residentServiceAbility"; 98 hapModuleInfo.process = "com.ix.residentservcie"; 99 bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo); 100 101 bundleInfos.emplace_back(bundleInfo); 102 return true; 103 } 104 }; 105 } // namespace OHOS 106 #endif // OHOS_MOCK_BUNDLE_MANAGER_SERVICE_H