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 "test/unittest/core/manager/drag_animation_helper_test_ng.h"
17 #include "test/mock/core/common/mock_theme_manager.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS::Ace::NG {
23 namespace {
24 constexpr size_t DEFAULT_CHILD_COUNT = 4;
25 constexpr float GRID_WIDTH = 480.0f;
26 constexpr float GRID_HEIGHT = 800.0f;
27 constexpr float ITEM_WIDTH = 120.0f;
28 constexpr float ITEM_HEIGHT = 200.0f;
29 } // namespace
30 
SetUpTestSuite()31 void DragAnimationHelperTestNg::SetUpTestSuite()
32 {
33     TestNG::SetUpTestSuite();
34 }
35 
TearDownTestSuite()36 void DragAnimationHelperTestNg::TearDownTestSuite()
37 {
38     TestNG::TearDownTestSuite();
39 }
40 
SetUp()41 void DragAnimationHelperTestNg::SetUp()
42 {
43     ResetElmtId();
44     auto [gridNode, gridItemNodes] = CreateGridNodeWithChild(DEFAULT_CHILD_COUNT);
45     parentNode_ = gridNode;
46     CHECK_NULL_VOID(parentNode_);
47     childNodes_ = gridItemNodes;
48 }
49 
TearDown()50 void DragAnimationHelperTestNg::TearDown()
51 {
52     parentNode_ = nullptr;
53     childNodes_.clear();
54 }
55 
CreateGridNodeWithChild(size_t childCount,const GridItemStyle & gridItemStyle)56 std::pair<RefPtr<FrameNode>, std::list<RefPtr<FrameNode>>> DragAnimationHelperTestNg::CreateGridNodeWithChild(
57     size_t childCount, const GridItemStyle& gridItemStyle)
58 {
59     ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
60     auto nodeId = ViewStackProcessor::GetInstance()->ClaimNodeId();
61     auto frameNode = FrameNode::GetOrCreateFrameNode(V2::GRID_ETS_TAG, nodeId,
62         []() {return AceType::MakeRefPtr<GridPattern>(); });
63     ViewAbstract::SetWidth(frameNode.GetRawPtr(), CalcLength(GRID_WIDTH));
64     ViewAbstract::SetHeight(frameNode.GetRawPtr(), CalcLength(GRID_HEIGHT));
65     std::list<RefPtr<FrameNode>> childNodes;
66 
67     for (size_t i = 0; i < childCount; ++i) {
68         ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
69         auto itemNodeId = ViewStackProcessor::GetInstance()->ClaimNodeId();
70         auto childNode = FrameNode::GetOrCreateFrameNode(V2::GRID_ITEM_ETS_TAG, itemNodeId,
71             [itemStyle = gridItemStyle]() { return AceType::MakeRefPtr<GridItemPattern>(nullptr, itemStyle); });
72         ViewAbstract::SetWidth(childNode.GetRawPtr(), CalcLength(ITEM_WIDTH));
73         ViewAbstract::SetHeight(childNode.GetRawPtr(), CalcLength(ITEM_HEIGHT));
74         childNode->MountToParent(frameNode);
75         childNodes.emplace_back(childNode);
76     }
77     return { frameNode, childNodes };
78 }
79 
80 /**
81  * @tc.name: PlayGatherAnimationBeforeLifting001
82  * @tc.desc: test PlayGatherAnimationBeforeLifting func.
83  * @tc.type: FUNC
84  */
85 HWTEST_F(DragAnimationHelperTestNg, PlayGatherAnimationBeforeLifting001, TestSize.Level1)
86 {
87     auto iter = childNodes_.begin();
88     ASSERT_TRUE(iter != childNodes_.end());
89     auto gestureHub = (*iter)->GetOrCreateGestureEventHub();
90     auto actuator = AceType::MakeRefPtr<DragEventActuator>(
91         AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
92     actuator->FindItemParentNode(*(childNodes_.begin()));
93     DragAnimationHelper::PlayGatherAnimationBeforeLifting(actuator);
94 
95     for (const auto& child : childNodes_) {
96         ASSERT_TRUE(child != nullptr);
97         auto gridItemPattern = child->GetPattern<GridItemPattern>();
98         ASSERT_TRUE(gridItemPattern != nullptr);
99         gridItemPattern->SetSelected(true);
100     }
101     actuator->isSelectedItemNode_ = true;
102     auto gridPattern = parentNode_->GetPattern<GridPattern>();
103     GatherNodeChildInfo gatherNodeInfo;
104     auto imageNodeId = GetElmtId();
105     auto gatherNodeId = GetElmtId();
106     auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anon4f5cc2df0402() 107         []() { return AceType::MakeRefPtr<Pattern>(); });
108     auto gatherNode = FrameNode::GetOrCreateFrameNode("gatherNode", gatherNodeId,
__anon4f5cc2df0502() 109         []() { return AceType::MakeRefPtr<Pattern>(); });
110     gatherNodeInfo.imageNode =  AceType::WeakClaim(AceType::RawPtr(imageNode));
111     actuator->PushBackGatherNodeChild(gatherNodeInfo);
112     actuator->SetGatherNode(gatherNode);
113     ASSERT_TRUE(gridPattern != nullptr);
114     gridPattern->gridLayoutInfo_.endIndex_ = DEFAULT_CHILD_COUNT;
115     DragAnimationHelper::PlayGatherAnimationBeforeLifting(actuator);
116     ASSERT_NE(actuator->GetGatherNode(), nullptr);
117 }
118 
119 /**
120  * @tc.name: CalcOffsetToTarget001
121  * @tc.desc: test CalcOffsetToTarget func.
122  * @tc.type: FUNC
123  */
124 HWTEST_F(DragAnimationHelperTestNg, CalcOffsetToTarget001, TestSize.Level1)
125 {
126     CalcResult result;
127     auto offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(), result);
128     EXPECT_TRUE(IsEqual(offset, OffsetF(0.0f, 0.0f)));
129 
130     result.maxDistance = 10.0f;
131     result.minDistance =5.0f;
132     result.maxTranslation =1.0f;
133     offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(), result);
134     EXPECT_TRUE(IsEqual(offset, OffsetF(0.0f, 0.0f)));
135 
136     offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(3.0f, 4.0f), result);
137     EXPECT_EQ(result.maxTranslation, 1.0f);
138 }
139 
140 /**
141  * @tc.name: PlayNodeAnimationBeforeLifting001
142  * @tc.desc: test PlayNodeAnimationBeforeLifting func.
143  * @tc.type: FUNC
144  */
145 HWTEST_F(DragAnimationHelperTestNg, PlayNodeAnimationBeforeLifting001, TestSize.Level1)
146 {
147     ASSERT_TRUE(parentNode_ != nullptr);
148     DragPreviewOption option;
149     option.defaultAnimationBeforeLifting = false;
150     parentNode_->SetDragPreviewOptions(option);
151     DragAnimationHelper::PlayNodeAnimationBeforeLifting(parentNode_);
152 
153     option.defaultAnimationBeforeLifting = true;
154     parentNode_->SetDragPreviewOptions(option);
155     DragAnimationHelper::PlayNodeAnimationBeforeLifting(parentNode_);
156     auto vector = parentNode_->GetRenderContext()->GetTransformScale();
157     ASSERT_TRUE(vector.has_value());
158 }
159 
160 /**
161  * @tc.name: PlayNodeAnimationBeforeLifting001
162  * @tc.desc: test PlayNodeResetAnimation func.
163  * @tc.type: FUNC
164  */
165 HWTEST_F(DragAnimationHelperTestNg, PlayNodeResetAnimation001, TestSize.Level1)
166 {
167     ASSERT_TRUE(parentNode_ != nullptr);
168     DragPreviewOption option;
169     option.defaultAnimationBeforeLifting = false;
170     parentNode_->SetDragPreviewOptions(option);
171     auto gestureHub = parentNode_->GetOrCreateGestureEventHub();
172     auto actuator = AceType::MakeRefPtr<DragEventActuator>(
173         AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
174     DragAnimationHelper::PlayNodeResetAnimation(actuator);
175 
176     option.defaultAnimationBeforeLifting = true;
177     parentNode_->SetDragPreviewOptions(option);
178     DragAnimationHelper::PlayNodeResetAnimation(actuator);
179     auto type = parentNode_->GetLayoutProperty()->GetVisibilityValue(VisibleType::INVISIBLE);
180     ASSERT_TRUE(type == VisibleType::VISIBLE);
181 }
182 
183 /**
184  * @tc.name: PlayGatherAnimation001
185  * @tc.desc: test PlayGatherAnimation func.
186  * @tc.type: FUNC
187  */
188 HWTEST_F(DragAnimationHelperTestNg, PlayGatherAnimation001, TestSize.Level1)
189 {
190     ASSERT_TRUE(parentNode_ != nullptr);
191     std::vector<GatherNodeChildInfo> gatherNodeInfos;
192     GatherNodeChildInfo gatherNodeInfo;
193     auto imageNodeId = GetElmtId();
194     auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anon4f5cc2df0602() 195         []() { return AceType::MakeRefPtr<Pattern>(); });
196     gatherNodeInfo.imageNode =  AceType::WeakClaim(AceType::RawPtr(imageNode));
197     gatherNodeInfos.emplace_back(gatherNodeInfo);
198     auto pipelineContext = MockPipelineContext::GetCurrent();
199     ASSERT_TRUE(pipelineContext != nullptr);
200     auto overlayManager = pipelineContext->GetOverlayManager();
201     ASSERT_TRUE(overlayManager != nullptr);
202     overlayManager->MountGatherNodeToRootNode(parentNode_, gatherNodeInfos);
203     DragAnimationHelper::PlayGatherAnimation(parentNode_, overlayManager);
204 
205     auto renderContext = imageNode->GetRenderContext();
206     ASSERT_TRUE(renderContext != nullptr);
207     auto borderRadius = renderContext->GetBorderRadius();
208     ASSERT_TRUE(borderRadius.has_value());
209     EXPECT_FALSE(borderRadius.value().multiValued);
210 }
211 
212 /**
213  * @tc.name: ShowBadgeAnimation001
214  * @tc.desc: test ShowBadgeAnimation func.
215  * @tc.type: FUNC
216  */
217 HWTEST_F(DragAnimationHelperTestNg, ShowBadgeAnimation001, TestSize.Level1)
218 {
219     auto textNodeId = GetElmtId();
220     auto textNode = FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, textNodeId,
__anon4f5cc2df0702() 221         []() { return AceType::MakeRefPtr<Pattern>(); });
222 
223     DragAnimationHelper::ShowBadgeAnimation(textNode);
224     auto renderContext = textNode->GetRenderContext();
225     ASSERT_TRUE(renderContext != nullptr);
226     auto transformScale = renderContext->GetTransformScale();
227     EXPECT_TRUE(transformScale.has_value());
228 }
229 
230 /**
231  * @tc.name: CalcBadgeTextPosition001
232  * @tc.desc: test CalcBadgeTextPosition func.
233  * @tc.type: FUNC
234  */
235 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextPosition001, TestSize.Level1)
236 {
237     std::vector<GatherNodeChildInfo> gatherNodeInfos;
238     GatherNodeChildInfo gatherNodeInfo;
239     auto imageNodeId = GetElmtId();
240     auto textNodeId = GetElmtId();
241     auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
242     ASSERT_TRUE(frameNode != nullptr);
243     auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
244     auto textNode = FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, textNodeId,
__anon4f5cc2df0802() 245         []() { return AceType::MakeRefPtr<TextPattern>(); });
246     auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anon4f5cc2df0902() 247         []() { return AceType::MakeRefPtr<Pattern>(); });
248 
249     gatherNodeInfo.imageNode =  AceType::WeakClaim(AceType::RawPtr(imageNode));
250     gatherNodeInfos.emplace_back(gatherNodeInfo);
251     auto pipelineContext = MockPipelineContext::GetCurrent();
252     ASSERT_TRUE(pipelineContext != nullptr);
253     auto overlayManager = pipelineContext->GetOverlayManager();
254     ASSERT_TRUE(overlayManager != nullptr);
255     overlayManager->MountGatherNodeToRootNode(textNode, gatherNodeInfos);
256     DragAnimationHelper::CalcBadgeTextPosition(menuPattern, overlayManager, imageNode, textNode);
257 
258     auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
259     ASSERT_TRUE(textLayoutProperty != nullptr);
260     auto content = textLayoutProperty->GetContentValue();
261     EXPECT_STREQ(content.c_str(), std::to_string(overlayManager->GetGatherNodeChildrenInfo().size() + 1).c_str());
262 }
263 } // namespace OHOS::Ace::NG