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 "event_bus_test.h"
17
18 #include <gtest/gtest.h>
19 #include <memory>
20 #include <new>
21 #include <sys/time.h>
22
23 #include "distributed_hardware_log.h"
24 #include "event_bus.h"
25 #include "event_registration.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace DistributedHardware {
31 namespace {
32 FakeObject *g_obj = nullptr;
33 FakeSender *g_sender = nullptr;
34 FakeListener *g_listener = nullptr;
35 std::shared_ptr<OHOS::DistributedHardware::EventRegistration> g_regHandler = nullptr;
36 EventBus* g_eventBus = nullptr;
37 const int32_t TEST_TWENTY_MS = 20000;
38 }
39
SetUpTestCase(void)40 void EventbusTest::SetUpTestCase(void)
41 {
42 }
43
TearDownTestCase(void)44 void EventbusTest::TearDownTestCase(void)
45 {
46 }
47
SetUp()48 void EventbusTest::SetUp()
49 {
50 g_obj = new(std::nothrow) FakeObject("Player");
51 if (g_obj == nullptr) {
52 DHLOGE("obj is null, because applying memory fail!");
53 return;
54 }
55 g_listener = new(std::nothrow) FakeListener();
56 if (g_listener == nullptr) {
57 DHLOGE("listener is null, because applying memory fail!");
58 return;
59 }
60 g_sender = new(std::nothrow) FakeSender();
61 if (g_sender == nullptr) {
62 DHLOGE("sender is null, because applying memory fail!");
63 return;
64 }
65 g_regHandler = nullptr;
66 g_eventBus = new(std::nothrow) EventBus();
67 if (g_eventBus == nullptr) {
68 DHLOGE("eventBus is null, because applying memory fail!");
69 return;
70 }
71 }
72
TearDown()73 void EventbusTest::TearDown()
74 {
75 usleep(TEST_TWENTY_MS);
76 if (g_obj != nullptr) {
77 delete g_obj;
78 g_obj = nullptr;
79 }
80 if (g_listener != nullptr) {
81 delete g_listener;
82 g_listener = nullptr;
83 }
84 if (g_sender != nullptr) {
85 delete g_sender;
86 g_sender = nullptr;
87 }
88 g_regHandler = nullptr;
89
90 if (g_eventBus != nullptr) {
91 delete g_eventBus;
92 g_eventBus = nullptr;
93 }
94 }
95
96 /**
97 * @tc.name: event_bus_test_001
98 * @tc.desc: Verify the EventBus AddHandler function.
99 * @tc.type: FUNC
100 * @tc.require: AR000GHSK9
101 */
102 HWTEST_F(EventbusTest, event_bus_test_001, TestSize.Level0)
103 {
104 EXPECT_EQ(g_regHandler, nullptr);
105
106 FakeEvent e(*g_sender, *g_obj);
107 g_regHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
108
109 EXPECT_NE(g_regHandler, nullptr);
110 EXPECT_EQ(g_regHandler->GetSender(), nullptr);
111 EXPECT_EQ(g_regHandler->GetHandler(), (void *)g_listener);
112 }
113
114 /**
115 * @tc.name: event_bus_test_002
116 * @tc.desc: Verify the EventBus AddHandler function.
117 * @tc.type: FUNC
118 * @tc.require: AR000GHSK9
119 */
120 HWTEST_F(EventbusTest, event_bus_test_002, TestSize.Level0)
121 {
122 EXPECT_EQ(g_regHandler, nullptr);
123
124 FakeEvent e(*g_sender, *g_obj);
125 g_regHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
126 std::shared_ptr<EventRegistration> secondHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
127
128 EXPECT_NE(secondHandler, nullptr);
129 EXPECT_EQ(g_regHandler, secondHandler);
130 }
131
132 /**
133 * @tc.name: event_bus_test_003
134 * @tc.desc: Verify the EventBus AddHandler with sender function.
135 * @tc.type: FUNC
136 * @tc.require: AR000GHSK9
137 */
138 HWTEST_F(EventbusTest, event_bus_test_003, TestSize.Level0)
139 {
140 EXPECT_EQ(g_regHandler, nullptr);
141
142 FakeEvent e(*g_sender, *g_obj);
143 g_regHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
144 std::shared_ptr<EventRegistration> secondHandler =
145 g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener, *g_sender);
146
147 EXPECT_NE(g_regHandler, nullptr);
148 EXPECT_NE(secondHandler, nullptr);
149 EXPECT_NE(g_regHandler, secondHandler);
150 EXPECT_EQ(secondHandler->GetSender(), g_sender);
151 EXPECT_EQ(secondHandler->GetHandler(), (void *)g_listener);
152 }
153
154 /**
155 * @tc.name: event_bus_test_004
156 * @tc.desc: Verify the EventBus AddHandler and RemoveHandler function.
157 * @tc.type: FUNC
158 * @tc.require: AR000GHSK9
159 */
160 HWTEST_F(EventbusTest, event_bus_test_004, TestSize.Level0)
161 {
162 EXPECT_EQ(g_regHandler, nullptr);
163 FakeEvent e(*g_sender, *g_obj);
164 EXPECT_EQ(false, g_eventBus->RemoveHandler<FakeEvent>(e.GetType(), g_regHandler));
165
166 g_regHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
167 EXPECT_EQ(true, g_eventBus->RemoveHandler<FakeEvent>(e.GetType(), g_regHandler));
168 }
169
170 /**
171 * @tc.name: event_bus_test_005
172 * @tc.desc: Verify the EventBus PostEvent function.
173 * @tc.type: FUNC
174 * @tc.require: AR000GHSK9
175 */
176 HWTEST_F(EventbusTest, event_bus_test_005, TestSize.Level0)
177 {
178 EXPECT_EQ(0, g_obj->GetAge());
179
180 FakeEvent e(*g_sender, *g_obj, 10);
181 g_regHandler = g_eventBus->AddHandler<FakeEvent>(e.GetType(), *g_listener);
182 g_eventBus->PostEvent<FakeEvent>(e, POSTMODE::POST_SYNC);
183 EXPECT_EQ(10, g_obj->GetAge());
184 }
185 } // namespace DistributedHardware
186 } // namespace OHOS
187