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 "gtest/gtest.h"
17 #include "test/unittest/core/manager/drag_drop/drag_drop_initiating_test_ng.h"
18 #define private public
19 #include "core/components_ng/manager/drag_drop/drag_drop_initiating/drag_drop_initiating_handler.h"
20 #undef private
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS::Ace::NG {
26 namespace {
27 const std::string CUSTOM_DRAG_END_EVENT_TYPE = "custom drag end";
28 const std::string DRAG_END_EVENT_TYPE = "drag end";
29 } // namespace
30
SetUpTestCase()31 void DragDropInitiatingHandlerTestNg::SetUpTestCase()
32 {
33 MockPipelineContext::SetUp();
34 MockContainer::SetUp();
35 }
36
TearDownTestCase()37 void DragDropInitiatingHandlerTestNg::TearDownTestCase()
38 {
39 MockPipelineContext::TearDown();
40 MockContainer::TearDown();
41 }
42
43 /**
44 * @tc.name: IsNeedGatherTestNG001
45 * @tc.desc: Test IsNeedGather Function When initiatingFlow_ is nullptr.
46 * @tc.type: FUNC
47 */
48 HWTEST_F(DragDropInitiatingHandlerTestNg, IsNeedGatherTestNG001, TestSize.Level1)
49 {
50 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0202() 51 []() { return AceType::MakeRefPtr<ImagePattern>(); });
52 ASSERT_NE(frameNode, nullptr);
53 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
54 ASSERT_NE(dragDropInitiatingHandler, nullptr);
55 dragDropInitiatingHandler->initiatingFlow_ = nullptr;
56 EXPECT_FALSE(dragDropInitiatingHandler->IsNeedGather());
57 dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
58 ASSERT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
59 dragDropInitiatingHandler->initiatingFlow_->GetDragDropInitiatingParams().isNeedGather = true;
60 EXPECT_TRUE(dragDropInitiatingHandler->IsNeedGather());
61 }
62
63 /**
64 * @tc.name: GetDragDropInitiatingStatusTestNG001
65 * @tc.desc: Test GetDragDropInitiatingStatus Function When initiatingFlow_ is not nullptr.
66 * @tc.type: FUNC
67 */
68 HWTEST_F(DragDropInitiatingHandlerTestNg, GetDragDropInitiatingStatusTestNG001, TestSize.Level1)
69 {
70 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0302() 71 []() { return AceType::MakeRefPtr<ImagePattern>(); });
72 ASSERT_NE(frameNode, nullptr);
73 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
74 ASSERT_NE(dragDropInitiatingHandler, nullptr);
75 ASSERT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
76 EXPECT_EQ(dragDropInitiatingHandler->GetDragDropInitiatingStatus(),
77 dragDropInitiatingHandler->initiatingFlow_->GetDragDropInitiatingStatus());
78 }
79
80 /**
81 * @tc.name: GetDragDropInitiatingStatusTestNG002
82 * @tc.desc: Test GetDragDropInitiatingStatus Function When initiatingFlow_ is nullptr.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(DragDropInitiatingHandlerTestNg, GetDragDropInitiatingStatusTestNG002, TestSize.Level1)
86 {
87 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0402() 88 []() { return AceType::MakeRefPtr<ImagePattern>(); });
89 ASSERT_NE(frameNode, nullptr);
90 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
91 ASSERT_NE(dragDropInitiatingHandler, nullptr);
92 dragDropInitiatingHandler->initiatingFlow_ = nullptr;
93 EXPECT_EQ(dragDropInitiatingHandler->GetDragDropInitiatingStatus(), DragDropInitiatingStatus::IDLE);
94 }
95
96 /**
97 * @tc.name: DragDropInitiatingHandlerTestNG001
98 * @tc.desc: Test DragDropInitiatingHandler Function When initiatingFlow_ is not nullptr.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(DragDropInitiatingHandlerTestNg, DragDropInitiatingHandlerTestNG001, TestSize.Level1)
102 {
103 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0502() 104 []() { return AceType::MakeRefPtr<ImagePattern>(); });
105 ASSERT_NE(frameNode, nullptr);
106 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
107 ASSERT_NE(dragDropInitiatingHandler, nullptr);
108 EXPECT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
109 }
110
111 /**
112 * @tc.name: SetThumbnailCallbackNG001
113 * @tc.desc: Test DragDropInitiatingHandler Function When initiatingFlow_ is not nullptr.
114 * @tc.type: FUNC
115 */
116 HWTEST_F(DragDropInitiatingHandlerTestNg, SetThumbnailCallbackNG001, TestSize.Level1)
117 {
118 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0602() 119 []() { return AceType::MakeRefPtr<ImagePattern>(); });
120 ASSERT_NE(frameNode, nullptr);
121 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
122 ASSERT_NE(dragDropInitiatingHandler, nullptr);
123 EXPECT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
__anon18dd4d6c0702(const Offset&) 124 dragDropInitiatingHandler->SetThumbnailCallback([](const Offset&) {});
125 EXPECT_NE(dragDropInitiatingHandler->initiatingFlow_->dragDropInitiatingParams_.getTextThumbnailPixelMapCallback,
126 nullptr);
127 }
128
129 /**
130 * @tc.name: FireCustomerOnDragEndNG001
131 * @tc.desc: Test DragDropInitiatingHandler Function When initiatingFlow_ is not nullptr.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(DragDropInitiatingHandlerTestNg, FireCustomerOnDragEndNG001, TestSize.Level1)
135 {
136 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0802() 137 []() { return AceType::MakeRefPtr<ImagePattern>(); });
138 ASSERT_NE(frameNode, nullptr);
139 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
140 ASSERT_NE(dragDropInitiatingHandler, nullptr);
141 EXPECT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
142 auto machine = dragDropInitiatingHandler->initiatingFlow_;
143 ASSERT_NE(machine, nullptr);
144 machine->InitializeState();
145 machine->currentState_ = static_cast<int32_t>(DragDropInitiatingStatus::IDLE);
146 DragDropGlobalController::GetInstance().SetDragStartRequestStatus(DragStartRequestStatus::WAITING);
147 DragDropGlobalController::GetInstance().UpdateDragDropInitiatingStatus(frameNode, DragDropInitiatingStatus::MOVING);
148 EXPECT_EQ(DragDropGlobalController::GetInstance().currentDragNode_, frameNode);
149 auto gestureHub = frameNode->GetOrCreateGestureEventHub();
150 ASSERT_NE(gestureHub, nullptr);
151 auto eventHub = frameNode->GetOrCreateEventHub<EventHub>();
152 std::string customerDragEventType;
153 std::string dragEventType;
__anon18dd4d6c0902(const RefPtr<OHOS::Ace::DragEvent>& info) 154 auto customerOnDragEnd = [&customerDragEventType](const RefPtr<OHOS::Ace::DragEvent>& info) {
155 EXPECT_EQ(info->GetResult(), DragRet::DRAG_FAIL);
156 customerDragEventType = CUSTOM_DRAG_END_EVENT_TYPE;
157 };
158 eventHub->SetCustomerOnDragFunc(DragFuncType::DRAG_END, customerOnDragEnd);
__anon18dd4d6c0a02(const RefPtr<OHOS::Ace::DragEvent>& info) 159 auto onDragEnd = [&dragEventType](const RefPtr<OHOS::Ace::DragEvent>& info) {
160 EXPECT_EQ(info->GetResult(), DragRet::DRAG_FAIL);
161 dragEventType = DRAG_END_EVENT_TYPE;
162 };
163 eventHub->SetOnDragEnd(onDragEnd);
164 ASSERT_NE(machine->dragDropInitiatingState_[machine->currentState_], nullptr);
165 machine->dragDropInitiatingState_[machine->currentState_]->Init(machine->currentState_);
166 EXPECT_EQ(DragDropGlobalController::GetInstance().currentDragNode_, nullptr);
167 EXPECT_EQ(customerDragEventType, CUSTOM_DRAG_END_EVENT_TYPE);
168 EXPECT_EQ(dragEventType, DRAG_END_EVENT_TYPE);
169 }
170
171 /**
172 * @tc.name: IsAllowedDragNG001
173 * @tc.desc: Test DragDropInitiatingHandler Function When initiatingFlow_ is not nullptr.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(DragDropInitiatingHandlerTestNg, IsAllowedDragNG001, TestSize.Level1)
177 {
178 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG, ElementRegister::GetInstance()->MakeUniqueId(),
__anon18dd4d6c0b02() 179 []() { return AceType::MakeRefPtr<ImagePattern>(); });
180 ASSERT_NE(frameNode, nullptr);
181 auto dragDropInitiatingHandler = AceType::MakeRefPtr<DragDropInitiatingHandler>(frameNode);
182 ASSERT_NE(dragDropInitiatingHandler, nullptr);
183 EXPECT_NE(dragDropInitiatingHandler->initiatingFlow_, nullptr);
184 auto machine = dragDropInitiatingHandler->initiatingFlow_;
185 ASSERT_NE(machine, nullptr);
186 machine->InitializeState();
187 machine->currentState_ = static_cast<int32_t>(DragDropInitiatingStatus::IDLE);
188 ASSERT_NE(machine->dragDropInitiatingState_[machine->currentState_], nullptr);
189 EXPECT_TRUE(machine->dragDropInitiatingState_[machine->currentState_]->IsAllowedDrag());
190 }
191 } // namespace OHOS::Ace::NG