• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "test/unittest/core/manager/drag_drop_manager_test_ng.h"
17 
18 #include "core/components_ng/manager/drag_drop/drag_drop_behavior_reporter/drag_drop_behavior_reporter.h"
19 #include "test/mock/base/mock_task_executor.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 namespace OHOS::Ace::NG {
SetUpTestCase()24 void DragDropManagerTestNgPlus::SetUpTestCase()
25 {
26     MockPipelineContext::SetUp();
27     MockContainer::SetUp(NG::PipelineContext::GetCurrentContext());
28 }
29 
TearDownTestCase()30 void DragDropManagerTestNgPlus::TearDownTestCase()
31 {
32     MockPipelineContext::TearDown();
33     MockContainer::TearDown();
34 }
35 
36 /**
37  * @tc.name: DragDropManagerTestNgPlus001
38  * @tc.desc: Test FindTargetInChildNodes and CheckFrameNodeCanDrop
39  * @tc.type: FUNC
40  * @tc.author:
41  */
42 HWTEST_F(DragDropManagerTestNgPlus, DragDropManagerTestNgPlus001, TestSize.Level1)
43 {
44     /**
45      * @tc.steps: step1. construct a DragDropManager, create DC.
46      */
47     auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
48     ASSERT_NE(dragDropManager, nullptr);
49     auto frameNodeDCId = ElementRegister::GetInstance()->MakeUniqueId();
50     auto frameNodeDC = AceType::MakeRefPtr<FrameNode>(
51         V2::DYNAMIC_COMPONENT_ETS_TAG, frameNodeDCId, AceType::MakeRefPtr<Pattern>());
52     ASSERT_NE(frameNodeDC, nullptr);
53     frameNodeDC->GetLayoutProperty()->UpdateVisibility(VisibleType::VISIBLE);
54     frameNodeDC->SetActive(true);
55 
56     /**
57      * @tc.steps: step2. test function FindTargetInChildNodes.
58      */
59     auto geometryNode = AceType::MakeRefPtr<GeometryNode>();
60     geometryNode->SetFrameSize(FRAME_SIZE);
61     frameNodeDC->SetGeometryNode(geometryNode);
62     dragDropManager->AddGridDragFrameNode(frameNodeDC->GetId(), frameNodeDC);
63     std::vector<RefPtr<FrameNode>> hitFrameNodes;
64     hitFrameNodes.push_back(frameNodeDC);
65     auto result = dragDropManager->FindTargetInChildNodes(frameNodeDC, hitFrameNodes, true);
66     EXPECT_NE(result, nullptr);
67 
68     /**
69      * @tc.steps: step3. create isolated component and test function FindTargetInChildNodes.
70      */
71     auto frameNodeICId = ElementRegister::GetInstance()->MakeUniqueId();
72     auto frameNodeIC = AceType::MakeRefPtr<FrameNode>(
73         V2::ISOLATED_COMPONENT_ETS_TAG, frameNodeICId, AceType::MakeRefPtr<Pattern>());
74     ASSERT_NE(frameNodeIC, nullptr);
75     frameNodeIC->GetLayoutProperty()->UpdateVisibility(VisibleType::VISIBLE);
76     frameNodeIC->SetActive(true);
77     auto eventHubIC = frameNodeIC->GetEventHub<EventHub>();
78     ASSERT_FALSE(eventHubIC);
79 
80     frameNodeIC->SetGeometryNode(geometryNode);
81     dragDropManager->AddGridDragFrameNode(frameNodeIC->GetId(), frameNodeIC);
82     hitFrameNodes.pop_back();
83     hitFrameNodes.push_back(frameNodeIC);
84     result = dragDropManager->FindTargetInChildNodes(frameNodeIC, hitFrameNodes, true);
85     EXPECT_NE(result, nullptr);
86 
87     /**
88      * @tc.steps: step4. test CheckFrameNodeCanDrop.
89      */
90     auto frameNode = AceType::MakeRefPtr<FrameNode>(
91         NODE_TAG, frameNodeDCId, AceType::MakeRefPtr<Pattern>());
92     auto dropResult = dragDropManager->CheckFrameNodeCanDrop(frameNodeDC);
93     EXPECT_TRUE(dropResult);
94 
95     dropResult = dragDropManager->CheckFrameNodeCanDrop(frameNodeIC);
96     EXPECT_TRUE(dropResult);
97 
98     dropResult = dragDropManager->CheckFrameNodeCanDrop(frameNode);
99     EXPECT_FALSE(dropResult);
100 }
101 
102 /**
103  * @tc.name: DragDropManagerTestNgPlus002
104  * @tc.desc: Test IsUIExtensionOrDynamicComponent
105  * @tc.type: FUNC
106  * @tc.author:
107  */
108 HWTEST_F(DragDropManagerTestNgPlus, DragDropManagerTestNgPlus002, TestSize.Level1)
109 {
110     auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
111     EXPECT_NE(dragDropManager, nullptr);
112     auto frameNodeDCId = ElementRegister::GetInstance()->MakeUniqueId();
113     auto frameNodeDC = AceType::MakeRefPtr<FrameNode>(
114         V2::DYNAMIC_COMPONENT_ETS_TAG, frameNodeDCId, AceType::MakeRefPtr<Pattern>());
115     EXPECT_NE(frameNodeDC, nullptr);
116     auto frameNodeICId = ElementRegister::GetInstance()->MakeUniqueId();
117     auto frameNodeIC = AceType::MakeRefPtr<FrameNode>(
118         V2::ISOLATED_COMPONENT_ETS_TAG, frameNodeICId, AceType::MakeRefPtr<Pattern>());
119     EXPECT_NE(frameNodeIC, nullptr);
120     DragPointerEvent point;
121     point.x = 1;
122     point.y = 1;
123     auto container = MockContainer::Current();
124     ASSERT_NE(container, nullptr);
125 
126     dragDropManager->HandleOnDragEnd(point, EXTRA_INFO, frameNodeDC);
127     EXPECT_NE(DragDropBehaviorReporter::GetInstance().stopResult_, DragStopResult::GET_UDKEY_FAIL);
128 
129     dragDropManager->HandleOnDragEnd(point, EXTRA_INFO, frameNodeIC);
130     EXPECT_NE(DragDropBehaviorReporter::GetInstance().stopResult_, DragStopResult::GET_UDKEY_FAIL);
131 }
132 
133 /**
134  * @tc.name: DragDropManagerTestNgPlus003
135  * @tc.desc: Test OnDragEnd
136  * @tc.type: FUNC
137  * @tc.author:
138  */
139 HWTEST_F(DragDropManagerTestNgPlus, DragDropManagerTestNgPlus003, TestSize.Level1)
140 {
141     auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
142     ASSERT_NE(dragDropManager, nullptr);
143     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
144     ASSERT_NE(frameNode, nullptr);
145     auto node = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
146     ASSERT_NE(node, nullptr);
147     auto uec = AceType::MakeRefPtr<FrameNode>(V2::DYNAMIC_COMPONENT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
148     ASSERT_NE(uec, nullptr);
149     DragPointerEvent pointerEvent(1, 1);
150     Point point = pointerEvent.GetPoint();
151     auto container = MockContainer::Current();
152     ASSERT_NE(container, nullptr);
153     container->SetIsSceneBoardWindow(false);
154     dragDropManager->SetIsDragCancel(false);
155     dragDropManager->preTargetFrameNode_ = uec;
156     auto dragFrameNode = dragDropManager->FindDragFrameNodeByPosition(static_cast<float>(point.GetX()),
157         static_cast<float>(point.GetY()),
158         dragDropManager->FilterSubwindowDragRootNode(node));
159     dragDropManager->OnDragEnd(pointerEvent, EXTRA_INFO, frameNode);
160     dragDropManager->preTargetFrameNode_ = node;
161     dragDropManager->OnDragEnd(pointerEvent, EXTRA_INFO, frameNode);
162     dragDropManager->preTargetFrameNode_ = nullptr;
163     dragDropManager->OnDragEnd(pointerEvent, EXTRA_INFO, frameNode);
164     EXPECT_EQ(dragFrameNode, dragDropManager->preTargetFrameNode_);
165 }
166 
167 /**
168  * @tc.name: DragDropManagerTestNgPlus004
169  * @tc.desc: Test IsAnyDraggableHit Funcition When iter == touchTestResults.end() Is False AND iter->second.empty() Is
170  * Fasle
171  * @tc.type: FUNC
172  * @tc.author:
173  */
174 HWTEST_F(DragDropManagerTestNgPlus, DragDropManagerTestNgPlus004, TestSize.Level1)
175 {
176     auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
177     ASSERT_NE(dragDropManager, nullptr);
178     auto pipelineContext = MockPipelineContext::GetCurrentContext();
179     ASSERT_NE(pipelineContext, nullptr);
180     auto eventManager = pipelineContext->GetEventManager();
181     ASSERT_NE(eventManager, nullptr);
182     TouchTestResult hitTestResult;
183     auto scrollNode = AceType::MakeRefPtr<OHOS::Ace::NG::Scrollable>();
184     ASSERT_NE(scrollNode, nullptr);
185     hitTestResult.emplace_back(scrollNode);
186     TouchTestResult hitTestResultDC;
187     auto scrollDCNode = AceType::MakeRefPtr<OHOS::Ace::NG::Scrollable>();
188     hitTestResultDC.emplace_back(scrollDCNode);
189     eventManager->touchTestResults_[0] = hitTestResult;
190     eventManager->touchTestResults_[1] = hitTestResultDC;
191     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
192     ASSERT_NE(frameNode, nullptr);
193     scrollNode->AttachFrameNode(WeakPtr<NG::FrameNode>(frameNode));
194     bool result = dragDropManager->IsAnyDraggableHit(pipelineContext, 0);
195     EXPECT_FALSE(result);
196     auto dynamicComponent =
197         AceType::MakeRefPtr<FrameNode>(V2::DYNAMIC_COMPONENT_ETS_TAG, -1, AceType::MakeRefPtr<Pattern>());
198     ASSERT_NE(dynamicComponent, nullptr);
199     scrollDCNode->AttachFrameNode(WeakPtr<NG::FrameNode>(dynamicComponent));
200     result = dragDropManager->IsAnyDraggableHit(pipelineContext, 1);
201     EXPECT_TRUE(result);
202 }
203 
204 /**
205  * @tc.name: DragDropManagerTestNgPlus005
206  * @tc.desc: Test PostStopDrag Funcition When requestId_ == requestId and requestId_ != requestId.
207  * @tc.type: FUNC
208  * @tc.author:
209  */
210 HWTEST_F(DragDropManagerTestNgPlus, DragDropManagerTestNgPlus005, TestSize.Level1)
211 {
212     /**
213      * @tc.steps: step1. construct a frameNode and dragEvent.
214      */
215     auto mockTaskExecutor = AceType::MakeRefPtr<MockTaskExecutor>();
216     MockPipelineContext::GetCurrentContext()->taskExecutor_ = mockTaskExecutor;
217     auto pipelineContext = MockPipelineContext::GetCurrentContext();
218     ASSERT_NE(pipelineContext, nullptr);
219     auto frameNode = AceType::MakeRefPtr<FrameNode>(NODE_TAG, -1, AceType::MakeRefPtr<Pattern>());
220     ASSERT_NE(frameNode, nullptr);
221     auto dragEvent = AceType::MakeRefPtr<OHOS::Ace::DragEvent>();
222     ASSERT_NE(dragEvent, nullptr);
223     auto dragDropManager = AceType::MakeRefPtr<DragDropManager>();
224     ASSERT_NE(dragDropManager, nullptr);
225     DragPointerEvent pointerEvent;
226 
227     /**
228      * @tc.steps: step1. test PostStopDrag.
229      */
230     dragEvent->SetRequestIdentify(1);
231     DragDropGlobalController::GetInstance().requestId_ = 0;
232     DragDropGlobalController::GetInstance().SetIsOnOnDropPhase(true);
233     dragDropManager->PostStopDrag(frameNode, pointerEvent, dragEvent, "");
234     EXPECT_TRUE(DragDropGlobalController::GetInstance().IsOnOnDropPhase());
235 
236     DragDropGlobalController::GetInstance().requestId_ = 1;
237     dragDropManager->PostStopDrag(frameNode, pointerEvent, dragEvent, "");
238     EXPECT_FALSE(DragDropGlobalController::GetInstance().IsOnOnDropPhase());
239 }
240 }