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