1 /*
2  * Copyright (c) 2021-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 MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
17 #define MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
18 
19 #include <vector>
20 #include "gmock/gmock.h"
21 #include "ability_info.h"
22 #include "application_info.h"
23 #include "want.h"
24 #include "iremote_proxy.h"
25 #include "iremote_stub.h"
26 #include "bundle_mgr_interface.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31 const std::string COM_OHOS_HELLO = "com.ohos.test.helloworld";
32 const int32_t APPLICATION_NUMHELLO = 104;
33 const std::string COM_OHOS_SPECIAL = "com.ohos.test.special";
34 }  // namespace
35 class BundleMgrProxy : public IRemoteProxy<IBundleMgr> {
36 public:
BundleMgrProxy(const sptr<IRemoteObject> & impl)37     explicit BundleMgrProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IBundleMgr>(impl)
38     {}
~BundleMgrProxy()39     virtual ~BundleMgrProxy()
40     {}
41     bool QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo) override;
42     bool QueryAbilityInfoByUri(const std::string& uri, AbilityInfo& abilityInfo) override;
43     std::string GetAppType(const std::string& bundleName) override;
44 
45     virtual bool GetApplicationInfo(
46         const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo) override;
47     virtual bool GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo);
48     virtual bool GetHapModuleInfo(
49         const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo) override;
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)50     virtual bool QueryKeepAliveBundleInfos(std::vector<BundleInfo>& bundleInfos) override
51     {
52         int appUid = 2100;
53         GTEST_LOG_(INFO) << "QueryKeepAliveBundleInfos()";
54         ApplicationInfo info;
55         info.name = "KeepAliveApp";
56         info.bundleName = "KeepAliveApplication";
57         info.uid = appUid;
58 
59         BundleInfo bundleInfo;
60         bundleInfo.applicationInfo = info;
61         HapModuleInfo hapModuleInfo;
62         hapModuleInfo.name = "Module";
63         HapModuleInfo hapModuleInfo1;
64         hapModuleInfo1.name = "Module1";
65         bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
66         bundleInfo.hapModuleInfos.push_back(hapModuleInfo1);
67 
68         bundleInfos.push_back(bundleInfo);
69         GTEST_LOG_(INFO) << "bundleInfos size : " << bundleInfos.size();
70         return true;
71     };
72 
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos,AppExecFwk::GetDependentBundleInfoFlag flag)73     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
74         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos,
75         AppExecFwk::GetDependentBundleInfoFlag flag) override
76     {
77         return ERR_OK;
78     }
79 
80     sptr<IOverlayManager> GetOverlayManagerProxy() override;
81 };
82 
83 class BundleMgrStub : public IRemoteStub<IBundleMgr> {
84 public:
85     virtual int OnRemoteRequest(
86         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
87 };
88 
89 class BundleMgrService : public BundleMgrStub {
90 public:
91     MOCK_METHOD0(GetBundleInstaller, sptr<AppExecFwk::IBundleInstaller>());
92     bool QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo) override;
93     bool QueryAbilityInfoByUri(const std::string& uri, AbilityInfo& abilityInfo) override;
94 
95     std::string GetAppType(const std::string& bundleName) override;
96 
97     virtual bool GetApplicationInfo(
98         const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo) override;
99     virtual bool GetBundleInfo(
100         const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId) override;
101     virtual bool GetBundleInfos(
102         const BundleFlag flag, std::vector<BundleInfo>& bundleInfos, int32_t userId) override;
103     virtual bool GetBundleGids(const std::string& bundleName, std::vector<int>& gids) override;
104     virtual bool GetHapModuleInfo(const AbilityInfo& abilityInfo, HapModuleInfo& hapModuleInfo);
105     virtual bool GetHapModuleInfo(
106         const AbilityInfo& abilityInfo, int32_t userId, HapModuleInfo& hapModuleInfo) override;
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos)107     virtual bool QueryKeepAliveBundleInfos(std::vector<BundleInfo>& bundleInfos) override
108     {
109         int appUid = 2100;
110         GTEST_LOG_(INFO) << "QueryKeepAliveBundleInfos()";
111         ApplicationInfo info;
112         info.name = "KeepAliveApp";
113         info.bundleName = "KeepAliveApplication";
114         info.uid = appUid;
115 
116         BundleInfo bundleInfo;
117         bundleInfo.applicationInfo = info;
118         HapModuleInfo hapModuleInfo;
119         hapModuleInfo.name = "Module";
120         HapModuleInfo hapModuleInfo1;
121         hapModuleInfo1.name = "Module1";
122         bundleInfo.hapModuleInfos.push_back(hapModuleInfo);
123         bundleInfo.hapModuleInfos.push_back(hapModuleInfo1);
124 
125         bundleInfos.push_back(bundleInfo);
126         GTEST_LOG_(INFO) << "bundleInfos size : " << bundleInfos.size();
127         return true;
128     };
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)129     virtual bool ImplicitQueryInfoByPriority(const Want& want, int32_t flags, int32_t userId,
130         AbilityInfo& abilityInfo, ExtensionAbilityInfo& extensionInfo) override
131     {
132         abilityInfo.name = "MainAbility";
133         abilityInfo.bundleName = "com.ohos.launcher";
134         return true;
135     }
GetGroupDir(const std::string & dataGroupId,std::string & dir)136     virtual bool GetGroupDir(const std::string &dataGroupId, std::string &dir) override
137     {
138         dir = "group";
139         return true;
140     }
141 
142     sptr<IQuickFixManager> GetQuickFixManagerProxy() override;
143 
144     BundleMgrService();
~BundleMgrService()145     virtual ~BundleMgrService() {}
146     void MakingPackageData();
147     void PushTestHelloIndexAbility(int index);
148     void PushTestSpecialAbility();
149     void PushTestHelloAbility();
150     void MakingResidentProcData();
151     ErrCode GetBundleInfoForSelf(int32_t flags, BundleInfo &bundleInfo);
GetBaseSharedBundleInfos(const std::string & bundleName,std::vector<BaseSharedBundleInfo> & baseSharedBundleInfos,AppExecFwk::GetDependentBundleInfoFlag flag)152     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
153         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos,
154         AppExecFwk::GetDependentBundleInfoFlag flag) override
155     {
156         return ERR_OK;
157     }
158     sptr<IOverlayManager> GetOverlayManagerProxy() override;
159 private:
160     std::vector<BundleInfo> bundleInfos_;
161     sptr<IQuickFixManager> quickFixManager_ = nullptr;
162 };
163 
164 class QuickFixManagerHost : public IRemoteStub<IQuickFixManager> {
165 public:
166     int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
167 };
168 
169 class QuickFixManagerHostImpl : public QuickFixManagerHost {
170 public:
171     MOCK_METHOD4(DeployQuickFix, ErrCode(const std::vector<std::string>& bundleFilePaths,
172         const sptr<IQuickFixStatusCallback>& statusCallback, bool isDebug, const std::string& targetPath));
173     MOCK_METHOD3(SwitchQuickFix, ErrCode(const std::string& bundleName, bool enable,
174         const sptr<IQuickFixStatusCallback>& statusCallback));
175     MOCK_METHOD2(DeleteQuickFix, ErrCode(const std::string& bundleName,
176         const sptr<IQuickFixStatusCallback>& statusCallback));
177     MOCK_METHOD3(CreateFd, ErrCode(const std::string& fileName, int32_t& fd, std::string& path));
178 
CopyFiles(const std::vector<std::string> & sourceFiles,std::vector<std::string> & destFiles)179     virtual ErrCode CopyFiles(const std::vector<std::string>& sourceFiles, std::vector<std::string>& destFiles) override
180     {
181         destFiles = sourceFiles;
182         return 0;
183     }
184 };
185 }  // namespace AppExecFwk
186 }  // namespace OHOS
187 
188 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
189