• 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/event/drag_event/drag_event_common_test_ng.h"
17 
18 #include "core/components_ng/manager/drag_drop/drag_drop_initiating/drag_drop_initiating_handler.h"
19 #include "core/gestures/drag_event.h"
20 
21 #define private public
22 #define protected public
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS::Ace::NG {
28 struct DragDropStartTestCase {
29     GlobalDraggingInfo globalDraggingInfo;
30     DragInfo dragInfo;
31     MultiDragInfo multiDragInfo;
32     InputEventType inputEventType;
33 
34     // calculate drag status
35     DragDropInitiatingStatus expectStatus = DragDropInitiatingStatus::IDLE;
DragDropStartTestCaseOHOS::Ace::NG::DragDropStartTestCase36     DragDropStartTestCase(GlobalDraggingInfo globalDraggingInfo, DragInfo dragInfo, MultiDragInfo multiDragInfo,
37         InputEventType inputEventType, DragDropInitiatingStatus expectStatus)
38         : globalDraggingInfo(globalDraggingInfo), dragInfo(dragInfo), multiDragInfo(multiDragInfo),
39           inputEventType(inputEventType), expectStatus(expectStatus)
40     {}
41 };
42 
43 class DragDropEventTestNgIssue : public DragEventCommonTestNg {
44 public:
45     static void SetUpTestSuite();
46     static void TearDownTestSuite();
47 
48 protected:
49     bool CheckDragDropInitiatingStatus(
50         int32_t caseNum, DragDropInitiatingStatus dragStatus, DragDropInitiatingStatus status);
51 };
52 
SetUpTestSuite()53 void DragDropEventTestNgIssue::SetUpTestSuite()
54 {
55     MockPipelineContext::SetUp();
56     MockContainer::SetUp();
57     SystemProperties::dragDropFrameworkStatus_ = 1;
58 }
59 
TearDownTestSuite()60 void DragDropEventTestNgIssue::TearDownTestSuite()
61 {
62     MockPipelineContext::TearDown();
63     MockContainer::TearDown();
64     SystemProperties::dragDropFrameworkStatus_ = 0;
65 }
66 
CheckDragDropInitiatingStatus(int32_t caseNum,DragDropInitiatingStatus dragStatus,DragDropInitiatingStatus expectStatus)67 bool DragDropEventTestNgIssue::CheckDragDropInitiatingStatus(
68     int32_t caseNum, DragDropInitiatingStatus dragStatus, DragDropInitiatingStatus expectStatus)
69 {
70     if (dragStatus == expectStatus) {
71         return testing::AssertionSuccess();
72     }
73     return testing::AssertionFailure() << "TestCaseNum: " << caseNum
74                                        << ", actual state: " << static_cast<int32_t>(dragStatus)
75                                        << ", expect state: " << static_cast<int32_t>(expectStatus);
76 }
77 
78 const std::vector<DragDropStartTestCase> DRAG_DROP_STATUS_DRAG_INFO_IMAGE_TEST_CASES = {
79     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(false, false, false, false),
80         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 0
81     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(false, false, true, false),
82         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::IDLE), // case 1
83     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(false, true, false, false),
84         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 2
85     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(false, true, true, false),
86         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 3
87     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(true, false, false, false),
88         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 4
89     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(true, false, true, false),
90         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::IDLE), // case 5
91     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(true, true, false, false),
92         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 6
93     DragDropStartTestCase(GlobalDraggingInfo(false, false), DragInfo(true, true, true, false),
94         MultiDragInfo(false, false, false), InputEventType::TOUCH_SCREEN, DragDropInitiatingStatus::READY), // case 7
95 };
96 
97 /**
98  * @tc.name: DragDropEventTestNGIssue001
99  * @tc.desc: Test CollectTouchTarget function when frameNode is image.
100  * @tc.type: FUNC
101  */
102 HWTEST_F(DragDropEventTestNgIssue, DragDropEventTestNGIssue001, TestSize.Level1)
103 {
104     int32_t caseNum = 0;
105     for (const auto& testCase : DRAG_DROP_STATUS_DRAG_INFO_IMAGE_TEST_CASES) {
106         /**
107          * @tc.steps: step1. create DragDropEventActuator.
108          */
109         auto eventHub = AceType::MakeRefPtr<EventHub>();
110         ASSERT_NE(eventHub, nullptr);
111         auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
112         ASSERT_NE(gestureEventHub, nullptr);
113         gestureEventHub->SetDragForbiddenForcely(testCase.dragInfo.isDragForbidden);
114         auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG,
__anond9d653330102() 115             ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<ImagePattern>(); });
116         ASSERT_NE(frameNode, nullptr);
117         eventHub->host_ = AceType::WeakClaim(AceType::RawPtr(frameNode));
118         auto dragDropEventActuator =
119             AceType::MakeRefPtr<DragDropEventActuator>(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
120         ASSERT_NE(dragDropEventActuator, nullptr);
121         /**
122          * @tc.steps: step2. call OnCollectTouchTarget function.
123          * @tc.expected: step2. DragDropInitiatingStatus equals.
124          */
125         frameNode->draggable_ = testCase.dragInfo.isDraggable;
126         frameNode->customerSet_ = testCase.dragInfo.isCustomerSet;
127         TouchRestrict dragTouchRestrict = { TouchRestrict::NONE };
128         dragTouchRestrict.inputEventType = testCase.inputEventType;
129         auto getEventTargetImpl = eventHub->CreateGetEventTargetImpl();
130         EXPECT_NE(getEventTargetImpl, nullptr);
131         TouchTestResult finalResult;
132         ResponseLinkResult responseLinkResult;
133         dragDropEventActuator->OnCollectTouchTarget(
134             COORDINATE_OFFSET, DRAG_TOUCH_RESTRICT, getEventTargetImpl, finalResult, responseLinkResult);
135         auto handler = dragDropEventActuator->dragDropInitiatingHandler_;
136         ASSERT_NE(handler, nullptr);
137         EXPECT_TRUE(
138             CheckDragDropInitiatingStatus(caseNum, handler->GetDragDropInitiatingStatus(), testCase.expectStatus));
139         caseNum++;
140     }
141 }
142 
143 } // namespace OHOS::Ace::NG