• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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)73     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
74         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos) override
75     {
76         return ERR_OK;
77     }
78 
79     sptr<IOverlayManager> GetOverlayManagerProxy() override;
80 };
81 
82 class BundleMgrStub : public IRemoteStub<IBundleMgr> {
83 public:
84     virtual int OnRemoteRequest(
85         uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
86 };
87 
88 class BundleMgrService : public BundleMgrStub {
89 public:
90     bool QueryAbilityInfo(const AAFwk::Want& want, AbilityInfo& abilityInfo) override;
91     bool QueryAbilityInfoByUri(const std::string& uri, AbilityInfo& abilityInfo) override;
92 
93     std::string GetAppType(const std::string& bundleName) override;
94 
95     virtual bool GetApplicationInfo(
96         const std::string& appName, const ApplicationFlag flag, const int userId, ApplicationInfo& appInfo) override;
97     virtual bool GetBundleInfo(
98         const std::string& bundleName, const BundleFlag flag, BundleInfo& bundleInfo, int32_t userId) override;
99     virtual bool GetBundleInfos(
100         const BundleFlag flag, std::vector<BundleInfo>& bundleInfos, int32_t userId) override;
101     bool GetBundleGidsByUid(
102         const std::string& bundleName, const int& uid, std::vector<int>& gids) 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)152     ErrCode GetBaseSharedBundleInfos(const std::string &bundleName,
153         std::vector<BaseSharedBundleInfo> &baseSharedBundleInfos) override
154     {
155         return ERR_OK;
156     }
157     sptr<IOverlayManager> GetOverlayManagerProxy() override;
158 private:
159     std::vector<BundleInfo> bundleInfos_;
160     sptr<IQuickFixManager> quickFixManager_ = nullptr;
161 };
162 
163 class QuickFixManagerHost : public IRemoteStub<IQuickFixManager> {
164 public:
165     int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override;
166 };
167 
168 class QuickFixManagerHostImpl : public QuickFixManagerHost {
169 public:
170     MOCK_METHOD2(DeployQuickFix, ErrCode(const std::vector<std::string>& bundleFilePaths,
171         const sptr<IQuickFixStatusCallback>& statusCallback));
172     MOCK_METHOD3(SwitchQuickFix, ErrCode(const std::string& bundleName, bool enable,
173         const sptr<IQuickFixStatusCallback>& statusCallback));
174     MOCK_METHOD2(DeleteQuickFix, ErrCode(const std::string& bundleName,
175         const sptr<IQuickFixStatusCallback>& statusCallback));
176     MOCK_METHOD3(CreateFd, ErrCode(const std::string& fileName, int32_t& fd, std::string& path));
177 
CopyFiles(const std::vector<std::string> & sourceFiles,std::vector<std::string> & destFiles)178     virtual ErrCode CopyFiles(const std::vector<std::string>& sourceFiles, std::vector<std::string>& destFiles) override
179     {
180         destFiles = sourceFiles;
181         return 0;
182     }
183 };
184 }  // namespace AppExecFwk
185 }  // namespace OHOS
186 
187 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_BUNDLE_MANAGER_H
188