1bc03f14fSopenharmony_ci/* 2bc03f14fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3bc03f14fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4bc03f14fSopenharmony_ci * you may not use this file except in compliance with the License. 5bc03f14fSopenharmony_ci * You may obtain a copy of the License at 6bc03f14fSopenharmony_ci * 7bc03f14fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8bc03f14fSopenharmony_ci * 9bc03f14fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10bc03f14fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11bc03f14fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12bc03f14fSopenharmony_ci * See the License for the specific language governing permissions and 13bc03f14fSopenharmony_ci * limitations under the License. 14bc03f14fSopenharmony_ci */ 15bc03f14fSopenharmony_ci 16bc03f14fSopenharmony_ci#include <gmock/gmock.h> 17bc03f14fSopenharmony_ci#include <gtest/gtest.h> 18bc03f14fSopenharmony_ci 19bc03f14fSopenharmony_ci#include "eventcenter/event_center.h" 20bc03f14fSopenharmony_ci 21bc03f14fSopenharmony_cinamespace OHOS::MiscServices { 22bc03f14fSopenharmony_ciusing namespace testing::ext; 23bc03f14fSopenharmony_ciusing namespace testing; 24bc03f14fSopenharmony_ciclass EventCenterTest : public testing::Test { 25bc03f14fSopenharmony_cipublic: 26bc03f14fSopenharmony_ci enum TestEventId { 27bc03f14fSopenharmony_ci TEST_EVT_UNKNOWN = Event::EVT_REMOTE_CHANGE, 28bc03f14fSopenharmony_ci TEST_EVT_BEGIN = Event::EVT_REMOTE_CHANGE + 1, 29bc03f14fSopenharmony_ci TEST_EVT_MIDDLE, 30bc03f14fSopenharmony_ci TEST_EVT_END, 31bc03f14fSopenharmony_ci }; 32bc03f14fSopenharmony_ci class TestBegin : public Event { 33bc03f14fSopenharmony_ci public: 34bc03f14fSopenharmony_ci TestBegin() : Event(TEST_EVT_BEGIN){}; 35bc03f14fSopenharmony_ci }; 36bc03f14fSopenharmony_ci class TestMiddle : public Event { 37bc03f14fSopenharmony_ci public: 38bc03f14fSopenharmony_ci TestMiddle() : Event(TEST_EVT_MIDDLE){}; 39bc03f14fSopenharmony_ci }; 40bc03f14fSopenharmony_ci class TestEnd : public Event { 41bc03f14fSopenharmony_ci public: 42bc03f14fSopenharmony_ci TestEnd() : Event(TEST_EVT_END){}; 43bc03f14fSopenharmony_ci }; 44bc03f14fSopenharmony_ci static void SetUpTestCase(void) {} 45bc03f14fSopenharmony_ci static void TearDownTestCase(void) {} 46bc03f14fSopenharmony_ci void SetUp() 47bc03f14fSopenharmony_ci { 48bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_UNKNOWN; 49bc03f14fSopenharmony_ci currEvent_ = TEST_EVT_UNKNOWN; 50bc03f14fSopenharmony_ci EventCenter::GetInstance().Subscribe(TEST_EVT_BEGIN, [this](const Event &evt) { 51bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN); 52bc03f14fSopenharmony_ci EventCenter::Defer defer; 53bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestMiddle>()); 54bc03f14fSopenharmony_ci currEvent_ = TEST_EVT_BEGIN; 55bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_MIDDLE; 56bc03f14fSopenharmony_ci }); 57bc03f14fSopenharmony_ci EventCenter::GetInstance().Subscribe(TEST_EVT_MIDDLE, [this](const Event &evt) { 58bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_MIDDLE); 59bc03f14fSopenharmony_ci EventCenter::Defer defer; 60bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestEnd>()); 61bc03f14fSopenharmony_ci currEvent_ = TEST_EVT_MIDDLE; 62bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_END; 63bc03f14fSopenharmony_ci }); 64bc03f14fSopenharmony_ci EventCenter::GetInstance().Subscribe(TEST_EVT_END, [this](const Event &evt) { 65bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_END); 66bc03f14fSopenharmony_ci currEvent_ = TEST_EVT_END; 67bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_UNKNOWN; 68bc03f14fSopenharmony_ci }); 69bc03f14fSopenharmony_ci } 70bc03f14fSopenharmony_ci 71bc03f14fSopenharmony_ci void TearDown() 72bc03f14fSopenharmony_ci { 73bc03f14fSopenharmony_ci EventCenter::GetInstance().Unsubscribe(TEST_EVT_BEGIN); 74bc03f14fSopenharmony_ci EventCenter::GetInstance().Unsubscribe(TEST_EVT_MIDDLE); 75bc03f14fSopenharmony_ci EventCenter::GetInstance().Unsubscribe(TEST_EVT_END); 76bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_UNKNOWN; 77bc03f14fSopenharmony_ci currEvent_ = TEST_EVT_UNKNOWN; 78bc03f14fSopenharmony_ci } 79bc03f14fSopenharmony_ci 80bc03f14fSopenharmony_ciprotected: 81bc03f14fSopenharmony_ci int32_t waitEvent_ = TEST_EVT_UNKNOWN; 82bc03f14fSopenharmony_ci int32_t currEvent_ = TEST_EVT_UNKNOWN; 83bc03f14fSopenharmony_ci}; 84bc03f14fSopenharmony_ci 85bc03f14fSopenharmony_ci/** 86bc03f14fSopenharmony_ci* @tc.name: TopLayerASyncEvent 87bc03f14fSopenharmony_ci* @tc.desc: the async event on the top layer will dispatch, until the function completed. 88bc03f14fSopenharmony_ci* @tc.type: FUNC 89bc03f14fSopenharmony_ci* @tc.require: 90bc03f14fSopenharmony_ci* @tc.author: Sven Wang 91bc03f14fSopenharmony_ci*/ 92bc03f14fSopenharmony_ciHWTEST_F(EventCenterTest, TopLayerASyncEvent, TestSize.Level2) 93bc03f14fSopenharmony_ci{ 94bc03f14fSopenharmony_ci auto test = [this]() { 95bc03f14fSopenharmony_ci EventCenter::Defer defer; 96bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 97bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_BEGIN; 98bc03f14fSopenharmony_ci }; 99bc03f14fSopenharmony_ci test(); 100bc03f14fSopenharmony_ci ASSERT_EQ(currEvent_, TEST_EVT_END); 101bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN); 102bc03f14fSopenharmony_ci} 103bc03f14fSopenharmony_ci 104bc03f14fSopenharmony_ci/** 105bc03f14fSopenharmony_ci* @tc.name: SubLayerASyncEvent 106bc03f14fSopenharmony_ci* @tc.desc: the async event on sub layer will defer to dispatch, until the top layer function completed. 107bc03f14fSopenharmony_ci* @tc.type: FUNC 108bc03f14fSopenharmony_ci* @tc.require: 109bc03f14fSopenharmony_ci* @tc.author: Sven Wang 110bc03f14fSopenharmony_ci*/ 111bc03f14fSopenharmony_ciHWTEST_F(EventCenterTest, SubLayerASyncEvent, TestSize.Level2) 112bc03f14fSopenharmony_ci{ 113bc03f14fSopenharmony_ci EventCenter::Defer defer; 114bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 115bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_BEGIN; 116bc03f14fSopenharmony_ci ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN); 117bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN); 118bc03f14fSopenharmony_ci} 119bc03f14fSopenharmony_ci 120bc03f14fSopenharmony_ci/** 121bc03f14fSopenharmony_ci* @tc.name: ASyncEventWithoutDefer 122bc03f14fSopenharmony_ci* @tc.desc: async event without defer may call or not 123bc03f14fSopenharmony_ci* @tc.type: FUNC 124bc03f14fSopenharmony_ci* @tc.require: 125bc03f14fSopenharmony_ci* @tc.author: Sven Wang 126bc03f14fSopenharmony_ci*/ 127bc03f14fSopenharmony_ciHWTEST_F(EventCenterTest, ASyncEventWithoutDefer, TestSize.Level2) 128bc03f14fSopenharmony_ci{ 129bc03f14fSopenharmony_ci EventCenter::Defer defer; 130bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_BEGIN; 131bc03f14fSopenharmony_ci auto test = [this]() { 132bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 133bc03f14fSopenharmony_ci }; 134bc03f14fSopenharmony_ci test(); 135bc03f14fSopenharmony_ci ASSERT_EQ(currEvent_, TEST_EVT_UNKNOWN); 136bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_BEGIN); 137bc03f14fSopenharmony_ci} 138bc03f14fSopenharmony_ci 139bc03f14fSopenharmony_ci/** 140bc03f14fSopenharmony_ci* @tc.name: ImmediatelyASyncEvent 141bc03f14fSopenharmony_ci* @tc.desc: post the async event, there is top layer and no defer; we will dispatch the async event Immediately. 142bc03f14fSopenharmony_ci* @tc.type: FUNC 143bc03f14fSopenharmony_ci* @tc.require: 144bc03f14fSopenharmony_ci* @tc.author: Sven Wang 145bc03f14fSopenharmony_ci*/ 146bc03f14fSopenharmony_ciHWTEST_F(EventCenterTest, ImmediatelyASyncEvent, TestSize.Level2) 147bc03f14fSopenharmony_ci{ 148bc03f14fSopenharmony_ci waitEvent_ = TEST_EVT_BEGIN; 149bc03f14fSopenharmony_ci EventCenter::GetInstance().PostEvent(std::make_unique<TestBegin>()); 150bc03f14fSopenharmony_ci ASSERT_EQ(currEvent_, TEST_EVT_END); 151bc03f14fSopenharmony_ci ASSERT_EQ(waitEvent_, TEST_EVT_UNKNOWN); 152bc03f14fSopenharmony_ci} 153bc03f14fSopenharmony_ci} // namespace OHOS::MiscServices