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 "eventcenter/event_center.h" 17 #include <gmock/gmock.h> 18 #include <gtest/gtest.h> 19 20 namespace OHOS::MiscServices { 21 using namespace testing::ext; 22 using namespace testing; 23 class EventCenterTest : public testing::Test { 24 public: 25 enum TestEventId { 26 TEST_EVT_UNKNOWN = Event::EVT_REMOTE_CHANGE, 27 TEST_EVT_BEGIN = Event::EVT_REMOTE_CHANGE + 1, 28 TEST_EVT_MIDDLE, 29 TEST_EVT_END, 30 }; 31 class TestBegin : public Event { 32 public: TestBegin()33 TestBegin(): Event(TEST_EVT_BEGIN) {}; 34 }; 35 class TestMiddle : public Event { 36 public: TestMiddle()37 TestMiddle(): Event(TEST_EVT_MIDDLE) {}; 38 }; 39 class TestEnd : public Event { 40 public: TestEnd()41 TestEnd(): Event(TEST_EVT_END) {}; 42 }; SetUpTestCase(void)43 static void SetUpTestCase(void) {} TearDownTestCase(void)44 static void TearDownTestCase(void) {} SetUp()45 void SetUp() 46 { 47 waitEvent_ = TEST_EVT_UNKNOWN; 48 currEvent_ = TEST_EVT_UNKNOWN; 49 EventCenter::GetInstance().Subscribe(TEST_EVT_BEGIN, [this](const Event &evt) { 50 ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN); 51 EventCenter::Defer defer; 52 EventCenter::GetInstance().PostEvent(std::make_unique<TestMiddle>()); 53 currEvent_ = TEST_EVT_BEGIN; 54 waitEvent_ = TEST_EVT_MIDDLE; 55 }); 56 EventCenter::GetInstance().Subscribe(TEST_EVT_MIDDLE, [this](const Event &evt) { 57 ASSERT_EQ(waitEvent_, TEST_EVT_MIDDLE); 58 EventCenter::Defer defer; 59 EventCenter::GetInstance().PostEvent(std::make_unique<TestEnd>()); 60 currEvent_ = TEST_EVT_MIDDLE; 61 waitEvent_ = TEST_EVT_END; 62 }); 63 EventCenter::GetInstance().Subscribe(TEST_EVT_END, [this](const Event &evt) { 64 ASSERT_EQ(waitEvent_, TEST_EVT_END); 65 currEvent_ = TEST_EVT_END; 66 waitEvent_ = TEST_EVT_UNKNOWN; 67 }); 68 } 69 TearDown()70 void TearDown() 71 { 72 EventCenter::GetInstance().Unsubscribe(TEST_EVT_BEGIN); 73 EventCenter::GetInstance().Unsubscribe(TEST_EVT_MIDDLE); 74 EventCenter::GetInstance().Unsubscribe(TEST_EVT_END); 75 waitEvent_ = TEST_EVT_UNKNOWN; 76 currEvent_ = TEST_EVT_UNKNOWN; 77 } 78 79 protected: 80 int32_t waitEvent_ = TEST_EVT_UNKNOWN; 81 int32_t currEvent_ = TEST_EVT_UNKNOWN; 82 }; 83 84 /** 85 * @tc.name: TopLayerASyncEvent 86 * @tc.desc: the async event on the top layer will dispatch, until the function completed. 87 * @tc.type: FUNC 88 * @tc.require: 89 * @tc.author: Sven Wang 90 */ 91 HWTEST_F(EventCenterTest, TopLayerASyncEvent, TestSize.Level2) 92 { __anond7f7bf220402() 93 auto test = [this]() { 94 EventCenter::Defer defer; 95 EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 96 waitEvent_ = TEST_EVT_BEGIN; 97 }; 98 test(); 99 ASSERT_EQ(currEvent_, TEST_EVT_END); 100 ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN); 101 } 102 103 /** 104 * @tc.name: SubLayerASyncEvent 105 * @tc.desc: the async event on sub layer will defer to dispatch, until the top layer function completed. 106 * @tc.type: FUNC 107 * @tc.require: 108 * @tc.author: Sven Wang 109 */ 110 HWTEST_F(EventCenterTest, SubLayerASyncEvent, TestSize.Level2) 111 { 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 EventCenter::Defer defer; 129 waitEvent_ = TEST_EVT_BEGIN; __anond7f7bf220502() 130 auto test = [this]() { 131 EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 132 }; 133 test(); 134 ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN); 135 ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN); 136 } 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 } // namespace OHOS::MiscServices