18bf80f4bSopenharmony_ci/* 28bf80f4bSopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd. 38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License. 58bf80f4bSopenharmony_ci * You may obtain a copy of the License at 68bf80f4bSopenharmony_ci * 78bf80f4bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88bf80f4bSopenharmony_ci * 98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and 138bf80f4bSopenharmony_ci * limitations under the License. 148bf80f4bSopenharmony_ci */ 158bf80f4bSopenharmony_ci 168bf80f4bSopenharmony_ci#include <gtest/gtest.h> 178bf80f4bSopenharmony_ci 188bf80f4bSopenharmony_ci#include <meta/api/deferred_callback.h> 198bf80f4bSopenharmony_ci#include <meta/api/property/property_event_handler.h> 208bf80f4bSopenharmony_ci#include <meta/ext/object.h> 218bf80f4bSopenharmony_ci#include <meta/interface/intf_object_registry.h> 228bf80f4bSopenharmony_ci#include <meta/interface/object_macros.h> 238bf80f4bSopenharmony_ci#include <meta/interface/property/intf_property.h> 248bf80f4bSopenharmony_ci 258bf80f4bSopenharmony_ci#include "src/test_runner.h" 268bf80f4bSopenharmony_ci#include "src/util.h" 278bf80f4bSopenharmony_ci 288bf80f4bSopenharmony_ciMETA_BEGIN_NAMESPACE() 298bf80f4bSopenharmony_ci 308bf80f4bSopenharmony_ciusing namespace testing::ext; 318bf80f4bSopenharmony_cinamespace { 328bf80f4bSopenharmony_ci 338bf80f4bSopenharmony_cistruct IOnTestInfo { 348bf80f4bSopenharmony_ci constexpr static BASE_NS::Uid UID { "3e55eee7-f0e4-4363-b4fc-65d8b3574a4e" }; 358bf80f4bSopenharmony_ci constexpr static char const *name { "OnTest" }; 368bf80f4bSopenharmony_ci}; 378bf80f4bSopenharmony_ci 388bf80f4bSopenharmony_ciusing IOnTest = SimpleEvent<IOnTestInfo, void(int)>; 398bf80f4bSopenharmony_ci 408bf80f4bSopenharmony_ciusing Callable = SimpleEvent<IOnTestInfo, void()>::InterfaceType; 418bf80f4bSopenharmony_ci 428bf80f4bSopenharmony_ci}; 438bf80f4bSopenharmony_ci 448bf80f4bSopenharmony_ci 458bf80f4bSopenharmony_ciclass DeferredCallbackTest : public testing::Test { 468bf80f4bSopenharmony_cipublic: 478bf80f4bSopenharmony_ci static void SetUpTestSuite() 488bf80f4bSopenharmony_ci { 498bf80f4bSopenharmony_ci SetTest(); 508bf80f4bSopenharmony_ci } 518bf80f4bSopenharmony_ci static void TearDownTestSuite() 528bf80f4bSopenharmony_ci { 538bf80f4bSopenharmony_ci ResetTest(); 548bf80f4bSopenharmony_ci } 558bf80f4bSopenharmony_ci void SetUp() override {} 568bf80f4bSopenharmony_ci void TearDown() override {} 578bf80f4bSopenharmony_ci}; 588bf80f4bSopenharmony_ci 598bf80f4bSopenharmony_ci 608bf80f4bSopenharmony_ci/** 618bf80f4bSopenharmony_ci * @tc.name: DeferredCallbackTest 628bf80f4bSopenharmony_ci * @tc.desc: test Call With Callable function 638bf80f4bSopenharmony_ci * @tc.type: FUNC 648bf80f4bSopenharmony_ci * @tc.require: I7DMS1 658bf80f4bSopenharmony_ci */ 668bf80f4bSopenharmony_ciHWTEST_F(DeferredCallbackTest, CallWithCallable, TestSize.Level1) 678bf80f4bSopenharmony_ci{ 688bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 698bf80f4bSopenharmony_ci 708bf80f4bSopenharmony_ci int count = 0; 718bf80f4bSopenharmony_ci auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 728bf80f4bSopenharmony_ci auto callback = MakeDeferred<Callable>([&] { ++count; }, q); 738bf80f4bSopenharmony_ci q->ProcessTasks(); 748bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 758bf80f4bSopenharmony_ci 768bf80f4bSopenharmony_ci callback->Invoke(); 778bf80f4bSopenharmony_ci callback->Invoke(); 788bf80f4bSopenharmony_ci 798bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 808bf80f4bSopenharmony_ci 818bf80f4bSopenharmony_ci q->ProcessTasks(); 828bf80f4bSopenharmony_ci EXPECT_EQ(count, 2); 838bf80f4bSopenharmony_ci} 848bf80f4bSopenharmony_ci 858bf80f4bSopenharmony_ci/** 868bf80f4bSopenharmony_ci * @tc.name: DeferredCallbackTest 878bf80f4bSopenharmony_ci * @tc.desc: test CallWithEvent function 888bf80f4bSopenharmony_ci * @tc.type: FUNC 898bf80f4bSopenharmony_ci * @tc.require: I7DMS1 908bf80f4bSopenharmony_ci */ 918bf80f4bSopenharmony_ciHWTEST_F(DeferredCallbackTest, CallWithEvent, TestSize.Level1) 928bf80f4bSopenharmony_ci{ 938bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 948bf80f4bSopenharmony_ci 958bf80f4bSopenharmony_ci int count = 0; 968bf80f4bSopenharmony_ci auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 978bf80f4bSopenharmony_ci auto callback = MakeDeferred<IOnTest>([&](int i) { count += i; }, q); 988bf80f4bSopenharmony_ci q->ProcessTasks(); 998bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 1008bf80f4bSopenharmony_ci 1018bf80f4bSopenharmony_ci callback->Invoke(1); 1028bf80f4bSopenharmony_ci callback->Invoke(2); 1038bf80f4bSopenharmony_ci 1048bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 1058bf80f4bSopenharmony_ci 1068bf80f4bSopenharmony_ci q->ProcessTasks(); 1078bf80f4bSopenharmony_ci EXPECT_EQ(count, 3); 1088bf80f4bSopenharmony_ci} 1098bf80f4bSopenharmony_ci 1108bf80f4bSopenharmony_ci/** 1118bf80f4bSopenharmony_ci * @tc.name: DeferredCallbackTest 1128bf80f4bSopenharmony_ci * @tc.desc: test PropertyOnChange function 1138bf80f4bSopenharmony_ci * @tc.type: FUNC 1148bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1158bf80f4bSopenharmony_ci */ 1168bf80f4bSopenharmony_ciHWTEST_F(DeferredCallbackTest, PropertyOnChange, TestSize.Level1) 1178bf80f4bSopenharmony_ci{ 1188bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1198bf80f4bSopenharmony_ci 1208bf80f4bSopenharmony_ci auto p = META_NS::ConstructProperty<int>(registry, "P", {}); 1218bf80f4bSopenharmony_ci ASSERT_TRUE(p); 1228bf80f4bSopenharmony_ci 1238bf80f4bSopenharmony_ci int count = 0; 1248bf80f4bSopenharmony_ci auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 1258bf80f4bSopenharmony_ci 1268bf80f4bSopenharmony_ci auto t = p->OnChanged()->AddHandler(MakeDeferred<IOnChanged>([&] { ++count; }, q)); 1278bf80f4bSopenharmony_ci 1288bf80f4bSopenharmony_ci p->SetValue(1); 1298bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 1308bf80f4bSopenharmony_ci 1318bf80f4bSopenharmony_ci q->ProcessTasks(); 1328bf80f4bSopenharmony_ci EXPECT_EQ(count, 1); 1338bf80f4bSopenharmony_ci 1348bf80f4bSopenharmony_ci p->OnChanged()->RemoveHandler(t); 1358bf80f4bSopenharmony_ci 1368bf80f4bSopenharmony_ci p->SetValue(2); 1378bf80f4bSopenharmony_ci q->ProcessTasks(); 1388bf80f4bSopenharmony_ci EXPECT_EQ(count, 1); 1398bf80f4bSopenharmony_ci} 1408bf80f4bSopenharmony_ci 1418bf80f4bSopenharmony_ci/** 1428bf80f4bSopenharmony_ci * @tc.name: DeferredCallbackTest 1438bf80f4bSopenharmony_ci * @tc.desc: test PropertyChangedHandler function 1448bf80f4bSopenharmony_ci * @tc.type: FUNC 1458bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1468bf80f4bSopenharmony_ci */ 1478bf80f4bSopenharmony_ciHWTEST_F(DeferredCallbackTest, PropertyChangedHandler, TestSize.Level1) 1488bf80f4bSopenharmony_ci{ 1498bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1508bf80f4bSopenharmony_ci 1518bf80f4bSopenharmony_ci auto p = META_NS::ConstructProperty<int>(registry, "p", {}); 1528bf80f4bSopenharmony_ci ASSERT_TRUE(p); 1538bf80f4bSopenharmony_ci 1548bf80f4bSopenharmony_ci int count = 0; 1558bf80f4bSopenharmony_ci auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 1568bf80f4bSopenharmony_ci 1578bf80f4bSopenharmony_ci PropertyChangedEventHandler handler_; 1588bf80f4bSopenharmony_ci handler_.Subscribe( 1598bf80f4bSopenharmony_ci p, [&] { ++count; }, q); 1608bf80f4bSopenharmony_ci 1618bf80f4bSopenharmony_ci p->SetValue(1); 1628bf80f4bSopenharmony_ci 1638bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 1648bf80f4bSopenharmony_ci q->ProcessTasks(); 1658bf80f4bSopenharmony_ci EXPECT_EQ(count, 1); 1668bf80f4bSopenharmony_ci} 1678bf80f4bSopenharmony_ci 1688bf80f4bSopenharmony_ci/** 1698bf80f4bSopenharmony_ci * @tc.name: DeferredCallbackTest 1708bf80f4bSopenharmony_ci * @tc.desc: test PropertyChangedHandlerId function 1718bf80f4bSopenharmony_ci * @tc.type: FUNC 1728bf80f4bSopenharmony_ci * @tc.require: I7DMS1 1738bf80f4bSopenharmony_ci */ 1748bf80f4bSopenharmony_ciHWTEST_F(DeferredCallbackTest, PropertyChangedHandlerId, TestSize.Level1) 1758bf80f4bSopenharmony_ci{ 1768bf80f4bSopenharmony_ci auto& registry = META_NS::GetObjectRegistry(); 1778bf80f4bSopenharmony_ci 1788bf80f4bSopenharmony_ci auto p = META_NS::ConstructProperty<int>(registry, "P", {}); 1798bf80f4bSopenharmony_ci ASSERT_TRUE(p); 1808bf80f4bSopenharmony_ci 1818bf80f4bSopenharmony_ci int count = 0; 1828bf80f4bSopenharmony_ci auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue); 1838bf80f4bSopenharmony_ci 1848bf80f4bSopenharmony_ci auto queueId = interface_cast<IObjectInstance>(q)->GetInstanceId(); 1858bf80f4bSopenharmony_ci META_NS::GetTaskQueueRegistry().RegisterTaskQueue(q, queueId.ToUid()); 1868bf80f4bSopenharmony_ci PropertyChangedEventHandler handler_; 1878bf80f4bSopenharmony_ci handler_.Subscribe( 1888bf80f4bSopenharmony_ci p, [&] { ++count; }, queueId.ToUid()); 1898bf80f4bSopenharmony_ci 1908bf80f4bSopenharmony_ci p->SetValue(1); 1918bf80f4bSopenharmony_ci EXPECT_EQ(count, 0); 1928bf80f4bSopenharmony_ci 1938bf80f4bSopenharmony_ci q->ProcessTasks(); 1948bf80f4bSopenharmony_ci EXPECT_EQ(count, 1); 1958bf80f4bSopenharmony_ci 1968bf80f4bSopenharmony_ci META_NS::GetTaskQueueRegistry().UnregisterTaskQueue(queueId.ToUid()); 1978bf80f4bSopenharmony_ci} 1988bf80f4bSopenharmony_ciMETA_END_NAMESPACE()