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 OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H 17 #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H 18 19 #include <memory> 20 #include <string> 21 22 #include <gtest/gtest.h> 23 24 #include "event_handler.h" 25 26 #include "event.h" 27 #include "event_bus.h" 28 #include "event_sender.h" 29 #include "eventbus_handler.h" 30 31 namespace OHOS { 32 namespace DistributedHardware { 33 class FakeObject { 34 public: FakeObject(std::string name)35 explicit FakeObject(std::string name) : name_(name), age_(0) {} ~FakeObject()36 virtual ~FakeObject() {} SetAge(int32_t age)37 void SetAge(int32_t age) 38 { 39 age_ = age; 40 } GetAge()41 int32_t GetAge() const 42 { 43 return age_; 44 } 45 46 private: 47 std::string name_; 48 int32_t age_; 49 }; 50 51 class FakeEvent : public OHOS::DistributedHardware::Event { TYPEINDENT(FakeEvent)52 TYPEINDENT(FakeEvent) 53 public: 54 FakeEvent(OHOS::DistributedHardware::EventSender &sender, FakeObject &object) 55 : Event(sender), object_(object), action_(0) 56 {} FakeEvent(OHOS::DistributedHardware::EventSender & sender,FakeObject & object,int32_t action)57 FakeEvent(OHOS::DistributedHardware::EventSender &sender, FakeObject &object, int32_t action) 58 : Event(sender), object_(object), action_(action) 59 {} ~FakeEvent()60 virtual ~FakeEvent() {} 61 GetObject()62 FakeObject *GetObject() const 63 { 64 return &object_; 65 } GetAction()66 int32_t GetAction() const 67 { 68 return action_; 69 } 70 71 private: 72 FakeObject &object_; 73 int32_t action_; 74 }; 75 76 class FakeListener : public OHOS::DistributedHardware::EventBusHandler<FakeEvent> { 77 public: FakeListener()78 FakeListener() {} ~FakeListener()79 virtual ~FakeListener() {} 80 OnEvent(FakeEvent & ev)81 virtual void OnEvent(FakeEvent &ev) 82 { 83 FakeObject *obj = ev.GetObject(); 84 obj->SetAge(ev.GetAction()); 85 } 86 }; 87 88 class FakeSender : public OHOS::DistributedHardware::EventSender { 89 public: FakeSender()90 FakeSender() {} ~FakeSender()91 virtual ~FakeSender() {} 92 }; 93 94 class EventbusTest : public testing::Test { 95 public: 96 static void SetUpTestCase(void); 97 static void TearDownTestCase(void); 98 void SetUp(); 99 void TearDown(); 100 }; 101 } 102 } 103 #endif