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 OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H
17 #define OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H
18 
19 #include <vector>
20 
21 #include "ability_info.h"
22 #include "application_info.h"
23 #include "bundle_mgr_interface.h"
24 #include "gmock/gmock.h"
25 #include "iremote_proxy.h"
26 #include "iremote_stub.h"
27 #include "want.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 const int32_t APP_600 = 600;
32 const uint32_t COMPATIBLE_VERSION = 1;
33 const uint32_t TARGET_VERSION = 1;
34 
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 
42     MOCK_METHOD5(QueryAbilityInfo, bool(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo,
43         const sptr<IRemoteObject> &callBack));
QueryAbilityInfo(const AAFwk::Want & want,AbilityInfo & abilityInfo)44     bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override
45     {
46         return true;
47     }
48 
GetAppType(const std::string & bundleName)49     std::string GetAppType(const std::string &bundleName) override
50     {
51         return "system";
52     }
53 
GetUidByBundleName(const std::string & bundleName,const int userId)54     virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override
55     {
56         if (bundleName.compare("com.form.host.app600") == 0) {
57             return APP_600;
58         }
59         return 0;
60     }
61 
GetBundleNameForUid(const int uid,std::string & bundleName)62     virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override
63     {
64         bundleName = "com.form.provider.service";
65         return true;
66     }
67 
68     virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfo) override;
69     virtual bool GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName,
70     std::vector<FormInfo> &formInfo) override;
71 };
72 
73 class BundleMgrStub : public IRemoteStub<IBundleMgr> {
74 public:
75     virtual int OnRemoteRequest(
76         uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
77 };
78 
79 class BundleMgrService : public BundleMgrStub {
80 public:
81     static bool IsSystemApp;
82 
83     bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override;
84     MOCK_METHOD5(QueryAbilityInfo, bool(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo,
85         const sptr<IRemoteObject> &callBack));
86 
87     virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override;
88 
89     virtual bool GetBundleInfo(
90         const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override;
91 
92     virtual ErrCode GetBundleInfoV9(const std::string &bundleName, int32_t flags,
93         BundleInfo &bundleInfo, int32_t userId) override;
94 
GetBundleNameForUid(const int uid,std::string & bundleName)95     virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override
96     {
97         bundleName = "com.form.provider.service";
98         return true;
99     };
100 
101     virtual std::string GetAppType(const std::string &bundleName);
102 
CheckIsSystemAppByUid(const int uid)103     virtual bool CheckIsSystemAppByUid(const int uid) override
104     {
105         return IsSystemApp;
106     }
107 
108     virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfo) override;
109     virtual bool GetFormsInfoByModule(const std::string &bundleName, const std::string &moduleName,
110         std::vector<FormInfo> &formInfo) override;
111 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)112     virtual bool ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
113         AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo) override
114     {
115         abilityInfo.name = "MainAbility";
116         abilityInfo.bundleName = "com.ohos.launcher";
117         extensionInfo.name = "MainAbility";
118         extensionInfo.bundleName = "com.ohos.launcher";
119         return true;
120     }
121 
122     MOCK_METHOD3(SetModuleRemovable, bool(const std::string &bundleName, const std::string &moduleName, bool isEnable));
123 
124     MOCK_METHOD2(GetNameForUid, int32_t(const int, std::string &));
125 };
126 
127 class MockBundleMgrService : public BundleMgrService {
128 public:
129     MOCK_METHOD(bool, GetBundleInfo,
130         (const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId), (override));
131 
132     MOCK_METHOD(ErrCode, GetBundleInfoV9,
133         (const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId), (override));
134 
135     MOCK_METHOD(ErrCode, GetApplicationInfoV9,
136         (const std::string &, int32_t, int32_t, ApplicationInfo &), (override));
137 
138     MOCK_METHOD2(GetBundleNameForUid, bool(const int, std::string &));
139 
140     MOCK_METHOD1(CheckIsSystemAppByUid, bool(const int));
141 
142     MOCK_METHOD2(GetNameForUid, int32_t(const int, std::string &));
143 };
144 } // namespace AppExecFwk
145 } // namespace OHOS
146 
147 #endif // OHOS_FORM_FWK_MOCK_BUNDLE_MANAGER_H
148