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,const sptr<IRemoteObject> & commonEventListener,const int32_t & userId)38 int32_t MockCommonEventStub::PublishCommonEvent(const CommonEventData &event, const CommonEventPublishInfo &publishinfo,
39     const sptr<IRemoteObject> &commonEventListener, const int32_t &userId)
40 {
41     EVENT_LOGI("enter");
42 
43     return ERR_OK;
44 }
45 
SubscribeCommonEvent(const CommonEventSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & commonEventListener,const int32_t instanceKey)46 int32_t MockCommonEventStub::SubscribeCommonEvent(const CommonEventSubscribeInfo &subscribeInfo,
47     const sptr<IRemoteObject> &commonEventListener, const int32_t instanceKey)
48 {
49     EVENT_LOGI("enter");
50 
51     subscribeInfoPtr = std::make_shared<CommonEventSubscribeInfo>(subscribeInfo);
52 
53     return ERR_OK;
54 }
55 
DumpState(const uint8_t & dumpType,const std::string & event,const int32_t & userId,std::vector<std::string> & state)56 bool MockCommonEventStub::DumpState(const uint8_t &dumpType, const std::string &event, const int32_t &userId,
57     std::vector<std::string> &state)
58 {
59     EVENT_LOGI("enter");
60 
61     if (subscribeInfoPtr) {
62         // get matchingSkills
63         auto matchingSkills = subscribeInfoPtr->GetMatchingSkills();
64         // get events
65         auto events = matchingSkills.GetEvents();
66         EVENT_LOGI("event size %{public}zu", events.size());
67 
68         for (auto it : events) {
69             state.emplace_back(it);
70         }
71     } else {
72         // do nothing
73     }
74 
75     subscribeInfoPtr = nullptr;
76 
77     return true;
78 }
79 } // namespace EventFwk
80 } // namespace OHOS
81