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 "../ui_input_event_test.h" 17 #include "interfaces/native/event/ui_input_event_impl.h" 18 #include "interfaces/native/ui_input_event.h" 19 20 using namespace testing; 21 using namespace testing::ext; 22 namespace OHOS::Ace { 23 24 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_NullEvent, TestSize.Level0) 25 { 26 int32_t result = OH_ArkUI_MouseEvent_GetMouseAction(nullptr); 27 EXPECT_EQ(result, -1) << "Input event is nullptr, expected -1, got " << result; 28 } 29 30 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_EventTypeNotMouse, TestSize.Level0) 31 { 32 ArkUIMouseEvent mouseEvent = {}; 33 mouseEvent.action = 1; // Press action 34 ArkUI_UIInputEvent inputEvent = { ARKUI_UIINPUTEVENT_TYPE_MOUSE, 35 C_CLICK_EVENT_ID, // Not mouse event 36 &mouseEvent }; 37 int32_t result = OH_ArkUI_MouseEvent_GetMouseAction(&inputEvent); 38 EXPECT_EQ(result, -1); 39 } 40 41 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_NullInputEvent, TestSize.Level0) 42 { 43 ArkUI_UIInputEvent inputEvent = { ARKUI_UIINPUTEVENT_TYPE_MOUSE, C_MOUSE_EVENT_ID, nullptr }; 44 int32_t result = OH_ArkUI_MouseEvent_GetMouseAction(&inputEvent); 45 EXPECT_EQ(result, -1); 46 } 47 48 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_Normal, TestSize.Level0) 49 { 50 ArkUIMouseEvent mouseEvent = {}; 51 mouseEvent.action = 1; // Press action 52 ArkUI_UIInputEvent inputEvent = { ARKUI_UIINPUTEVENT_TYPE_MOUSE, C_MOUSE_EVENT_ID, &mouseEvent }; 53 int32_t result = OH_ArkUI_MouseEvent_GetMouseAction(&inputEvent); 54 EXPECT_EQ(result, 1); 55 } 56 57 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_ActionVariants, TestSize.Level0) 58 { 59 struct ActionTestCase { 60 int32_t rawAction; // Raw action value from event 61 int32_t expected; // Expected converted value 62 const char* desc; 63 }; 64 std::vector<ActionTestCase> cases = { { 0, 0, "No action" }, { 1, 1, "Press action" }, { 2, 2, "Release action" }, 65 { 3, 3, "Move action" }, { 4, 0, "Hover enter (unsupported)" }, { 5, 0, "Hover exit (unsupported)" }, 66 { 6, 0, "Drag action (unsupported)" }, { 7, 0, "Unsupported action type" } }; 67 68 ArkUIMouseEvent mouseEvent = {}; 69 ArkUI_UIInputEvent inputEvent = { ARKUI_UIINPUTEVENT_TYPE_MOUSE, C_MOUSE_EVENT_ID, &mouseEvent }; 70 int count = 0; 71 for (const auto& testCase : cases) { 72 mouseEvent.action = testCase.rawAction; 73 int32_t result = OH_ArkUI_MouseEvent_GetMouseAction(&inputEvent); 74 EXPECT_EQ(result, testCase.expected) 75 << "Test case " << count << " failed: " << testCase.desc << ", rawAction=" << testCase.rawAction 76 << ", expected=" << testCase.expected << ", actual=" << result; 77 count++; 78 } 79 } 80 81 HWTEST_F(UIInputEventTest, OH_ArkUI_MouseEvent_GetMouseAction_ConvertedValues, TestSize.Level0) 82 { 83 // Test that the conversion function returns expected values 84 EXPECT_EQ(OHOS::Ace::NodeModel::ConvertToCMouseActionType(1), 1) << "Press action conversion failed"; 85 EXPECT_EQ(OHOS::Ace::NodeModel::ConvertToCMouseActionType(2), 2) << "Release action conversion failed"; 86 EXPECT_EQ(OHOS::Ace::NodeModel::ConvertToCMouseActionType(3), 3) << "Move action conversion failed"; 87 EXPECT_EQ(OHOS::Ace::NodeModel::ConvertToCMouseActionType(4), 0) << "Unsupported action should return 0"; 88 } 89 90 } // namespace OHOS::Ace