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/drag_drop_initiating_test_ng.h"
17
18 #define private public
19 #define protected public
20
21 #include "core/components_ng/manager/drag_drop/drag_drop_initiating/drag_drop_initiating_handler.h"
22 #include "core/gestures/drag_event.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS::Ace::NG {
28
29 struct DragDropInitiatingStateMachineTestCase : public DragDropInitiatingStateTestCase {
30 bool nextStatusInit = false;
DragDropInitiatingStateMachineTestCaseOHOS::Ace::NG::DragDropInitiatingStateMachineTestCase31 DragDropInitiatingStateMachineTestCase(DragDropInitiatingStatus originStatus,
32 DragDropInitiatingReceivedInput receivedInput, DragDropInitiatingStatus expectStatus, bool nextStatusInit)
33 : DragDropInitiatingStateTestCase(originStatus, receivedInput, expectStatus), nextStatusInit(nextStatusInit)
34 {}
35 };
36
37 const std::vector<DragDropInitiatingStateMachineTestCase> DRAG_DROP_INITIATING_STATE_MACHINE_IMAGE_TEST_CASES = {
38 DragDropInitiatingStateMachineTestCase(DragDropInitiatingStatus::IDLE,
39 DragDropInitiatingReceivedInput::HandleLongPressOnAction, DragDropInitiatingStatus::IDLE, true),
40 DragDropInitiatingStateMachineTestCase(DragDropInitiatingStatus::IDLE,
41 DragDropInitiatingReceivedInput::HandleLongPressOnAction, DragDropInitiatingStatus::IDLE, false),
42 };
43
SetUpTestCase()44 void DragDropInitiatingStateMachineTestNG::SetUpTestCase()
45 {
46 MockPipelineContext::SetUp();
47 MockContainer::SetUp();
48 }
49
TearDownTestCase()50 void DragDropInitiatingStateMachineTestNG::TearDownTestCase()
51 {
52 MockPipelineContext::TearDown();
53 MockContainer::TearDown();
54 }
55
56 /**
57 * @tc.name: DragDropInitiatingStateMachineTestNG001
58 * @tc.desc: Test CollectTouchTarget function when frameNode is image.
59 * @tc.type: FUNC
60 */
61 HWTEST_F(DragDropInitiatingStateMachineTestNG, DragDropInitiatingStateMachineTestNG001, TestSize.Level1)
62 {
63 int32_t caseNum = 0;
64 for (const auto& testCase : DRAG_DROP_INITIATING_STATE_MACHINE_IMAGE_TEST_CASES) {
65 /**
66 * @tc.steps: step1. create DragDropEventActuator.
67 */
68 auto eventHub = AceType::MakeRefPtr<EventHub>();
69 ASSERT_NE(eventHub, nullptr);
70 auto gestureEventHub = AceType::MakeRefPtr<GestureEventHub>(AceType::WeakClaim(AceType::RawPtr(eventHub)));
71 ASSERT_NE(gestureEventHub, nullptr);
72 auto frameNode = FrameNode::GetOrCreateFrameNode(V2::IMAGE_ETS_TAG,
__anon967a89e30102() 73 ElementRegister::GetInstance()->MakeUniqueId(), []() { return AceType::MakeRefPtr<ImagePattern>(); });
74 ASSERT_NE(frameNode, nullptr);
75 eventHub->host_ = AceType::WeakClaim(AceType::RawPtr(frameNode));
76 auto dragDropEventActuator =
77 AceType::MakeRefPtr<DragDropEventActuator>(AceType::WeakClaim(AceType::RawPtr(gestureEventHub)));
78 ASSERT_NE(dragDropEventActuator, nullptr);
79 auto handler = dragDropEventActuator->dragDropInitiatingHandler_;
80 ASSERT_NE(handler, nullptr);
81 auto machine = handler->initiatingFlow_;
82 ASSERT_NE(machine, nullptr);
83 machine->InitializeState();
84 machine->currentState_ = static_cast<int32_t>(testCase.originStatus);
85 EXPECT_TRUE(DragDropInitiatingStateTestNG::CheckDragDropInitiatingStatus(
86 caseNum, static_cast<DragDropInitiatingStatus>(machine->currentState_), testCase.expectStatus));
87 caseNum++;
88 }
89 }
90
91 } // namespace OHOS::Ace::NG