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 #include <cstdint>
16 #include <iostream>
17
18 #include "gtest/gtest.h"
19 #define private public
20 #define protected public
21 #include "drag_and_drop.h"
22 #include "event_converter.h"
23 #include "native_interface.h"
24 #include "native_node.h"
25 #include "native_type.h"
26 #include "node_model.h"
27 #include "test/mock/base/mock_task_executor.h"
28 #include "test/mock/core/common/mock_container.h"
29 #include "test/mock/core/common/mock_theme_manager.h"
30 #include "test/mock/core/pipeline/mock_pipeline_context.h"
31
32 using namespace testing;
33 using namespace testing::ext;
34 namespace OHOS::Ace {
35 namespace {
36 const float WINDOW_X = 1.0f;
37 const float WINDOW_Y = 1.0f;
38 const float TOUCH_POINT_X = 1.0f;
39 const float TOUCH_POINT_Y = 1.0f;
40 const float PREVIEW_RECT_WIDTH = 1.0f;
41 const float PREVIEW_RECT_HEIGHT = 1.0f;
42 const float DISPLAY_X = 1.0f;
43 const float DISPLAY_Y = 1.0f;
44 const float VELOCITY_X = 1.0f;
45 const float VELOCITY_Y = 1.0f;
46 const float VELOCITY = 1.0f;
47 } // namespace
48 class DragAndDropTest : public testing::Test {
49 public:
SetUpTestSuite()50 static void SetUpTestSuite()
51 {
52 NG::MockPipelineContext::SetUp();
53 MockContainer::SetUp();
54 MockContainer::Current()->pipelineContext_ = NG::MockPipelineContext::GetCurrent();
55 MockContainer::Current()->taskExecutor_ = AceType::MakeRefPtr<MockTaskExecutor>();
56 MockContainer::Current()->pipelineContext_->taskExecutor_ = MockContainer::Current()->taskExecutor_;
57 auto themeManager = AceType::MakeRefPtr<MockThemeManager>();
58 PipelineBase::GetCurrentContext()->SetThemeManager(themeManager);
59 }
TearDownTestSuite()60 static void TearDownTestSuite()
61 {
62 NG::MockPipelineContext::TearDown();
63 MockContainer::TearDown();
64 }
65 };
66
67 /**
68 * @tc.name: DragAndDropTest001
69 * @tc.desc: Test the DragAction for C-API.
70 * @tc.type: FUNC
71 */
HWTEST_F(DragAndDropTest, DragAndDropTest001, TestSize.Level1)72 HWTEST_F(DragAndDropTest, DragAndDropTest001, TestSize.Level1)
73 {
74 /**
75 * @tc.steps: step1.create dragAction.
76 */
77 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>(
78 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
79 auto rootNode = nodeAPI->createNode(ARKUI_NODE_STACK);
80 auto rootFrameNode = reinterpret_cast<ArkUI_Node*>(rootNode);
81 auto frameNode = reinterpret_cast<NG::FrameNode*>(rootFrameNode->uiNodeHandle);
82 frameNode->context_ = NG::MockPipelineContext::GetCurrent().GetRawPtr();
83 auto* dragAction = OH_ArkUI_CreateDragActionWithNode(rootNode);
84 EXPECT_NE(dragAction, nullptr);
85 EXPECT_EQ(OH_ArkUI_DragAction_SetPointerId(dragAction, 0), ARKUI_ERROR_CODE_NO_ERROR);
86 EXPECT_EQ(OH_ArkUI_DragAction_SetTouchPointX(dragAction, 1.0), ARKUI_ERROR_CODE_NO_ERROR);
87 EXPECT_EQ(OH_ArkUI_DragAction_SetTouchPointY(dragAction, 1.0), ARKUI_ERROR_CODE_NO_ERROR);
88 auto* interalDragAction = reinterpret_cast<ArkUIDragAction*>(dragAction);
89 EXPECT_EQ(interalDragAction->pointerId, 0);
90 EXPECT_EQ(interalDragAction->touchPointX, 1.0);
91 EXPECT_EQ(interalDragAction->touchPointY, 1.0);
92 OH_ArkUI_DragAction_RegisterStatusListener(
93 dragAction, nullptr, [](ArkUI_DragAndDropInfo* dragAndDropInfo, void* userData) -> void {
94 EXPECT_NE(
95 OH_ArkUI_DragAndDropInfo_GetDragStatus(dragAndDropInfo), ArkUI_DragStatus::ARKUI_DRAG_STATUS_UNKNOWN);
96 EXPECT_NE(OH_ArkUI_DragAndDropInfo_GetDragEvent(dragAndDropInfo), nullptr);
97 });
98 OH_ArkUI_DragAction_UnregisterStatusListener(dragAction);
99 EXPECT_EQ(OH_ArkUI_StartDrag(dragAction), ARKUI_ERROR_CODE_NO_ERROR);
100 OH_ArkUI_DragAction_Dispose(dragAction);
101 }
102
103 /**
104 * @tc.name: DragAndDropTest002
105 * @tc.desc: Test the DragAction for C-API.
106 * @tc.type: FUNC
107 */
HWTEST_F(DragAndDropTest, DragAndDropTest002, TestSize.Level1)108 HWTEST_F(DragAndDropTest, DragAndDropTest002, TestSize.Level1)
109 {
110 /**
111 * @tc.steps: step1.create dragAction.
112 */
113 auto uiContext = new ArkUI_Context({ .id = 1 });
114 auto dragAction = OH_ArkUI_CreateDragActionWithContext(uiContext);
115 EXPECT_NE(dragAction, nullptr);
116 EXPECT_EQ(OH_ArkUI_DragAction_SetPointerId(dragAction, 0), ARKUI_ERROR_CODE_NO_ERROR);
117 EXPECT_EQ(OH_ArkUI_DragAction_SetTouchPointX(dragAction, 1.0), ARKUI_ERROR_CODE_NO_ERROR);
118 EXPECT_EQ(OH_ArkUI_DragAction_SetTouchPointY(dragAction, 1.0), ARKUI_ERROR_CODE_NO_ERROR);
119 auto* interalDragAction = reinterpret_cast<ArkUIDragAction*>(dragAction);
120 EXPECT_EQ(interalDragAction->pointerId, 0);
121 EXPECT_EQ(interalDragAction->touchPointX, 1.0);
122 EXPECT_EQ(interalDragAction->touchPointY, 1.0);
123 OH_ArkUI_DragAction_RegisterStatusListener(
124 dragAction, nullptr, [](ArkUI_DragAndDropInfo* dragAndDropInfo, void* userData) -> void {
125 EXPECT_NE(
126 OH_ArkUI_DragAndDropInfo_GetDragStatus(dragAndDropInfo), ArkUI_DragStatus::ARKUI_DRAG_STATUS_UNKNOWN);
127 EXPECT_NE(OH_ArkUI_DragAndDropInfo_GetDragEvent(dragAndDropInfo), nullptr);
128 });
129 OH_ArkUI_DragAction_UnregisterStatusListener(dragAction);
130 EXPECT_EQ(OH_ArkUI_StartDrag(dragAction), ARKUI_ERROR_CODE_NO_ERROR);
131 OH_ArkUI_DragAction_Dispose(dragAction);
132 }
133
134 /**
135 * @tc.name: DragAndDropTest003
136 * @tc.desc: test OH_ArkUI_NodeEvent_GetDragEvent function;
137 * @tc.type: FUNC
138 */
HWTEST_F(DragAndDropTest, DragAndDropTest003, TestSize.Level1)139 HWTEST_F(DragAndDropTest, DragAndDropTest003, TestSize.Level1)
140 {
141 ArkUI_NodeEvent event = { 0, -1 };
142 auto eventType = OH_ArkUI_NodeEvent_GetDragEvent(&event);
143 EXPECT_EQ(eventType, nullptr);
144 }
145
146 /**
147 * @tc.name: DragAndDropTest004
148 * @tc.desc: test NodeDrag function;
149 * @tc.type: FUNC
150 */
HWTEST_F(DragAndDropTest, DragAndDropTest004, TestSize.Level1)151 HWTEST_F(DragAndDropTest, DragAndDropTest004, TestSize.Level1)
152 {
153 /**
154 * @tc.steps: step1.create FrameNode is not null, related function is called.
155 */
156 auto nodeAPI = reinterpret_cast<ArkUI_NativeNodeAPI_1*>(
157 OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_NODE, "ArkUI_NativeNodeAPI_1"));
158 auto textNode = new ArkUI_Node({ ARKUI_NODE_TEXT, nullptr });
159 auto areaNode = new ArkUI_Node({ ARKUI_NODE_TEXT_AREA, nullptr });
160
161 /**
162 * @tc.expected: Return expected results.
163 */
164 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_PRE_DRAG, 0, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
165 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_DRAG_START, 1, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
166 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_DRAG_ENTER, 2, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
167 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_DRAG_MOVE, 3, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
168 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_DRAG_LEAVE, 4, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
169 EXPECT_EQ(nodeAPI->registerNodeEvent(areaNode, NODE_ON_DROP, 5, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
170 EXPECT_EQ(nodeAPI->registerNodeEvent(textNode, NODE_ON_DRAG_END, 6, nullptr), ARKUI_ERROR_CODE_NO_ERROR);
171 }
172
173 /**
174 * @tc.name: DragAndDropTest005
175 * @tc.desc: Test the OH_ArkUI_NodeEvent_GetPreDragStatus.
176 * @tc.type: FUNC
177 */
HWTEST_F(DragAndDropTest, DragAndDropTest005, TestSize.Level1)178 HWTEST_F(DragAndDropTest, DragAndDropTest005, TestSize.Level1)
179 {
180 /**
181 * @tc.steps: step1.create ArkUI_NodeEvent, related function is called.
182 */
183 ArkUI_NodeEvent nodeEvent;
184 ArkUINodeEvent event;
185 event.kind = ArkUIEventCategory::COMPONENT_ASYNC_EVENT;
186 event.componentAsyncEvent.subKind = ON_PRE_DRAG;
187 event.componentAsyncEvent.data[0].i32 =
188 static_cast<ArkUI_Int32>(OHOS::Ace::PreDragStatus::READY_TO_TRIGGER_DRAG_ACTION);
189 nodeEvent.origin = &event;
190 nodeEvent.category = NodeEventCategory::NODE_EVENT_CATEGORY_COMPONENT_EVENT;
191 auto ret1 = OH_ArkUI_NodeEvent_GetPreDragStatus(&nodeEvent);
192
193 /**
194 * @tc.steps: step2.set DragEvent is nullptr, related function is called.
195 */
196 auto ret2 = OH_ArkUI_NodeEvent_GetPreDragStatus(nullptr);
197
198 /**
199 * @tc.steps: step3.set category to other type, related function is called.
200 */
201 nodeEvent.category = NodeEventCategory::NODE_EVENT_CATEGORY_INPUT_EVENT;
202 auto ret3 = OH_ArkUI_NodeEvent_GetPreDragStatus(&nodeEvent);
203
204 /**
205 * @tc.steps: step4.set origin to nullptr, related function is called.
206 */
207 nodeEvent.category = NodeEventCategory::NODE_EVENT_CATEGORY_COMPONENT_EVENT;
208 nodeEvent.origin = nullptr;
209 auto ret4 = OH_ArkUI_NodeEvent_GetPreDragStatus(&nodeEvent);
210
211 /**
212 * @tc.expected: Return expected results.
213 */
214 EXPECT_EQ(ret1, ArkUI_PreDragStatus::ARKUI_PRE_DRAG_STATUS_READY_TO_TRIGGER_DRAG);
215 EXPECT_EQ(ret2, ArkUI_PreDragStatus::ARKUI_PRE_DRAG_STATUS_UNKNOWN);
216 EXPECT_EQ(ret3, ArkUI_PreDragStatus::ARKUI_PRE_DRAG_STATUS_UNKNOWN);
217 EXPECT_EQ(ret4, ArkUI_PreDragStatus::ARKUI_PRE_DRAG_STATUS_UNKNOWN);
218 }
219
220 /**
221 * @tc.name: DragAndDropTest006
222 * @tc.desc: Test the OH_ArkUI_DragEvent_DisableDefaultDropAnimation.
223 * @tc.type: FUNC
224 */
HWTEST_F(DragAndDropTest, DragAndDropTest006, TestSize.Level1)225 HWTEST_F(DragAndDropTest, DragAndDropTest006, TestSize.Level1)
226 {
227 /**
228 * @tc.steps: step1.create DragEvent, related function is called.
229 */
230 ArkUIDragEvent dragEvent;
231 auto* drag_Event = reinterpret_cast<ArkUI_DragEvent*>(&dragEvent);
232 auto ret1 = OH_ArkUI_DragEvent_DisableDefaultDropAnimation(drag_Event, true);
233
234 /**
235 * @tc.steps: step2.set DragEvent is nullptr, related function is called.
236 */
237 auto ret2 = OH_ArkUI_DragEvent_DisableDefaultDropAnimation(nullptr, false);
238
239 /**
240 * @tc.expected: Return expected results.
241 */
242 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
243 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
244 EXPECT_EQ(dragEvent.useCustomDropAnimation, true);
245 }
246
247 /**
248 * @tc.name: DragAndDropTest007
249 * @tc.desc: Test the OH_ArkUI_SetNodeDraggable.
250 * @tc.type: FUNC
251 */
HWTEST_F(DragAndDropTest, DragAndDropTest007, TestSize.Level1)252 HWTEST_F(DragAndDropTest, DragAndDropTest007, TestSize.Level1)
253 {
254 /**
255 * @tc.steps: step1.create DragEvent, related function is called.
256 */
257 auto textNode = new ArkUI_Node({ ARKUI_NODE_TEXT, nullptr });
258 auto ret1 = OH_ArkUI_SetNodeDraggable(textNode, true);
259
260 /**
261 * @tc.steps: step2.set DragEvent is nullptr, related function is called.
262 */
263 auto ret2 = OH_ArkUI_SetNodeDraggable(nullptr, false);
264
265 /**
266 * @tc.expected: Return expected results.
267 */
268 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
269 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
270 }
271
272 /**
273 * @tc.name: DragAndDropTest008
274 * @tc.desc: Test the OH_ArkUI_CreateDragPreviewOption.
275 * @tc.type: FUNC
276 */
HWTEST_F(DragAndDropTest, DragAndDropTest008, TestSize.Level1)277 HWTEST_F(DragAndDropTest, DragAndDropTest008, TestSize.Level1)
278 {
279 /**
280 * @tc.steps: step1.create ArkUI_DragPreviewOption.
281 * @tc.expected: Return expected results.
282 */
283 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
284 EXPECT_NE(dragPreviewOption, nullptr);
285
286 /**
287 * @tc.steps: step2.create ArkUI_DragPreviewOption.
288 * @tc.expected: Return expected results.
289 */
290 OH_ArkUI_DragPreviewOption_Dispose(dragPreviewOption);
291 dragPreviewOption = nullptr;
292 EXPECT_EQ(dragPreviewOption, nullptr);
293 }
294
295 /**
296 * @tc.name: DragAndDropTest009
297 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetScaleMode.
298 * @tc.type: FUNC
299 */
HWTEST_F(DragAndDropTest, DragAndDropTest009, TestSize.Level1)300 HWTEST_F(DragAndDropTest, DragAndDropTest009, TestSize.Level1)
301 {
302 /**
303 * @tc.steps: step1.create dragPreviewOption and set scaleMode.
304 * @tc.expected: Return expected results.
305 */
306 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
307 auto ret1 = OH_ArkUI_DragPreviewOption_SetScaleMode(
308 dragPreviewOption, ArkUI_DragPreviewScaleMode::ARKUI_DRAG_PREVIEW_SCALE_AUTO);
309 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
310 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
311 EXPECT_TRUE(option->isScaleEnabled);
312 EXPECT_FALSE(option->isDefaultShadowEnabled);
313 EXPECT_FALSE(option->isDefaultRadiusEnabled);
314
315 /**
316 * @tc.steps: step2.set preview option with nullptr.
317 * @tc.expected: Return expected results.
318 */
319 auto ret2 =
320 OH_ArkUI_DragPreviewOption_SetScaleMode(nullptr, ArkUI_DragPreviewScaleMode::ARKUI_DRAG_PREVIEW_SCALE_AUTO);
321 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
322 }
323
324 /**
325 * @tc.name: DragAndDropTest010
326 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled.
327 * @tc.type: FUNC
328 */
HWTEST_F(DragAndDropTest, DragAndDropTest010, TestSize.Level1)329 HWTEST_F(DragAndDropTest, DragAndDropTest010, TestSize.Level1)
330 {
331 /**
332 * @tc.steps: step1.create dragPreviewOption and call related function.
333 * @tc.expected: Return expected results.
334 */
335 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
336 auto ret1 = OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled(dragPreviewOption, true);
337 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
338 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
339 EXPECT_TRUE(option->isDefaultShadowEnabled);
340
341 /**
342 * @tc.steps: step2.set preview option with nullptr.
343 * @tc.expected: Return expected results.
344 */
345 auto ret2 = OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled(nullptr, true);
346 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
347 }
348
349 /**
350 * @tc.name: DragAndDropTest011
351 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled.
352 * @tc.type: FUNC
353 */
HWTEST_F(DragAndDropTest, DragAndDropTest011, TestSize.Level1)354 HWTEST_F(DragAndDropTest, DragAndDropTest011, TestSize.Level1)
355 {
356 /**
357 * @tc.steps: step1.create dragPreviewOption and call related function.
358 * @tc.expected: Return expected results.
359 */
360 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
361 auto ret1 = OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled(dragPreviewOption, true);
362 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
363 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
364 EXPECT_TRUE(option->isDefaultRadiusEnabled);
365
366 /**
367 * @tc.steps: step2.set preview option with nullptr.
368 * @tc.expected: Return expected results.
369 */
370 auto ret2 = OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled(nullptr, true);
371 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
372 }
373
374 /**
375 * @tc.name: DragAndDropTest012
376 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled.
377 * @tc.type: FUNC
378 */
HWTEST_F(DragAndDropTest, DragAndDropTest012, TestSize.Level1)379 HWTEST_F(DragAndDropTest, DragAndDropTest012, TestSize.Level1)
380 {
381 /**
382 * @tc.steps: step1.create dragPreviewOption and call related function.
383 * @tc.expected: Return expected results.
384 */
385 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
386 auto ret1 = OH_ArkUI_DragPreviewOption_SetNumberBadgeEnabled(dragPreviewOption, true);
387 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
388 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
389 EXPECT_FALSE(option->isNumberBadgeEnabled);
390 EXPECT_TRUE(option->isShowBadge);
391
392 /**
393 * @tc.steps: step2.set preview option with nullptr.
394 * @tc.expected: Return expected results.
395 */
396 auto ret2 = OH_ArkUI_DragPreviewOption_SetNumberBadgeEnabled(nullptr, true);
397 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
398 }
399
400 /**
401 * @tc.name: DragAndDropTest013
402 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetBadgeNumber.
403 * @tc.type: FUNC
404 */
HWTEST_F(DragAndDropTest, DragAndDropTest013, TestSize.Level1)405 HWTEST_F(DragAndDropTest, DragAndDropTest013, TestSize.Level1)
406 {
407 /**
408 * @tc.steps: step1.create dragPreviewOption and call related function.
409 * @tc.expected: Return expected results.
410 */
411 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
412 auto ret1 = OH_ArkUI_DragPreviewOption_SetBadgeNumber(dragPreviewOption, 2);
413 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
414 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
415 EXPECT_TRUE(option->isNumberBadgeEnabled);
416 EXPECT_EQ(option->badgeNumber, 2);
417
418 /**
419 * @tc.steps: step2.set preview option with nullptr.
420 * @tc.expected: Return expected results.
421 */
422 auto ret2 = OH_ArkUI_DragPreviewOption_SetBadgeNumber(nullptr, true);
423 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
424 }
425
426 /**
427 * @tc.name: DragAndDropTest014
428 * @tc.desc: Test the OH_ArkUI_DragPreviewOption_SetDefaultAnimationBeforeLiftingEnabled.
429 * @tc.type: FUNC
430 */
HWTEST_F(DragAndDropTest, DragAndDropTest014, TestSize.Level1)431 HWTEST_F(DragAndDropTest, DragAndDropTest014, TestSize.Level1)
432 {
433 /**
434 * @tc.steps: step1.create dragPreviewOption and call related function.
435 * @tc.expected: Return expected results.
436 */
437 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
438 auto ret1 = OH_ArkUI_DragPreviewOption_SetDefaultAnimationBeforeLiftingEnabled(dragPreviewOption, true);
439 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
440 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
441 EXPECT_TRUE(option->defaultAnimationBeforeLifting);
442
443 /**
444 * @tc.steps: step2.set preview option with nullptr.
445 * @tc.expected: Return expected results.
446 */
447 auto ret2 = OH_ArkUI_DragPreviewOption_SetDefaultAnimationBeforeLiftingEnabled(nullptr, true);
448 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
449 }
450
451 /**
452 * @tc.name: DragAndDropTest015
453 * @tc.desc: Test the preview option mix setting.
454 * @tc.type: FUNC
455 */
HWTEST_F(DragAndDropTest, DragAndDropTest015, TestSize.Level1)456 HWTEST_F(DragAndDropTest, DragAndDropTest015, TestSize.Level1)
457 {
458 /**
459 * @tc.steps: step1.create dragPreviewOption and call mix related function.
460 * @tc.expected: Return expected results.
461 */
462 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
463 OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled(dragPreviewOption, true);
464 OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled(dragPreviewOption, true);
465 OH_ArkUI_DragPreviewOption_SetScaleMode(
466 dragPreviewOption, ArkUI_DragPreviewScaleMode::ARKUI_DRAG_PREVIEW_SCALE_AUTO);
467 auto* option = reinterpret_cast<ArkUIDragPreViewAndInteractionOptions*>(dragPreviewOption);
468 EXPECT_TRUE(option->isScaleEnabled);
469 EXPECT_FALSE(option->isDefaultShadowEnabled);
470 EXPECT_FALSE(option->isDefaultRadiusEnabled);
471 }
472
473 /**
474 * @tc.name: DragAndDropTest016
475 * @tc.desc: Test OH_ArkUI_SetNodeDragPreviewOption.
476 * @tc.type: FUNC
477 */
HWTEST_F(DragAndDropTest, DragAndDropTest016, TestSize.Level1)478 HWTEST_F(DragAndDropTest, DragAndDropTest016, TestSize.Level1)
479 {
480 /**
481 * @tc.steps: step1.create dragPreviewOption and call mix related function.
482 * @tc.expected: Return expected results.
483 */
484 auto* dragPreviewOption = OH_ArkUI_CreateDragPreviewOption();
485 OH_ArkUI_DragPreviewOption_SetDefaultRadiusEnabled(dragPreviewOption, true);
486 OH_ArkUI_DragPreviewOption_SetDefaultShadowEnabled(dragPreviewOption, true);
487 OH_ArkUI_DragPreviewOption_SetScaleMode(
488 dragPreviewOption, ArkUI_DragPreviewScaleMode::ARKUI_DRAG_PREVIEW_SCALE_AUTO);
489 auto textNode = new ArkUI_Node({ ARKUI_NODE_TEXT, nullptr });
490 auto ret1 = OH_ArkUI_SetNodeDragPreviewOption(textNode, dragPreviewOption);
491 EXPECT_EQ(ret1, ARKUI_ERROR_CODE_NO_ERROR);
492
493 /**
494 * @tc.steps: step2.set preview option with nullptr.
495 * @tc.expected: Return expected results.
496 */
497 auto ret2 = OH_ArkUI_SetNodeDragPreviewOption(nullptr, dragPreviewOption);
498 EXPECT_EQ(ret2, ARKUI_ERROR_CODE_PARAM_INVALID);
499 }
500
501 /**
502 * @tc.name: DragAndDropTest0017
503 * @tc.desc: test set DragEvent property function;
504 * @tc.type: FUNC
505 */
HWTEST_F(DragAndDropTest, DragAndDropTest0017, TestSize.Level1)506 HWTEST_F(DragAndDropTest, DragAndDropTest0017, TestSize.Level1)
507 {
508 /**
509 *@tc.steps : step1.create and set property.
510 */
511 ArkUIDragEvent dragEvent;
512 dragEvent.windowX = WINDOW_X;
513 dragEvent.windowY = WINDOW_Y;
514 dragEvent.displayX = DISPLAY_X;
515 dragEvent.displayY = DISPLAY_Y;
516 dragEvent.touchPointX = TOUCH_POINT_X;
517 dragEvent.touchPointY = TOUCH_POINT_Y;
518
519 dragEvent.previewRectWidth = PREVIEW_RECT_WIDTH;
520 dragEvent.previewRectHeight = PREVIEW_RECT_HEIGHT;
521 dragEvent.velocityX = VELOCITY_X;
522 dragEvent.velocityY = VELOCITY_Y;
523 dragEvent.velocity = VELOCITY;
524 auto* drag_Event = reinterpret_cast<ArkUI_DragEvent*>(&dragEvent);
525
526 /**
527 * @tc.expected: Return expected results.
528 */
529 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointXToWindow(drag_Event), WINDOW_X);
530 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointYToWindow(drag_Event), WINDOW_Y);
531 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewTouchPointX(drag_Event), TOUCH_POINT_X);
532 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewTouchPointY(drag_Event), TOUCH_POINT_Y);
533 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointXToDisplay(drag_Event), DISPLAY_X);
534 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointYToDisplay(drag_Event), DISPLAY_Y);
535 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewRectWidth(drag_Event), PREVIEW_RECT_WIDTH);
536 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewRectHeight(drag_Event), PREVIEW_RECT_HEIGHT);
537 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocityX(drag_Event), VELOCITY_X);
538 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocityY(drag_Event), VELOCITY_Y);
539 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocity(drag_Event), VELOCITY);
540 }
541
542 /**
543 * @tc.name: DragAndDropTest0018
544 * @tc.desc: test set DragEvent property function;
545 * @tc.type: FUNC
546 */
HWTEST_F(DragAndDropTest, DragAndDropTest0018, TestSize.Level1)547 HWTEST_F(DragAndDropTest, DragAndDropTest0018, TestSize.Level1)
548 {
549 /**
550 * @tc.expected: Return expected results.
551 */
552 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointXToWindow(nullptr), 0.0f);
553 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointYToWindow(nullptr), 0.0f);
554 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewTouchPointX(nullptr), 0.0f);
555 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewTouchPointY(nullptr), 0.0f);
556 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointXToDisplay(nullptr), 0.0f);
557 EXPECT_EQ(OH_ArkUI_DragEvent_GetTouchPointYToDisplay(nullptr), 0.0f);
558 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewRectWidth(nullptr), 0.0f);
559 EXPECT_EQ(OH_ArkUI_DragEvent_GetPreviewRectHeight(nullptr), 0.0f);
560 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocityX(nullptr), 0.0f);
561 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocityY(nullptr), 0.0f);
562 EXPECT_EQ(OH_ArkUI_DragEvent_GetVelocity(nullptr), 0.0f);
563 }
564
565 /**
566 * @tc.name: DragAndDropTest0019
567 * @tc.desc: test ConvertOriginEventType function for drag event.
568 * @tc.type: FUNC
569 */
HWTEST_F(DragAndDropTest, DragAndDropTest019, TestSize.Level1)570 HWTEST_F(DragAndDropTest, DragAndDropTest019, TestSize.Level1)
571 {
572 int32_t ret;
573 int32_t nodeType = static_cast<int32_t>(NODE_ON_PRE_DRAG);
574 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_PRE_DRAG, nodeType);
575 EXPECT_EQ(ret, static_cast<int32_t>(ON_PRE_DRAG));
576
577 nodeType = static_cast<int32_t>(NODE_ON_DRAG_START);
578 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DRAG_START, nodeType);
579 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_START));
580
581 nodeType = static_cast<int32_t>(NODE_ON_DRAG_ENTER);
582 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DRAG_ENTER, nodeType);
583 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_ENTER));
584
585 nodeType = static_cast<int32_t>(NODE_ON_DRAG_MOVE);
586 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DRAG_MOVE, nodeType);
587 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_MOVE));
588
589 nodeType = static_cast<int32_t>(NODE_ON_DRAG_LEAVE);
590 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DRAG_LEAVE, nodeType);
591 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_LEAVE));
592
593 nodeType = static_cast<int32_t>(NODE_ON_DROP);
594 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DROP, nodeType);
595 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_DROP));
596
597 nodeType = static_cast<int32_t>(NODE_ON_DRAG_END);
598 ret = OHOS::Ace::NodeModel::ConvertOriginEventType(NODE_ON_DRAG_END, nodeType);
599 EXPECT_EQ(ret, static_cast<int32_t>(ON_DRAG_END));
600 }
601
602 /**
603 * @tc.name: DragAndDropTest0020
604 * @tc.desc: test ConvertToNodeEventType function for drag event.
605 * @tc.type: FUNC
606 */
HWTEST_F(DragAndDropTest, DragAndDropTest020, TestSize.Level1)607 HWTEST_F(DragAndDropTest, DragAndDropTest020, TestSize.Level1)
608 {
609 int32_t ret;
610 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_PRE_DRAG);
611 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_PRE_DRAG));
612
613 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_START);
614 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DRAG_START));
615
616 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_ENTER);
617 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DRAG_ENTER));
618
619 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_MOVE);
620 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DRAG_MOVE));
621
622 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_LEAVE);
623 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DRAG_LEAVE));
624
625 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_DROP);
626 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DROP));
627
628 ret = OHOS::Ace::NodeModel::ConvertToNodeEventType(ON_DRAG_END);
629 EXPECT_EQ(ret, static_cast<int32_t>(NODE_ON_DRAG_END));
630 }
631
632 /**
633 * @tc.name: DragAndDropTest0021
634 * @tc.desc: test OH_ArkUI_DragEvent_GetDropOperation.
635 * @tc.type: FUNC
636 */
HWTEST_F(DragAndDropTest, DragAndDropTest021, TestSize.Level1)637 HWTEST_F(DragAndDropTest, DragAndDropTest021, TestSize.Level1)
638 {
639 /**
640 *@tc.steps : step1.create and set property.
641 */
642 ArkUIDragEvent dragEvent;
643 dragEvent.dragBehavior = ArkUI_DropOperation::ARKUI_DROP_OPERATION_MOVE;
644 auto* drag_Event = reinterpret_cast<ArkUI_DragEvent*>(&dragEvent);
645 ArkUI_DropOperation operation;
646 auto ret = OH_ArkUI_DragEvent_GetDropOperation(drag_Event, &operation);
647
648 /**
649 * @tc.expected: Return expected results.
650 */
651 EXPECT_EQ(ret, ARKUI_ERROR_CODE_NO_ERROR);
652 EXPECT_EQ(operation, ArkUI_DropOperation::ARKUI_DROP_OPERATION_MOVE);
653 EXPECT_EQ(OH_ArkUI_DragEvent_GetDropOperation(nullptr, &operation), ARKUI_ERROR_CODE_PARAM_INVALID);
654 EXPECT_EQ(OH_ArkUI_DragEvent_GetDropOperation(drag_Event, nullptr), ARKUI_ERROR_CODE_PARAM_INVALID);
655 }
656 } // namespace OHOS::Ace