• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <TestRunner.h>
17 
18 #include <gtest/gtest.h>
19 
20 #include <meta/api/deferred_callback.h>
21 #include <meta/api/property/property_event_handler.h>
22 #include <meta/ext/object_fwd.h>
23 #include <meta/interface/intf_object_registry.h>
24 #include <meta/interface/object_macros.h>
25 #include <meta/interface/property/intf_property.h>
26 
27 #include "src/util.h"
28 
29 META_BEGIN_NAMESPACE()
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace {
35 
36 struct IOnTestInfo {
37     constexpr static BASE_NS::Uid UID { "3e55eee7-f0e4-4363-b4fc-65d8b3574a4e" };
38     constexpr static char const* NAME { "OnTest" };
39 };
40 using IOnTest = SimpleEvent<IOnTestInfo, void(int)>;
41 
42 using Callable = SimpleEvent<IOnTestInfo, void()>::InterfaceType;
43 
44 } // namespace
45 
46 class DeferredCallbackTest : public testing::Test {
47 public:
SetUpTestSuite()48     static void SetUpTestSuite()
49     {
50         SetTest();
51     }
TearDownTestSuite()52     static void TearDownTestSuite()
53     {
54         TearDownTest();
55     }
SetUp()56     void SetUp() {}
TearDown()57     void TearDown() {}
58 };
59 
60 /**
61  * @tc.name: CallWithCallable
62  * @tc.desc: test CallWithCallable
63  * @tc.type:FUNC
64  * @tc.require:
65  */
66 HWTEST_F(DeferredCallbackTest, CallWithCallable, TestSize.Level1)
67 {
68     auto& registry = META_NS::GetObjectRegistry();
69 
70     int count = 0;
71     auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue);
__anon460cb53a0202null72     auto callback = MakeDeferred<Callable>([&] { ++count; }, q);
73 
74     q->ProcessTasks();
75     EXPECT_EQ(count, 0);
76 
77     callback->Invoke();
78     callback->Invoke();
79 
80     EXPECT_EQ(count, 0);
81 
82     q->ProcessTasks();
83     EXPECT_EQ(count, 2);
84 }
85 
86 /**
87  * @tc.name: CallWithEvent
88  * @tc.desc: test CallWithEvent
89  * @tc.type:FUNC
90  * @tc.require:
91  */
92 HWTEST_F(DeferredCallbackTest, CallWithEvent, TestSize.Level1)
93 {
94     auto& registry = META_NS::GetObjectRegistry();
95 
96     int count = 0;
97     auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue);
__anon460cb53a0302(int i) 98     auto callback = MakeDeferred<IOnTest>([&](int i) { count += i; }, q);
99 
100     q->ProcessTasks();
101     EXPECT_EQ(count, 0);
102 
103     callback->Invoke(1);
104     callback->Invoke(2);
105 
106     EXPECT_EQ(count, 0);
107 
108     q->ProcessTasks();
109     EXPECT_EQ(count, 3);
110 }
111 
112 /**
113  * @tc.name: PropertyOnChange
114  * @tc.desc: test PropertyOnChange
115  * @tc.type:FUNC
116  * @tc.require:
117  */
118 HWTEST_F(DeferredCallbackTest, PropertyOnChange, TestSize.Level1)
119 {
120     auto& registry = META_NS::GetObjectRegistry();
121 
122     auto p = META_NS::ConstructProperty<int>(registry, "P", {});
123     ASSERT_TRUE(p);
124 
125     int count = 0;
126     auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue);
127 
__anon460cb53a0402null128     auto t = p->OnChanged()->AddHandler(MakeDeferred<IOnChanged>([&] { ++count; }, q));
129 
130     p->SetValue(1);
131 
132     EXPECT_EQ(count, 0);
133     q->ProcessTasks();
134     EXPECT_EQ(count, 1);
135 
136     p->OnChanged()->RemoveHandler(t);
137 
138     p->SetValue(2);
139     q->ProcessTasks();
140     EXPECT_EQ(count, 1);
141 }
142 
143 /**
144  * @tc.name: PropertyChangedHandler
145  * @tc.desc: test PropertyChangedHandler
146  * @tc.type:FUNC
147  * @tc.require:
148  */
149 HWTEST_F(DeferredCallbackTest, PropertyChangedHandler, TestSize.Level1)
150 {
151     auto& registry = META_NS::GetObjectRegistry();
152 
153     auto p = META_NS::ConstructProperty<int>(registry, "P", {});
154     ASSERT_TRUE(p);
155 
156     int count = 0;
157     auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue);
158 
159     PropertyChangedEventHandler handler_;
160     handler_.Subscribe(
__anon460cb53a0502null161         p, [&] { ++count; }, q);
162 
163     p->SetValue(1);
164 
165     EXPECT_EQ(count, 0);
166     q->ProcessTasks();
167     EXPECT_EQ(count, 1);
168 }
169 
170 /**
171  * @tc.name: PropertyChangedHandlerId
172  * @tc.desc: test PropertyChangedHandlerId
173  * @tc.type:FUNC
174  * @tc.require:
175  */
176 HWTEST_F(DeferredCallbackTest, PropertyChangedHandlerId, TestSize.Level1)
177 {
178     auto& registry = META_NS::GetObjectRegistry();
179 
180     auto p = META_NS::ConstructProperty<int>(registry, "P", {});
181     ASSERT_TRUE(p);
182 
183     int count = 0;
184     auto q = registry.Create<IPollingTaskQueue>(ClassId::PollingTaskQueue);
185     auto queueId = interface_cast<IObjectInstance>(q)->GetInstanceId();
186     META_NS::GetTaskQueueRegistry().RegisterTaskQueue(q, queueId.ToUid());
187 
188     PropertyChangedEventHandler handler_;
189     handler_.Subscribe(
__anon460cb53a0602null190         p, [&] { ++count; }, queueId.ToUid());
191 
192     p->SetValue(1);
193 
194     EXPECT_EQ(count, 0);
195     q->ProcessTasks();
196     EXPECT_EQ(count, 1);
197     META_NS::GetTaskQueueRegistry().UnregisterTaskQueue(queueId.ToUid());
198 }
199 
200 META_END_NAMESPACE()
201