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 "core/components_ng/manager/drag_drop/drag_drop_func_wrapper.h"
18 #include "test/mock/base/mock_pixel_map.h"
19 #include "test/mock/core/common/mock_theme_manager.h"
20
21 using namespace testing;
22 using namespace testing::ext;
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr size_t DEFAULT_CHILD_COUNT = 4;
27 constexpr float GRID_WIDTH = 480.0f;
28 constexpr float GRID_HEIGHT = 800.0f;
29 constexpr float ITEM_WIDTH = 120.0f;
30 constexpr float ITEM_HEIGHT = 200.0f;
31 constexpr float PIXELMAP_DRAG_SCALE_MULTIPLE = 1.05f;
32 constexpr int32_t DEFAULT_BADGE_NUM = 2;
33 constexpr Dimension BADGE_RELATIVE_OFFSET = 8.0_vp;
34 const float EPSILON = 0.1f;
35 } // namespace
36
SetUpTestSuite()37 void DragAnimationHelperTestNg::SetUpTestSuite()
38 {
39 TestNG::SetUpTestSuite();
40 }
41
TearDownTestSuite()42 void DragAnimationHelperTestNg::TearDownTestSuite()
43 {
44 TestNG::TearDownTestSuite();
45 }
46
SetUp()47 void DragAnimationHelperTestNg::SetUp()
48 {
49 ResetElmtId();
50 auto [gridNode, gridItemNodes] = CreateGridNodeWithChild(DEFAULT_CHILD_COUNT);
51 parentNode_ = gridNode;
52 auto pattern = gridNode->GetPattern<GridPattern>();
53 CHECK_NULL_VOID(pattern);
54 pattern->info_.endIndex_ = DEFAULT_CHILD_COUNT;
55 CHECK_NULL_VOID(parentNode_);
56 childNodes_ = gridItemNodes;
57 }
58
TearDown()59 void DragAnimationHelperTestNg::TearDown()
60 {
61 parentNode_ = nullptr;
62 childNodes_.clear();
63 }
64
CreateGridNodeWithChild(size_t childCount,const GridItemStyle & gridItemStyle)65 std::pair<RefPtr<FrameNode>, std::list<RefPtr<FrameNode>>> DragAnimationHelperTestNg::CreateGridNodeWithChild(
66 size_t childCount, const GridItemStyle& gridItemStyle)
67 {
68 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
69 auto nodeId = ViewStackProcessor::GetInstance()->ClaimNodeId();
70 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::GRID_ETS_TAG, nodeId,
71 []() {return AceType::MakeRefPtr<GridPattern>(); });
72 ViewAbstract::SetWidth(Referenced::RawPtr(frameNode), CalcLength(GRID_WIDTH));
73 ViewAbstract::SetHeight(Referenced::RawPtr(frameNode), CalcLength(GRID_HEIGHT));
74 std::list<RefPtr<FrameNode>> childNodes;
75
76 for (size_t i = 0; i < childCount; ++i) {
77 ViewStackProcessor::GetInstance()->StartGetAccessRecordingFor(GetElmtId());
78 auto itemNodeId = ViewStackProcessor::GetInstance()->ClaimNodeId();
79 auto childNode = FrameNode::GetOrCreateFrameNode(V2::GRID_ITEM_ETS_TAG, itemNodeId,
80 [itemStyle = gridItemStyle]() { return AceType::MakeRefPtr<GridItemPattern>(nullptr, itemStyle); });
81 if (!childNode) {
82 continue;
83 }
84 ViewAbstract::SetWidth(Referenced::RawPtr(childNode), CalcLength(ITEM_WIDTH));
85 ViewAbstract::SetHeight(Referenced::RawPtr(childNode), CalcLength(ITEM_HEIGHT));
86
87 auto gridItemPattern = childNode->GetPattern<GridItemPattern>();
88 if (gridItemPattern) {
89 gridItemPattern->SetSelected(true);
90 }
91
92 auto dragPreviewOption = childNode->GetDragPreviewOption();
93 dragPreviewOption.isMultiSelectionEnabled = true;
94 childNode->SetDragPreviewOptions(dragPreviewOption);
95
96 auto dragPreviewInfo = childNode->GetDragPreview();
97 dragPreviewInfo.pixelMap = AceType::MakeRefPtr<MockPixelMap>();
98 childNode->SetDragPreview(dragPreviewInfo);
99
100 auto gestureHub = childNode->GetOrCreateGestureEventHub();
101 if (!gestureHub) {
102 continue;
103 }
104 auto eventHub = gestureHub->eventHub_.Upgrade();
105 if (!eventHub) {
106 continue;
107 }
108 auto func = [](const RefPtr<OHOS::Ace::DragEvent>&, const std::string&) { return DragDropInfo(); };
109 eventHub->onDragStart_ = func;
110 childNode->MountToParent(frameNode);
111 childNodes.emplace_back(childNode);
112 }
113 return { frameNode, childNodes };
114 }
115
116 /**
117 * @tc.name: PlayGatherAnimationBeforeLifting001
118 * @tc.desc: test PlayGatherAnimationBeforeLifting func.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(DragAnimationHelperTestNg, PlayGatherAnimationBeforeLifting001, TestSize.Level1)
122 {
123 auto iter = childNodes_.begin();
124 ASSERT_TRUE(iter != childNodes_.end());
125 auto gestureHub = (*iter)->GetOrCreateGestureEventHub();
126 auto actuator = AceType::MakeRefPtr<DragEventActuator>(
127 AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
128 actuator->FindItemParentNode(*(childNodes_.begin()));
129 DragAnimationHelper::PlayGatherAnimationBeforeLifting(actuator);
130
131 for (const auto& child : childNodes_) {
132 ASSERT_TRUE(child != nullptr);
133 auto gridItemPattern = child->GetPattern<GridItemPattern>();
134 ASSERT_TRUE(gridItemPattern != nullptr);
135 gridItemPattern->SetSelected(true);
136 }
137 actuator->isSelectedItemNode_ = true;
138 auto gridPattern = parentNode_->GetPattern<GridPattern>();
139 GatherNodeChildInfo gatherNodeInfo;
140 auto imageNodeId = GetElmtId();
141 auto gatherNodeId = GetElmtId();
142 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc90502() 143 []() { return AceType::MakeRefPtr<Pattern>(); });
144 auto gatherNode = FrameNode::GetOrCreateFrameNode("gatherNode", gatherNodeId,
__anondb911dc90602() 145 []() { return AceType::MakeRefPtr<Pattern>(); });
146 gatherNodeInfo.imageNode = AceType::WeakClaim(AceType::RawPtr(imageNode));
147 actuator->PushBackGatherNodeChild(gatherNodeInfo);
148 actuator->SetGatherNode(gatherNode);
149 ASSERT_TRUE(gridPattern != nullptr);
150 gridPattern->info_.endIndex_ = DEFAULT_CHILD_COUNT;
151 DragAnimationHelper::PlayGatherAnimationBeforeLifting(actuator);
152 ASSERT_NE(actuator->GetGatherNode(), nullptr);
153 }
154
155 /**
156 * @tc.name: CalcOffsetToTarget001
157 * @tc.desc: test CalcOffsetToTarget func.
158 * @tc.type: FUNC
159 */
160 HWTEST_F(DragAnimationHelperTestNg, CalcOffsetToTarget001, TestSize.Level1)
161 {
162 CalcResult result;
163 auto offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(), result);
164 EXPECT_TRUE(IsEqual(offset, OffsetF(0.0f, 0.0f)));
165
166 result.maxDistance = 10.0f;
167 result.minDistance =5.0f;
168 result.maxTranslation =1.0f;
169 offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(), result);
170 EXPECT_TRUE(IsEqual(offset, OffsetF(0.0f, 0.0f)));
171
172 offset = DragAnimationHelper::CalcOffsetToTarget(OffsetF(), OffsetF(3.0f, 4.0f), result);
173 EXPECT_EQ(result.maxTranslation, 1.0f);
174 }
175
176 /**
177 * @tc.name: PlayNodeAnimationBeforeLifting001
178 * @tc.desc: test PlayNodeAnimationBeforeLifting func.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(DragAnimationHelperTestNg, PlayNodeAnimationBeforeLifting001, TestSize.Level1)
182 {
183 ASSERT_TRUE(parentNode_ != nullptr);
184 DragPreviewOption option;
185 option.defaultAnimationBeforeLifting = false;
186 parentNode_->SetDragPreviewOptions(option);
187 DragAnimationHelper::PlayNodeAnimationBeforeLifting(parentNode_);
188
189 option.defaultAnimationBeforeLifting = true;
190 parentNode_->SetDragPreviewOptions(option);
191 DragAnimationHelper::PlayNodeAnimationBeforeLifting(parentNode_);
192 auto vector = parentNode_->GetRenderContext()->GetTransformScale();
193 ASSERT_TRUE(vector.has_value());
194 }
195
196 /**
197 * @tc.name: PlayNodeAnimationBeforeLifting001
198 * @tc.desc: test PlayNodeResetAnimation func.
199 * @tc.type: FUNC
200 */
201 HWTEST_F(DragAnimationHelperTestNg, PlayNodeResetAnimation001, TestSize.Level1)
202 {
203 ASSERT_TRUE(parentNode_ != nullptr);
204 DragPreviewOption option;
205 option.defaultAnimationBeforeLifting = false;
206 parentNode_->SetDragPreviewOptions(option);
207 auto gestureHub = parentNode_->GetOrCreateGestureEventHub();
208 auto actuator = AceType::MakeRefPtr<DragEventActuator>(
209 AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
210 DragAnimationHelper::PlayNodeResetAnimation(actuator);
211
212 option.defaultAnimationBeforeLifting = true;
213 parentNode_->SetDragPreviewOptions(option);
214 DragAnimationHelper::PlayNodeResetAnimation(actuator);
215 auto type = parentNode_->GetLayoutProperty()->GetVisibilityValue(VisibleType::INVISIBLE);
216 ASSERT_TRUE(type == VisibleType::VISIBLE);
217 }
218
219 /**
220 * @tc.name: PlayGatherAnimation001
221 * @tc.desc: test PlayGatherAnimation func.
222 * @tc.type: FUNC
223 */
224 HWTEST_F(DragAnimationHelperTestNg, PlayGatherAnimation001, TestSize.Level1)
225 {
226 ASSERT_TRUE(parentNode_ != nullptr);
227 std::vector<GatherNodeChildInfo> gatherNodeInfos;
228 GatherNodeChildInfo gatherNodeInfo;
229 auto imageNodeId = GetElmtId();
230 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc90702() 231 []() { return AceType::MakeRefPtr<Pattern>(); });
232 gatherNodeInfo.imageNode = AceType::WeakClaim(AceType::RawPtr(imageNode));
233 gatherNodeInfos.emplace_back(gatherNodeInfo);
234 auto pipelineContext = MockPipelineContext::GetCurrent();
235 ASSERT_TRUE(pipelineContext != nullptr);
236 auto overlayManager = pipelineContext->GetOverlayManager();
237 ASSERT_TRUE(overlayManager != nullptr);
238 overlayManager->MountGatherNodeToRootNode(parentNode_, gatherNodeInfos);
239 DragAnimationHelper::PlayGatherAnimation(parentNode_, overlayManager);
240
241 auto renderContext = imageNode->GetRenderContext();
242 ASSERT_TRUE(renderContext != nullptr);
243 auto borderRadius = renderContext->GetBorderRadius();
244 ASSERT_TRUE(borderRadius.has_value());
245 EXPECT_FALSE(borderRadius.value().multiValued);
246 }
247
248 /**
249 * @tc.name: GetLiftingNodeScale001
250 * @tc.desc: test GetLiftingNodeScale func.
251 * @tc.type: FUNC
252 */
253 HWTEST_F(DragAnimationHelperTestNg, GetLiftingNodeScale001, TestSize.Level1)
254 {
255 auto scale = DragAnimationHelper::GetLiftingNodeScale(nullptr);
256 EXPECT_EQ(scale, PIXELMAP_DRAG_SCALE_MULTIPLE);
257 auto imageNodeId = GetElmtId();
258 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc90802() 259 []() { return AceType::MakeRefPtr<Pattern>(); });
260 ASSERT_NE(imageNode, nullptr);
261 auto renderContext = imageNode->GetRenderContext();
262 ASSERT_NE(renderContext, nullptr);
263 const float customScale = 2.0f;
264 renderContext->UpdateTransformScale({ customScale, customScale });
265 scale = DragAnimationHelper::GetLiftingNodeScale(renderContext);
266 EXPECT_EQ(scale, customScale);
267 }
268
269 /**
270 * @tc.name: ShowBadgeAnimation001
271 * @tc.desc: test ShowBadgeAnimation func.
272 * @tc.type: FUNC
273 */
274 HWTEST_F(DragAnimationHelperTestNg, ShowBadgeAnimation001, TestSize.Level1)
275 {
276 auto textNodeId = GetElmtId();
277 auto textNode = FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, textNodeId,
__anondb911dc90902() 278 []() { return AceType::MakeRefPtr<Pattern>(); });
279
280 DragAnimationHelper::ShowBadgeAnimation(textNode);
281 auto renderContext = textNode->GetRenderContext();
282 ASSERT_TRUE(renderContext != nullptr);
283 auto transformScale = renderContext->GetTransformScale();
284 EXPECT_TRUE(transformScale.has_value());
285 }
286
287 /**
288 * @tc.name: CalcBadgeTextPosition001
289 * @tc.desc: test CalcBadgeTextPosition func.
290 * @tc.type: FUNC
291 */
292 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextPosition001, TestSize.Level1)
293 {
294 std::vector<GatherNodeChildInfo> gatherNodeInfos;
295 GatherNodeChildInfo gatherNodeInfo;
296 auto imageNodeId = GetElmtId();
297 auto textNodeId = GetElmtId();
298 auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
299 ASSERT_TRUE(frameNode != nullptr);
300 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
301 auto textNode = FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, textNodeId,
__anondb911dc90a02() 302 []() { return AceType::MakeRefPtr<TextPattern>(); });
303 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc90b02() 304 []() { return AceType::MakeRefPtr<Pattern>(); });
305
306 gatherNodeInfo.imageNode = AceType::WeakClaim(AceType::RawPtr(imageNode));
307 gatherNodeInfos.emplace_back(gatherNodeInfo);
308 auto pipelineContext = MockPipelineContext::GetCurrent();
309 ASSERT_TRUE(pipelineContext != nullptr);
310 auto overlayManager = pipelineContext->GetOverlayManager();
311 ASSERT_TRUE(overlayManager != nullptr);
312 overlayManager->MountGatherNodeToRootNode(textNode, gatherNodeInfos);
313 DragAnimationHelper::CalcBadgeTextPosition(menuPattern, overlayManager, imageNode, textNode);
314
315 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
316 ASSERT_TRUE(textLayoutProperty != nullptr);
317 auto content = textLayoutProperty->GetContentValue();
318 EXPECT_STREQ(StringUtils::Str16ToStr8(content).c_str(),
319 std::to_string(overlayManager->GetGatherNodeChildrenInfo().size() + 1).c_str());
320 }
321
322 /**
323 * @tc.name: CalcBadgeTextPosition002
324 * @tc.desc: test CalcBadgeTextPosition func.
325 * @tc.type: FUNC
326 */
327 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextPosition002, TestSize.Level1)
328 {
329 std::vector<GatherNodeChildInfo> gatherNodeInfos;
330 GatherNodeChildInfo gatherNodeInfo;
331 auto imageNodeId = GetElmtId();
332 auto textNodeId = GetElmtId();
333 auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
334 ASSERT_NE(frameNode, nullptr);
335 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
336 auto textNode = FrameNode::GetOrCreateFrameNode(V2::TEXT_ETS_TAG, textNodeId,
__anondb911dc90c02() 337 []() { return AceType::MakeRefPtr<TextPattern>(); });
338 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc90d02() 339 []() { return AceType::MakeRefPtr<Pattern>(); });
340 ASSERT_NE(menuPattern, nullptr);
341
342 gatherNodeInfo.imageNode = AceType::WeakClaim(AceType::RawPtr(imageNode));
343 gatherNodeInfos.emplace_back(gatherNodeInfo);
344 auto dragPreviewOption = frameNode->GetDragPreviewOption();
345 dragPreviewOption.isNumber = true;
346 dragPreviewOption.badgeNumber = 3;
347 frameNode->SetDragPreviewOptions(dragPreviewOption);
348
349 auto pipelineContext = MockPipelineContext::GetCurrent();
350 ASSERT_NE(pipelineContext, nullptr);
351 auto overlayManager = pipelineContext->GetOverlayManager();
352 ASSERT_NE(overlayManager, nullptr);
353 overlayManager->MountGatherNodeToRootNode(textNode, gatherNodeInfos);
354 DragAnimationHelper::CalcBadgeTextPosition(menuPattern, overlayManager, imageNode, textNode);
355
356 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
357 ASSERT_NE(textLayoutProperty, nullptr);
358 auto content = textLayoutProperty->GetContentValue();
359
360 EXPECT_STREQ(StringUtils::Str16ToStr8(content).c_str(),
361 std::to_string(3).c_str());
362 }
363
364 /**
365 * @tc.name: CalcBadgeTextPosition003
366 * @tc.desc: Test CalcBadgeTextPosition when textNode->GetContext() is nullptr
367 * @tc.type: FUNC
368 */
369 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextPosition003, TestSize.Level1)
370 {
371 auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
372 ASSERT_NE(frameNode, nullptr);
373 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
374 ASSERT_NE(menuPattern, nullptr);
375 auto textNode = FrameNode::GetOrCreateFrameNode(
__anondb911dc90e02() 376 V2::TEXT_ETS_TAG, GetElmtId(), []() { return AceType::MakeRefPtr<TextPattern>(); });
377 auto imageNode = FrameNode::GetOrCreateFrameNode(
__anondb911dc90f02() 378 V2::IMAGE_ETS_TAG, GetElmtId(), []() { return AceType::MakeRefPtr<Pattern>(); });
379 textNode->context_ = nullptr;
380 auto pipelineContext = MockPipelineContext::GetCurrent();
381 ASSERT_NE(pipelineContext, nullptr);
382 auto overlayManager = pipelineContext->GetOverlayManager();
383 ASSERT_NE(overlayManager, nullptr);
384
385 overlayManager->MountGatherNodeToRootNode(textNode, {});
386 DragAnimationHelper::CalcBadgeTextPosition(menuPattern, overlayManager, imageNode, textNode);
387 auto layoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
388 ASSERT_NE(layoutProperty, nullptr);
389 auto content = layoutProperty->GetContentValue();
390 EXPECT_NE(content.empty(), true);
391 }
392
393 /**
394 * @tc.name: CalcBadgeTextPosition004
395 * @tc.desc: Test CalcBadgeTextPosition when frameNode does not set badgeNumber
396 * @tc.type: FUNC
397 */
398 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextPosition004, TestSize.Level1)
399 {
400 /**
401 * @tc.steps: step1. Create required dependency nodes imageNode and textNode.
402 */
403 auto imageNode = FrameNode::GetOrCreateFrameNode(
__anondb911dc91002() 404 V2::IMAGE_ETS_TAG, GetElmtId(), []() { return AceType::MakeRefPtr<Pattern>(); });
405 auto textNode = FrameNode::GetOrCreateFrameNode(
__anondb911dc91102() 406 V2::TEXT_ETS_TAG, GetElmtId(), []() { return AceType::MakeRefPtr<TextPattern>(); });
407 ASSERT_NE(imageNode, nullptr);
408 ASSERT_NE(textNode, nullptr);
409
410 /**
411 * @tc.steps: step2. Create frameNode and assign as MenuPattern target.
412 */
413 auto frameNode =
__anondb911dc91202() 414 FrameNode::GetOrCreateFrameNode("menuTarget", GetElmtId(), []() { return AceType::MakeRefPtr<Pattern>(); });
415 ASSERT_NE(frameNode, nullptr);
416 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
417 ASSERT_NE(menuPattern, nullptr);
418
419 /**
420 * @tc.steps: step3. Configure OverlayManager and insert 1 child node.
421 */
422 auto pipelineContext = MockPipelineContext::GetCurrent();
423 ASSERT_NE(pipelineContext, nullptr);
424 auto overlayManager = pipelineContext->GetOverlayManager();
425 ASSERT_NE(overlayManager, nullptr);
426
427 std::vector<GatherNodeChildInfo> gatherInfos;
428 auto gatherImageNode = FrameNode::GetOrCreateFrameNode(
__anondb911dc91302() 429 V2::IMAGE_ETS_TAG, GetElmtId(), []() { return AceType::MakeRefPtr<Pattern>(); });
430 GatherNodeChildInfo info;
431 info.imageNode = AceType::WeakClaim(AceType::RawPtr(gatherImageNode));
432 gatherInfos.emplace_back(info);
433 overlayManager->MountGatherNodeToRootNode(textNode, gatherInfos);
434
435 /**
436 * @tc.steps: step4. Call the target function.
437 * @tc.expected: The text content of textNode should be childrenCount + 1, i.e., "2".
438 */
439 DragAnimationHelper::CalcBadgeTextPosition(menuPattern, overlayManager, imageNode, textNode);
440 auto textLayoutProperty = textNode->GetLayoutProperty<TextLayoutProperty>();
441 ASSERT_NE(textLayoutProperty, nullptr);
442 auto content = textLayoutProperty->GetContentValue();
443 EXPECT_STREQ(StringUtils::Str16ToStr8(content).c_str(), "2");
444 }
445
446 /**
447 * @tc.name: CreateImageNode
448 * @tc.desc: test CreateImageNode func.
449 * @tc.type: FUNC
450 */
451 HWTEST_F(DragAnimationHelperTestNg, CreateImageNode, TestSize.Level1)
452 {
453 /**
454 * @tc.steps: step1. create pixelMap.
455 * @tc.expected: pixelMap is not null.
456 */
457 RefPtr<MockPixelMap> mockPixelMap = AceType::MakeRefPtr<MockPixelMap>();
458 ASSERT_NE(mockPixelMap, nullptr);
459
460 /**
461 * @tc.steps: step2. create imageNode.
462 * @tc.expected: imageNode is not null.
463 */
464 auto imageNode = DragAnimationHelper::CreateImageNode(mockPixelMap);
465 ASSERT_NE(imageNode, nullptr);
466
467 /**
468 * @tc.steps: step3. check imageNode attr
469 */
470 auto imageContext = imageNode->GetRenderContext();
471 ASSERT_NE(imageContext, nullptr);
472 auto opacity = imageContext->GetOpacity().value_or(0);
473 EXPECT_EQ(opacity, 1.0f);
474 }
475
476 /**
477 * @tc.name: CreateGatherImageNode
478 * @tc.desc: test CreateGatherImageNode func.
479 * @tc.type: FUNC
480 */
481 HWTEST_F(DragAnimationHelperTestNg, CreateGatherImageNode, TestSize.Level1)
482 {
483 /**
484 * @tc.steps: step1. create gatherNode.
485 * @tc.expected: gatherChildNode is not null.
486 */
487 int32_t size = childNodes_.size();
488 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
489 auto iter = childNodes_.begin();
490 ASSERT_TRUE(iter != childNodes_.end());
491 auto itemNode = *(iter);
492 ASSERT_NE(itemNode, nullptr);
493 GatherNodeChildInfo gatherNodeChildInfo;
494 auto gatherChildNode = DragAnimationHelper::CreateGatherImageNode(itemNode, gatherNodeChildInfo);
495 ASSERT_NE(gatherChildNode, nullptr);
496 }
497
498 /**
499 * @tc.name: CreateGatherNode
500 * @tc.desc: test CreateGatherNode func.
501 * @tc.type: FUNC
502 */
503 HWTEST_F(DragAnimationHelperTestNg, CreateGatherNode, TestSize.Level1)
504 {
505 /**
506 * @tc.steps: step1. create gatherNode.
507 * @tc.expected: gatherNode is not null.
508 */
509 int32_t size = childNodes_.size();
510 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
511 auto iter = childNodes_.begin();
512 ASSERT_TRUE(iter != childNodes_.end());
513 auto itemNode = *(iter);
514 ASSERT_NE(itemNode, nullptr);
515 std::vector<GatherNodeChildInfo> gatherNodeInfo;
516 auto gatherNode = DragAnimationHelper::CreateGatherNode(itemNode, gatherNodeInfo);
517 ASSERT_NE(gatherNode, nullptr);
518
519 /**
520 * @tc.steps: step2. check gatherNodeInfo size
521 */
522 auto gatherChildSize = gatherNodeInfo.size();
523 EXPECT_EQ(gatherChildSize + 1, size);
524 }
525
526 /**
527 * @tc.name: GetOrCreateGatherNode
528 * @tc.desc: test GetOrCreateGatherNode func.
529 * @tc.type: FUNC
530 */
531 HWTEST_F(DragAnimationHelperTestNg, GetOrCreateGatherNode001, TestSize.Level1)
532 {
533 /**
534 * @tc.steps: step1. create gatherNode.
535 * @tc.expected: gatherNode is not null.
536 */
537 int32_t size = childNodes_.size();
538 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
539 auto iter = childNodes_.begin();
540 ASSERT_TRUE(iter != childNodes_.end());
541 auto itemNode = *(iter);
542 ASSERT_NE(itemNode, nullptr);
543 std::vector<GatherNodeChildInfo> gatherNodeInfo;
544 auto gestureHub = itemNode->GetOrCreateGestureEventHub();
545 ASSERT_NE(gestureHub, nullptr);
546
547 /**
548 * @tc.steps: step2. Get overlayManager
549 */
550 auto pipelineContext = MockPipelineContext::GetCurrent();
551 ASSERT_NE(pipelineContext, nullptr);
552 auto overlayManager = pipelineContext->GetOverlayManager();
553 ASSERT_NE(overlayManager, nullptr);
554 overlayManager->RemoveGatherNode();
555
556 /**
557 * @tc.steps: step2. check gatherNodeInfo size
558 */
559 auto actuator = AceType::MakeRefPtr<DragEventActuator>(
560 AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
561 ASSERT_NE(actuator, nullptr);
562 actuator->itemParentNode_ = parentNode_;
563 actuator->isSelectedItemNode_ = true;
564 auto gatherNode = DragAnimationHelper::GetOrCreateGatherNode(overlayManager, actuator, gatherNodeInfo);
565 ASSERT_NE(gatherNode, nullptr);
566
567 auto gatherChildSize = gatherNodeInfo.size();
568 EXPECT_EQ(gatherChildSize + 1, size);
569 }
570
571 /**
572 * @tc.name: GetOrCreateGatherNode
573 * @tc.desc: test GetOrCreateGatherNode func.
574 * @tc.type: FUNC
575 */
576 HWTEST_F(DragAnimationHelperTestNg, GetOrCreateGatherNode002, TestSize.Level1)
577 {
578 /**
579 * @tc.steps: step1. create gatherNode.
580 * @tc.expected: gatherNode is not null.
581 */
582 int32_t size = childNodes_.size();
583 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
584 auto iter = childNodes_.begin();
585 ASSERT_TRUE(iter != childNodes_.end());
586 auto itemNode = *(iter);
587 ASSERT_NE(itemNode, nullptr);
588 std::vector<GatherNodeChildInfo> gatherNodeInfo;
589 auto gatherNode = DragAnimationHelper::CreateGatherNode(itemNode, gatherNodeInfo);
590 ASSERT_NE(gatherNode, nullptr);
591
592 /**
593 * @tc.steps: step2. Get overlayManager
594 */
595 auto pipelineContext = MockPipelineContext::GetCurrent();
596 ASSERT_NE(pipelineContext, nullptr);
597 auto overlayManager = pipelineContext->GetOverlayManager();
598 ASSERT_NE(overlayManager, nullptr);
599 overlayManager->RemoveGatherNode();
600
601 /**
602 * @tc.steps: step3. Call MountGatherNode
603 */
604 DragAnimationHelper::MountGatherNode(overlayManager, itemNode, gatherNode, gatherNodeInfo);
605 EXPECT_NE(overlayManager->GetGatherNode(), nullptr);
606 auto gatherChildSize = overlayManager->GetGatherNodeChildrenInfo().size();
607 EXPECT_EQ(gatherChildSize + 1, size);
608
609 /**
610 * @tc.steps: step4. call GetOrCreateGatherNode
611 */
612 auto gestureHub = itemNode->GetOrCreateGestureEventHub();
613 ASSERT_NE(gestureHub, nullptr);
614 auto actuator = AceType::MakeRefPtr<DragEventActuator>(
615 AceType::WeakClaim(AceType::RawPtr(gestureHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
616 ASSERT_NE(actuator, nullptr);
617 actuator->itemParentNode_ = parentNode_;
618 actuator->isSelectedItemNode_ = true;
619 auto newGatherNode = DragAnimationHelper::GetOrCreateGatherNode(overlayManager, actuator, gatherNodeInfo);
620 ASSERT_EQ(gatherNode, newGatherNode);
621 }
622
623
624 /**
625 * @tc.name: MountGatherNode
626 * @tc.desc: test MountGatherNode func.
627 * @tc.type: FUNC
628 */
629 HWTEST_F(DragAnimationHelperTestNg, MountGatherNode, TestSize.Level1)
630 {
631 /**
632 * @tc.steps: step1. create gatherNode.
633 * @tc.expected: gatherNode is not null.
634 */
635 int32_t size = childNodes_.size();
636 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
637 auto iter = childNodes_.begin();
638 ASSERT_TRUE(iter != childNodes_.end());
639 auto itemNode = *(iter);
640 ASSERT_NE(itemNode, nullptr);
641 std::vector<GatherNodeChildInfo> gatherNodeInfo;
642 auto gatherNode = DragAnimationHelper::CreateGatherNode(itemNode, gatherNodeInfo);
643 ASSERT_NE(gatherNode, nullptr);
644
645 /**
646 * @tc.steps: step2. Get overlayManager
647 */
648 auto pipelineContext = MockPipelineContext::GetCurrent();
649 ASSERT_NE(pipelineContext, nullptr);
650 auto overlayManager = pipelineContext->GetOverlayManager();
651 ASSERT_NE(overlayManager, nullptr);
652
653 /**
654 * @tc.steps: step3. Call MountGatherNode
655 */
656 DragAnimationHelper::MountGatherNode(overlayManager, itemNode, gatherNode, gatherNodeInfo);
657 EXPECT_NE(overlayManager->GetGatherNode(), nullptr);
658 auto gatherChildSize = overlayManager->GetGatherNodeChildrenInfo().size();
659 EXPECT_EQ(gatherChildSize + 1, size);
660 }
661
662 /**
663 * @tc.name: InitGatherNodeAttr
664 * @tc.desc: test InitGatherNodeAttr func.
665 * @tc.type: FUNC
666 */
667 HWTEST_F(DragAnimationHelperTestNg, InitGatherNodeAttr, TestSize.Level1)
668 {
669 /**
670 * @tc.steps: step1. create gatherNode.
671 * @tc.expected: gatherNode is not null.
672 */
673 int32_t size = childNodes_.size();
674 ASSERT_EQ(DEFAULT_CHILD_COUNT, size);
675 auto iter = childNodes_.begin();
676 ASSERT_TRUE(iter != childNodes_.end());
677 auto itemNode = *(iter);
678 ASSERT_NE(itemNode, nullptr);
679 std::vector<GatherNodeChildInfo> gatherNodeInfo;
680 auto gatherNode = DragAnimationHelper::CreateGatherNode(itemNode, gatherNodeInfo);
681 ASSERT_NE(gatherNode, nullptr);
682
683 /**
684 * @tc.steps: step2. InitGatherNodeAttr
685 */
686 DragAnimationHelper::InitGatherNodeAttr(gatherNode, gatherNodeInfo);
687 auto renderContext = gatherNode->GetRenderContext();
688 ASSERT_NE(renderContext, nullptr);
689 ASSERT_NE(renderContext->GetPosition(), std::nullopt);
690 EXPECT_EQ(renderContext->GetPosition()->GetX().Value(), 0);
691 EXPECT_EQ(renderContext->GetPosition()->GetY().Value(), 0);
692 }
693
694 /**
695 * @tc.name: HideDragNodeCopy
696 * @tc.desc: test HideDragNodeCopy func.
697 * @tc.type: FUNC
698 */
699 HWTEST_F(DragAnimationHelperTestNg, HideDragNodeCopy, TestSize.Level1)
700 {
701 /**
702 * @tc.steps: step1. Create DragNodeCopy
703 */
704 auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
705 ASSERT_NE(frameNode, nullptr);
706 auto pipelineContext = MockPipelineContext::GetCurrent();
707 ASSERT_NE(pipelineContext, nullptr);
708 auto overlayManager = pipelineContext->GetOverlayManager();
709 ASSERT_NE(overlayManager, nullptr);
710 overlayManager->dragNodeCopyWeak_ = frameNode;
711 auto renderContext = frameNode->GetRenderContext();
712 renderContext->UpdateOpacity(1.0f);
713 ASSERT_NE(renderContext, nullptr);
714 auto opacity = renderContext->GetOpacity().value_or(0);
715 EXPECT_EQ(opacity, 1.0f);
716
717 /**
718 * @tc.steps: step2. Call HideDragNodeCopy
719 */
720 DragAnimationHelper::HideDragNodeCopy(overlayManager);
721 opacity = renderContext->GetOpacity().value_or(0);
722 EXPECT_EQ(opacity, 0.0f);
723 }
724
725 /**
726 * @tc.name: DragStartAnimation
727 * @tc.desc: Test DragStartAnimation func with Offset(10.0, 10.0). TranslateOptions value will be right after drag
728 * animation.
729 * @tc.type: FUNC
730 */
731 HWTEST_F(DragAnimationHelperTestNg, DragStartAnimation, TestSize.Level1)
732 {
733 /**
734 * @tc.steps: step1. Create overlayManager and dragDropManager.
735 * @tc.expected: overlayManager and dragDropManager is not null.
736 */
737 int32_t containerId = 100;
738 auto pipelineContext = PipelineContext::GetContextByContainerId(containerId);
739 ASSERT_NE(pipelineContext, nullptr);
740 auto overlayManager = pipelineContext->GetOverlayManager();
741 ASSERT_NE(overlayManager, nullptr);
742 auto dragDropManager = pipelineContext->GetDragDropManager();
743 ASSERT_NE(dragDropManager, nullptr);
744
745 /**
746 * @tc.steps: step2. Create imageNode and dragPreviewInfo.
747 * @tc.expected: dragPreviewInfo's imageNode is not nullptr.
748 */
749 void* voidPtr = static_cast<void*>(new char[0]);
750 RefPtr<PixelMap> refPixelMap = PixelMap::CreatePixelMap(voidPtr);
751 auto imageNode = DragAnimationHelper::CreateImageNode(refPixelMap);
752 ASSERT_NE(imageNode, nullptr);
753 DragDropManager::DragPreviewInfo dragPreviewInfo;
754 dragPreviewInfo.imageNode = imageNode;
755 ASSERT_NE(dragPreviewInfo.imageNode, nullptr);
756 dragDropManager->SetDragPreviewInfo(dragPreviewInfo);
757
758 /**
759 * @tc.steps: step3. Call DragStartAnimation with Offset(10.0, 10.0).
760 * @tc.expected: TranslateOptions x value is 10.0f.
761 */
762 auto newOffset = Offset(10.0, 10.0);
763 auto gatherNodeCenter =
764 NG::DragDropFuncWrapper::GetPaintRectCenter(dragDropManager->GetDragPreviewInfo().imageNode);
765 Point point = { 0, 0 };
766 DragAnimationHelper::DragStartAnimation(newOffset, overlayManager, gatherNodeCenter, point, containerId);
767 auto renderContext = imageNode->GetRenderContext();
768 TranslateOptions result = renderContext->GetTransformTranslate().value();
769 TranslateOptions expectValue { 10.0f, 10.0f, 0.0f };
770 EXPECT_EQ(result.x.CalcValue(), expectValue.x.CalcValue());
771 }
772
773 /**
774 * @tc.name: CreateBadgeTextNodeTest001
775 * @tc.desc: Test CreateBadgeTextNode function.
776 * @tc.type: FUNC
777 */
778 HWTEST_F(DragAnimationHelperTestNg, CreateBadgeTextNodeTest001, TestSize.Level1)
779 {
780 /**
781 * @tc.steps: step1. Create DragEventActuator.
782 */
783 auto eventHub = AceType::MakeRefPtr<EventHub>();
784 ASSERT_NE(eventHub, nullptr);
785 auto frameNode = FrameNode::CreateFrameNode("test", 1, AceType::MakeRefPtr<Pattern>(), false);
786 ASSERT_NE(frameNode, nullptr);
787 eventHub->host_ = AceType::WeakClaim(AceType::RawPtr(frameNode));
788 auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
789 ASSERT_NE(gestureEventHub, nullptr);
790 auto dragEventActuator = AceType::MakeRefPtr<DragEventActuator>(
791 AceType::WeakClaim(AceType::RawPtr(gestureEventHub)), DRAG_DIRECTION, FINGERS_NUMBER, DISTANCE);
792 ASSERT_NE(dragEventActuator, nullptr);
793
794 /**
795 * @tc.steps: step2. Invoke CreateBadgeTextNode.
796 */
797 auto textNode = DragAnimationHelper::CreateBadgeTextNode(2);
798 EXPECT_NE(textNode, nullptr);
799 }
800
801 /**
802 * @tc.name: CreateTextNode001
803 * @tc.desc: Test CreateTextNode
804 * @tc.type: FUNC
805 */
806 HWTEST_F(DragAnimationHelperTestNg, CreateTextNode001, TestSize.Level1)
807 {
808 PreparedInfoForDrag data;
809 data.badgeNumber = DEFAULT_BADGE_NUM;
810 EXPECT_EQ(data.textRowNode, nullptr);
811 DragAnimationHelper::CreateTextNode(data);
812 AceApplicationInfo::GetInstance().isRightToLeft_ = true;
813 DragAnimationHelper::CreateTextNode(data);
814 data.deviceType = SourceType::MOUSE;
815 EXPECT_NE(data.textRowNode, nullptr);
816 data.textRowNode = nullptr;
817 data.badgeNumber = 1;
818 DragAnimationHelper::CreateTextNode(data);
819 EXPECT_EQ(data.textRowNode, nullptr);
820 }
821
822 /**
823 * @tc.name: GetPreviewMenuAnimationRate001
824 * @tc.desc: Test GetPreviewMenuAnimationRate
825 * @tc.type: FUNC
826 */
827 HWTEST_F(DragAnimationHelperTestNg, GetPreviewMenuAnimationRate001, TestSize.Level1)
828 {
829 auto pipelineContext = MockPipelineContext::GetCurrent();
830 auto dragDropManager = pipelineContext->GetDragDropManager();
831 auto frameNode = FrameNode::CreateFrameNode(
832 V2::MENU_WRAPPER_ETS_TAG, 1, AceType::MakeRefPtr<MenuWrapperPattern>(1));
833 dragDropManager->SetMenuWrapperNode(frameNode);
834
835 auto getsize = DragAnimationHelper::GetPreviewMenuAnimationRate();
836 EXPECT_EQ(getsize, -1);
837 }
838
839
840 /**
841 * @tc.name: MountPixelMap001
842 * @tc.desc: Test MountPixelMap
843 * @tc.type: FUNC
844 */
845 HWTEST_F(DragAnimationHelperTestNg, MountPixelMap001, TestSize.Level1)
846 {
847 /**
848 * @tc.steps: step1. Create DragEventActuator.
849 */
850 auto eventHub = AceType::MakeRefPtr<EventHub>();
851 ASSERT_NE(eventHub, nullptr);
852 auto frameNode = FrameNode::CreateFrameNode("test", 1, AceType::MakeRefPtr<Pattern>(), false);
853 ASSERT_NE(frameNode, nullptr);
854 eventHub->host_ = AceType::WeakClaim(AceType::RawPtr(frameNode));
855 auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
856 ASSERT_NE(gestureEventHub, nullptr);
857
858 /**
859 * @tc.steps: step2. Get OverlayManager.
860 */
861 auto pipelineContext = MockPipelineContext::GetCurrent();
862 ASSERT_NE(pipelineContext, nullptr);
863 auto overlayManager = pipelineContext->GetOverlayManager();
864 ASSERT_NE(overlayManager, nullptr);
865
866 /**
867 * @tc.steps: step3. Trigger MountPixelMap.
868 */
869 PreparedInfoForDrag data;
870 auto imageNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
871 ASSERT_NE(imageNode, nullptr);
872 data.imageNode = imageNode;
873 data.sizeChangeEffect = DraggingSizeChangeEffect::DEFAULT;
874 DragAnimationHelper::MountPixelMap(overlayManager, gestureEventHub, data, true);
875 EXPECT_EQ(overlayManager->hasDragPixelMap_, true);
876 }
877
878 /**
879 * @tc.name: CalcBadgeTextOffset001
880 * @tc.desc: CalcBadgeTextOffset
881 * @tc.type: FUNC
882 */
883 HWTEST_F(DragAnimationHelperTestNg, CalcBadgeTextOffset001, TestSize.Level1)
884 {
885 /**
886 * @tc.steps: step1. Create node.
887 */
888 auto imageNodeId = GetElmtId();
889 auto frameNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
890 ASSERT_NE(frameNode, nullptr);
891 auto menuPattern = AceType::MakeRefPtr<MenuPattern>(frameNode->GetId(), frameNode->GetTag(), MenuType::MENU);
892 ASSERT_NE(menuPattern, nullptr);
893 auto imageNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, imageNodeId,
__anondb911dc91402() 894 []() { return AceType::MakeRefPtr<Pattern>(); });
895 ASSERT_NE(imageNode, nullptr);
896 auto renderContext = imageNode->GetRenderContext();
897 ASSERT_NE(renderContext, nullptr);
898 renderContext->UpdatePosition(OffsetT<Dimension>(Dimension(0.0f), Dimension(0.0f)));
899
900 /**
901 * @tc.steps: step2. Execute CalcBadgeTextOffset
902 */
903 auto pipelineContext = MockPipelineContext::GetCurrent();
904 ASSERT_NE(pipelineContext, nullptr);
905 const int32_t badgeLength = 2;
906 AceApplicationInfo::GetInstance().isRightToLeft_ = true;
907 auto offset = DragAnimationHelper::CalcBadgeTextOffset(menuPattern, imageNode, pipelineContext, badgeLength);
908 AceApplicationInfo::GetInstance().isRightToLeft_ = false;
909 float expectX = -BADGE_RELATIVE_OFFSET.ConvertToPx();
910 float expectY = -BADGE_RELATIVE_OFFSET.ConvertToPx();
911 EXPECT_NEAR(offset.GetX(), expectX, EPSILON);
912 EXPECT_NEAR(offset.GetY(), expectY, EPSILON);
913 }
914
915 /**
916 * @tc.name: ShowMenuHideAnimation001
917 * @tc.desc: test ShowMenuHideAnimation func.
918 * @tc.type: FUNC
919 */
920 HWTEST_F(DragAnimationHelperTestNg, ShowMenuHideAnimation001, TestSize.Level1)
921 {
922 PreparedInfoForDrag data;
923 auto menuNode = FrameNode::CreateFrameNode(V2::COLUMN_ETS_TAG, GetElmtId(), AceType::MakeRefPtr<Pattern>());
924 ASSERT_NE(menuNode, nullptr);
925 data.menuNode = menuNode;
926 data.isMenuNotShow = true;
927 data.sizeChangeEffect = DraggingSizeChangeEffect::DEFAULT;
928 auto renderContext = menuNode->GetRenderContext();
929 ASSERT_NE(renderContext, nullptr);
930 renderContext->UpdateOpacity(1.0f);
931 DragAnimationHelper::ShowMenuHideAnimation(data);
932 auto opacity = renderContext->GetOpacity().value();
933 EXPECT_TRUE(opacity == 1.0f);
934 data.sizeChangeEffect = DraggingSizeChangeEffect::SIZE_TRANSITION;
935 DragAnimationHelper::ShowMenuHideAnimation(data);
936 auto opacity1 = renderContext->GetOpacity().value();
937 EXPECT_TRUE(opacity1 == 0.0f);
938 data.isMenuNotShow = false;
939 DragAnimationHelper::ShowMenuHideAnimation(data);
940 auto opacity2 = renderContext->GetOpacity().value();
941 EXPECT_TRUE(opacity2 == 0.0f);
942 }
943 } // namespace OHOS::Ace::NG