• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 OHOS_AAFWK_ABILITY_MOCK_BUNDLE_MANAGER_H
17 #define OHOS_AAFWK_ABILITY_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 "bundlemgr/bundle_mgr_interface.h"
24 #include "ohos/aafwk/content/want.h"
25 #include "iremote_proxy.h"
26 #include "iremote_stub.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 const std::string COM_IX_HIWORLD = "com.ix.hiworld";
31 const std::string COM_IX_HIMUSIC = "com.ix.hiMusic";
32 const std::string COM_IX_HIRADIO = "com.ix.hiRadio";
33 const std::string COM_IX_HISERVICE = "com.ix.hiService";
34 const std::string COM_IX_MUSICSERVICE = "com.ix.musicService";
35 const std::string COM_IX_HIDATA = "com.ix.hiData";
36 
37 auto HiWordInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
38     abilityInfo.name = elementTemp.GetAbilityName();
39     abilityInfo.bundleName = elementTemp.GetBundleName();
40     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
41     abilityInfo.applicationName = "Helloworld";
42     abilityInfo.applicationInfo.name = "Helloworld";
43     abilityInfo.type = AbilityType::PAGE;
44     abilityInfo.applicationInfo.isLauncherApp = true;
45     return true;
46 };
47 
48 auto HiMusicInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
49     abilityInfo.name = elementTemp.GetAbilityName();
50     abilityInfo.bundleName = elementTemp.GetBundleName();
51     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
52     abilityInfo.applicationName = "hiMusic";
53     abilityInfo.applicationInfo.name = "hiMusic";
54     abilityInfo.type = AbilityType::PAGE;
55     abilityInfo.applicationInfo.isLauncherApp = false;
56 
57     if (elementTemp.GetAbilityName() == "MusicAbility") {
58         abilityInfo.process = "p1";
59         abilityInfo.launchMode = LaunchMode::STANDARD;
60     }
61     if (elementTemp.GetAbilityName() == "MusicTopAbility") {
62         abilityInfo.process = "p1";
63         abilityInfo.launchMode = LaunchMode::SINGLETOP;
64     }
65     if (elementTemp.GetAbilityName() == "MusicSAbility") {
66         abilityInfo.process = "p2";
67         abilityInfo.launchMode = LaunchMode::SINGLETON;
68     }
69     return true;
70 };
71 
72 auto HiRadioInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
73     abilityInfo.name = elementTemp.GetAbilityName();
74     abilityInfo.bundleName = elementTemp.GetBundleName();
75     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
76     abilityInfo.applicationName = "hiRadio";
77     abilityInfo.applicationInfo.name = "hiRadio";
78     abilityInfo.type = AbilityType::PAGE;
79     abilityInfo.process = "p3";
80     if (elementTemp.GetAbilityName() == "RadioAbility") {
81         abilityInfo.launchMode = LaunchMode::STANDARD;
82     }
83     if (elementTemp.GetAbilityName() == "RadioTopAbility") {
84         abilityInfo.launchMode = LaunchMode::SINGLETON;
85     }
86     return true;
87 };
88 
89 auto HiServiceInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
90     abilityInfo.name = elementTemp.GetAbilityName();
91     abilityInfo.bundleName = elementTemp.GetBundleName();
92     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
93     abilityInfo.applicationName = "hiService";
94     abilityInfo.applicationInfo.name = "hiService";
95     abilityInfo.type = AbilityType::SERVICE;
96     abilityInfo.process = "p4";
97     return true;
98 };
99 
100 auto MusicServiceInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
101     abilityInfo.name = elementTemp.GetAbilityName();
102     abilityInfo.bundleName = elementTemp.GetBundleName();
103     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
104     abilityInfo.applicationName = "musicService";
105     abilityInfo.applicationInfo.name = "musicService";
106     abilityInfo.type = AbilityType::SERVICE;
107     abilityInfo.process = "p5";
108     return true;
109 };
110 
111 auto HiDataInfo = [](std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp) {
112     abilityInfo.name = elementTemp.GetAbilityName();
113     abilityInfo.bundleName = elementTemp.GetBundleName();
114     abilityInfo.applicationInfo.bundleName = elementTemp.GetBundleName();
115     abilityInfo.applicationName = "hiData";
116     abilityInfo.applicationInfo.name = "hiData";
117     abilityInfo.type = AbilityType::DATA;
118     abilityInfo.process = "p6";
119     return true;
120 };
121 
122 class BundleMgrStub : public IRemoteStub<IBundleMgr> {
123 public:
124     DECLARE_INTERFACE_DESCRIPTOR(u"IBundleMgr");
125     virtual int OnRemoteRequest(
126         uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
127 };
128 
129 class BundleMgrService : public BundleMgrStub {
130 public:
131     bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override;
132     bool QueryAbilityInfoByUri(const std::string &uri, AbilityInfo &abilityInfo) override;
133     bool GetApplicationInfo(
134         const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo) override;
135 
136     bool QueryAbilityInfos(const Want &want, std::vector<AbilityInfo> &abilityInfos) override;
137     bool QueryAbilityInfosForClone(const Want &want, std::vector<AbilityInfo> &abilityInfos) override;
138     bool GetAllFormsInfo(std::vector<FormInfo> &formInfos) override;
139     bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos) override;
140     bool GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName,
141                             std::vector<FormInfo> &formInfos) override;
142     bool GetShortcutInfos(const std::string &bundleName, std::vector<ShortcutInfo> &shortcutInfos) override;
143     bool GetModuleUsageRecords(const int32_t number, std::vector<ModuleUsageRecord> &moduleUsageRecords) override;
144     bool NotifyAbilityLifeStatus(const std::string &bundleName, const std::string &abilityName,
145         const int64_t launchTime, const int uid = 0) override;
146 
147     MOCK_METHOD2(QueryWantAbility, int(const AAFwk::Want &want, std::vector<AbilityInfo> &abilityInfos));
148     MOCK_METHOD3(GetApplicationInfos,
149         bool(const ApplicationFlag flag, const int userId, std::vector<ApplicationInfo> &appInfos));
150     MOCK_METHOD3(GetBundleInfos, bool(const BundleFlag flag, std::vector<BundleInfo> &bundleInfos, int32_t userId));
151     MOCK_METHOD3(GetBundleInfos, bool(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId));
152     MOCK_METHOD2(GetUidByBundleName, int(const std::string &bundleName, const int userId));
153     MOCK_METHOD2(GetBundleNameForUid, bool(const int uid, std::string &bundleName));
154     MOCK_METHOD2(GetBundleGids, bool(const std::string &bundleName, std::vector<int> &gids));
155     MOCK_METHOD3(GetBundleGidsByUid, bool(const std::string &bundleName, const int &uid, std::vector<int> &gids));
156     MOCK_METHOD2(QueryAbilityInfosByUri, bool(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos));
157     MOCK_METHOD3(CheckPermissionByUid,
158         int(const std::string &bundleName, const std::string &permission, const int userId));
159     MOCK_METHOD2(GetAllCommonEventInfo,
160         bool(const std::string &eventKey, std::vector<CommonEventInfo> &commonEventInfos));
161     MOCK_METHOD2(RemoveClonedBundle, bool(const std::string &bundleName, const int32_t uid));
162     MOCK_METHOD1(BundleClone, bool(const std::string &bundleName));
163     MOCK_METHOD1(CheckBundleNameInAllowList, bool(const std::string &bundleName));
164     MOCK_METHOD0(GetBundleUserMgr, sptr<IBundleUserMgr>());
165     MOCK_METHOD4(GetDistributedBundleInfo, bool(const std::string &networkId, int32_t userId,
166         const std::string &bundleName, DistributedBundleInfo &distributedBundleInfo));
167     MOCK_METHOD1(GetAppType, std::string(const std::string &bundleName));
168     MOCK_METHOD1(CheckIsSystemAppByUid, bool(const int uid));
169     MOCK_METHOD2(GetBundleInfosByMetaData, bool(const std::string &metaData, std::vector<BundleInfo> &bundleInfos));
170     MOCK_METHOD1(QueryKeepAliveBundleInfos, bool(std::vector<BundleInfo> &bundleInfos));
171     MOCK_METHOD2(GetAbilityLabel, std::string(const std::string &bundleName, const std::string &className));
172     MOCK_METHOD3(
173         GetBundleArchiveInfo, bool(const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo));
174     MOCK_METHOD2(GetHapModuleInfo, bool(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo));
175     MOCK_METHOD2(GetLaunchWantForBundle, bool(const std::string &bundleName, Want &want));
176     MOCK_METHOD2(CheckPublicKeys, int(const std::string &firstBundleName, const std::string &secondBundleName));
177     MOCK_METHOD2(CheckPermission, int(const std::string &bundleName, const std::string &permission));
178     MOCK_METHOD2(GetPermissionDef, bool(const std::string &permissionName, PermissionDef &permissionDef));
179     MOCK_METHOD1(GetAllPermissionGroupDefs, bool(std::vector<PermissionDef> &permissionDefs));
180     MOCK_METHOD2(GetAppsGrantedPermissions,
181         bool(const std::vector<std::string> &permissions, std::vector<std::string> &appNames));
182     MOCK_METHOD1(HasSystemCapability, bool(const std::string &capName));
183     MOCK_METHOD1(GetSystemAvailableCapabilities, bool(std::vector<std::string> &systemCaps));
184     MOCK_METHOD0(IsSafeMode, bool());
185     MOCK_METHOD3(CleanBundleCacheFiles,
186         bool(const std::string &bundleName, const sptr<ICleanCacheCallback> &cleanCacheCallback, int32_t userId));
187     MOCK_METHOD2(CleanBundleDataFiles, bool(const std::string &bundleName, const int userId));
188     MOCK_METHOD1(RegisterBundleStatusCallback, bool(const sptr<IBundleStatusCallback> &bundleStatusCallback));
189     MOCK_METHOD1(ClearBundleStatusCallback, bool(const sptr<IBundleStatusCallback> &bundleStatusCallback));
190     MOCK_METHOD0(UnregisterBundleStatusCallback, bool());
191     MOCK_METHOD4(DumpInfos, bool(const DumpFlag flag, const std::string &bundleName,
192         int32_t userId, std::string &result));
193     MOCK_METHOD1(IsApplicationEnabled, bool(const std::string &bundleName));
194     MOCK_METHOD3(SetApplicationEnabled, bool(const std::string &bundleName, bool isEnable, int32_t userId));
195     MOCK_METHOD0(GetBundleInstaller, sptr<IBundleInstaller>());
196     MOCK_METHOD2(GetAppIdByBundleName, std::string(const std::string &bundleName, const int userId));
197     MOCK_METHOD2(GetBundlesForUid, bool(const int uid, std::vector<std::string> &bundleNames));
198     MOCK_METHOD2(GetNameForUid, bool(const int uid, std::string &name));
199     MOCK_METHOD1(IsAbilityEnabled, bool(const AbilityInfo &abilityInfo));
200     MOCK_METHOD3(SetAbilityEnabled, bool(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId));
201     MOCK_METHOD2(GetAbilityIcon, std::string(const std::string &bundleName, const std::string &className));
202     MOCK_METHOD3(CanRequestPermission, bool(const std::string &bundleName, const std::string &permissionName,
203         const int userId));
204     MOCK_METHOD3(RequestPermissionFromUser, bool(const std::string &bundleName, const std::string &permission,
205         const int userId));
206     MOCK_METHOD1(UnregisterPermissionsChanged, bool(const sptr<OnPermissionChangedCallback> &callback));
207     MOCK_METHOD2(RegisterPermissionsChanged, bool(const std::vector<int> &uids,
208         const sptr<OnPermissionChangedCallback> &callback));
209     MOCK_METHOD1(RegisterAllPermissionsChanged, bool(const sptr<OnPermissionChangedCallback> &callback));
210 
211     bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag,
212         BundleInfo &bundleInfo, int32_t userId = Constants::UNSPECIFIED_USERID) override;
213 
BundleMgrService()214     BundleMgrService()
215     {
216         abilityInfoMap_.emplace(COM_IX_HIWORLD, HiWordInfo);
217         abilityInfoMap_.emplace(COM_IX_HIMUSIC, HiMusicInfo);
218         abilityInfoMap_.emplace(COM_IX_HIRADIO, HiRadioInfo);
219         abilityInfoMap_.emplace(COM_IX_HISERVICE, HiServiceInfo);
220         abilityInfoMap_.emplace(COM_IX_MUSICSERVICE, MusicServiceInfo);
221         abilityInfoMap_.emplace(COM_IX_HIDATA, HiDataInfo);
222     }
223 
~BundleMgrService()224     virtual ~BundleMgrService()
225     {}
226 
227     bool CheckWantEntity(const AAFwk::Want &, AbilityInfo &);
228 
229 public:
230     using QueryAbilityInfoFunType =
231         std::function<bool(std::string bundleName, AbilityInfo &abilityInfo, ElementName &elementTemp)>;
232     std::map<std::string, QueryAbilityInfoFunType> abilityInfoMap_;
233 };
234 } // namespace AppExecFwk
235 } // namespace OHOS
236 
237 #endif  // OHOS_AAFWK_ABILITY_MOCK_BUNDLE_MANAGER_H
238