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 #include "mock_common_event_stub.h"
17 #include "refbase.h"
18 namespace OHOS {
19 namespace EventFwk {
20
21 sptr<MockCommonEventStub> MockCommonEventStub::instance_;
22 std::mutex MockCommonEventStub::instanceMutex_;
23
GetInstance()24 OHOS::sptr<MockCommonEventStub> MockCommonEventStub::GetInstance()
25 {
26 std::lock_guard<std::mutex> lock(instanceMutex_);
27
28 if (instance_ == nullptr) {
29 instance_ = new (std::nothrow) MockCommonEventStub();
30 if (instance_ == nullptr) {
31 return nullptr;
32 }
33 }
34
35 return instance_;
36 }
37
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,int32_t userId,int32_t & funcResult)38 ErrCode MockCommonEventStub::PublishCommonEvent(
39 const CommonEventData& event,
40 const CommonEventPublishInfo& publishInfo,
41 int32_t userId,
42 int32_t& funcResult)
43 {
44 EVENT_LOGI("enter");
45 funcResult = ERR_OK;
46 return ERR_OK;
47 }
48
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,int32_t userId,int32_t & funcResult)49 ErrCode MockCommonEventStub::PublishCommonEvent(
50 const CommonEventData& event,
51 const CommonEventPublishInfo& publishInfo,
52 const sptr<IRemoteObject>& commonEventListener,
53 int32_t userId,
54 int32_t& funcResult)
55 {
56 EVENT_LOGI("enter");
57 funcResult = ERR_OK;
58 return ERR_OK;
59 }
60
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,uint32_t uid,int32_t callerToken,int32_t userId,bool & funcResult)61 ErrCode MockCommonEventStub::PublishCommonEvent(
62 const CommonEventData& event,
63 const CommonEventPublishInfo& publishInfo,
64 uint32_t uid,
65 int32_t callerToken,
66 int32_t userId,
67 bool& funcResult)
68 {
69 EVENT_LOGI("enter");
70 funcResult = ERR_OK;
71 return ERR_OK;
72 }
73
PublishCommonEvent(const CommonEventData & event,const CommonEventPublishInfo & publishInfo,const sptr<IRemoteObject> & commonEventListener,uint32_t uid,int32_t callerToken,int32_t userId,bool & funcResult)74 ErrCode MockCommonEventStub::PublishCommonEvent(
75 const CommonEventData& event,
76 const CommonEventPublishInfo& publishInfo,
77 const sptr<IRemoteObject>& commonEventListener,
78 uint32_t uid,
79 int32_t callerToken,
80 int32_t userId,
81 bool& funcResult)
82 {
83 EVENT_LOGI("enter");
84 funcResult = true;
85 return ERR_OK;
86 }
87
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,int32_t instanceKey,int32_t & funcResult)88 int32_t MockCommonEventStub::SubscribeCommonEvent(
89 const CommonEventSubscribeInfo& subscribeInfo,
90 const sptr<IRemoteObject>& commonEventListener,
91 int32_t instanceKey,
92 int32_t& funcResult)
93 {
94 EVENT_LOGI("enter");
95
96 subscribeInfoPtr = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
97
98 funcResult = ERR_OK;
99 return ERR_OK;
100 }
101
UnsubscribeCommonEvent(const sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)102 ErrCode MockCommonEventStub::UnsubscribeCommonEvent(
103 const sptr<IRemoteObject>& commonEventListener,
104 int32_t& funcResult)
105 {
106 EVENT_LOGI("enter");
107 funcResult = true;
108 return ERR_OK;
109 }
110
UnsubscribeCommonEventSync(const sptr<IRemoteObject> & commonEventListener,int32_t & funcResult)111 ErrCode MockCommonEventStub::UnsubscribeCommonEventSync(
112 const sptr<IRemoteObject>& commonEventListener,
113 int32_t& funcResult)
114 {
115 EVENT_LOGI("enter");
116 funcResult = true;
117 return ERR_OK;
118 }
119
GetStickyCommonEvent(const std::string & event,CommonEventData & eventData,bool & funcResult)120 ErrCode MockCommonEventStub::GetStickyCommonEvent(
121 const std::string& event,
122 CommonEventData& eventData,
123 bool& funcResult)
124 {
125 EVENT_LOGI("enter");
126 funcResult = true;
127 return ERR_OK;
128 }
129
DumpState(uint8_t dumpType,const std::string & event,int32_t userId,std::vector<std::string> & state,bool & funcResult)130 ErrCode MockCommonEventStub::DumpState(
131 uint8_t dumpType,
132 const std::string& event,
133 int32_t userId,
134 std::vector<std::string>& state,
135 bool& funcResult)
136 {
137 EVENT_LOGI("enter");
138
139 if (subscribeInfoPtr) {
140 // get matchingSkills
141 auto matchingSkills = subscribeInfoPtr->GetMatchingSkills();
142 // get events
143 auto events = matchingSkills.GetEvents();
144 EVENT_LOGI("event size %{public}zu", events.size());
145
146 for (auto it : events) {
147 state.emplace_back(it);
148 }
149 } else {
150 // do nothing
151 }
152
153 subscribeInfoPtr = nullptr;
154 funcResult = true;
155 return ERR_OK;
156 }
157
FinishReceiver(const sptr<IRemoteObject> & proxy,int32_t code,const std::string & receiverData,bool abortEvent,bool & funcResult)158 ErrCode MockCommonEventStub::FinishReceiver(
159 const sptr<IRemoteObject>& proxy,
160 int32_t code,
161 const std::string& receiverData,
162 bool abortEvent,
163 bool& funcResult)
164 {
165 EVENT_LOGI("enter");
166 funcResult = true;
167 return ERR_OK;
168 }
169
Freeze(uint32_t uid,bool & funcResult)170 ErrCode MockCommonEventStub::Freeze(
171 uint32_t uid,
172 bool& funcResult)
173 {
174 EVENT_LOGI("enter");
175 funcResult = true;
176 return ERR_OK;
177 }
178
Unfreeze(uint32_t uid,bool & funcResult)179 ErrCode MockCommonEventStub::Unfreeze(
180 uint32_t uid,
181 bool& funcResult)
182 {
183 EVENT_LOGI("enter");
184 funcResult = true;
185 return ERR_OK;
186 }
187
UnfreezeAll(bool & funcResult)188 ErrCode MockCommonEventStub::UnfreezeAll(
189 bool& funcResult)
190 {
191 EVENT_LOGI("enter");
192 funcResult = true;
193 return ERR_OK;
194 }
195
RemoveStickyCommonEvent(const std::string & event,int32_t & funcResult)196 ErrCode MockCommonEventStub::RemoveStickyCommonEvent(
197 const std::string& event,
198 int32_t& funcResult)
199 {
200 EVENT_LOGI("enter");
201 funcResult = ERR_OK;
202 return ERR_OK;
203 }
204
SetStaticSubscriberState(bool enable,int32_t & funcResult)205 ErrCode MockCommonEventStub::SetStaticSubscriberState(
206 bool enable,
207 int32_t& funcResult)
208 {
209 EVENT_LOGI("enter");
210 funcResult = ERR_OK;
211 return ERR_OK;
212 }
213
SetStaticSubscriberStateByEvents(const std::vector<std::string> & events,bool enable,int32_t & funcResult)214 ErrCode MockCommonEventStub::SetStaticSubscriberStateByEvents(
215 const std::vector<std::string>& events,
216 bool enable,
217 int32_t& funcResult)
218 {
219 EVENT_LOGI("enter");
220 funcResult = ERR_OK;
221 return ERR_OK;
222 }
223
SetFreezeStatus(const std::set<int32_t> & pidList,bool isFreeze,bool & funcResult)224 ErrCode MockCommonEventStub::SetFreezeStatus(
225 const std::set<int32_t>& pidList,
226 bool isFreeze,
227 bool& funcResult)
228 {
229 EVENT_LOGI("enter");
230 funcResult = ERR_OK;
231 return ERR_OK;
232 }
233 } // namespace EventFwk
234 } // namespace OHOS
235