1 /*
2  * Copyright (c) 2021 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_FORM_MGR_SERVICE_H
17 #define MOCK_OHOS_ABILITY_RUNTIME_MOCK_FORM_MGR_SERVICE_H
18 
19 #include <codecvt>
20 #include <memory>
21 #include <singleton.h>
22 #include <thread_ex.h>
23 #include <unordered_map>
24 #include <vector>
25 
26 #include "event_handler.h"
27 #include "form_constants.h"
28 #include "form_mgr_stub.h"
29 #include "form_js_info.h"
30 #include "form_provider_data.h"
31 #include "ipc_types.h"
32 #include "iremote_object.h"
33 
34 namespace OHOS {
35 namespace AppExecFwk {
36 /**
37  * @class MockFormMgrService.
38  * The mockFormMgrService for ability test.
39  */
40 class MockFormMgrService : public IFormMgr {
41 public:
MockFormMgrService()42     MockFormMgrService() {};
~MockFormMgrService()43     virtual ~MockFormMgrService() {};
44 
AsObject()45     sptr<IRemoteObject> AsObject()
46     {
47         return nullptr;
48     }
49 
50     /**
51      * @brief Start envent for the form manager service.
52      */
OnStart()53     void OnStart() {}
54 
55     /**
56      * @brief Stop envent for the form manager service.
57      */
OnStop()58     void OnStop() {}
59 
60     /**
61      * @brief Add form with want, send want to form manager service.
62      * @param formId The Id of the forms to add.
63      * @param want The want of the form to add.
64      * @param callerToken Caller ability token.
65      * @param formInfo Form info.
66      * @return Returns ERR_OK on success, others on failure.
67      */
AddForm(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken,FormJsInfo & formInfo)68     int AddForm(const int64_t formId, const Want& want, const sptr<IRemoteObject>& callerToken, FormJsInfo& formInfo)
69     {
70         if (formId == 300L) {
71             formInfo.formId = formId;
72         }
73         return 0;
74     }
75 
76     /**
77      * @brief Delete forms with formIds, send formIds to form manager service.
78      * @param formId The Id of the forms to delete.
79      * @param callerToken Caller ability token.
80      * @return Returns ERR_OK on success, others on failure.
81      */
DeleteForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)82     int DeleteForm(const int64_t formId, const sptr<IRemoteObject>& callerToken)
83     {
84         return 0;
85     }
86 
87     /**
88      * @brief Release forms with formIds, send formIds to form manager service.
89      * @param formId The Id of the forms to release.
90      * @param callerToken Caller ability token.
91      * @param delCache Delete Cache or not.
92      * @return Returns ERR_OK on success, others on failure.
93      */
ReleaseForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const bool delCache)94     int ReleaseForm(const int64_t formId, const sptr<IRemoteObject>& callerToken, const bool delCache)
95     {
96         return 0;
97     }
98 
99     /**
100      * @brief Update form with formId, send formId to form manager service.
101      * @param formId The Id of the form to update.
102      * @param formBindingData Form binding data.
103      * @return Returns ERR_OK on success, others on failure.
104      */
UpdateForm(const int64_t formId,const FormProviderData & formBindingData)105     int UpdateForm(const int64_t formId, const FormProviderData& formBindingData)
106     {
107         if (formId == 300L) {
108             return -1;
109         }
110         return 0;
111     }
112 
113     /**
114      * @brief Request form with formId and want, send formId and want to form manager service.
115      * @param formId The Id of the form to update.
116      * @param callerToken Caller ability token.
117      * @param want The want of the form to add.
118      * @return Returns ERR_OK on success, others on failure.
119      */
RequestForm(const int64_t formId,const sptr<IRemoteObject> & callerToken,const Want & want)120     int RequestForm(const int64_t formId, const sptr<IRemoteObject>& callerToken, const Want& want)
121     {
122         return 0;
123     }
124 
125     /**
126      * @brief Form visible/invisible notify, send formIds to form manager service.
127      * @param formIds The Id list of the forms to notify.
128      * @param callerToken Caller ability token.
129      * @return Returns ERR_OK on success, others on failure.
130      */
NotifyWhetherVisibleForms(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t formVisibleType)131     int NotifyWhetherVisibleForms(
132         const std::vector<int64_t>& formIds,
133         const sptr<IRemoteObject>& callerToken,
134         const int32_t formVisibleType)
135     {
136         if (formIds.size() == 1) {
137             return 0;
138         }
139         return 1;
140     }
141 
142     /**
143      * @brief temp form to normal form.
144      * @param formId The Id of the form.
145      * @param callerToken Caller ability token.
146      * @return Returns ERR_OK on success, others on failure.
147      */
CastTempForm(const int64_t formId,const sptr<IRemoteObject> & callerToken)148     int CastTempForm(const int64_t formId, const sptr<IRemoteObject>& callerToken)
149     {
150         if (formId == 175L)return 0;
151         return 1;
152     }
153 
154     /**
155      * @brief Dump all of form storage infos.
156      * @param formInfos All of form storage infos.
157      * @return Returns ERR_OK on success, others on failure.
158      */
DumpStorageFormInfos(std::string & formInfos)159     int DumpStorageFormInfos(std::string& formInfos)
160     {
161         return 0;
162     }
163 
164     /**
165      * @brief Dump form info by a bundle name.
166      * @param bundleName The bundle name of form provider.
167      * @param formInfos Form infos.
168      * @return Returns ERR_OK on success, others on failure.
169      */
DumpFormInfoByBundleName(const std::string & bundleName,std::string & formInfos)170     int DumpFormInfoByBundleName(const std::string& bundleName, std::string& formInfos)
171     {
172         return 0;
173     }
174 
175     /**
176      * @brief Dump form info by a bundle name.
177      * @param formId The id of the form.
178      * @param formInfo Form info.
179      * @return Returns ERR_OK on success, others on failure.
180      */
DumpFormInfoByFormId(const std::int64_t formId,std::string & formInfo)181     int DumpFormInfoByFormId(const std::int64_t formId, std::string& formInfo)
182     {
183         return 0;
184     }
185 
186     /**
187      * @brief Set next refresh time.
188      * @param formId The Id of the form.
189      * @param nextTime The next refresh time.
190      * @return Returns ERR_OK on success, others on failure.
191      */
SetNextRefreshTime(const int64_t formId,const int64_t nextTime)192     int SetNextRefreshTime(const int64_t formId, const int64_t nextTime)
193     {
194         if (formId >= 1) {
195             return ERR_OK;
196         } else {
197             return ERR_INVALID_VALUE;
198         }
199     }
GetRunningFormInfos(bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)200     ErrCode GetRunningFormInfos(bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
201     {
202         return ERR_OK;
203     }
204 
GetRunningFormInfosByBundleName(const std::string & bundleName,bool isUnusedIncluded,std::vector<RunningFormInfo> & runningFormInfos)205     ErrCode GetRunningFormInfosByBundleName(
206         const std::string &bundleName, bool isUnusedIncluded, std::vector<RunningFormInfo> &runningFormInfos)
207     {
208         return ERR_OK;
209     }
210 
RegisterFormAddObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)211     ErrCode RegisterFormAddObserverByBundle(const std::string bundleName,
212         const sptr<IRemoteObject> &callerToken)
213     {
214         return ERR_OK;
215     }
216 
RegisterFormRemoveObserverByBundle(const std::string bundleName,const sptr<IRemoteObject> & callerToken)217     ErrCode RegisterFormRemoveObserverByBundle(const std::string bundleName,
218         const sptr<IRemoteObject> &callerToken)
219     {
220         return ERR_OK;
221     }
222 
RegisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)223     ErrCode RegisterClickEventObserver(
224         const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer) override
225     {
226         return ERR_OK;
227     }
228 
UnregisterClickEventObserver(const std::string & bundleName,const std::string & formEventType,const sptr<IRemoteObject> & observer)229     ErrCode UnregisterClickEventObserver(
230         const std::string &bundleName, const std::string &formEventType, const sptr<IRemoteObject> &observer) override
231     {
232         return ERR_OK;
233     }
234 
235     /**
236      * @brief Lifecycle Update.
237      * @param formIds The Id list of the forms to notify.
238      * @param callerToken Caller ability token.
239      * @param updateType The update type.
240      * @return Returns ERR_OK on success, others on failure.
241      */
LifecycleUpdate(const std::vector<int64_t> & formIds,const sptr<IRemoteObject> & callerToken,const int32_t updateType)242     int LifecycleUpdate(
243         const std::vector<int64_t>& formIds,
244         const sptr<IRemoteObject>& callerToken,
245         const int32_t updateType)
246     {
247         if (formIds.size() == 0) {
248             return ERR_INVALID_VALUE;
249         }
250         return ERR_OK;
251     }
252 
253     /**
254      * @brief Process js message event.
255      * @param formId Indicates the unique id of form.
256      * @param want information passed to supplier.
257      * @param callerToken Caller ability token.
258      * @return Returns true if execute success, false otherwise.
259      */
MessageEvent(const int64_t formId,const Want & want,const sptr<IRemoteObject> & callerToken)260     int MessageEvent(const int64_t formId, const Want& want, const sptr<IRemoteObject>& callerToken)
261     {
262         return 0;
263     }
264 
SetBackgroundFunction(const std::string method,const std::string params)265     int32_t SetBackgroundFunction(const std::string method, const std::string params)
266     {
267         return ERR_OK;
268     };
AcquireFormData(int64_t formId,int64_t requestCode,const sptr<IRemoteObject> & callerToken,AAFwk::WantParams & formData)269     int32_t AcquireFormData(int64_t formId, int64_t requestCode, const sptr<IRemoteObject> &callerToken,
270         AAFwk::WantParams &formData)
271     {
272         return 0;
273     }
274 };
275 }  // namespace AppExecFwk
276 }  // namespace OHOS
277 #endif  // MOCK_OHOS_ABILITY_RUNTIME_MOCK_FORM_MGR_SERVICE_H
278