• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "eventcenter/event_center.h"
20 
21 namespace OHOS::MiscServices {
22 using namespace testing::ext;
23 using namespace testing;
24 class EventCenterTest : public testing::Test {
25 public:
26     enum TestEventId {
27         TEST_EVT_UNKNOWN = Event::EVT_REMOTE_CHANGE,
28         TEST_EVT_BEGIN = Event::EVT_REMOTE_CHANGE + 1,
29         TEST_EVT_MIDDLE,
30         TEST_EVT_END,
31     };
32     class TestBegin : public Event {
33     public:
TestBegin()34         TestBegin() : Event(TEST_EVT_BEGIN) {};
35     };
36     class TestMiddle : public Event {
37     public:
TestMiddle()38         TestMiddle() : Event(TEST_EVT_MIDDLE) {};
39     };
40     class TestEnd : public Event {
41     public:
TestEnd()42         TestEnd() : Event(TEST_EVT_END) {};
43     };
SetUpTestCase(void)44     static void SetUpTestCase(void) { }
TearDownTestCase(void)45     static void TearDownTestCase(void) { }
SetUp()46     void SetUp()
47     {
48         waitEvent_ = TEST_EVT_UNKNOWN;
49         currEvent_ = TEST_EVT_UNKNOWN;
50         EventCenter::GetInstance().Subscribe(TEST_EVT_BEGIN, [this](const Event &evt) {
51             ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN);
52             EventCenter::Defer defer;
53             EventCenter::GetInstance().PostEvent(std::make_unique<TestMiddle>());
54             currEvent_ = TEST_EVT_BEGIN;
55             waitEvent_ = TEST_EVT_MIDDLE;
56         });
57         EventCenter::GetInstance().Subscribe(TEST_EVT_MIDDLE, [this](const Event &evt) {
58             ASSERT_EQ(waitEvent_, TEST_EVT_MIDDLE);
59             EventCenter::Defer defer;
60             EventCenter::GetInstance().PostEvent(std::make_unique<TestEnd>());
61             currEvent_ = TEST_EVT_MIDDLE;
62             waitEvent_ = TEST_EVT_END;
63         });
64         EventCenter::GetInstance().Subscribe(TEST_EVT_END, [this](const Event &evt) {
65             ASSERT_EQ(waitEvent_, TEST_EVT_END);
66             currEvent_ = TEST_EVT_END;
67             waitEvent_ = TEST_EVT_UNKNOWN;
68         });
69     }
70 
TearDown()71     void TearDown()
72     {
73         EventCenter::GetInstance().Unsubscribe(TEST_EVT_BEGIN);
74         EventCenter::GetInstance().Unsubscribe(TEST_EVT_MIDDLE);
75         EventCenter::GetInstance().Unsubscribe(TEST_EVT_END);
76         waitEvent_ = TEST_EVT_UNKNOWN;
77         currEvent_ = TEST_EVT_UNKNOWN;
78     }
79 
80 protected:
81     int32_t waitEvent_ = TEST_EVT_UNKNOWN;
82     int32_t currEvent_ = TEST_EVT_UNKNOWN;
83 };
84 
85 /**
86  * @tc.name: TopLayerASyncEvent
87  * @tc.desc: the async event on the top layer will dispatch, until the function completed.
88  * @tc.type: FUNC
89  * @tc.require:
90  * @tc.author: Sven Wang
91  */
92 HWTEST_F(EventCenterTest, TopLayerASyncEvent, TestSize.Level2)
93 {
__anone125dc2a0402() 94     auto test = [this]() {
95         EventCenter::Defer defer;
96         EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
97         waitEvent_ = TEST_EVT_BEGIN;
98     };
99     test();
100     ASSERT_EQ(currEvent_, TEST_EVT_END);
101     ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN);
102 }
103 
104 /**
105  * @tc.name: SubLayerASyncEvent
106  * @tc.desc: the async event on sub layer will defer to dispatch, until the top layer function completed.
107  * @tc.type: FUNC
108  * @tc.require:
109  * @tc.author: Sven Wang
110  */
111 HWTEST_F(EventCenterTest, SubLayerASyncEvent, TestSize.Level2)
112 {
113     EventCenter::Defer defer;
114     EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
115     waitEvent_ = TEST_EVT_BEGIN;
116     ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN);
117     ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN);
118 }
119 
120 /**
121  * @tc.name: ASyncEventWithoutDefer
122  * @tc.desc: async event without defer may call or not
123  * @tc.type: FUNC
124  * @tc.require:
125  * @tc.author: Sven Wang
126  */
127 HWTEST_F(EventCenterTest, ASyncEventWithoutDefer, TestSize.Level2)
128 {
129     EventCenter::Defer defer;
130     waitEvent_ = TEST_EVT_BEGIN;
__anone125dc2a0502() 131     auto test = [this]() {
132         EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
133     };
134     test();
135     ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN);
136     ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN);
137 }
138 
139 /**
140  * @tc.name: ImmediatelyASyncEvent
141  * @tc.desc: post the async event, there is top layer and no defer; we will dispatch the async event Immediately.
142  * @tc.type: FUNC
143  * @tc.require:
144  * @tc.author: Sven Wang
145  */
146 HWTEST_F(EventCenterTest, ImmediatelyASyncEvent, TestSize.Level2)
147 {
148     waitEvent_ = TEST_EVT_BEGIN;
149     EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
150     ASSERT_EQ(currEvent_, TEST_EVT_END);
151     ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN);
152 }
153 
154 /**
155  * @tc.name: PostEvent
156  * @tc.desc: post the async event.
157  * @tc.type: FUNC
158  * @tc.require:
159  * @tc.author: Sven Wang
160  */
161 HWTEST_F(EventCenterTest, PostEvent, TestSize.Level2)
162 {
163     int32_t result = EventCenter::GetInstance().PostEvent(nullptr);
164     EXPECT_EQ(result, EventCenter::CODE_INVALID_ARGS);
165 }
166 
167 /**
168  * @tc.name: SubscribeTest
169  * @tc.desc: Subscribe.
170  * @tc.type: FUNC
171  * @tc.require:
172  * @tc.author: Sven Wang
173  */
174 HWTEST_F(EventCenterTest, SubscribeTest, TestSize.Level2)
175 {
176     int32_t evtId = 1;
177     std::function<void(const Event &)> observer = nullptr;
178     bool result = EventCenter::GetInstance().Subscribe(evtId, observer);
179     EXPECT_TRUE(result);
180 }
181 
182 /**
183  * @tc.name: UnsubscribeTest
184  * @tc.desc: Unsubscribe.
185  * @tc.type: FUNC
186  * @tc.require:
187  * @tc.author: Sven Wang
188  */
189 HWTEST_F(EventCenterTest, UnsubscribeTest, TestSize.Level2)
190 {
191     int32_t evtId = 2;
192     bool result = EventCenter::GetInstance().Unsubscribe(evtId);
193     EXPECT_FALSE(result);
194 }
195 } // namespace OHOS::MiscServices