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 #define LOG_TAG "EventCenterTest"
16 #include "eventcenter/event_center.h"
17 #include "gtest/gtest.h"
18 #include "log_print.h"
19 using namespace testing::ext;
20 using namespace OHOS::DistributedData;
21 class EventCenterTest : public testing::Test {
22 public:
23     enum TestEventId {
24         TEST_EVT_UNKNOWN = Event::EVT_CUSTOM,
25         TEST_EVT_BEGIN = Event::EVT_CUSTOM + 1,
26         TEST_EVT_MIDDLE,
27         TEST_EVT_END,
28     };
29     class TestBegin : public Event {
30     public:
TestBegin()31         TestBegin(): Event(TEST_EVT_BEGIN) {};
32     };
33     class TestMiddle : public Event {
34     public:
TestMiddle()35         TestMiddle(): Event(TEST_EVT_MIDDLE) {};
36     };
37     class TestEnd : public Event {
38     public:
TestEnd()39         TestEnd(): Event(TEST_EVT_END) {};
40     };
SetUpTestCase(void)41     static void SetUpTestCase(void) {}
TearDownTestCase(void)42     static void TearDownTestCase(void) {}
SetUp()43     void SetUp()
44     {
45         waitEvent_ = TEST_EVT_UNKNOWN;
46         currEvent_ = TEST_EVT_UNKNOWN;
47         EventCenter::GetInstance().Subscribe(TEST_EVT_BEGIN, [this](const Event &evt) {
48             ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN);
49             EventCenter::Defer defer;
50             EventCenter::GetInstance().PostEvent(std::make_unique<TestMiddle>());
51             currEvent_ = TEST_EVT_BEGIN;
52             waitEvent_ = TEST_EVT_MIDDLE;
53         });
54         EventCenter::GetInstance().Subscribe(TEST_EVT_MIDDLE, [this](const Event &evt) {
55             ASSERT_EQ(waitEvent_, TEST_EVT_MIDDLE);
56             EventCenter::Defer defer;
57             EventCenter::GetInstance().PostEvent(std::make_unique<TestEnd>());
58             currEvent_ = TEST_EVT_MIDDLE;
59             waitEvent_ = TEST_EVT_END;
60         });
61         EventCenter::GetInstance().Subscribe(TEST_EVT_END, [this](const Event &evt) {
62             ASSERT_EQ(waitEvent_, TEST_EVT_END);
63             currEvent_ = TEST_EVT_END;
64             waitEvent_ = TEST_EVT_UNKNOWN;
65         });
66     }
67 
TearDown()68     void TearDown()
69     {
70         EventCenter::GetInstance().Unsubscribe(TEST_EVT_BEGIN);
71         EventCenter::GetInstance().Unsubscribe(TEST_EVT_MIDDLE);
72         EventCenter::GetInstance().Unsubscribe(TEST_EVT_END);
73         waitEvent_ = TEST_EVT_UNKNOWN;
74         currEvent_ = TEST_EVT_UNKNOWN;
75     }
76 
77 protected:
78     int32_t waitEvent_ = TEST_EVT_UNKNOWN;
79     int32_t currEvent_ = TEST_EVT_UNKNOWN;
80 };
81 
82 /**
83 * @tc.name: TopLayerASyncEvent
84 * @tc.desc: the async event on the top layer will dispatch, until the function completed.
85 * @tc.type: FUNC
86 * @tc.require:
87 * @tc.author: Sven Wang
88 */
89 HWTEST_F(EventCenterTest, TopLayerASyncEvent, TestSize.Level2)
90 {
91     ZLOGI("EventCenterSuite ASyncEvent begin.");
__anon5dc09fc60402() 92     auto test = [this]() {
93         EventCenter::Defer defer;
94         EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
95         waitEvent_ = TEST_EVT_BEGIN;
96     };
97     test();
98     ASSERT_EQ(currEvent_, TEST_EVT_END);
99     ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN);
100 }
101 
102 /**
103 * @tc.name: SubLayerASyncEvent
104 * @tc.desc: the async event on sub layer will defer to dispatch, until the top layer function completed.
105 * @tc.type: FUNC
106 * @tc.require:
107 * @tc.author: Sven Wang
108 */
109 HWTEST_F(EventCenterTest, SubLayerASyncEvent, TestSize.Level2)
110 {
111     ZLOGI("EventCenterSuite ASyncEvent begin.");
112     EventCenter::Defer defer;
113     EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
114     waitEvent_ = TEST_EVT_BEGIN;
115     ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN);
116     ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN);
117 }
118 
119 /**
120 * @tc.name: ASyncEventWithoutDefer
121 * @tc.desc: async event without defer may call or not
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author: Sven Wang
125 */
126 HWTEST_F(EventCenterTest, ASyncEventWithoutDefer, TestSize.Level2)
127 {
128     ZLOGI("EventCenterSuite ASyncEvent begin.");
129     EventCenter::Defer defer;
130     waitEvent_ = TEST_EVT_BEGIN;
__anon5dc09fc60502() 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 /**
141 * @tc.name: ImmediatelyASyncEvent
142 * @tc.desc: post the async event, there is top layer and no defer; we will dispatch the async event Immediately.
143 * @tc.type: FUNC
144 * @tc.require:
145 * @tc.author: Sven Wang
146 */
147 HWTEST_F(EventCenterTest, ImmediatelyASyncEvent, TestSize.Level2)
148 {
149     ZLOGI("EventCenterSuite ASyncEvent begin.");
150     waitEvent_ = TEST_EVT_BEGIN;
151     EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>());
152     ASSERT_EQ(currEvent_, TEST_EVT_END);
153     ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN);
154 }
155