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 
GetBundleInfo(const std::string & bundleName,const AppExecFwk::BundleFlag flag,AppExecFwk::BundleInfo & bundleInfo,int32_t userId)50     bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag,
51         AppExecFwk::BundleInfo &bundleInfo, int32_t userId) override
52     {
53         if (bundleName == "test_contextImpl") {
54             bundleInfo.name = "test_contextImpl";
55             bundleInfo.applicationInfo.name = "test_contextImpl";
56             AppExecFwk::HapModuleInfo moduleInfo1;
57             moduleInfo1.moduleName = "test_moduleName";
58             bundleInfo.hapModuleInfos.push_back(moduleInfo1);
59         }
60         return true;
61     }
62 
GetApplicationInfo(const std::string & appName,const AppExecFwk::ApplicationFlag flag,const int userId,AppExecFwk::ApplicationInfo & appInfo)63     bool GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, const int userId,
64         AppExecFwk::ApplicationInfo &appInfo)
65     {
66         if (appName.empty()) {
67             return false;
68         }
69         appInfo.name = appName;
70         appInfo.bundleName = appName;
71         appInfo.uid = userId * BASE_USER_RANGE;
72         if (appName.compare("com.test.crowdtest") == 0) {
73             appInfo.appDistributionType = "crowdtesting";
74             appInfo.crowdtestDeadline = 0;
75         }
76         if (appName.compare("com.test.atomicservice") == 0) {
77             appInfo.bundleType = AppExecFwk::BundleType::ATOMIC_SERVICE;
78         }
79         return true;
80     }
81 
82     bool GetBundleInfos(const AppExecFwk::BundleFlag flag, std::vector<AppExecFwk::BundleInfo> &bundleInfos,
83         int32_t userId = AppExecFwk::Constants::UNSPECIFIED_USERID)
84     {
85         OHOS::AppExecFwk::BundleInfo bundleInfo;
86         bundleInfo.name = "com.ix.residentservcie";
87         bundleInfo.isKeepAlive = true;
88         bundleInfo.applicationInfo.process = "com.ix.residentservcie";
89 
90         OHOS::AppExecFwk::HapModuleInfo hapModuleInfo;
91         hapModuleInfo.isModuleJson = true;
92         hapModuleInfo.mainElementName = "residentServiceAbility";
93         hapModuleInfo.process = "com.ix.residentservcie";
94         bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo);
95 
96         bundleInfos.emplace_back(bundleInfo);
97         return true;
98     }
99 };
100 } // namespace OHOS
101 #endif // OHOS_MOCK_BUNDLE_MANAGER_SERVICE_H