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 #include "test/unittest/core/base/frame_node_test_ng.h"
16 
17 #include "core/event/touch_event.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Ace::NG {
23 namespace {} // namespace
24 
25 /**
26  * @tc.name: FrameNodeOnGenerateOneDepthVisibleFrameWithOffset01
27  * @tc.desc: Test the function OnGenerateOneDepthVisibleFrameWithOffset
28  * @tc.type: FUNC
29  */
30 HWTEST_F(FrameNodeTestNg, FrameNodeOnGenerateOneDepthVisibleFrameWithOffset01, TestSize.Level1)
31 {
32     /**
33      * @tc.steps: step1. create frameNode.
34      */
35     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
36     auto childNode = FrameNode::CreateFrameNode("childNode", 1, AceType::MakeRefPtr<Pattern>(), true);
37     frameNode->isLayoutNode_ = true;
38     frameNode->isActive_ = true;
39     frameNode->overlayNode_ = nullptr;
40 
41     /**
42      * @tc.steps: step2. call the function OnGenerateOneDepthVisibleFrameWithOffset.
43      */
44     OffsetF Offset = { 0.0, 0.0 };
45     std::list<RefPtr<FrameNode>> children;
46     children.push_back(childNode);
47     frameNode->OnGenerateOneDepthVisibleFrameWithOffset(children, Offset);
48     frameNode->overlayNode_ = AceType::MakeRefPtr<FrameNode>("test", 1, AceType::MakeRefPtr<Pattern>());
49     frameNode->OnGenerateOneDepthVisibleFrameWithOffset(children, Offset);
50 }
51 
52 /**
53  * @tc.name: FrameNodeOnGenerateOneDepthVisibleFrameWithOffset02
54  * @tc.desc: Test the function OnGenerateOneDepthVisibleFrameWithOffset
55  * @tc.type: FUNC
56  */
57 HWTEST_F(FrameNodeTestNg, FrameNodeOnGenerateOneDepthVisibleFrameWithOffset02, TestSize.Level1)
58 {
59     /**
60      * @tc.steps: step1. create frameNode.
61      */
62     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
63     auto childNode = FrameNode::CreateFrameNode("childNode", 1, AceType::MakeRefPtr<Pattern>(), true);
64     frameNode->isLayoutNode_ = false;
65     frameNode->isActive_ = true;
66     frameNode->overlayNode_ = AceType::MakeRefPtr<FrameNode>("test", 1, AceType::MakeRefPtr<Pattern>());
67 
68     /**
69      * @tc.steps: step2. call the function OnGenerateOneDepthVisibleFrameWithOffset.
70      */
71     auto layoutProperty = AceType::MakeRefPtr<LayoutProperty>();
72     layoutProperty->propVisibility_ = VisibleType::INVISIBLE;
73     OffsetF Offset = { 0.0, 0.0 };
74     std::list<RefPtr<FrameNode>> children;
75     children.push_back(childNode);
76     frameNode->SetLayoutProperty(layoutProperty);
77     frameNode->OnGenerateOneDepthVisibleFrameWithOffset(children, Offset);
78 }
79 
80 /**
81  * @tc.name: FrameNodeOnGenerateOneDepthVisibleFrameWithOffset03
82  * @tc.desc: Test the function OnGenerateOneDepthVisibleFrameWithOffset
83  * @tc.type: FUNC
84  */
85 HWTEST_F(FrameNodeTestNg, FrameNodeOnGenerateOneDepthVisibleFrameWithOffset03, TestSize.Level1)
86 {
87     /**
88      * @tc.steps: step1. create frameNode.
89      */
90     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
91     auto childNode = FrameNode::CreateFrameNode("childNode", 1, AceType::MakeRefPtr<Pattern>(), true);
92     frameNode->isLayoutNode_ = false;
93     frameNode->isActive_ = true;
94     frameNode->overlayNode_ = AceType::MakeRefPtr<FrameNode>("test", 1, AceType::MakeRefPtr<Pattern>());
95 
96     /**
97      * @tc.steps: step2. call the function OnGenerateOneDepthVisibleFrameWithOffset.
98      */
99     auto layoutProperty = AceType::MakeRefPtr<LayoutProperty>();
100     layoutProperty->propVisibility_ = VisibleType::VISIBLE;
101     OffsetF Offset = { 0.0, 0.0 };
102     std::list<RefPtr<FrameNode>> children;
103     children.push_back(childNode);
104     frameNode->SetLayoutProperty(layoutProperty);
105     frameNode->OnGenerateOneDepthVisibleFrameWithOffset(children, Offset);
106 }
107 
108 /**
109  * @tc.name: FrameNodeOnGenerateOneDepthVisibleFrameWithOffset04
110  * @tc.desc: Test the function OnGenerateOneDepthVisibleFrameWithOffset
111  * @tc.type: FUNC
112  */
113 HWTEST_F(FrameNodeTestNg, FrameNodeOnGenerateOneDepthVisibleFrameWithOffset04, TestSize.Level1)
114 {
115     /**
116      * @tc.steps: step1. create frameNode.
117      */
118     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
119     auto childNode = FrameNode::CreateFrameNode("childNode", 1, AceType::MakeRefPtr<Pattern>(), true);
120     frameNode->isLayoutNode_ = false;
121     frameNode->isActive_ = false;
122     frameNode->overlayNode_ = AceType::MakeRefPtr<FrameNode>("test", 1, AceType::MakeRefPtr<Pattern>());
123 
124     /**
125      * @tc.steps: step2. call the function OnGenerateOneDepthVisibleFrameWithOffset.
126      */
127     auto layoutProperty = AceType::MakeRefPtr<LayoutProperty>();
128     layoutProperty->propVisibility_ = VisibleType::VISIBLE;
129     OffsetF Offset = { 0.0, 0.0 };
130     std::list<RefPtr<FrameNode>> children;
131     children.push_back(childNode);
132     frameNode->SetLayoutProperty(layoutProperty);
133     frameNode->OnGenerateOneDepthVisibleFrameWithOffset(children, Offset);
134 }
135 
136 /**
137  * @tc.name: FrameNodeIsOutOfTouchTestRegion01
138  * @tc.desc: Test the function IsOutOfTouchTestRegion
139  * @tc.type: FUNC
140  */
141 HWTEST_F(FrameNodeTestNg, FrameNodeIsOutOfTouchTestRegion01, TestSize.Level1)
142 {
143     /**
144      * @tc.steps: step1. create frameNode.
145      */
146     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
147     auto testNode = FrameNode::CreateFrameNode("testNode", 1, AceType::MakeRefPtr<Pattern>(), true);
148 
149     /**
150      * @tc.steps: step2. call the function IsOutOfTouchTestRegion.
151      */
152     NG::PointF point { 1.0, 1.0 };
153     frameNode->renderContext_->UpdateClipEdge(true);
154     TouchEvent touchEvent;
155     touchEvent.sourceType = static_cast<SourceType>(1);
156     frameNode->IsOutOfTouchTestRegion(point, touchEvent);
157     testNode->renderContext_->UpdateClipEdge(false);
158     testNode->IsOutOfTouchTestRegion(point, touchEvent);
159 }
160 
161 /**
162  * @tc.name: FrameNodeAddJudgeToTargetComponent01
163  * @tc.desc: Test the function AddJudgeToTargetComponent
164  * @tc.type: FUNC
165  */
166 HWTEST_F(FrameNodeTestNg, FrameNodeAddJudgeToTargetComponent01, TestSize.Level1)
167 {
168     /**
169      * @tc.steps: step1. create frameNode.
170      */
171     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
172 
173     /**
174      * @tc.steps: step2. call the function AddJudgeToTargetComponent.
175      */
176     auto gestureHub = frameNode->GetOrCreateGestureEventHub();
177     auto gestureJudgeFunc = [](const RefPtr<GestureInfo>& gestureInfo,
__anoncee0c4270202(const RefPtr<GestureInfo>& gestureInfo, const std::shared_ptr<BaseGestureEvent>&) 178                                 const std::shared_ptr<BaseGestureEvent>&) -> GestureJudgeResult {
179         return GestureJudgeResult(1);
180     };
181     gestureHub->SetOnGestureJudgeNativeBegin(gestureJudgeFunc);
182     RefPtr<TargetComponent> targetComponent = AceType::MakeRefPtr<TargetComponent>();
183     frameNode->AddJudgeToTargetComponent(targetComponent);
184 }
185 
186 /**
187  * @tc.name: FrameNodeAddJudgeToTargetComponent02
188  * @tc.desc: Test the function AddJudgeToTargetComponent
189  * @tc.type: FUNC
190  */
191 HWTEST_F(FrameNodeTestNg, FrameNodeAddJudgeToTargetComponent02, TestSize.Level1)
192 {
193     /**
194      * @tc.steps: step1. create frameNode.
195      */
196     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
197 
198     /**
199      * @tc.steps: step2. call the function AddJudgeToTargetComponent.
200      */
201     frameNode->eventHub_->gestureEventHub_ = nullptr;
202     RefPtr<TargetComponent> targetComponent = AceType::MakeRefPtr<TargetComponent>();
203     frameNode->AddJudgeToTargetComponent(targetComponent);
204 }
205 
206 /**
207  * @tc.name: TriggerShouldParallelInnerWithTest01
208  * @tc.desc: Test the function TriggerShouldParallelInnerWith
209  * @tc.type: FUNC
210  */
211 HWTEST_F(FrameNodeTestNg, TriggerShouldParallelInnerWithTest01, TestSize.Level1)
212 {
213     /**
214      * @tc.steps: step1. create frameNode.
215      */
216     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
217     auto gestureHub = frameNode->eventHub_->GetOrCreateGestureEventHub();
218     ShouldBuiltInRecognizerParallelWithFunc shouldBuiltInRecognizerParallelWithFunc =
__anoncee0c4270302(RefPtr<NGGestureRecognizer> target, std::vector<RefPtr<NGGestureRecognizer>> targets) 219         [](RefPtr<NGGestureRecognizer> target, std::vector<RefPtr<NGGestureRecognizer>> targets) {
220             return RefPtr<NGGestureRecognizer>();
221         };
222     gestureHub->SetShouldBuildinRecognizerParallelWithFunc(std::move(shouldBuiltInRecognizerParallelWithFunc));
223     ResponseLinkResult currentRecognizers;
224     ResponseLinkResult responseLinkRecognizers;
225     auto recognizer = AceType::MakeRefPtr<ClickRecognizer>();
226     recognizer->SetRecognizerType(GestureTypeName::PAN_GESTURE);
227     recognizer->GetGestureInfo()->SetIsSystemGesture(true);
228     currentRecognizers.emplace_back(recognizer);
229     responseLinkRecognizers.emplace_back(recognizer);
230     currentRecognizers.emplace_back(AceType::MakeRefPtr<ClickRecognizer>());
231 
232     auto recognizer1 = AceType::MakeRefPtr<ClickRecognizer>();
233     recognizer1->SetRecognizerType(GestureTypeName::UNKNOWN);
234     recognizer1->GetGestureInfo()->SetIsSystemGesture(true);
235     currentRecognizers.emplace_back(recognizer1);
236 
237     auto recognizer2 = AceType::MakeRefPtr<ClickRecognizer>();
238     recognizer2->SetRecognizerType(GestureTypeName::PAN_GESTURE);
239     recognizer2->GetGestureInfo()->SetIsSystemGesture(true);
240     TouchEvent touchEvent;
241     TouchEvent touchEvent1;
242     recognizer2->touchPoints_[touchEvent.id] = touchEvent;
243     recognizer2->touchPoints_[touchEvent1.id] = touchEvent1;
244     currentRecognizers.emplace_back(recognizer2);
245     /**
246      * @tc.steps: step2. call the function TriggerShouldParallelInnerWith.
247      */
248     frameNode->TriggerShouldParallelInnerWith(currentRecognizers, responseLinkRecognizers);
249     EXPECT_FALSE(recognizer->IsBridgeMode());
250 }
251 
252 /**
253  * @tc.name: OnSyncGeometryFrameFinishTest
254  * @tc.desc: Test the function OnSyncGeometryFrameFinish
255  * @tc.type: FUNC
256  */
257 HWTEST_F(FrameNodeTestNg, OnSyncGeometryFrameFinishTest, TestSize.Level1)
258 {
259     /**
260      * @tc.steps: step1. create frameNode.
261      */
262     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
263     RectF paintRect = { 10.0f, 10.0f, 10.0f, 10.0f };
264     /**
265      * @tc.steps: step2. call the function TriggerShouldParallelInnerWith.
266      */
267     frameNode->OnSyncGeometryFrameFinish(paintRect);
268     EXPECT_TRUE(frameNode->syncedFramePaintRect_.has_value());
269     frameNode->OnSyncGeometryFrameFinish(paintRect);
270     RectF paintRect1 = { 20.0f, 20.0f, 20.0f, 20.0f };
271     frameNode->OnSyncGeometryFrameFinish(paintRect1);
272     EXPECT_EQ(frameNode->syncedFramePaintRect_, paintRect1);
273 }
274 
275 /**
276  * @tc.name: IsContextTransparentTest001
277  * @tc.desc: Test the function IsContextTransparent
278  * @tc.type: FUNC
279  */
280 HWTEST_F(FrameNodeTestNg, IsContextTransparentTest001, TestSize.Level1)
281 {
282     /**
283      * @tc.steps: step1. create frameNode.
284      */
285     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
286 
287     /**
288      * @tc.steps: step2. call the function IsContextTransparent.
289      */
290     EXPECT_TRUE(frameNode->IsContextTransparent());
291     frameNode->GetRenderContext()->UpdateOpacity(0.0);
292     EXPECT_TRUE(frameNode->IsContextTransparent());
293     frameNode->GetRenderContext()->UpdateOpacity(10.0);
294     EXPECT_TRUE(frameNode->IsContextTransparent());
295     auto mockRenderContext = AceType::DynamicCast<MockRenderContext>(frameNode->GetRenderContext());
296     mockRenderContext->SetPaintRectWithTransform(RectF(10.0f, 10.0f, 10.0f, 0.0f));
297     EXPECT_TRUE(frameNode->IsContextTransparent());
298     mockRenderContext->SetPaintRectWithTransform(RectF(10.0f, 10.0f, 10.0f, 10.0f));
299     EXPECT_FALSE(frameNode->IsContextTransparent());
300     frameNode->layoutProperty_->propVisibility_ = VisibleType::INVISIBLE;
301     EXPECT_TRUE(frameNode->IsContextTransparent());
302 }
303 
304 /**
305  * @tc.name: IsContextTransparentTest002
306  * @tc.desc: Test the function IsContextTransparent
307  * @tc.type: FUNC
308  */
309 HWTEST_F(FrameNodeTestNg, IsContextTransparentTest002, TestSize.Level1)
310 {
311     /**
312      * @tc.steps: step1. create frameNode.
313      */
314     auto frameNode = FrameNode::CreateFrameNode("Flex", 1, AceType::MakeRefPtr<Pattern>(), true);
315     auto frameNode1 = FrameNode::CreateFrameNode("framenode", 2, AceType::MakeRefPtr<Pattern>(), true);
316     frameNode->AddChild(frameNode1);
317     /**
318      * @tc.steps: step2. call the function IsContextTransparent.
319      */
320     EXPECT_TRUE(frameNode->IsContextTransparent());
321     auto mockRenderContext = AceType::DynamicCast<MockRenderContext>(frameNode->GetRenderContext());
322     mockRenderContext->SetPaintRectWithTransform(RectF(10.0f, 10.0f, 10.0f, 0.0f));
323     auto mockRenderContext1 = AceType::DynamicCast<MockRenderContext>(frameNode1->GetRenderContext());
324     mockRenderContext1->SetPaintRectWithTransform(RectF(10.0f, 10.0f, 10.0f, 10.0f));
325     EXPECT_FALSE(frameNode->IsContextTransparent());
326     mockRenderContext->SetPaintRectWithTransform(RectF(10.0f, 10.0f, 10.0f, 10.0f));
327     EXPECT_FALSE(frameNode->IsContextTransparent());
328     frameNode->layoutProperty_->propVisibility_ = VisibleType::INVISIBLE;
329     EXPECT_FALSE(frameNode->IsContextTransparent());
330     frameNode->layoutProperty_->propVisibility_ = VisibleType::VISIBLE;
331     frameNode->GetRenderContext()->UpdateBackgroundColor(Color::BLUE);
332     EXPECT_FALSE(frameNode->IsContextTransparent());
333 }
334 
335 /**
336  * @tc.name: AddTouchEventAllFingersInfoTest
337  * @tc.desc: Test the function AddTouchEventAllFingersInfo
338  * @tc.type: FUNC
339  */
340 HWTEST_F(FrameNodeTestNg, AddTouchEventAllFingersInfoTest, TestSize.Level1)
341 {
342     /**
343      * @tc.steps: step1. create frameNode.
344      */
345     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
346     /**
347      * @tc.steps: step2. call the function AddTouchEventAllFingersInfoTest.
348      */
349     TouchEventInfo touchEventInfo("touch");
350     TouchEvent touchEvent;
351     TouchPoint touchPoint;
352     touchEvent.pointers.emplace_back(touchPoint);
353     TouchPoint touchPoint1;
354     touchPoint1.tiltX = 10.0;
355     touchPoint1.tiltY = 10.0;
356     touchEvent.pointers.emplace_back(touchPoint1);
357     frameNode->AddTouchEventAllFingersInfo(touchEventInfo, touchEvent);
358     EXPECT_EQ(touchEventInfo.touches_.size(), 2);
359 }
360 
361 /**
362  * @tc.name: TriggerOnTouchInterceptTest
363  * @tc.desc: Test the function TriggerOnTouchIntercept
364  * @tc.type: FUNC
365  */
366 HWTEST_F(FrameNodeTestNg, TriggerOnTouchInterceptTest, TestSize.Level1)
367 {
368     /**
369      * @tc.steps: step1. create frameNode.
370      */
371     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
372     /**
373      * @tc.steps: step2. call the function AddTouchEventAllFingersInfoTest.
374      */
375     auto gestureHub = frameNode->eventHub_->GetOrCreateGestureEventHub();
__anoncee0c4270402(TouchEventInfo& touchEventInfo) 376     TouchInterceptFunc touchInterceptFunc = [](TouchEventInfo& touchEventInfo) { return HitTestMode::HTMBLOCK; };
377     gestureHub->SetOnTouchIntercept(std::move(touchInterceptFunc));
378     TouchEventInfo touchEventInfo("touch");
379     TouchEvent touchEvent;
380     EXPECT_EQ(frameNode->TriggerOnTouchIntercept(touchEvent), HitTestMode::HTMBLOCK);
381     touchEvent.tiltX = 10.0;
382     touchEvent.tiltY = 10.0;
383     EXPECT_EQ(frameNode->TriggerOnTouchIntercept(touchEvent), HitTestMode::HTMBLOCK);
384 }
385 
386 /**
387  * @tc.name: CalculateCachedTransformRelativeOffsetTest
388  * @tc.desc: Test the function CalculateCachedTransformRelativeOffset
389  * @tc.type: FUNC
390  */
391 HWTEST_F(FrameNodeTestNg, CalculateCachedTransformRelativeOffsetTest, TestSize.Level1)
392 {
393     /**
394      * @tc.steps: step1. create frameNode.
395      */
396     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
397     /**
398      * @tc.steps: step2. call the function CalculateCachedTransformRelativeOffset.
399      */
400     frameNode->CalculateCachedTransformRelativeOffset(0);
401     auto child = FrameNode::CreateFrameNode("child", 2, AceType::MakeRefPtr<Pattern>(), true);
402     frameNode->AddChild(child);
403     child->CalculateCachedTransformRelativeOffset(0);
404     frameNode->exposureProcessor_ = AceType::MakeRefPtr<Recorder::ExposureProcessor>("test", "0");
405     frameNode->RecordExposureInner();
406     EXPECT_EQ(child->CalculateCachedTransformRelativeOffset(0), OffsetF(0, 0));
407     EXPECT_EQ(child->CalculateCachedTransformRelativeOffset(10), OffsetF(0, 0));
408 }
409 
410 /**
411  * @tc.name: ProcessVisibleAreaChangeEventTest
412  * @tc.desc: Test the function ProcessVisibleAreaChangeEvent
413  * @tc.type: FUNC
414  */
415 HWTEST_F(FrameNodeTestNg, ProcessVisibleAreaChangeEventTest, TestSize.Level1)
416 {
417     /**
418      * @tc.steps: step1. create frameNode.
419      */
420     auto frameNode = FrameNode::CreateFrameNode("framenode", 1, AceType::MakeRefPtr<Pattern>(), true);
421     RectF visibleRect = { 10.0, 10.0, 10.0, 10.0 };
422     RectF frameRect = { 10.0, 10.0, 10.0, 10.0 };
423     std::vector<double> visibleAreaRatios = { 0.0, 0.0 };
424     VisibleCallbackInfo visibleAreaCallback;
425 
426     /**
427      * @tc.steps: step2. call the function ProcessVisibleAreaChangeEvent.
428      */
429     // !NearEqual
430     frameNode->ProcessVisibleAreaChangeEvent(visibleRect, frameRect, visibleAreaRatios, visibleAreaCallback, true);
431     // NearEqual
432     frameNode->ProcessVisibleAreaChangeEvent(visibleRect, frameRect, visibleAreaRatios, visibleAreaCallback, true);
433     EXPECT_EQ(frameNode->lastVisibleRatio_, 1.0);
434     // !NearEqual
435     frameNode->ProcessVisibleAreaChangeEvent(visibleRect, frameRect, visibleAreaRatios, visibleAreaCallback, false);
436     // NearEqual
437     frameNode->ProcessVisibleAreaChangeEvent(visibleRect, frameRect, visibleAreaRatios, visibleAreaCallback, false);
438     EXPECT_EQ(frameNode->lastInnerVisibleRatio_, 1.0);
439 }
440 } // namespace OHOS::Ace::NG