1 /*
2 * Copyright (c) 2022-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_drop_manager_test_ng.h"
17
18 #include "test/mock/core/render/mock_render_context.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22 namespace OHOS::Ace::NG {
SetUpTestCase()23 void DragDropManagerTestNg::SetUpTestCase()
24 {
25 MockPipelineContext::SetUp();
26 MockContainer::SetUp();
27 }
28
TearDownTestCase()29 void DragDropManagerTestNg::TearDownTestCase()
30 {
31 MockPipelineContext::TearDown();
32 MockContainer::TearDown();
33 }
34
35 /**
36 * @tc.name: DragDropManagerTest001
37 * @tc.desc: CreateAndShowItemDragOverlay via pixelMap and gestureEvent
38 * @tc.type: FUNC
39 * @tc.author:
40 */
41 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest001, TestSize.Level1)
42 {
43 /**
44 * @tc.steps: step1. construct a DragDropManager
45 */
46 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
47
48 /**
49 * @tc.steps: step2. call CreateAndShowItemDragOverlay
50 * @tc.expected: step2. return a dragDropProxy successfully
51 * DragWindow.DrawPixelMap() will be called
52 */
53 void* voidPtr = static_cast<void*>(new char[0]);
54 RefPtr<PixelMap> pixelMap = PixelMap::CreatePixelMap(voidPtr);
55 GestureEvent gestureEvent;
56 RefPtr<EventHub> eventhub;
57 auto dragDropProxy = dragDropManager->CreateAndShowItemDragOverlay(pixelMap, gestureEvent, eventhub);
58 EXPECT_TRUE(dragDropProxy);
59
60 /**
61 * @tc.steps: step3. call CreateAndShowItemDragOverlay with null.
62 * @tc.expected: step3. return dragDropProxy to false.
63 */
64 pixelMap = nullptr;
65 dragDropProxy = dragDropManager->CreateAndShowItemDragOverlay(pixelMap, gestureEvent, eventhub);
66 EXPECT_FALSE(dragDropProxy);
67 }
68
69 /**
70 * @tc.name: DragDropManagerTest002
71 * @tc.desc: CreateAndShowItemDragOverlay via customNode and gestureEvent
72 * @tc.type: FUNC
73 * @tc.author:
74 */
75 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest002, TestSize.Level1)
76 {
77 /**
78 * @tc.steps: step1. construct a DragDropManager
79 */
80 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
81
82 /**
83 * @tc.steps: step2. call CreateAndShowItemDragOverlay
84 * @tc.expected: step2. return a dragDropProxy successfully
85 * DragWindow.DrawFrameNode() will be called
86 */
87 RefPtr<UINode> customNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
88 GestureEvent gestureEvent;
89 RefPtr<EventHub> eventhub;
90 auto dragDropProxy = dragDropManager->CreateAndShowItemDragOverlay(customNode, gestureEvent, eventhub);
91 EXPECT_TRUE(dragDropProxy);
92
93 /**
94 * @tc.expected: step2. the customNode's parent is root
95 */
96 auto root = customNode->GetParent();
97 auto rootTag = root->GetTag();
98 EXPECT_EQ(rootTag, ROOT_ETS_TAG);
99 }
100
101 /**
102 * @tc.name: DragDropManagerTest003
103 * @tc.desc: Test UpdateItemDragPosition
104 * @tc.type: FUNC
105 * @tc.author:
106 */
107 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest003, TestSize.Level1)
108 {
109 /**
110 * @tc.steps: step1. construct a DragDropManager
111 */
112 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
113
114 /**
115 * @tc.steps: step2. call CreateAndShowItemDragOverlay
116 * @tc.expected: step2. return a dragDropProxy successfully
117 */
118 RefPtr<UINode> customNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
119 GestureEvent gestureEvent;
120 RefPtr<EventHub> eventhub;
121 auto dragDropProxy = dragDropManager->CreateAndShowItemDragOverlay(customNode, gestureEvent, eventhub);
122 EXPECT_TRUE(dragDropProxy);
123 auto pipelineContext = MockPipelineContext::GetCurrentContext();
124 ASSERT_NE(pipelineContext, nullptr);
125 pipelineContext->SetDisplayWindowRectInfo({ 0, 0, 800, 800 }); // 800: window width and height
126 /**
127 * @tc.steps: step3. call UpdateItemDragPosition
128 * @tc.expected: step3. itemDragOverlayNode_'s position is updated to (GLOBAL_X, GLOBAL_Y)
129 */
130 dragDropManager->UpdateItemDragPosition(GLOBAL_X, GLOBAL_Y);
131 auto renderContext = dragDropManager->itemDragOverlayNode_->GetRenderContext();
132 EXPECT_EQ(renderContext->GetPosition().value(), OffsetT<Dimension>(Dimension(GLOBAL_X), Dimension(GLOBAL_Y)));
133 }
134
135 /**
136 * @tc.name: DragDropManagerTest004
137 * @tc.desc: Test Functions relevant to Clipboard
138 Call this functions in order (AddDataToClipboard->GetExtraInfoFromClipboard->RestoreClipboardData)
139 * @tc.type: FUNC
140 * @tc.author:
141 */
142 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest004, TestSize.Level1)
143 {
144 /**
145 * @tc.steps: step1. construct a DragDropManager
146 */
147 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
148
149 /**
150 * @tc.steps: step2. call AddDataToClipboard
151 * @tc.expected: step2. ClipBoard.SetData() & ClipBoard.GetData() will be called with printing logs
152 * they're defined in "components_ng/test/mock/clipboard/mock_clipboard.cpp"
153 */
154 dragDropManager->AddDataToClipboard(EXTRA_INFO);
155
156 /**
157 * @tc.steps: step3. call GetExtraInfoFromClipboard after calling AddDataToClipboard
158 * @tc.expected: step3. get the extraInfo successfully
159 * ClipBoard.GetData() will be called with printing a log
160 * it's defined in "components_ng/test/mock/clipboard/mock_clipboard.cpp"
161 */
162 std::string extraInfo;
163 dragDropManager->GetExtraInfoFromClipboard(extraInfo);
164 EXPECT_EQ(extraInfo, EXTRA_INFO);
165
166 /**
167 * @tc.steps: step4. call GetExtraInfoFromClipboard
168 * @tc.expected: step4. extraInfo is equal to EXTRA_INFO
169 */
170 dragDropManager->clipboard_ = nullptr;
171 dragDropManager->GetExtraInfoFromClipboard(extraInfo);
172 EXPECT_EQ(extraInfo, EXTRA_INFO);
173
174 /**
175 * @tc.steps: step5. call AddDataToClipboard with empty.
176 * @tc.expected: step5. extraInfo is equal to EXTRA_INFO
177 */
178 dragDropManager->AddDataToClipboard(extraInfo);
179 EXPECT_EQ(extraInfo, EXTRA_INFO);
180 }
181
182 /**
183 * @tc.name: DragDropManagerTest005
184 * @tc.desc: Test Functions relevant to Clipboard
185 Call this functions not in order (to test boundary cases)
186 * @tc.type: FUNC
187 * @tc.author:
188 */
189 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest005, TestSize.Level1)
190 {
191 /**
192 * @tc.steps: step1. construct a DragDropManager
193 */
194 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
195
196 /**
197 * @tc.steps: step3. call AddDataToClipboard twice
198 * case: clipboard_ & addDataCallback_ has exited,
199 * @tc.expected: step3. EXTRA_INFO_2 will be added
200 */
201 dragDropManager->AddDataToClipboard(EXTRA_INFO);
202 dragDropManager->AddDataToClipboard(EXTRA_INFO_2);
203 std::string extraInfo;
204 dragDropManager->GetExtraInfoFromClipboard(extraInfo);
205 EXPECT_EQ(extraInfo, EXTRA_INFO_2);
206
207 /**
208 * @tc.steps: step4. call GetExtraInfoFromClipboard twice
209 * case: clipboard_ & addDataCallback_ has exited
210 * @tc.expected: step4. get the extraInfo successfully
211 */
212 dragDropManager->GetExtraInfoFromClipboard(extraInfo);
213 EXPECT_EQ(extraInfo, EXTRA_INFO_2);
214 }
215
216 /**
217 * @tc.name: DragDropManagerTest006
218 * @tc.desc: Test DestroyDragWindow & CheckDragDropProxy
219 * @tc.type: FUNC
220 * @tc.author:
221 */
222 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest006, TestSize.Level1)
223 {
224 /**
225 * @tc.steps: step1. construct a DragDropManager
226 */
227 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
228
229 /**
230 * @tc.steps: step2. call DestroyDragWindow without creating dragWindow
231 * @tc.expected: step2. no fatal error happens
232 */
233 dragDropManager->DestroyDragWindow();
234
235 /**
236 * @tc.steps: step3. call CheckDragDropProxy without creating dragWindow
237 * @tc.expected: step3. currentId is -1 at first
238 */
239 auto flag = dragDropManager->CheckDragDropProxy(INVALID_CURRENT_ID);
240 EXPECT_TRUE(flag);
241
242 /**
243 * @tc.steps: step4. call CreateFrameworkDragDropProxy
244 * @tc.expected: step4. return a dragDropProxy successfully
245 */
246 auto dragDropProxy = dragDropManager->CreateFrameworkDragDropProxy();
247 EXPECT_TRUE(dragDropProxy);
248
249 /**
250 * @tc.steps: step5. call CheckDragDropProxy after creating dragWindow
251 * @tc.expected: step5. currentId recover to -1
252 * MockDragWindow.Destroy() will be called
253 */
254 dragDropManager->DestroyDragWindow();
255 flag = dragDropManager->CheckDragDropProxy(INVALID_CURRENT_ID);
256 EXPECT_TRUE(flag);
257
258 /**
259 * @tc.steps: step6. call CheckDragDropProxy (use proxy to create drag window)
260 * @tc.expected: step6. currentId recover to -1
261 */
262 auto dragDropManager2 = AceType::MakeRefPtr<DragDropManager>();
263 auto dragDropProxy2 = dragDropManager2->CreateFrameworkDragDropProxy();
264 EXPECT_TRUE(dragDropProxy2);
265 dragDropManager2->DestroyDragWindow();
266 flag = dragDropManager2->CheckDragDropProxy(INVALID_CURRENT_ID);
267 EXPECT_TRUE(flag);
268 }
269
270 /**
271 * @tc.name: DragDropManagerTest007
272 * @tc.desc: Test OnDragStart & onDragCancel & OnItemDragStart & onItemDragCancel
273 * @tc.type: FUNC
274 * @tc.author:
275 */
276 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest007, TestSize.Level1)
277 {
278 /**
279 * @tc.steps: step1. construct a DragDropManager
280 */
281 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
282
283 /**
284 * @tc.steps: step2. call OnDragStart
285 * @tc.expected: step2. draggedFrameNode_ & preTargetFrameNode_ are assigned to the frameNode created previously
286 */
287 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
288 dragDropManager->OnDragStart({ GLOBAL_X, GLOBAL_Y }, frameNode);
289 auto draggedNode = dragDropManager->draggedFrameNode_;
290 auto preTargetNode = dragDropManager->preTargetFrameNode_;
291 ASSERT_TRUE(draggedNode);
292 ASSERT_TRUE(preTargetNode);
293 auto draggedNodeTag = draggedNode->GetTag();
294 auto preTargetNodeTag = preTargetNode->GetTag();
295 EXPECT_EQ(draggedNodeTag, NODE_TAG);
296 EXPECT_EQ(preTargetNodeTag, NODE_TAG);
297
298 /**
299 * @tc.steps: step3. call onDragCancel
300 * @tc.expected: step3. draggedFrameNode_ & preTargetFrameNode_ are assigned to nullptr
301 */
302 dragDropManager->onDragCancel();
303 draggedNode = dragDropManager->draggedFrameNode_;
304 preTargetNode = dragDropManager->preTargetFrameNode_;
305 EXPECT_FALSE(draggedNode);
306 EXPECT_FALSE(preTargetNode);
307
308 /**
309 * @tc.steps: step4. call OnItemDragStart
310 * @tc.expected: step4. preGridTargetNodeTag is assigned to the gridFrameNode created previously
311 */
312 auto gridFrameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<Pattern>());
313 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, gridFrameNode);
314 auto preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
315 ASSERT_TRUE(preGridTargetNode);
316 auto preGridTargetNodeTag = preGridTargetNode->GetTag();
317 EXPECT_EQ(preGridTargetNodeTag, GRID_TAG);
318
319 /**
320 * @tc.steps: step5. call onItemDragCancel
321 * @tc.expected: step5. preGridTargetFrameNode_ is assigned to nullptr
322 */
323 dragDropManager->onItemDragCancel();
324 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
325 EXPECT_FALSE(preGridTargetNode);
326 }
327
328 /**
329 * @tc.name: DragDropManagerTest009
330 * @tc.desc: Test OnItemDragMove DragType is Grid
331 * @tc.type: FUNC
332 * @tc.author:
333 */
334 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest008, TestSize.Level1)
335 {
336 /**
337 * @tc.steps: step1. construct a DragDropManager
338 */
339 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
340
341 /**
342 * @tc.steps: step2. construct a frameNode whose tag is Grid set its ItemDragEvent and GeometryNode
343 */
344 auto frameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<GridPattern>());
345 auto eventHub = frameNode->GetEventHub<GridEventHub>();
346 ASSERT_TRUE(eventHub);
347 RefPtr<UINode> customNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
348 GestureEvent gestureEvent;
349 auto dragDropProxy = dragDropManager->CreateAndShowItemDragOverlay(customNode, gestureEvent, eventHub);
350 EXPECT_TRUE(dragDropProxy);
351
352 // Set OnItemDragLeave callback
353 std::string itemInfoLeave;
__anon64a0c6390102(const ItemDragInfo& , int32_t ) 354 auto onItemDragLeave = [&itemInfoLeave](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */) {
355 itemInfoLeave = ITEM_INFO_LEAVE;
356 };
357 eventHub->SetOnItemDragLeave(std::move(onItemDragLeave));
358
359 // Set OnItemDragMove callback
360 std::string itemInfoMove;
361 auto onItemDragMove = [&itemInfoMove](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390202(const ItemDragInfo& , int32_t , int32_t ) 362 int32_t /* insertIndex */) { itemInfoMove = ITEM_INFO_MOVE; };
363 eventHub->SetOnItemDragMove(std::move(onItemDragMove));
364
365 // Set OnItemDragEnter callback
366 std::string itemInfoEnter;
__anon64a0c6390302(const ItemDragInfo& ) 367 auto onItemDragEnter = [&itemInfoEnter](const ItemDragInfo& /* dragInfo */) { itemInfoEnter = ITEM_INFO_ENTER; };
368 eventHub->SetOnItemDragEnter(std::move(onItemDragEnter));
369
370 // Set geometry node to make sure (GLOBAL_X, GLOBAL_Y) in geoNode.frameRect_
371 auto geoNode = AceType::MakeRefPtr<GeometryNode>();
372 geoNode->SetMarginFrameOffset(FRAME_OFFSET);
373 geoNode->SetFrameSize(FRAME_SIZE);
374 frameNode->SetGeometryNode(geoNode);
375
376 /**
377 * @tc.steps: step4. call OnItemDragMove
378 * case: gridDragFrameNodes_ is empty & preGridTargetFrameNode_ is not null
379 * @tc.expected: step4. frameNode's onItemDragLeave_ will be called
380 * itemInfoLeave will be assigned to ITEM_INFO_LEAVE
381 */
382 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, frameNode);
383 auto preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
384 ASSERT_TRUE(preGridTargetNode);
385 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
386 EXPECT_EQ(itemInfoLeave, ITEM_INFO_LEAVE);
387 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
388 EXPECT_FALSE(preGridTargetNode);
389
390 /**
391 * @tc.steps: step5. call AddGridDragFrameNode
392 * after that, gridDragFrameNodes_ is not empty
393 */
394 dragDropManager->AddGridDragFrameNode(frameNode->GetId(), frameNode);
395
396 /**
397 * @tc.steps: step6. call OnItemDragMove
398 * case: gridDragFrameNodes_ is not empty & preGridTargetFrameNode_ equals the frameNode
399 * @tc.expected: step6. frameNode's OnItemDragMove_ will be called
400 * itemInfoMove will be assigned to ITEM_INFO_MOVE
401 */
402 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, frameNode);
403 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
404 EXPECT_TRUE(preGridTargetNode);
405 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
406
407 /**
408 * @tc.steps: step7. call OnItemDragMove
409 * case: gridDragFrameNodes_ is not empty & preGridTargetFrameNode_ not equals the frameNode
410 * @tc.expected: step7. frameNode's onDragItemEnter_ will be called
411 * itemInfoEnter will be assigned to ITEM_INFO_ENTER
412 * preGridTargetFrameNode_'s onDragItemLeave will be called
413 * leaveExtraInfoNew will be assigned to ITEM_INFO_ENTER
414 * preGridTargetFrameNode_ is assigned to frameNode
415 */
416 auto newFrameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<GridPattern>());
417 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, newFrameNode);
418 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
419 EXPECT_TRUE(preGridTargetNode);
420 // Set newFrameNode's onDragLeave callback
421 auto eventHubNew = newFrameNode->GetEventHub<GridEventHub>();
422 ASSERT_TRUE(eventHubNew);
423 std::string itemInfoLeaveNew;
__anon64a0c6390402(const ItemDragInfo& , int32_t ) 424 auto onItemDragLeaveNew = [&itemInfoLeaveNew](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */) {
425 itemInfoLeaveNew = ITEM_INFO_ENTER;
426 };
427 eventHubNew->SetOnItemDragLeave(std::move(onItemDragLeaveNew));
428 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
429 EXPECT_EQ(itemInfoLeaveNew, ITEM_INFO_ENTER);
430 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
431 ASSERT_FALSE(preGridTargetNode);
432 }
433
434 /**
435 * @tc.name: DragDropManagerTest010
436 * @tc.desc: Test OnItemDragMove DragType is List
437 * @tc.type: FUNC
438 * @tc.author:
439 */
440 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest009, TestSize.Level1)
441 {
442 /**
443 * @tc.steps: step1. construct a DragDropManager
444 */
445 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
446
447 /**
448 * @tc.steps: step2. construct a frameNode whose tag is List set its ItemDragEvent and GeometryNode
449 */
450 auto frameNode = AceType::MakeRefPtr<FrameNode>(LIST_TAG, -1, AceType::MakeRefPtr<ListPattern>());
451 auto eventHub = frameNode->GetEventHub<ListEventHub>();
452 ASSERT_TRUE(eventHub);
453
454 // Set OnItemDragLeave callback
455 std::string itemInfoLeave;
__anon64a0c6390502(const ItemDragInfo& , int32_t ) 456 auto onItemDragLeave = [&itemInfoLeave](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */) {
457 itemInfoLeave = ITEM_INFO_LEAVE;
458 };
459 eventHub->SetOnItemDragLeave(std::move(onItemDragLeave));
460
461 // Set OnItemDragMove callback
462 std::string itemInfoMove;
463 auto onItemDragMove = [&itemInfoMove](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390602(const ItemDragInfo& , int32_t , int32_t ) 464 int32_t /* insertIndex */) { itemInfoMove = ITEM_INFO_MOVE; };
465 eventHub->SetOnItemDragMove(std::move(onItemDragMove));
466
467 // Set OnItemDragEnter callback
468 std::string itemInfoEnter;
__anon64a0c6390702(const ItemDragInfo& ) 469 auto onItemDragEnter = [&itemInfoEnter](const ItemDragInfo& /* dragInfo */) { itemInfoEnter = ITEM_INFO_ENTER; };
470 eventHub->SetOnItemDragEnter(std::move(onItemDragEnter));
471
472 // Set geometry node to make sure (GLOBAL_X, GLOBAL_Y) in geoNode.frameRect_
473 auto geoNode = AceType::MakeRefPtr<GeometryNode>();
474 geoNode->SetMarginFrameOffset(FRAME_OFFSET);
475 geoNode->SetFrameSize(FRAME_SIZE);
476 frameNode->SetGeometryNode(geoNode);
477
478 /**
479 * @tc.steps: step3. call OnItemDragMove
480 * case: listDragFrameNodes_ is empty & preGridTargetFrameNode_ is not null
481 * @tc.expected: step3. frameNode's onItemDragLeave_ will be called
482 * itemInfoLeave will be assigned to ITEM_INFO_LEAVE
483 */
484 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, frameNode);
485 auto preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
486 ASSERT_TRUE(preGridTargetNode);
487 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
488 EXPECT_EQ(itemInfoLeave, ITEM_INFO_LEAVE);
489 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
490 EXPECT_FALSE(preGridTargetNode);
491
492 /**
493 * @tc.steps: step4. call AddGridDragFrameNode
494 * after that, listDragFrameNodes_ is not empty
495 * need adding grid maybe a bug
496 */
497 dragDropManager->AddGridDragFrameNode(frameNode->GetId(), frameNode);
498
499 /**
500 * @tc.steps: step5. call OnItemDragMove
501 * case: listDragFrameNodes_ is not empty & preGridTargetFrameNode_ equals the frameNode
502 * @tc.expected: step5. a gridEventHub is trying to get by the frameNode,
503 * but it's list type, so will return early(maybe that is a bug)
504 * itemInfoMove will not be assigned DragWindow.MoveTo() will be called
505 */
506 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, frameNode);
507 preGridTargetNode = dragDropManager->preGridTargetFrameNode_;
508 EXPECT_TRUE(preGridTargetNode);
509 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
510 EXPECT_EQ(itemInfoMove, "");
511 // to force call the FireOnItemDragEvent with DragType::LIST and DragEventType::MOVE
512 OHOS::Ace::ItemDragInfo itemDragInfo;
513 dragDropManager->FireOnItemDragEvent(frameNode, DragType::LIST, itemDragInfo, DragEventType::MOVE, DRAGGED_INDEX);
514 EXPECT_EQ(itemInfoMove, ITEM_INFO_MOVE);
515
516 /**
517 * @tc.steps: step6. call OnItemDragMove
518 * case: listDragFrameNodes_ is not empty & preGridTargetFrameNode_ not equals the frameNode
519 * @tc.expected: step6. frameNode's onDragItemEnter_ will be called
520 * itemInfoEnter will be assigned to ITEM_INFO_ENTER
521 * preGridTargetFrameNode_'s onDragItemLeave will be called
522 * leaveExtraInfoNew will be assigned to ITEM_INFO_ENTER
523 * preGridTargetFrameNode_ is assigned to frameNode
524 */
525 auto newFrameNode = AceType::MakeRefPtr<FrameNode>(LIST_TAG, -1, AceType::MakeRefPtr<ListPattern>());
526 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, newFrameNode);
527 EXPECT_TRUE(dragDropManager->preGridTargetFrameNode_);
528 // Set newFrameNode's onDragLeave callback
529 auto eventHubNew = newFrameNode->GetEventHub<ListEventHub>();
530 ASSERT_TRUE(eventHubNew);
531 std::string itemInfoLeaveNew;
__anon64a0c6390802(const ItemDragInfo& , int32_t ) 532 auto onItemDragLeaveNew = [&itemInfoLeaveNew](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */) {
533 itemInfoLeaveNew = ITEM_INFO_ENTER;
534 };
535 eventHubNew->SetOnItemDragLeave(std::move(onItemDragLeaveNew));
536 EXPECT_EQ(itemInfoEnter, "");
537 EXPECT_EQ(itemInfoLeaveNew, "");
538 ASSERT_TRUE(dragDropManager->preGridTargetFrameNode_);
539 auto preGridTargetNodeTag = dragDropManager->preGridTargetFrameNode_->GetTag();
540 EXPECT_EQ(preGridTargetNodeTag, LIST_TAG);
541
542 /**
543 * @tc.steps: step7. call OnItemDragMove
544 * case: listDragFrameNodes_ is not empty & preGridTargetFrameNode_ is null
545 * @tc.expected: step7. frameNode's onDragItemEnter_ will be called
546 * itemInfoEnter will be assigned to ITEM_INFO_ENTER
547 */
548 dragDropManager->onItemDragCancel();
549 EXPECT_FALSE(dragDropManager->preGridTargetFrameNode_);
550 itemInfoEnter = "";
551 dragDropManager->OnItemDragMove(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
552 EXPECT_EQ(itemInfoEnter, "");
553 }
554
555 /**
556 * @tc.name: DragDropManagerTest011
557 * @tc.desc: Test OnItemDragEnd type is grid
558 * @tc.type: FUNC
559 * @tc.author:
560 */
561 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest011, TestSize.Level1)
562 {
563 /**
564 * @tc.steps: step1. construct a DragDropManager
565 */
566 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
567
568 /**
569 * @tc.steps: step2. construct a frameNode which type is grid and set its GeometryNode
570 */
571 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<GridPattern>());
572 auto eventHub = frameNode->GetEventHub<GridEventHub>();
573 ASSERT_TRUE(eventHub);
574
575 // Set geometry node to make sure (GLOBAL_X, GLOBAL_Y) in geoNode.frameRect_
576 auto geoNode = AceType::MakeRefPtr<GeometryNode>();
577 geoNode->SetMarginFrameOffset(FRAME_OFFSET);
578 geoNode->SetFrameSize(FRAME_SIZE);
579 frameNode->SetGeometryNode(geoNode);
580
581 /**
582 * @tc.steps: step3. call OnItemDragEnd
583 * case: gridDragFrameNodes_ is empty
584 * @tc.expected: step3. preGridTargetFrameNode_ is null
585 */
586 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
587 auto preGridTargetFrameNode = dragDropManager->preGridTargetFrameNode_;
588 EXPECT_FALSE(preGridTargetFrameNode);
589
590 // case: gridDragFrameNodes_ is empty & preGridTargetFrameNode_ is not null
591 auto preGridTargetNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<GridPattern>());
592 auto preGridEvent = preGridTargetNode->GetEventHub<GridEventHub>();
593 std::string preGridDropInfo;
594 auto onPreGridItemDrop = [&preGridDropInfo](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390902(const ItemDragInfo& , int32_t , int32_t , bool ) 595 int32_t /* insertIndex */, bool /* isSuccess */) { preGridDropInfo = EXTRA_INFO; };
596 preGridEvent->SetOnItemDrop(std::move(onPreGridItemDrop));
597 dragDropManager->preGridTargetFrameNode_ = preGridTargetNode;
598 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
599 EXPECT_EQ(preGridDropInfo, "");
600
601 /**
602 * @tc.steps: step4. call AddDragFrameNode
603 * after that, gridDragFrameNodes_ is not empty
604 */
605 dragDropManager->AddGridDragFrameNode(frameNode->GetId(), frameNode);
606
607 /**
608 * @tc.steps: step5. call OnItemDragEnd
609 * case: eventHub dose not have onDrop_
610 * @tc.expected: step5. preGridTargetFrameNode_ is null
611 */
612 dragDropManager->preGridTargetFrameNode_ = nullptr;
613 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
614 preGridTargetFrameNode = dragDropManager->preGridTargetFrameNode_;
615 EXPECT_FALSE(preGridTargetFrameNode);
616
617 /**
618 * @tc.steps: step6. call OnItemDragEnd
619 * case: eventHub dose have onDrop_
620 * @tc.expected: step6. frameNode's OnDrop_ will be called
621 * itemDropInfo will be assigned to EXTRA_INFO
622 * preGridTargetFrameNode_ be assigned to nullptr
623 */
624 std::string itemDropInfo;
625 auto onItemDrop = [&itemDropInfo](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390a02(const ItemDragInfo& , int32_t , int32_t , bool ) 626 int32_t /* insertIndex */, bool /* isSuccess */) { itemDropInfo = EXTRA_INFO; };
627 eventHub->SetOnItemDrop(onItemDrop);
628 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
629 preGridTargetFrameNode = dragDropManager->preGridTargetFrameNode_;
630 EXPECT_FALSE(preGridTargetFrameNode);
631
632 // case: preGridTargetFrameNode_ == dragFrameNode
633 dragDropManager->preGridTargetFrameNode_ = frameNode;
634 itemDropInfo.clear();
635 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
636
637 // case: preGridTargetFrameNode_ != dragFrameNode
638 dragDropManager->preGridTargetFrameNode_ = preGridTargetNode;
639 itemDropInfo.clear();
640 preGridDropInfo.clear();
641 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
642 EXPECT_EQ(preGridDropInfo, "");
643 }
644
645 /**
646 * @tc.name: DragDropManagerTest012
647 * @tc.desc: Test OnItemDragEnd type is list
648 * @tc.type: FUNC
649 * @tc.author:
650 */
651 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest012, TestSize.Level1)
652 {
653 /**
654 * @tc.steps: step1. construct a DragDropManager
655 */
656 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
657
658 /**
659 * @tc.steps: step2. construct a frameNode which type is list and set its GeometryNode
660 */
661 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<ListPattern>());
662 auto eventHub = frameNode->GetEventHub<ListEventHub>();
663 ASSERT_TRUE(eventHub);
664
665 // Set geometry node to make sure (GLOBAL_X, GLOBAL_Y) in geoNode.frameRect_
666 auto geoNode = AceType::MakeRefPtr<GeometryNode>();
667 geoNode->SetMarginFrameOffset(FRAME_OFFSET);
668 geoNode->SetFrameSize(FRAME_SIZE);
669 frameNode->SetGeometryNode(geoNode);
670
671 /**
672 * @tc.steps: step3. call OnItemDragEnd
673 * case: listDragFrameNodes_ is empty
674 * @tc.expected: step3. preGridTargetFrameNode_ is null
675 */
676 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
677 auto preGridTargetFrameNode = dragDropManager->preGridTargetFrameNode_;
678 EXPECT_FALSE(preGridTargetFrameNode);
679
680 /**
681 * @tc.steps: step4. call AddDragFrameNode
682 * after that, listDragFrameNodes_ is not empty
683 */
684 dragDropManager->AddListDragFrameNode(frameNode->GetId(), frameNode);
685
686 /**
687 * @tc.steps: step5. call OnItemDragEnd
688 * @tc.expected: step5. frameNode's OnDrop_ will be called
689 * itemDropInfo will be assigned to EXTRA_INFO
690 * preGridTargetFrameNode_ be assigned to nullptr
691 */
692 std::string itemDropInfo;
693 auto onItemDrop = [&itemDropInfo](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390b02(const ItemDragInfo& , int32_t , int32_t , bool ) 694 int32_t /* insertIndex */, bool /* isSuccess */) { itemDropInfo = EXTRA_INFO; };
695 eventHub->SetOnItemDrop(onItemDrop);
696 dragDropManager->preGridTargetFrameNode_ = frameNode;
697 EXPECT_TRUE(dragDropManager->preGridTargetFrameNode_);
698 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
699 preGridTargetFrameNode = dragDropManager->preGridTargetFrameNode_;
700 EXPECT_FALSE(preGridTargetFrameNode);
701 }
702
703 /**
704 * @tc.name: DragDropManagerTest013
705 * @tc.desc: Test corner case
706 * @tc.type: FUNC
707 * @tc.author:
708 */
709 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest013, TestSize.Level1)
710 {
711 /**
712 * @tc.steps: step1. construct a DragDropManager
713 */
714 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
715
716 /**
717 * @tc.steps: step2. call FindDragFrameNodeByPosition with frameNodes contains nullptr
718 * @tc.expected: step2.
719 */
720 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
721 auto frameNodeNull = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<Pattern>());
722 dragDropManager->AddDragFrameNode(frameNodeNull->GetId(), frameNodeNull);
723 frameNodeNull.Reset();
724 auto frameNodeGeoNullId = ElementRegister::GetInstance()->MakeUniqueId();
725 auto frameNodeGeoNull =
726 AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeGeoNullId, AceType::MakeRefPtr<Pattern>());
727 frameNodeGeoNull->SetGeometryNode(nullptr);
728 dragDropManager->AddDragFrameNode(frameNodeGeoNull->GetId(), frameNodeGeoNull);
729 EXPECT_EQ(static_cast<int32_t>(dragDropManager->dragFrameNodes_.size()), 2);
730 auto targetFrameNode = dragDropManager->FindDragFrameNodeByPosition(GLOBAL_X, GLOBAL_Y);
731 EXPECT_FALSE(targetFrameNode);
732
733 /**
734 * @tc.steps: step3. call FireOnDragEvent with type=DragEventType::DROP
735 * @tc.expected: step3. FireOnDrop will be called
736 */
737 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
738 auto eventHub = frameNode->GetEventHub<EventHub>();
739 std::string onDropInfo;
__anon64a0c6390c02(const RefPtr<OHOS::Ace::DragEvent>& , const std::string& ) 740 auto onDrop = [&onDropInfo](const RefPtr<OHOS::Ace::DragEvent>& /* dragEvent */, const std::string& /* info */) {
741 onDropInfo = EXTRA_INFO;
742 };
743 eventHub->SetOnDrop(std::move(onDrop));
744 DragPointerEvent point;
745 TouchEvent event;
746 event.x = 1.0f;
747 event.y = 2.0f;
748 dragDropManager->velocityTracker_.UpdateTouchPoint(event, false);
749 dragDropManager->FireOnDragEvent(frameNode, point, DragEventType::DROP, EXTRA_INFO);
750 EXPECT_EQ(onDropInfo, EXTRA_INFO);
751
752 /**
753 * @tc.steps: step4. call FireOnItemDropEvent with type=DragEventType::DROP
754 * @tc.expected: step4. FireOnItemDrop will be called
755 */
756 ItemDragInfo itemDragInfo;
757 auto gridNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<GridPattern>());
758 auto gridEvent = gridNode->GetEventHub<GridEventHub>();
759 std::string onItemDropInfo;
760 ItemDropFunc onItemDrop = [&onItemDropInfo](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6390d02(const ItemDragInfo& , int32_t , int32_t , bool ) 761 int32_t /* insertIndex */, bool /* isSuccess */) { onItemDropInfo = EXTRA_INFO; };
762 gridEvent->SetOnItemDrop(std::move(onItemDrop));
763 dragDropManager->FireOnItemDropEvent(gridNode, DragType::GRID, itemDragInfo, 0, 0, true);
764 EXPECT_EQ(onItemDropInfo, "");
765
766 auto listNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<ListPattern>());
767 auto listEvent = listNode->GetEventHub<ListEventHub>();
768 std::string onItemDropInfoList;
769 ItemDropFunc onItemDropList = [&onItemDropInfoList](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
770 int32_t /* insertIndex */,
__anon64a0c6390e02(const ItemDragInfo& , int32_t , int32_t , bool ) 771 bool /* isSuccess */) { onItemDropInfoList = EXTRA_INFO; };
772 listEvent->SetOnItemDrop(std::move(onItemDropList));
773 dragDropManager->FireOnItemDropEvent(listNode, DragType::LIST, itemDragInfo, 0, 0, true);
774 EXPECT_EQ(onItemDropInfoList, EXTRA_INFO);
775 }
776
777 /**
778 * @tc.name: DragDropManagerTest014
779 * @tc.desc: Test OnTextDragEnd
780 * @tc.type: FUNC
781 * @tc.author:
782 */
783 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest014, TestSize.Level1)
784 {
785 /**
786 * @tc.steps: step1. Create DragDropManager.
787 * @tc.expected: dragDropManager is not null.
788 */
789 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
790 ASSERT_NE(dragDropManager, nullptr);
791
792 /**
793 * @tc.steps: step2. call OnTextDragEnd.
794 * @tc.expected: dragDropManager->currentId_ equals -1.
795 */
796 float globalX = 20.0f;
797 float globalY = 20.0f;
798 std::string extraInfo;
799 dragDropManager->OnTextDragEnd(globalX, globalY, extraInfo);
800 EXPECT_EQ(dragDropManager->currentId_, -1);
801
802 /**
803 * @tc.steps: step3. construct TextFrameNode.
804 * @tc.expected: frameNode is not null.
805 */
806 auto frameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<TextPattern>());
807 ASSERT_NE(frameNode, nullptr);
808
809 /**
810 * @tc.steps: step4. updat edragDropManager->textFieldDragFrameNodes_ .
811 */
812 dragDropManager->textFieldDragFrameNodes_.insert(std::make_pair(100, WeakPtr<NG::FrameNode>(frameNode)));
813
814 /**
815 * @tc.steps: step5. call OnTextDragEnd.
816 * @tc.expected: dragDropManager->currentId_ equals -1.
817 */
818 dragDropManager->OnTextDragEnd(globalX, globalY, extraInfo);
819 auto dragFrameNode = dragDropManager->FindDragFrameNodeByPosition(globalX, globalY);
820 EXPECT_EQ(dragDropManager->currentId_, -1);
821 }
822
823 /**
824 * @tc.name: DragDropManagerTest015
825 * @tc.desc: Test RestoreClipboardData
826 * @tc.type: FUNC
827 * @tc.author:
828 */
829 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest015, TestSize.Level1)
830 {
831 /**
832 * @tc.steps: step1. Create DragDropManager.
833 * @tc.expected: dragDropManager is not null.
834 */
835 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
836 ASSERT_NE(dragDropManager, nullptr);
837
838 /**
839 * @tc.steps: step2. call CancelItemDrag.
840 * @tc.expected: dragDropManager->deleteDataCallback_ is not null.
841 */
842 dragDropManager->RestoreClipboardData();
843 ASSERT_NE(dragDropManager->deleteDataCallback_, nullptr);
844
845 /**
846 * @tc.steps: step3. call CancelItemDrag(clipboard_ is null branch).
847 * @tc.expected: step3. dragDropManager->deleteDataCallback_ is not null.
848 */
849 dragDropManager->clipboard_ = nullptr;
850 dragDropManager->RestoreClipboardData();
851 ASSERT_NE(dragDropManager->deleteDataCallback_, nullptr);
852
853 /**
854 * @tc.steps: step4. call CancelItemDrag(deleteDataCallback_ is not null branch).
855 * @tc.expected: step4. dragDropManager->deleteDataCallback_ is not null.
856 */
__anon64a0c6390f02(const std::string& data) 857 auto callback = [weakManager = AceType::WeakClaim(AceType::RawPtr(dragDropManager))](const std::string& data) {
858 auto dragDropManagerPtr = weakManager.Upgrade();
859 ASSERT_NE(dragDropManagerPtr, nullptr);
860 auto json = JsonUtil::ParseJsonString(data);
861 if (json->Contains("preData")) {
862 dragDropManagerPtr->clipboard_->SetData(json->GetString("preData"));
863 }
864 };
865 dragDropManager->deleteDataCallback_ = callback;
866 dragDropManager->RestoreClipboardData();
867 ASSERT_NE(dragDropManager->deleteDataCallback_, nullptr);
868 }
869
870 /**
871 * @tc.name: DragDropManagerTest017
872 * @tc.desc: Test UpdateDragEvent
873 * @tc.type: FUNC
874 * @tc.author:
875 */
876 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest017, TestSize.Level1)
877 {
878 /**
879 * @tc.steps: step1. Create DragDropManager.
880 * @tc.expected: dragDropManager is not null.
881 */
882 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
883 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
884 dragDropManager->OnDragStart({ GLOBAL_X, GLOBAL_Y }, frameNode);
885 ASSERT_NE(dragDropManager, nullptr);
886
887 /**
888 * @tc.steps: step2. construct a OHOS::Ace::DragEvent.
889 * @tc.expected: event is not null.
890 */
891 auto event = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
892 ASSERT_NE(event, nullptr);
893
894 /**
895 * @tc.steps: step3. call UpdateDragEvent.
896 * @tc.expected: pipeline is not null.
897 */
898 dragDropManager->UpdateDragEvent(event, DragPointerEvent(1, 1));
899 auto pipeline = PipelineContext::GetCurrentContext();
900 ASSERT_NE(pipeline, nullptr);
901 }
902
903 /**
904 * @tc.name: DragDropManagerTest021
905 * @tc.desc: Test CreateTextDragDropProxy
906 * @tc.type: FUNC
907 * @tc.author:
908 */
909 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest021, TestSize.Level1)
910 {
911 /**
912 * @tc.steps: step1. construct a DragDropManager
913 * @tc.expected: dragDropManager is not null.
914 */
915 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
916 ASSERT_NE(dragDropManager, nullptr);
917
918 /**
919 * @tc.steps: step2. Call CreateTextDragDropProxy.
920 * @tc.expected: CreateTextDragDropProxy the returns true value is not null.
921 */
922 auto textDragDropProxy = dragDropManager->CreateTextDragDropProxy();
923 ASSERT_NE(textDragDropProxy, nullptr);
924 }
925
926 /**
927 * @tc.name: DragDropManagerTest022
928 * @tc.desc: Test FindDragFrameNodeByPosition in default branches
929 * @tc.type: FUNC
930 * @tc.author:
931 */
932 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest022, TestSize.Level1)
933 {
934 /**
935 * @tc.steps: step1. construct a DragDropManager
936 */
937 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
938
939 /**
940 * @tc.steps: step2. call FindDragFrameNodeByPosition with frameNodes contains nullptr
941 * @tc.expected: step2.
942 */
943 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
944 auto frameNodeNull = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<Pattern>());
945 dragDropManager->AddDragFrameNode(frameNodeNull->GetId(), frameNodeNull);
946 frameNodeNull.Reset();
947 auto frameNodeGeoNullId = ElementRegister::GetInstance()->MakeUniqueId();
948 auto frameNodeGeoNull =
949 AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeGeoNullId, AceType::MakeRefPtr<Pattern>());
950 frameNodeGeoNull->SetGeometryNode(nullptr);
951 dragDropManager->AddDragFrameNode(frameNodeGeoNull->GetId(), frameNodeGeoNull);
952 EXPECT_EQ(static_cast<int32_t>(dragDropManager->dragFrameNodes_.size()), 2);
953 auto targetFrameNode = dragDropManager->FindDragFrameNodeByPosition(GLOBAL_X, GLOBAL_Y);
954 EXPECT_FALSE(targetFrameNode);
955 }
956
957 /**
958 * @tc.name: DragDropManagerTest023
959 * @tc.desc: Test OnDragStart(Point)
960 * @tc.type: FUNC
961 * @tc.author:
962 */
963 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest023, TestSize.Level1)
964 {
965 /**
966 * @tc.steps: step1. construct a DragDropManager
967 * @tc.expected: dragDropManager is not null.
968 */
969 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
970 ASSERT_NE(dragDropManager, nullptr);
971
972 /**
973 * @tc.steps: step2. Call OnDragStart with Point.
974 * @tc.expected: dragDropManager->dragDropState_ is equal to DragDropMgrState::DRAGGING.
975 */
976 dragDropManager->OnDragStart(Point(15.0f, 15.0f));
977 EXPECT_EQ(dragDropManager->dragDropState_, DragDropMgrState::DRAGGING);
978
979 /**
980 * @tc.steps: step3. Call OnDragStart with Point and FrameNode.
981 * @tc.expected: dragDropManager->dragDropState_ is equal to DragDropMgrState::DRAGGING.
982 */
983 RefPtr<FrameNode> frameNode = nullptr;
984 dragDropManager->OnDragStart(Point(15.0f, 15.0f), frameNode);
985 EXPECT_EQ(dragDropManager->dragDropState_, DragDropMgrState::DRAGGING);
986 }
987
988 /**
989 * @tc.name: DragDropManagerTest024
990 * @tc.desc: Test ClearVelocityInfo
991 * @tc.type: FUNC
992 * @tc.author:
993 */
994 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest024, TestSize.Level1)
995 {
996 /**
997 * @tc.steps: step1. construct a DragDropManager
998 * @tc.expected: dragDropManager is not null.
999 */
1000 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1001 ASSERT_NE(dragDropManager, nullptr);
1002
1003 /**
1004 * @tc.steps: step2. Call ClearVelocityInfo.
1005 * @tc.expected: dragDropManager->velocityTracker_.isFirstPoint_ is equal to true.
1006 */
1007 dragDropManager->UpdateVelocityTrackerPoint(Point(15.0f, 15.0f), true);
1008 dragDropManager->ClearVelocityInfo();
1009 EXPECT_EQ(dragDropManager->velocityTracker_.isFirstPoint_, true);
1010 }
1011
1012 /**
1013 * @tc.name: DragDropManagerTest025
1014 * @tc.desc: Test OnItemDragEnd out of eventHub is not an empty branche
1015 * @tc.type: FUNC
1016 * @tc.author:
1017 */
1018 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest025, TestSize.Level1)
1019 {
1020 /**
1021 * @tc.steps: step1. construct a DragDropManager.
1022 * @tc.expected: dragDropManager is not null.
1023 */
1024 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1025 ASSERT_NE(dragDropManager, nullptr);
1026
1027 /**
1028 * @tc.steps: step2. construct a GridFrameNode.
1029 * @tc.expected: frameNode is not null.
1030 */
1031 auto gridFrameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<GridPattern>());
1032 ASSERT_TRUE(gridFrameNode);
1033
1034 /**
1035 * @tc.steps: step3. Call OnItemDragEnd with DRAG_TYPE_GRID.
1036 * @tc.expected: dragDropManager->draggedFrameNode_ is false.
1037 */
1038 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, gridFrameNode);
1039 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_GRID);
1040 EXPECT_FALSE(dragDropManager->draggedFrameNode_);
1041
1042 /**
1043 * @tc.steps: step4. construct a ListFrameNode.
1044 * @tc.expected: frameNode is not null.
1045 */
1046 auto listFrameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<ListPattern>());
1047 ASSERT_TRUE(listFrameNode);
1048
1049 /**
1050 * @tc.steps: step5. Call OnItemDragEnd with DRAG_TYPE_LIST.
1051 * @tc.expected: dragDropManager->draggedFrameNode_ is false.
1052 */
1053 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, listFrameNode);
1054 dragDropManager->OnItemDragEnd(GLOBAL_X, GLOBAL_Y, DRAGGED_INDEX, DRAG_TYPE_LIST);
1055 EXPECT_FALSE(dragDropManager->draggedFrameNode_);
1056 }
1057
1058 /**
1059 * @tc.name: DragDropManagerTest026
1060 * @tc.desc: Test GetExtraInfo
1061 * @tc.type: FUNC
1062 * @tc.author:
1063 */
1064 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest026, TestSize.Level1)
1065 {
1066 /**
1067 * @tc.steps: step1. construct a DragDropManager
1068 * @tc.expected: dragDropManager is not null.
1069 */
1070 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1071 ASSERT_NE(dragDropManager, nullptr);
1072
1073 /**
1074 * @tc.steps: step2. Call GetExtraInfo.
1075 * @tc.expected: extraInfo is empty.
1076 */
1077 auto extraInfo = dragDropManager->GetExtraInfo();
1078 EXPECT_TRUE(extraInfo.empty());
1079 }
1080
1081 /**
1082 * @tc.name: DragDropManagerTest027
1083 * @tc.desc: Test SetExtraInfo
1084 * @tc.type: FUNC
1085 * @tc.author:
1086 */
1087 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest027, TestSize.Level1)
1088 {
1089 /**
1090 * @tc.steps: step1. construct a DragDropManager
1091 * @tc.expected: dragDropManager is not null.
1092 */
1093 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1094 ASSERT_NE(dragDropManager, nullptr);
1095
1096 /**
1097 * @tc.steps: step2. Call SetExtraInfo.
1098 * @tc.expected: dragDropManager->extraInfo_ is equal to "extraInfo".
1099 */
1100 dragDropManager->SetExtraInfo("extraInfo");
1101 EXPECT_EQ(dragDropManager->extraInfo_, "extraInfo");
1102 }
1103
1104 /**
1105 * @tc.name: DragDropManagerTest028
1106 * @tc.desc: Test ClearExtraInfo
1107 * @tc.type: FUNC
1108 * @tc.author:
1109 */
1110 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest028, TestSize.Level1)
1111 {
1112 /**
1113 * @tc.steps: step1. construct a DragDropManager
1114 * @tc.expected: dragDropManager is not null.
1115 */
1116 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1117 ASSERT_NE(dragDropManager, nullptr);
1118
1119 /**
1120 * @tc.steps: step2. Call ClearExtraInfo.
1121 * @tc.expected: dragDropManager->extraInfo_ is empty.
1122 */
1123 dragDropManager->SetExtraInfo("extraInfo");
1124 dragDropManager->ClearExtraInfo();
1125 EXPECT_TRUE(dragDropManager->extraInfo_.empty());
1126 }
1127
1128 /**
1129 * @tc.name: DragDropManagerTest029
1130 * @tc.desc: Test CancelItemDrag in draggedGridFrameNode branches
1131 * @tc.type: FUNC
1132 * @tc.author:
1133 */
1134 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest029, TestSize.Level1)
1135 {
1136 /**
1137 * @tc.steps: step1. Create DragDropManager.
1138 * @tc.expected: dragDropManager is not null.
1139 */
1140 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1141 ASSERT_NE(dragDropManager, nullptr);
1142
1143 /**
1144 * @tc.steps: step2. call CancelItemDrag.
1145 * @tc.expected: dragDropManager->draggedGridFrameNode_ is false.
1146 */
1147 dragDropManager->CancelItemDrag();
1148 EXPECT_FALSE(dragDropManager->draggedGridFrameNode_);
1149
1150 /**
1151 * @tc.steps: step3. Create FrameNode.
1152 * @tc.expected: frameNode is not null.
1153 */
1154 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
1155 ASSERT_NE(frameNode, nullptr);
1156
1157 /**
1158 * @tc.steps: step4. call CancelItemDrag.
1159 * @tc.expected: dragDropManager->draggedGridFrameNode_ is true.
1160 */
1161 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, frameNode);
1162 dragDropManager->CancelItemDrag();
1163 EXPECT_TRUE(dragDropManager->draggedGridFrameNode_);
1164 }
1165
1166 /**
1167 * @tc.name: DragDropManagerTest0291
1168 * @tc.desc: Test CancelItemDrag in listEventHub branches
1169 * @tc.type: FUNC
1170 * @tc.author:
1171 */
1172 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest0291, TestSize.Level1)
1173 {
1174 /**
1175 * @tc.steps: step1. Create DragDropManager.
1176 * @tc.expected: dragDropManager is not null.
1177 */
1178 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1179 ASSERT_NE(dragDropManager, nullptr);
1180
1181 /**
1182 * @tc.steps: step2. Create FrameNode.
1183 * @tc.expected: listFrameNode is not null.
1184 */
1185 auto listFrameNode = AceType::MakeRefPtr<FrameNode>(LIST_TAG, -1, AceType::MakeRefPtr<ListPattern>());
1186 ASSERT_NE(listFrameNode, nullptr);
1187
1188 /**
1189 * @tc.steps: step3. update draggedGridFrameNode_ with listFrameNode.
1190 */
1191 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, listFrameNode);
1192
1193 /**
1194 * @tc.steps: step4. call CancelItemDrag.
1195 * * @tc.expected: listEventHub is TRUE.
1196 */
1197 dragDropManager->CancelItemDrag();
1198 auto listEventHub = dragDropManager->draggedGridFrameNode_->GetEventHub<ListEventHub>();
1199 EXPECT_TRUE(listEventHub);
1200 }
1201
1202 /**
1203 * @tc.name: DragDropManagerTest0292
1204 * @tc.desc: Test CancelItemDrag in gridEventHub branches
1205 * @tc.type: FUNC
1206 * @tc.author:
1207 */
1208 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest0292, TestSize.Level1)
1209 {
1210 /**
1211 * @tc.steps: step1. Create DragDropManager.
1212 * @tc.expected: dragDropManager is not null.
1213 */
1214 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1215 ASSERT_NE(dragDropManager, nullptr);
1216
1217 /**
1218 * @tc.steps: step2. Create FrameNode.
1219 * @tc.expected: gridFrameNode is not null.
1220 */
1221 auto gridFrameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<GridPattern>());
1222 ASSERT_NE(gridFrameNode, nullptr);
1223
1224 /**
1225 * @tc.steps: step3. update draggedGridFrameNode_ with gridFrameNode.
1226 */
1227 dragDropManager->OnItemDragStart(GLOBAL_X, GLOBAL_Y, gridFrameNode);
1228
1229 /**
1230 * @tc.steps: step4. call CancelItemDrag.
1231 * * @tc.expected: gridEventHub is TRUE.
1232 */
1233 dragDropManager->CancelItemDrag();
1234 auto gridEventHub = dragDropManager->draggedGridFrameNode_->GetEventHub<GridEventHub>();
1235 EXPECT_TRUE(gridEventHub);
1236 }
1237
1238 /**
1239 * @tc.name: DragDropManagerTest030
1240 * @tc.desc: Test FireOnItemDragEvent in DragType::GRID branches
1241 * @tc.type: FUNC
1242 * @tc.author:
1243 */
1244 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest030, TestSize.Level1)
1245 {
1246 /**
1247 * @tc.steps: step1. Create DragDropManager.
1248 * @tc.expected: dragDropManager is not null.
1249 */
1250 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1251 ASSERT_NE(dragDropManager, nullptr);
1252
1253 /**
1254 * @tc.steps: step2. Create FrameNode.
1255 * @tc.expected: gridFrameNode is not null.
1256 */
1257 auto gridFrameNode = AceType::MakeRefPtr<FrameNode>(GRID_TAG, -1, AceType::MakeRefPtr<GridPattern>());
1258 ASSERT_NE(gridFrameNode, nullptr);
1259
1260 /**
1261 * @tc.steps: step3. call FireOnItemDragEvent with DragEventType::ENTER.
1262 * * @tc.expected: eventHub is TRUE.
1263 */
1264 OHOS::Ace::ItemDragInfo itemDragInfo;
1265 dragDropManager->FireOnItemDragEvent(gridFrameNode, DragType::GRID, itemDragInfo, DragEventType::ENTER, 0, 0);
1266 auto eventHub = gridFrameNode->GetEventHub<GridEventHub>();
1267 EXPECT_TRUE(eventHub);
1268
1269 /**
1270 * @tc.steps: step4. call FireOnItemDragEvent with DragEventType::MOVE.
1271 * * @tc.expected: eventHub is TRUE.
1272 */
1273 dragDropManager->FireOnItemDragEvent(gridFrameNode, DragType::GRID, itemDragInfo, DragEventType::MOVE, 0, 0);
1274 EXPECT_TRUE(eventHub);
1275
1276 /**
1277 * @tc.steps: step5. call FireOnItemDragEvent with DragEventType::default.
1278 * * @tc.expected: eventHub is TRUE.
1279 */
1280 dragDropManager->FireOnItemDragEvent(gridFrameNode, DragType::GRID, itemDragInfo, DragEventType(10), 0, 0);
1281 EXPECT_TRUE(eventHub);
1282 }
1283
1284 /**
1285 * @tc.name: DragDropManagerTest031
1286 * @tc.desc: Test FindTargetInChildNodes is an empty branch of parentNode
1287 * @tc.type: FUNC
1288 * @tc.author:
1289 */
1290 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest031, TestSize.Level1)
1291 {
1292 /**
1293 * @tc.steps: step1. Create DragDropManager.
1294 * @tc.expected: dragDropManager is not null.
1295 */
1296 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1297 ASSERT_NE(dragDropManager, nullptr);
1298
1299 /**
1300 * @tc.steps: step2. call FindTargetInChildNodes(parentNode is empty).
1301 * @tc.expected: childFindResult is false.
1302 */
1303 RefPtr<UINode> customNode = nullptr;
1304 std::vector<RefPtr<FrameNode>> hitFrameNodes;
1305 auto childFindResult = dragDropManager->FindTargetInChildNodes(customNode, hitFrameNodes, true);
1306 EXPECT_FALSE(childFindResult);
1307 }
1308
1309 /**
1310 * @tc.name: DragDropManagerTest033
1311 * @tc.desc: Test FindTargetInChildNodes
1312 * @tc.type: FUNC
1313 * @tc.author:
1314 */
1315 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest033, TestSize.Level1)
1316 {
1317 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1318 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1319 auto frameNodeNull = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<GridPattern>());
1320 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
1321 geometryNode->SetFrameSize(FRAME_SIZE);
1322 frameNodeNull->SetGeometryNode(geometryNode);
1323 auto pipelineTmp = NG::PipelineContext::GetCurrentContext();
1324 auto parentNodeTmp = pipelineTmp->GetRootElement();
1325 auto parentFrameNodeTmp = AceType::DynamicCast<FrameNode>(parentNodeTmp);
1326 parentFrameNodeTmp->frameChildren_.insert(WeakPtr<NG::FrameNode>(frameNodeNull));
1327 dragDropManager->AddGridDragFrameNode(frameNodeNull->GetId(), frameNodeNull);
1328 std::map<int32_t, WeakPtr<FrameNode>> frameNodes = dragDropManager->gridDragFrameNodes_;
1329 PointF point(GLOBAL_X, GLOBAL_Y);
1330 std::vector<RefPtr<FrameNode>> hitFrameNodes;
1331 for (auto iterOfFrameNode = frameNodes.begin(); iterOfFrameNode != frameNodes.end(); iterOfFrameNode++) {
1332 auto frameNode = iterOfFrameNode->second.Upgrade();
1333 auto geometryNode = frameNode->GetGeometryNode();
1334 if (!geometryNode) {
1335 continue;
1336 }
1337 auto globalFrameRect = geometryNode->GetFrameRect();
1338 globalFrameRect.SetOffset(frameNode->GetTransformRelativeOffset());
1339 if (globalFrameRect.IsInRegion(point)) {
1340 hitFrameNodes.push_back(frameNode);
1341 }
1342 }
1343 auto pipeline = NG::PipelineContext::GetCurrentContext();
1344 auto manager = pipeline->GetOverlayManager();
1345 auto parentNode = pipeline->GetRootElement();
1346 auto parentFrameNode = AceType::DynamicCast<FrameNode>(parentNode);
1347 auto children = parentFrameNode->GetFrameChildren();
1348 EXPECT_FALSE(children.empty());
1349 for (auto iter = children.rbegin(); iter != children.rend(); iter++) {
1350 auto child = iter->Upgrade();
1351 if (child == nullptr) {
1352 continue;
1353 }
1354 auto childNode = AceType::DynamicCast<UINode>(child);
1355 auto childFindResult = dragDropManager->FindTargetInChildNodes(childNode, hitFrameNodes, true);
1356 EXPECT_FALSE(childFindResult);
1357 }
1358 for (auto iter : hitFrameNodes) {
1359 EXPECT_NE(parentFrameNode, iter);
1360 }
1361 auto result = dragDropManager->FindTargetInChildNodes(parentNode, hitFrameNodes, true);
1362 EXPECT_FALSE(result);
1363 }
1364
1365 /**
1366 * @tc.name: DragDropManagerTest034
1367 * @tc.desc: Test PrintDragFrameNode.
1368 * @tc.type: FUNC
1369 * @tc.author:
1370 */
1371 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest034, TestSize.Level1)
1372 {
1373 /**
1374 * @tc.steps: step1. construct a DragDropManager
1375 * @tc.expected: dragDropManager is not null.
1376 */
1377 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1378 ASSERT_NE(dragDropManager, nullptr);
1379
1380 OHOS::Ace::DragPointerEvent point;
1381 RefPtr<FrameNode> dragFrameNode = nullptr;
1382 dragDropManager->OnDragStart({ GLOBAL_X, GLOBAL_Y }, dragFrameNode);
1383
1384 /**
1385 * @tc.steps: step2. call PrintDragFrameNode with dragFrameNode and point.
1386 * * @tc.expected: dragDropManager->draggedFrameNode_ is false.
1387 */
1388 dragDropManager->PrintDragFrameNode(point, dragFrameNode);
1389 ASSERT_FALSE(dragDropManager->draggedFrameNode_);
1390 }
1391
1392 /**
1393 * @tc.name: DragDropManagerTest035
1394 * @tc.desc: Test FireOnItemDropEvent is empty branchs.
1395 * @tc.type: FUNC
1396 * @tc.author:
1397 */
1398 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest035, TestSize.Level1)
1399 {
1400 /**
1401 * @tc.steps: step1. construct a DragDropManager.
1402 * @tc.expected: dragDropManager is not null.
1403 */
1404 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1405 ASSERT_NE(dragDropManager, nullptr);
1406
1407 /**
1408 * @tc.steps: step1. construct a DragDropManager.
1409 * @tc.expected: step2. FireOnItemDropEvent.
1410 */
1411 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
1412 auto draggedNode = dragDropManager->draggedFrameNode_;
1413 auto preTargetNode = dragDropManager->preTargetFrameNode_;
1414 OHOS::Ace::ItemDragInfo itemDragInfo;
1415
1416 /**
1417 * @tc.steps: step2. call OnDragStart with DragType::GRID and frameNode.
1418 * @tc.expected: step2. draggedNode is false.
1419 */
1420 dragDropManager->FireOnItemDropEvent(frameNode, DragType::GRID, itemDragInfo, 0, 0, true);
1421 ASSERT_FALSE(draggedNode);
1422
1423 /**
1424 * @tc.steps: step2. call OnDragStart with DragType::LIST and frameNode.
1425 * @tc.expected: step2. raggedNode is false.
1426 */
1427 dragDropManager->FireOnItemDropEvent(frameNode, DragType::LIST, itemDragInfo, 0, 0, true);
1428 ASSERT_FALSE(draggedNode);
1429 }
1430
1431 /**
1432 * @tc.name: DragDropManagerTest037
1433 * @tc.desc: Test GetItemIndex out of eventHub->CheckPostionInGrid is a true branch.
1434 * @tc.type: FUNC
1435 * @tc.author:
1436 */
1437 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest037, TestSize.Level1)
1438 {
1439 /**
1440 * @tc.steps: step1. construct a DragDropManager.
1441 * @tc.expected: dragDropManager is not null.
1442 */
1443 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1444 ASSERT_NE(dragDropManager, nullptr);
1445
1446 /**
1447 * @tc.steps: step2. construct a GeometryNode,updates MarginFrameOffset and FrameSize.
1448 * @tc.expected: geometryNode is not null.
1449 */
1450 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
1451 ASSERT_NE(geometryNode, nullptr);
1452 geometryNode->SetMarginFrameOffset(FRAME_OFFSET);
1453 geometryNode->SetFrameSize(FRAME_SIZE);
1454
1455 /**
1456 * @tc.steps: step3. construct a FrameNode with GeometryNode.
1457 * @tc.expected: gridNode is not null.
1458 */
1459 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1460 auto gridNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<GridPattern>());
1461 ASSERT_NE(dragDropManager, nullptr);
1462 gridNode->SetGeometryNode(geometryNode);
1463
1464 /**
1465 * @tc.steps: step4. construct a GridEventHub and updates OnItemDrop and AttachHost.
1466 */
1467 ItemDragInfo itemDragInfo;
1468 auto gridEvent = gridNode->GetEventHub<GridEventHub>();
1469 std::string onItemDropInfo;
1470 ItemDropFunc onItemDrop = [&onItemDropInfo](const ItemDragInfo& /* dragInfo */, int32_t /* itemIndex */,
__anon64a0c6391002(const ItemDragInfo& , int32_t , int32_t , bool ) 1471 int32_t /* insertIndex */, bool /* isSuccess */) { onItemDropInfo = EXTRA_INFO; };
1472 gridEvent->SetOnItemDrop(std::move(onItemDrop));
1473 gridEvent->AttachHost(WeakPtr<NG::FrameNode>(gridNode));
1474
1475 /**
1476 * @tc.steps: step5. call GetItemIndex with DragEventType::GRID and gridNode.
1477 * @tc.expected: retFlag is true.
1478 */
1479 dragDropManager->draggedGridFrameNode_ = gridNode;
1480 dragDropManager->GetItemIndex(gridNode, DragType::GRID, 0, 0);
1481 auto mockRenderContext = AceType::MakeRefPtr<MockRenderContext>();
1482 ASSERT_NE(mockRenderContext, nullptr);
1483 gridNode->renderContext_ = mockRenderContext;
1484 mockRenderContext->rect_ = { 0.0f, 0.0f, 1.0f, 1.0f };
1485 bool retFlag = gridEvent->CheckPostionInGrid(0, 0);
1486 EXPECT_TRUE(retFlag);
1487 }
1488
1489 /**
1490 * @tc.name: DragDropManagerTest038
1491 * @tc.desc: Test GetItemIndex out of itemFrameNode is a true branch.
1492 * @tc.type: FUNC
1493 * @tc.author:
1494 */
1495 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest038, TestSize.Level1)
1496 {
1497 /**
1498 * @tc.steps: step1. construct a DragDropManager.
1499 * @tc.expected: dragDropManager is not null.
1500 */
1501 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1502 ASSERT_NE(dragDropManager, nullptr);
1503
1504 /**
1505 * @tc.steps: step2. construct a GeometryNode,updates MarginFrameOffset and FrameSize.
1506 * @tc.expected: geometryNode is not null.
1507 */
1508 auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
1509 ASSERT_NE(geometryNode, nullptr);
1510 geometryNode->SetMarginFrameOffset(FRAME_OFFSET);
1511 geometryNode->SetFrameSize(FRAME_SIZE);
1512
1513 /**
1514 * @tc.steps: step3. construct a FrameNode with GeometryNode.
1515 * @tc.expected: gridNode is not null.
1516 */
1517 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1518 auto gridNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<GridPattern>());
1519 ASSERT_NE(gridNode, nullptr);
1520 gridNode->SetGeometryNode(geometryNode);
1521
1522 /**
1523 * @tc.steps: step4. construct a FrameNode.
1524 */
1525 auto childId = ElementRegister::GetInstance()->MakeUniqueId();
1526 auto childNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, childId, AceType::MakeRefPtr<Pattern>());
1527 childNode->SetGeometryNode(geometryNode);
1528 childNode->isActive_ = true;
1529
1530 /**
1531 * @tc.steps: step5. gridNode AddChild with childNode.
1532 */
1533 gridNode->AddChild(childNode);
1534
1535 /**
1536 * @tc.steps: step6. call GetItemIndex with DragEventType::GRID and gridNode.
1537 * @tc.expected: retFlag is true.
1538 */
1539 dragDropManager->draggedGridFrameNode_ = gridNode;
1540 dragDropManager->GetItemIndex(gridNode, DragType::GRID, 0, 0);
1541 bool retFlag = gridNode->FindChildByPosition(0, 0);
1542 EXPECT_TRUE(retFlag);
1543 }
1544
1545 /**
1546 * @tc.name: DragDropManagerTest039
1547 * @tc.desc: Test CheckParentVisible out of parent is a true branch.
1548 * @tc.type: FUNC
1549 * @tc.author:
1550 */
1551 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest039, TestSize.Level1)
1552 {
1553 /**
1554 * @tc.steps: step1. construct a DragDropManager.
1555 * @tc.expected: dragDropManager is not null.
1556 */
1557 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1558 ASSERT_NE(dragDropManager, nullptr);
1559
1560 /**
1561 * @tc.steps: step2. construct a frameNode and update attributes.
1562 * IsVisible is false branch
1563 */
1564 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1565 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<Pattern>());
1566 frameNode->GetLayoutProperty()->UpdateVisibility(VisibleType::INVISIBLE, true);
1567 auto eventHub = frameNode->GetEventHub<EventHub>();
1568 eventHub->SetEnabled(true);
1569
1570 /**
1571 * @tc.steps: step3. update nodesForDragNotify_.
1572 * @tc.expected: nodesForDragNotify_ is not empty.
1573 */
1574 dragDropManager->RegisterDragStatusListener(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode)));
1575 EXPECT_FALSE(dragDropManager->nodesForDragNotify_.empty());
1576
1577 /**
1578 * @tc.steps: step4. update NotifyDragEvent.
1579 * @tc.expected: notifyEvent attributes to be the set value.
1580 */
1581 RefPtr<NotifyDragEvent> notifyEvent = AceType::MakeRefPtr<NotifyDragEvent>();
1582 dragDropManager->UpdateNotifyDragEvent(notifyEvent, Point(1.0f, 1.0f), DragEventType::START);
1583 EXPECT_DOUBLE_EQ(notifyEvent->GetX(), 1.0);
1584 EXPECT_DOUBLE_EQ(notifyEvent->GetY(), 1.0);
1585
1586 /**
1587 * @tc.steps: step5. call NotifyDragRegisterFrameNode branches of CheckParentVisible out of isVisible.
1588 * @tc.expected: isVisible is false.
1589 */
1590 dragDropManager->NotifyDragRegisterFrameNode(
1591 dragDropManager->nodesForDragNotify_, DragEventType::START, notifyEvent);
1592 bool isVisible = frameNode->IsVisible();
1593 EXPECT_FALSE(isVisible);
1594
1595 /**
1596 * @tc.steps: step6. update nodesForDragNotify_.
1597 * @tc.expected: nodesForDragNotify_ is not empty.
1598 */
1599 dragDropManager->UnRegisterDragStatusListener(frameNode->GetId());
1600 EXPECT_TRUE(dragDropManager->nodesForDragNotify_.empty());
1601 dragDropManager->RegisterDragStatusListener(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode)));
1602 EXPECT_FALSE(dragDropManager->nodesForDragNotify_.empty());
1603
1604 /**
1605 * @tc.steps: step7. update frameNode attribute.
1606 * @tc.expected: parent is not null.
1607 */
1608 auto customNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
1609 frameNode->GetLayoutProperty()->UpdateVisibility(VisibleType::VISIBLE, true);
1610 frameNode->SetParent(WeakPtr<NG::UINode>(customNode));
1611 auto parent = frameNode->GetParent();
1612 EXPECT_TRUE(parent);
1613 parent->SetDepth(32);
1614 dragDropManager->UpdateNotifyDragEvent(notifyEvent, Point(1.0f, 1.0f), DragEventType::START);
1615
1616 /**
1617 * @tc.steps: step8. call NotifyDragRegisterFrameNode branches of CheckParentVisible out of parentFrameNode.
1618 * @tc.expected: parent->GetDepth() returns a value that is not equal to 1.
1619 */
1620 dragDropManager->NotifyDragRegisterFrameNode(
1621 dragDropManager->nodesForDragNotify_, DragEventType::START, notifyEvent);
1622 EXPECT_NE(parent->GetDepth(), 1);
1623 }
1624
1625 /**
1626 * @tc.name: DragDropManagerTest0391
1627 * @tc.desc: Test CheckParentVisible out of parentFrameNode is a true branch.
1628 * @tc.type: FUNC
1629 * @tc.author:
1630 */
1631 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest0391, TestSize.Level1)
1632 {
1633 /**
1634 * @tc.steps: step1. construct a DragDropManager.
1635 * @tc.expected: dragDropManager is not null.
1636 */
1637 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1638 ASSERT_NE(dragDropManager, nullptr);
1639
1640 /**
1641 * @tc.steps: step2. construct a frameNode and update attributes.
1642 * IsVisible is false branch
1643 */
1644 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1645 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<Pattern>());
1646 auto customNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
1647 customNode->GetLayoutProperty()->UpdateVisibility(VisibleType::INVISIBLE, true);
1648 frameNode->SetParent(WeakPtr<NG::UINode>(customNode));
1649 auto parent = frameNode->GetParent();
1650 EXPECT_TRUE(parent);
1651 parent->SetDepth(32);
1652 EXPECT_NE(parent->GetDepth(), 1);
1653
1654 /**
1655 * @tc.steps: step4. update nodesForDragNotify_ and NotifyDragEvent.
1656 * @tc.expected: nodesForDragNotify_ is not empty.
1657 */
1658 RefPtr<NotifyDragEvent> notifyEvent = AceType::MakeRefPtr<NotifyDragEvent>();
1659 dragDropManager->RegisterDragStatusListener(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode)));
1660 EXPECT_FALSE(dragDropManager->nodesForDragNotify_.empty());
1661 dragDropManager->UpdateNotifyDragEvent(notifyEvent, Point(1.0f, 1.0f), DragEventType::START);
1662
1663 /**
1664 * @tc.steps: step5. call NotifyDragRegisterFrameNode branches of CheckParentVisible out of parentFrameNode(is
1665 * false).
1666 * @tc.expected: customNode->IsVisible() returns a value that is not equal to false.
1667 */
1668 dragDropManager->NotifyDragRegisterFrameNode(
1669 dragDropManager->nodesForDragNotify_, DragEventType::START, notifyEvent);
1670 EXPECT_FALSE(customNode->IsVisible());
1671 }
1672
1673 /**
1674 * @tc.name: DragDropManagerTest040
1675 * @tc.desc: Test FindHitFrameNodes in frameNode branchs
1676 * @tc.type: FUNC
1677 * @tc.author:
1678 */
1679 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest040, TestSize.Level1)
1680 {
1681 /**
1682 * @tc.steps: step1. construct a DragDropManager.
1683 * @tc.expected: dragDropManager is not null.
1684 */
1685 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1686 ASSERT_NE(dragDropManager, nullptr);
1687
1688 /**
1689 * @tc.steps: step2. construct a frameNode and update attributes.
1690 * @tc.expected: geometryNode is null.
1691 */
1692 auto frameNodeNullId = ElementRegister::GetInstance()->MakeUniqueId();
1693 auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, frameNodeNullId, AceType::MakeRefPtr<Pattern>());
1694 frameNode->SetActive(true);
1695 frameNode->geometryNode_ = nullptr;
1696 auto geometryNode = frameNode->GetGeometryNode();
1697 EXPECT_FALSE(geometryNode);
1698
1699 /**
1700 * @tc.steps: step3. update nodesForDragNotify_.
1701 * @tc.expected: nodesForDragNotify_ is not empty.
1702 */
1703 dragDropManager->RegisterDragStatusListener(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode)));
1704 EXPECT_FALSE(dragDropManager->nodesForDragNotify_.empty());
1705
1706 /**
1707 * @tc.steps: step4. call FindHitFrameNodes out of geometryNode are false branches.
1708 * @tc.expected: frameNodeList is empty.
1709 */
1710 auto frameNodeList = dragDropManager->FindHitFrameNodes(Point(1.0f, 1.0f));
1711 EXPECT_TRUE(frameNodeList.empty());
1712
1713 /**
1714 * @tc.steps: step5. clear nodesForDragNotify_.
1715 */
1716 dragDropManager->UnRegisterDragStatusListener(frameNode->GetId());
1717 EXPECT_TRUE(dragDropManager->nodesForDragNotify_.empty());
1718
1719 /**
1720 * @tc.steps: step6. updates frameNode and nodesForDragNotify_.
1721 */
1722 frameNode->SetActive(false);
1723 frameNode->GetLayoutProperty()->UpdateVisibility(VisibleType::INVISIBLE, true);
1724 dragDropManager->RegisterDragStatusListener(frameNode->GetId(), AceType::WeakClaim(AceType::RawPtr(frameNode)));
1725
1726 /**
1727 * @tc.steps: step7. call FindHitFrameNodes out of IsVisible are false branches.
1728 * @tc.expected: frameNodeList is empty.
1729 */
1730 frameNodeList = dragDropManager->FindHitFrameNodes(Point(1.0f, 1.0f));
1731 EXPECT_TRUE(frameNodeList.empty());
1732 }
1733
1734 /**
1735 * @tc.name: DragDropManagerTest041
1736 * @tc.desc: Test SetDragDampStartPoint and GetDragDampStartPoint
1737 * @tc.type: FUNC
1738 * @tc.author:
1739 */
1740 HWTEST_F(DragDropManagerTestNg, DragDropManagerTest041, TestSize.Level1)
1741 {
1742 /**
1743 * @tc.steps: step1. construct a DragDropManager.
1744 * @tc.expected: dragDropManager is not null.
1745 */
1746 auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
1747 ASSERT_NE(dragDropManager, nullptr);
1748
1749 /**
1750 * @tc.steps: step2. create a point, than call SetDragDampStartPoint.
1751 * @tc.expected: The values of dragDampStartPoint_ and point are equal
1752 */
1753 Point point(1.0f, 1.0f);
1754 dragDropManager->SetDragDampStartPoint(point);
1755 EXPECT_EQ(dragDropManager->dragDampStartPoint_, point);
1756
1757 /**
1758 * @tc.steps: step3. create a point, than call GetDragDampStartPoint.
1759 * @tc.expected: The return values of dragDampStartPoint_ and point are equal
1760 */
1761 Point point2(2.0f, 2.0f);
1762 dragDropManager->dragDampStartPoint_ = point2;
1763 auto returnPoint = dragDropManager->GetDragDampStartPoint();
1764 EXPECT_EQ(returnPoint, point2);
1765 }
1766 } // namespace OHOS::Ace::NG
1767