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